Exemple #1
0
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
Exemple #2
0
    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)
Exemple #3
0
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
Exemple #4
0
    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)
Exemple #5
0
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
Exemple #6
0
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
Exemple #7
0
    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)
Exemple #8
0
    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)
Exemple #9
0
 def test_simple_success(self):
     text = u"EXEC SQL select a from b;"
     (success, msg) = ecpg.check_syntax(text)
     self.assertTrue(success)
Exemple #10
0
 def test_empty_sql_okay(self):
     text = u"EXEC SQL ;"
     (success, msg) = ecpg.check_syntax(text)
     self.assertTrue(success)
Exemple #11
0
 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)
Exemple #12
0
 def test_simple_success(self):
     text = u"EXEC SQL select a from b;"
     (success, msg) = ecpg.check_syntax(text)
     self.assertTrue(success)
Exemple #13
0
 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)