def check_file(filename=None, show_filename=False): #either work with sys.stdin or open the file filelike = sys.stdin if filename is not None: filelike = open(filename, "r") #prep the sql, store it in a temp file prepped_file = prep_file(filelike) filelike.close() #actually check the syntax of the prepped file (success, msg) = ecpg.check_syntax(prepped_file) #report results result = 0 if not success: #possibly show the filename with the error message prefix = "" if show_filename and filename is not None: prefix = filename + ": " print(prefix + msg) result = 1 #remove the temp file that contained the prepped sql os.remove(prepped_file) return result
def test_simple_failure(self): text = "EXEC SQL garbage select a from b;" write_out(self.file, text.encode('utf-8')) (success, msg) = ecpg.check_syntax(self.file.name) self.assertFalse(success) self.assertEqual('line 1: ERROR: unrecognized data type name "garbage"', msg)
def test_simple_failure(self): text = "EXEC SQL garbage select a from b;" write_out(self.file, text.encode('utf-8')) (success, msg) = ecpg.check_syntax(self.file.name) self.assertFalse(success) self.assertEqual( 'line 1: ERROR: unrecognized data type name "garbage"', msg)
def check_string(sql_string, add_semicolon=False): """ Check whether a string is valid PostgreSQL. Returns a boolean indicating validity and a message from ecpg, which will be an empty string if the input was valid, or a description of the problem otherwise. """ prepped_sql = sqlprep.prepare_sql(sql_string, add_semicolon=add_semicolon) success, msg = ecpg.check_syntax(prepped_sql) return success, msg
def check_string(sql_string): """ Check whether a string is valid PostgreSQL. Returns a list of messages from ecpg, which will be the empty list if the input was valid, or a list of error messages otherwise. """ prepped_sql = sqlprep.prepare_sql(sql_string) results = [] for (line_offset, sql) in prepped_sql: success, msglist = ecpg.check_syntax(line_offset, sql) if not success: results.extend(msglist) return results
def test_simple_success(self): text = "EXEC SQL select a from b;" write_out(self.file, text.encode('utf-8')) (success, msg) = ecpg.check_syntax(self.file.name) self.assertTrue(success)
def test_simple_success(self): text = u"EXEC SQL select a from b;" (success, msg) = ecpg.check_syntax(text) self.assertTrue(success)
def test_empty_sql_okay(self): text = u"EXEC SQL ;" (success, msg) = ecpg.check_syntax(text) self.assertTrue(success)
def test_simple_failure(self): text = u"EXEC SQL garbage select a from b;" (success, msg) = ecpg.check_syntax(text) self.assertFalse(success) self.assertEqual('line 1: ERROR: unrecognized data type name "garbage"', msg)
def test_simple_failure(self): text = u"EXEC SQL garbage select a from b;" (success, msg) = ecpg.check_syntax(text) self.assertFalse(success) self.assertEqual( 'line 1: ERROR: unrecognized data type name "garbage"', msg)