Ticket #4680: django-ignore-comments-in-sql.patch
| File django-ignore-comments-in-sql.patch, 5.2 kB (added by shaleh, 7 months ago) |
|---|
-
django/django/core/management/sql.py
old new 422 422 423 423 return final_output 424 424 425 def _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 425 448 def custom_sql_for_model(model): 426 449 from django.db import models 427 450 from django.conf import settings … … 430 453 app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql')) 431 454 output = [] 432 455 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 437 456 # Find custom SQL, if it's available. 438 457 sql_files = [os.path.join(app_dir, "%s.%s.sql" % (opts.object_name.lower(), settings.DATABASE_ENGINE)), 439 458 os.path.join(app_dir, "%s.sql" % opts.object_name.lower())] 459 440 460 for sql_file in sql_files: 441 461 if os.path.exists(sql_file): 442 462 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) 448 464 fp.close() 449 465 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 450 477 return output 451 478 452 479 def sql_indexes_for_model(model, style): -
django/tests/regressiontests/initial_sql_regress/models.py
old new 7 7 class Simple(models.Model): 8 8 name = models.CharField(max_length = 50) 9 9 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 """} 11 14 12 15 # NOTE: The format of the included SQL file for this test suite is important. 13 16 # It must end with a trailing newline in order to test the fix for #2161. -
django/tests/regressiontests/initial_sql_regress/sql/simple.sql
old new 1 INSERT 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 3 INSERT INTO initial_sql_regress_simple (name) VALUES ('Capt. Bob'); -- another one 4 INSERT INTO initial_sql_regress_simple (name) VALUES ('-- Comment Man'); 1 5 INSERT INTO initial_sql_regress_simple (name) VALUES ('John'); 2 6 INSERT INTO initial_sql_regress_simple (name) VALUES ('Paul'); 3 7 INSERT INTO initial_sql_regress_simple (name) VALUES ('Ringo'); -
django/tests/regressiontests/initial_sql_regress/tests.py
old new 1 import unittest 2 from django.core import management 3 4 from models import Simple 5 6 class 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 13 if __name__ == '__main__': 14 unittest.main()
