Ticket #4680: django-ignore-comments-in-sql.patch

File django-ignore-comments-in-sql.patch, 5.2 KB (added by shaleh, 17 years ago)

updated patch. Seems simpler than the previous one. Added test casesto simple.sql as well as a unittest for custom_sql_for_model itself

  • django/core/management/sql.py

    diff -pruN django/django/core/management/sql.py django-sperry/django/core/management/sql.py
    old new def many_to_many_sql_for_model(model, st  
    422422
    423423    return final_output
    424424
     425def _split_statements(value):
     426    result = []
     427    tmp = []
     428    in_quotes = None
     429
     430    for line in value.split('\n'):
     431        for c in line:
     432            if c == ';' and not in_quotes:
     433                result.append(u''.join(tmp))
     434                tmp = []
     435                continue
     436
     437            if c in ("'", '"'):
     438                if not in_quotes:
     439                    in_quotes = c
     440                elif in_quotes == c:
     441                    in_quotes = None
     442            tmp.append(c)
     443        result.append(u''.join(tmp))
     444        tmp = []
     445
     446    return result
     447
    425448def custom_sql_for_model(model):
    426449    from django.db import models
    427450    from django.conf import settings
    def custom_sql_for_model(model):  
    430453    app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql'))
    431454    output = []
    432455
    433     # Some backends can't execute more than one SQL statement at a time,
    434     # so split into separate statements.
    435     statements = re.compile(r";[ \t]*$", re.M)
    436 
    437456    # Find custom SQL, if it's available.
    438457    sql_files = [os.path.join(app_dir, "%s.%s.sql" % (opts.object_name.lower(), settings.DATABASE_ENGINE)),
    439458                 os.path.join(app_dir, "%s.sql" % opts.object_name.lower())]
     459
    440460    for sql_file in sql_files:
    441461        if os.path.exists(sql_file):
    442462            fp = open(sql_file, 'U')
    443             for statement in statements.split(fp.read().decode(settings.FILE_CHARSET)):
    444                 # Remove any comments from the file
    445                 statement = re.sub(ur"--.*[\n\Z]", "", statement)
    446                 if statement.strip():
    447                     output.append(statement + u";")
     463            sql_data = fp.read().decode(settings.FILE_CHARSET)
    448464            fp.close()
    449465
     466            # Some backends can't execute more than one SQL statement
     467            # at a time, so split into separate statements.
     468            for statement in _split_statements(sql_data):
     469                statement = statement.strip()
     470                if statement:
     471                    # Remove any comments from sql
     472                    if statement.startswith(u'--'):
     473                        continue
     474
     475                    output.append(statement + u";")
     476
    450477    return output
    451478
    452479def sql_indexes_for_model(model, style):
  • tests/regressiontests/initial_sql_regress/models.py

    diff -pruN django/tests/regressiontests/initial_sql_regress/models.py django-sperry/tests/regressiontests/initial_sql_regress/models.py
    old new from django.db import models  
    77class Simple(models.Model):
    88    name = models.CharField(max_length = 50)
    99
    10 __test__ = {'API_TESTS':""}
     10__test__ = {'API_TESTS': """
     11>>> [ obj.name for obj in Simple.objects.all() ]
     12[u'Mary', u'Anne', u'Ginger', u'Mrs. Mae', u'Capt. Bob', u'-- Comment Man', u'John', u'Paul', u'Ringo', u'George', u"Miles O'Brien", u'Semicolon;Man', u'This line has a Windows line ending']
     13"""}
    1114
    1215# NOTE: The format of the included SQL file for this test suite is important.
    1316# It must end with a trailing newline in order to test the fix for #2161.
  • tests/regressiontests/initial_sql_regress/sql/simple.sql

    diff -pruN django/tests/regressiontests/initial_sql_regress/sql/simple.sql django-sperry/tests/regressiontests/initial_sql_regress/sql/simple.sql
    old new  
     1INSERT INTO initial_sql_regress_simple (name) VALUES ('Mary'); INSERT INTO initial_sql_regress_simple (name) VALUES ('Anne'); INSERT INTO initial_sql_regress_simple (name) VALUES ('Ginger'); INSERT INTO initial_sql_regress_simple (name) VALUES ('Mrs. Mae');
     2-- a comment
     3INSERT INTO initial_sql_regress_simple (name) VALUES ('Capt. Bob'); -- another one
     4INSERT INTO initial_sql_regress_simple (name) VALUES ('-- Comment Man');
    15INSERT INTO initial_sql_regress_simple (name) VALUES ('John');
    26INSERT INTO initial_sql_regress_simple (name) VALUES ('Paul');
    37INSERT INTO initial_sql_regress_simple (name) VALUES ('Ringo');
  • tests/regressiontests/initial_sql_regress/tests.py

    diff -pruN django/tests/regressiontests/initial_sql_regress/tests.py django-sperry/tests/regressiontests/initial_sql_regress/tests.py
    old new  
     1import unittest
     2from django.core import management
     3
     4from models import Simple
     5
     6class CustomSqlForModel(unittest.TestCase):
     7    def setUp(self):
     8        self.output = management.sql.custom_sql_for_model(Simple)
     9       
     10    def test_multiline(self):
     11        self.assertEqual(len(self.output), 13)
     12
     13if __name__ == '__main__':
     14    unittest.main()
Back to Top