Browse Source

PEP8 sur sqlalter.py

Roland Haroutiounian 9 years ago
parent
commit
c39e4ef7a6
1 changed files with 22 additions and 15 deletions
  1. 22
    15
      Database/sqlalter.py

+ 22
- 15
Database/sqlalter.py View File

@@ -8,13 +8,13 @@ from sqlalchemy.ext.compiler import compiles
8 8
 
9 9
 ## @file sqlalter.py
10 10
 # This file defines all DDL (data definition langage) for the ALTER TABLE instructions
11
-# 
11
+#
12 12
 # It uses the SqlAlchemy compilation and quoting methos to generate SQL
13 13
 
14 14
 
15 15
 class AddColumn(sqla.schema.DDLElement):
16 16
     """ Defines the ddl for adding a column to a table """
17
-    def __init__(self,table, column):
17
+    def __init__(self, table, column):
18 18
         """ Instanciate the DDL
19 19
             @param table sqlalchemy.Table: A sqlalchemy table object
20 20
             @param column sqlalchemy.Column: A sqlalchemy column object
@@ -22,6 +22,7 @@ class AddColumn(sqla.schema.DDLElement):
22 22
         self.col = column
23 23
         self.table = table
24 24
 
25
+
25 26
 @compiles(AddColumn, 'mysql')
26 27
 @compiles(AddColumn, 'postgresql')
27 28
 @compiles(AddColumn, 'sqlite')
@@ -30,11 +31,13 @@ def visit_add_column(element, ddlcompiler, **kw):
30 31
     prep = ddlcompiler.sql_compiler.preparer
31 32
     tname = prep.format_table(element.table)
32 33
     colname = prep.format_column(element.col)
33
-    return 'ALTER TABLE %s ADD COLUMN %s %s'%(tname,  colname, element.col.type)
34
+    return 'ALTER TABLE %s ADD COLUMN %s %s' % (tname, colname, element.col.type)
35
+
34 36
 
35 37
 @compiles(AddColumn)
36 38
 def visit_add_column(element, ddlcompiler, **kw):
37
-    raise NotImplementedError('Add column not yet implemented for '+str(ddlcompiler.dialect.name))
39
+    raise NotImplementedError('Add column not yet implemented for ' + str(ddlcompiler.dialect.name))
40
+
38 41
 
39 42
 class DropColumn(sqla.schema.DDLElement):
40 43
     """ Defines the DDL for droping a column from a table """
@@ -46,14 +49,16 @@ class DropColumn(sqla.schema.DDLElement):
46 49
         self.col = column
47 50
         self.table = table
48 51
 
49
-@compiles(DropColumn,'mysql')
52
+
53
+@compiles(DropColumn, 'mysql')
50 54
 @compiles(DropColumn, 'postgresql')
51 55
 def visit_drop_column(element, ddlcompiler, **kw):
52 56
     """ Compiles the DropColumn DDL for mysql & postgresql """
53 57
     prep = ddlcompiler.sql_compiler.preparer
54 58
     tname = prep.format_table(element.table)
55 59
     colname = prep.format_column(element.col)
56
-    return 'ALTER TABLE %s DROP COLUMN %s'%(tname, colname)
60
+    return 'ALTER TABLE %s DROP COLUMN %s' % (tname, colname)
61
+
57 62
 
58 63
 @compiles(DropColumn)
59 64
 ##
@@ -63,11 +68,11 @@ def visit_drop_column(element, ddlcompiler, **kw):
63 68
     tname = prep.format_table(element.table)
64 69
 
65 70
     #Temporary table
66
-    tmpname = str(element.table)+'_copydropcol'
71
+    tmpname = str(element.table) + '_copydropcol'
67 72
     tmpTable = sqla.Table(tmpname, sqla.MetaData())
68 73
     tmptname = prep.format_table(tmpTable)
69 74
 
70
-    query =  'ALTER TABLE %s RENAME TO %s; '%(tname, tmptname)
75
+    query = 'ALTER TABLE %s RENAME TO %s; ' % (tname, tmptname)
71 76
 
72 77
     colname = prep.format_column(element.col)
73 78
 
@@ -80,13 +85,13 @@ def visit_drop_column(element, ddlcompiler, **kw):
80 85
         if col.name != element.col.name:
81 86
             newtable.append_column(col.copy())
82 87
             cols.append(prep.format_column(col))
83
-    cols=', '.join(cols)
88
+    cols = ', '.join(cols)
84 89
 
85
-    query += str(sqla.schema.CreateTable(newtable).compile(dialect = ddlcompiler.dialect))+';'
90
+    query += str(sqla.schema.CreateTable(newtable).compile(dialect=ddlcompiler.dialect)) + ';'
86 91
 
87 92
     query += 'INSERT INTO %s ( %s ) SELECT %s FROM %s;' % (newtable, cols, cols, tmptname)
88 93
 
89
-    query += 'DROP TABLE %s'% (tmpname)
94
+    query += 'DROP TABLE %s' % (tmpname)
90 95
 
91 96
     return query
92 97
 
@@ -103,13 +108,15 @@ class AlterColumn(sqla.schema.DDLElement):
103 108
         self.col = column
104 109
         self.table = table
105 110
 
111
+
106 112
 @compiles(AlterColumn, 'mysql')
107 113
 def visit_alter_column(element, ddlcompiler, **kw):
108 114
     """ Compiles the AlterColumn DDL for mysql """
109 115
     prep = ddlcompiler.sql_compiler.preparer
110 116
     tname = prep.format_table(element.table)
111 117
     colname = prep.format_column(element.col)
112
-    return 'ALTER TABLE %s ALTER COLUMN %s %s'%(tname, colname, element.col.type)
118
+    return 'ALTER TABLE %s ALTER COLUMN %s %s' % (tname, colname, element.col.type)
119
+
113 120
 
114 121
 @compiles(AlterColumn, 'postgresql')
115 122
 def visit_alter_column(element, ddlcompiler, **kw):
@@ -117,9 +124,9 @@ def visit_alter_column(element, ddlcompiler, **kw):
117 124
     prep = ddlcompiler.sql_compiler.preparer
118 125
     tname = prep.format_table(element.table)
119 126
     colname = prep.format_column(element.col)
120
-    return 'ALTER TABLE %s ALTER COLUMN %s TYPE %s'%(tname, colname, element.col.type)
127
+    return 'ALTER TABLE %s ALTER COLUMN %s TYPE %s' % (tname, colname, element.col.type)
128
+
121 129
 
122 130
 @compiles(AlterColumn)
123 131
 def visit_alter_column(element, ddlcompiler, **kw):
124
-    raise NotImplementedError('Alter column not yet implemented for '+str(ddlcompiler.dialect.name))
125
-
132
+    raise NotImplementedError('Alter column not yet implemented for ' + str(ddlcompiler.dialect.name))

Loading…
Cancel
Save