mirror of
https://github.com/yweber/lodel2.git
synced 2026-01-30 00:20:12 +01:00
PEP8 sur sqlalter.py
This commit is contained in:
parent
4efea40abf
commit
9cc3f9ce12
1 changed files with 22 additions and 15 deletions
|
|
@ -8,13 +8,13 @@ from sqlalchemy.ext.compiler import compiles
|
|||
|
||||
## @file sqlalter.py
|
||||
# This file defines all DDL (data definition langage) for the ALTER TABLE instructions
|
||||
#
|
||||
#
|
||||
# It uses the SqlAlchemy compilation and quoting methos to generate SQL
|
||||
|
||||
|
||||
class AddColumn(sqla.schema.DDLElement):
|
||||
""" Defines the ddl for adding a column to a table """
|
||||
def __init__(self,table, column):
|
||||
def __init__(self, table, column):
|
||||
""" Instanciate the DDL
|
||||
@param table sqlalchemy.Table: A sqlalchemy table object
|
||||
@param column sqlalchemy.Column: A sqlalchemy column object
|
||||
|
|
@ -22,6 +22,7 @@ class AddColumn(sqla.schema.DDLElement):
|
|||
self.col = column
|
||||
self.table = table
|
||||
|
||||
|
||||
@compiles(AddColumn, 'mysql')
|
||||
@compiles(AddColumn, 'postgresql')
|
||||
@compiles(AddColumn, 'sqlite')
|
||||
|
|
@ -30,11 +31,13 @@ def visit_add_column(element, ddlcompiler, **kw):
|
|||
prep = ddlcompiler.sql_compiler.preparer
|
||||
tname = prep.format_table(element.table)
|
||||
colname = prep.format_column(element.col)
|
||||
return 'ALTER TABLE %s ADD COLUMN %s %s'%(tname, colname, element.col.type)
|
||||
return 'ALTER TABLE %s ADD COLUMN %s %s' % (tname, colname, element.col.type)
|
||||
|
||||
|
||||
@compiles(AddColumn)
|
||||
def visit_add_column(element, ddlcompiler, **kw):
|
||||
raise NotImplementedError('Add column not yet implemented for '+str(ddlcompiler.dialect.name))
|
||||
raise NotImplementedError('Add column not yet implemented for ' + str(ddlcompiler.dialect.name))
|
||||
|
||||
|
||||
class DropColumn(sqla.schema.DDLElement):
|
||||
""" Defines the DDL for droping a column from a table """
|
||||
|
|
@ -46,14 +49,16 @@ class DropColumn(sqla.schema.DDLElement):
|
|||
self.col = column
|
||||
self.table = table
|
||||
|
||||
@compiles(DropColumn,'mysql')
|
||||
|
||||
@compiles(DropColumn, 'mysql')
|
||||
@compiles(DropColumn, 'postgresql')
|
||||
def visit_drop_column(element, ddlcompiler, **kw):
|
||||
""" Compiles the DropColumn DDL for mysql & postgresql """
|
||||
prep = ddlcompiler.sql_compiler.preparer
|
||||
tname = prep.format_table(element.table)
|
||||
colname = prep.format_column(element.col)
|
||||
return 'ALTER TABLE %s DROP COLUMN %s'%(tname, colname)
|
||||
return 'ALTER TABLE %s DROP COLUMN %s' % (tname, colname)
|
||||
|
||||
|
||||
@compiles(DropColumn)
|
||||
##
|
||||
|
|
@ -63,11 +68,11 @@ def visit_drop_column(element, ddlcompiler, **kw):
|
|||
tname = prep.format_table(element.table)
|
||||
|
||||
#Temporary table
|
||||
tmpname = str(element.table)+'_copydropcol'
|
||||
tmpname = str(element.table) + '_copydropcol'
|
||||
tmpTable = sqla.Table(tmpname, sqla.MetaData())
|
||||
tmptname = prep.format_table(tmpTable)
|
||||
|
||||
query = 'ALTER TABLE %s RENAME TO %s; '%(tname, tmptname)
|
||||
query = 'ALTER TABLE %s RENAME TO %s; ' % (tname, tmptname)
|
||||
|
||||
colname = prep.format_column(element.col)
|
||||
|
||||
|
|
@ -80,13 +85,13 @@ def visit_drop_column(element, ddlcompiler, **kw):
|
|||
if col.name != element.col.name:
|
||||
newtable.append_column(col.copy())
|
||||
cols.append(prep.format_column(col))
|
||||
cols=', '.join(cols)
|
||||
cols = ', '.join(cols)
|
||||
|
||||
query += str(sqla.schema.CreateTable(newtable).compile(dialect = ddlcompiler.dialect))+';'
|
||||
query += str(sqla.schema.CreateTable(newtable).compile(dialect=ddlcompiler.dialect)) + ';'
|
||||
|
||||
query += 'INSERT INTO %s ( %s ) SELECT %s FROM %s;' % (newtable, cols, cols, tmptname)
|
||||
|
||||
query += 'DROP TABLE %s'% (tmpname)
|
||||
query += 'DROP TABLE %s' % (tmpname)
|
||||
|
||||
return query
|
||||
|
||||
|
|
@ -103,13 +108,15 @@ class AlterColumn(sqla.schema.DDLElement):
|
|||
self.col = column
|
||||
self.table = table
|
||||
|
||||
|
||||
@compiles(AlterColumn, 'mysql')
|
||||
def visit_alter_column(element, ddlcompiler, **kw):
|
||||
""" Compiles the AlterColumn DDL for mysql """
|
||||
prep = ddlcompiler.sql_compiler.preparer
|
||||
tname = prep.format_table(element.table)
|
||||
colname = prep.format_column(element.col)
|
||||
return 'ALTER TABLE %s ALTER COLUMN %s %s'%(tname, colname, element.col.type)
|
||||
return 'ALTER TABLE %s ALTER COLUMN %s %s' % (tname, colname, element.col.type)
|
||||
|
||||
|
||||
@compiles(AlterColumn, 'postgresql')
|
||||
def visit_alter_column(element, ddlcompiler, **kw):
|
||||
|
|
@ -117,9 +124,9 @@ def visit_alter_column(element, ddlcompiler, **kw):
|
|||
prep = ddlcompiler.sql_compiler.preparer
|
||||
tname = prep.format_table(element.table)
|
||||
colname = prep.format_column(element.col)
|
||||
return 'ALTER TABLE %s ALTER COLUMN %s TYPE %s'%(tname, colname, element.col.type)
|
||||
return 'ALTER TABLE %s ALTER COLUMN %s TYPE %s' % (tname, colname, element.col.type)
|
||||
|
||||
|
||||
@compiles(AlterColumn)
|
||||
def visit_alter_column(element, ddlcompiler, **kw):
|
||||
raise NotImplementedError('Alter column not yet implemented for '+str(ddlcompiler.dialect.name))
|
||||
|
||||
raise NotImplementedError('Alter column not yet implemented for ' + str(ddlcompiler.dialect.name))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue