summaryrefslogtreecommitdiff
path: root/django/db/backends/sqlite3
diff options
context:
space:
mode:
Diffstat (limited to 'django/db/backends/sqlite3')
-rw-r--r--django/db/backends/sqlite3/base.py45
-rw-r--r--django/db/backends/sqlite3/introspection.py37
2 files changed, 82 insertions, 0 deletions
diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
index a0b1341b53..0bae2de216 100644
--- a/django/db/backends/sqlite3/base.py
+++ b/django/db/backends/sqlite3/base.py
@@ -214,6 +214,51 @@ def _sqlite_regexp(re_pattern, re_string):
except:
return False
+def get_change_column_name_sql( table_name, indexes, old_col_name, new_col_name, col_def ):
+ # sqlite doesn't support column renames, so we fake it
+ # TODO: only supports a single primary key so far
+ pk_name = None
+ for key in indexes.keys():
+ if indexes[key]['primary_key']: pk_name = key
+ output = []
+ output.append( 'ALTER TABLE '+ quote_name(table_name) +' ADD COLUMN '+ quote_name(new_col_name) +' '+ col_def + ';' )
+ output.append( 'UPDATE '+ quote_name(table_name) +' SET '+ new_col_name +' = '+ old_col_name +' WHERE '+ pk_name +'=(select '+ pk_name +' from '+ table_name +');' )
+ output.append( '-- FYI: sqlite does not support deleting columns, so '+ quote_name(old_col_name) +' remains as cruft' )
+ # use the following when sqlite gets drop support
+ #output.append( 'ALTER TABLE '+ quote_name(table_name) +' DROP COLUMN '+ quote_name(old_col_name) )
+ return '\n'.join(output)
+
+def get_change_column_def_sql( table_name, col_name, col_def ):
+ # sqlite doesn't support column modifications, so we fake it
+ output = []
+ # TODO: fake via renaming the table, building a new one and deleting the old
+ output.append('-- sqlite does not support column modifications '+ quote_name(table_name) +'.'+ quote_name(col_name) +' to '+ col_def)
+ return '\n'.join(output)
+
+def get_add_column_sql( table_name, col_name, col_type, null, unique, primary_key ):
+ output = []
+ field_output = []
+ field_output.append('ALTER TABLE')
+ field_output.append(quote_name(table_name))
+ field_output.append('ADD COLUMN')
+ field_output.append(quote_name(col_name))
+ field_output.append(col_type)
+ field_output.append(('%sNULL' % (not null and 'NOT ' or '')))
+ if unique:
+ field_output.append(('UNIQUE'))
+ if primary_key:
+ field_output.append(('PRIMARY KEY'))
+ output.append(' '.join(field_output) + ';')
+ return '\n'.join(output)
+
+def get_drop_column_sql( table_name, col_name ):
+ output = []
+ output.append( '-- FYI: sqlite does not support deleting columns, so '+ quote_name(old_col_name) +' remains as cruft' )
+ # use the following when sqlite gets drop support
+ # output.append( '-- ALTER TABLE '+ quote_name(table_name) +' DROP COLUMN '+ quote_name(col_name) )
+ return '\n'.join(output)
+
+
# SQLite requires LIKE statements to include an ESCAPE clause if the value
# being escaped has a percent or underscore in it.
# See http://www.sqlite.org/lang_expr.html for an explanation.
diff --git a/django/db/backends/sqlite3/introspection.py b/django/db/backends/sqlite3/introspection.py
index 4e22c5ea42..ce033757c9 100644
--- a/django/db/backends/sqlite3/introspection.py
+++ b/django/db/backends/sqlite3/introspection.py
@@ -43,6 +43,43 @@ def get_indexes(cursor, table_name):
indexes[name]['unique'] = True
return indexes
+def get_columns(cursor, table_name):
+ try:
+ cursor.execute("PRAGMA table_info(%s)" % quote_name(table_name))
+ return [row[1] for row in cursor.fetchall()]
+ except:
+ return []
+
+def get_known_column_flags( cursor, table_name, column_name ):
+ cursor.execute("PRAGMA table_info(%s)" % quote_name(table_name))
+ dict = {}
+ for row in cursor.fetchall():
+ if row[1] == column_name:
+
+ # maxlength check goes here
+ if row[2][0:7]=='varchar':
+ dict['maxlength'] = row[2][8:len(row[2])-1]
+
+ # default flag check goes here
+ #if row[2]=='YES': dict['allow_null'] = True
+ #else: dict['allow_null'] = False
+
+ # primary/foreign/unique key flag check goes here
+ #if row[3]=='PRI': dict['primary_key'] = True
+ #else: dict['primary_key'] = False
+ #if row[3]=='FOR': dict['foreign_key'] = True
+ #else: dict['foreign_key'] = False
+ #if row[3]=='UNI': dict['unique'] = True
+ #else: dict['unique'] = False
+
+ # default value check goes here
+ # if row[4]=='NULL': dict['default'] = None
+ # else: dict['default'] = row[4]
+ #dict['default'] = row[4]
+
+ print table_name, column_name, dict
+ return dict
+
def _table_info(cursor, name):
cursor.execute('PRAGMA table_info(%s)' % quote_name(name))
# cid, name, type, notnull, dflt_value, pk