Code

Ticket #470: 470-2900.diff

File 470-2900.diff, 4.8 KB (added by jws, 8 years ago)

update for 2900

Line 
1Index: django/core/management.py
2===================================================================
3--- django/core/management.py   (revision 2900)
4+++ django/core/management.py   (working copy)
5@@ -168,7 +168,13 @@
6                     # We haven't yet created the table to which this field
7                     # is related, so save it for later.
8                     pr = pending_references.setdefault(f.rel.to, []).append((klass, f))
9-            table_output.append(' '.join(field_output))
10+            if f.default <> models.fields.NOT_PROVIDED:
11+                try:
12+                    escaped_string =  django.db.backend.escapechars(f.default)
13+                    field_output.append(style.SQL_KEYWORD("DEFAULT '%s'" % (escaped_string,)))
14+                except NotImplemented:
15+                    pass
16+        table_output.append(' '.join(field_output))
17     if opts.order_with_respect_to:
18         table_output.append(style.SQL_FIELD(backend.quote_name('_order')) + ' ' + \
19             style.SQL_COLTYPE(data_types['IntegerField']) + ' ' + \
20Index: django/db/backends/ado_mssql/base.py
21===================================================================
22--- django/db/backends/ado_mssql/base.py        (revision 2900)
23+++ django/db/backends/ado_mssql/base.py        (working copy)
24@@ -111,6 +111,15 @@
25     if lookup_type=='day':
26         return "Convert(datetime, Convert(varchar(12), %s))" % field_name
27 
28+def escapechars(raw_str):
29+    "Escapes problematic characters from SQL in a backend-specific way"
30+    working_str = str(raw_str)
31+    rawchars = ['"',"'"]
32+    cookedchars = ['""',"''"]
33+    for i in range(0,len(rawchars)):
34+        working_str = working_str.replace(rawchars[i],cookedchars[i])
35+    return working_str
36+
37 def get_limit_offset_sql(limit, offset=None):
38     # TODO: This is a guess. Make sure this is correct.
39     sql = "LIMIT %s" % limit
40Index: django/db/backends/dummy/base.py
41===================================================================
42--- django/db/backends/dummy/base.py    (revision 2900)
43+++ django/db/backends/dummy/base.py    (working copy)
44@@ -28,6 +28,7 @@
45 dictfetchone = complain
46 dictfetchmany = complain
47 dictfetchall = complain
48+escapechars = complain
49 get_last_insert_id = complain
50 get_date_extract_sql = complain
51 get_date_trunc_sql = complain
52Index: django/db/backends/mysql/base.py
53===================================================================
54--- django/db/backends/mysql/base.py    (revision 2900)
55+++ django/db/backends/mysql/base.py    (working copy)
56@@ -139,6 +139,15 @@
57         sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str)
58     return sql
59 
60+def escapechars(raw_str):
61+    "Escapes problematic characters from SQL in a backend-specific way"
62+    working_str = str(raw_str)
63+    rawchars = ['\\','"',"'"]
64+    cookedchars = ['\\\\','\\"',"\\'"]
65+    for i in range(0,len(rawchars)):
66+        working_str = working_str.replace(rawchars[i],cookedchars[i])
67+    return working_str
68+
69 def get_limit_offset_sql(limit, offset=None):
70     sql = "LIMIT "
71     if offset and offset != 0:
72Index: django/db/backends/postgresql/base.py
73===================================================================
74--- django/db/backends/postgresql/base.py       (revision 2900)
75+++ django/db/backends/postgresql/base.py       (working copy)
76@@ -89,6 +89,15 @@
77     # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
78     return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name)
79 
80+def escapechars(raw_str):
81+    "Escapes problematic characters from SQL in a backend-specific way"
82+    working_str = str(raw_str)
83+    rawchars = ['\\','"',"'"]
84+    cookedchars = ['\\\\','\\"',"''"]
85+    for i in range(0,len(rawchars)):
86+        working_str = working_str.replace(rawchars[i],cookedchars[i])
87+    return working_str
88+
89 def get_limit_offset_sql(limit, offset=None):
90     sql = "LIMIT %s" % limit
91     if offset and offset != 0:
92Index: django/db/backends/sqlite3/base.py
93===================================================================
94--- django/db/backends/sqlite3/base.py  (revision 2900)
95+++ django/db/backends/sqlite3/base.py  (working copy)
96@@ -123,6 +123,16 @@
97 def get_drop_foreignkey_sql():
98     return ""
99 
100+def escapechars(raw_str):
101+    "Escapes problematic characters from SQL in a backend-specific way"
102+    # sqlite will happily accept a variety of characters without complaint. Single-quotes, however, must be doubled.
103+    working_str = str(raw_str)
104+    rawchars = ["'",]
105+    cookedchars = ["''",]
106+    for i in range(0,len(rawchars)):
107+        working_str = working_str.replace(rawchars[i],cookedchars[i])
108+    return working_str
109+
110 def _sqlite_date_trunc(lookup_type, dt):
111     try:
112         dt = util.typecast_timestamp(dt)
113@@ -152,3 +162,4 @@
114     'istartswith': "LIKE %s ESCAPE '\\'",
115     'iendswith': "LIKE %s ESCAPE '\\'",
116 }
117+