@@ -47,6 +47,7 @@ var MysqlDriver = Base.extend({
47
47
} ,
48
48
49
49
createColumnDef : function ( name , spec , options ) {
50
+ name = util . format ( '`%s`' , name ) ;
50
51
var t = this . mapDataType ( spec ) ;
51
52
var len ;
52
53
if ( spec . type !== type . TEXT && spec . type !== type . BLOB ) {
@@ -97,13 +98,106 @@ var MysqlDriver = Base.extend({
97
98
return constraint . join ( ' ' ) ;
98
99
} ,
99
100
101
+ createTable : function ( tableName , options , callback ) {
102
+ log . verbose ( 'creating table:' , tableName ) ;
103
+ var columnSpecs = options ;
104
+ var tableOptions = { } ;
105
+
106
+ if ( options . columns !== undefined ) {
107
+ columnSpecs = options . columns ;
108
+ delete options . columns ;
109
+ tableOptions = options ;
110
+ }
111
+
112
+ var ifNotExistsSql = "" ;
113
+ if ( tableOptions . ifNotExists ) {
114
+ ifNotExistsSql = "IF NOT EXISTS" ;
115
+ }
116
+
117
+ var primaryKeyColumns = [ ] ;
118
+ var columnDefOptions = {
119
+ emitPrimaryKey : false
120
+ } ;
121
+
122
+ for ( var columnName in columnSpecs ) {
123
+ var columnSpec = this . normalizeColumnSpec ( columnSpecs [ columnName ] ) ;
124
+ columnSpecs [ columnName ] = columnSpec ;
125
+ if ( columnSpec . primaryKey ) {
126
+ primaryKeyColumns . push ( columnName ) ;
127
+ }
128
+ }
129
+
130
+ var pkSql = '' ;
131
+ if ( primaryKeyColumns . length > 1 ) {
132
+ pkSql = util . format ( ', PRIMARY KEY (`%s`)' , primaryKeyColumns . join ( '`, `' ) ) ;
133
+ } else {
134
+ columnDefOptions . emitPrimaryKey = true ;
135
+ }
136
+
137
+ var columnDefs = [ ] ;
138
+ for ( var columnName in columnSpecs ) {
139
+ var columnSpec = columnSpecs [ columnName ] ;
140
+ columnDefs . push ( this . createColumnDef ( columnName , columnSpec , columnDefOptions ) ) ;
141
+ }
142
+
143
+ var sql = util . format ( 'CREATE TABLE %s `%s` (%s%s)' , ifNotExistsSql , tableName , columnDefs . join ( ', ' ) , pkSql ) ;
144
+ this . runSql ( sql , callback ) ;
145
+ } ,
146
+
100
147
renameTable : function ( tableName , newTableName , callback ) {
101
- var sql = util . format ( 'RENAME TABLE %s TO %s' , tableName , newTableName ) ;
148
+ var sql = util . format ( 'RENAME TABLE `%s` TO `%s`' , tableName , newTableName ) ;
149
+ this . runSql ( sql , callback ) ;
150
+ } ,
151
+
152
+ addColumn : function ( tableName , columnName , columnSpec , callback ) {
153
+ var def = this . createColumnDef ( columnName , this . normalizeColumnSpec ( columnSpec ) ) ;
154
+ var sql = util . format ( 'ALTER TABLE `%s` ADD COLUMN %s' , tableName , def ) ;
102
155
this . runSql ( sql , callback ) ;
103
156
} ,
104
157
105
158
removeColumn : function ( tableName , columnName , callback ) {
106
- var sql = util . format ( 'ALTER TABLE %s DROP COLUMN %s' , tableName , columnName ) ;
159
+ var sql = util . format ( 'ALTER TABLE `%s` DROP COLUMN `%s`' , tableName , columnName ) ;
160
+ this . runSql ( sql , callback ) ;
161
+ } ,
162
+
163
+ addIndex : function ( tableName , indexName , columns , unique , callback ) {
164
+ if ( typeof ( unique ) === 'function' ) {
165
+ callback = unique ;
166
+ unique = false ;
167
+ }
168
+
169
+ if ( ! Array . isArray ( columns ) ) {
170
+ columns = [ columns ] ;
171
+ }
172
+ var sql = util . format ( 'CREATE %s INDEX `%s` ON `%s` (`%s`)' , ( unique ? 'UNIQUE' : '' ) , indexName , tableName , columns . join ( '`, `' ) ) ;
173
+ this . runSql ( sql , callback ) ;
174
+ } ,
175
+
176
+ insert : function ( tableName , columnNameArray , valueArray , callback ) {
177
+ if ( columnNameArray . length !== valueArray . length ) {
178
+ return callback ( new Error ( 'The number of columns does not match the number of values.' ) ) ;
179
+ }
180
+
181
+ var sql = util . format ( 'INSERT INTO `%s` ' , tableName ) ;
182
+ var columnNames = '(' ;
183
+ var values = 'VALUES (' ;
184
+
185
+ for ( var index in columnNameArray ) {
186
+ columnNames += '`' + columnNameArray [ index ] + '`' ;
187
+
188
+ if ( typeof ( valueArray [ index ] ) === 'string' ) {
189
+ values += "'" + valueArray [ index ] . replace ( "'" , "''" ) + "'" ;
190
+ } else {
191
+ values += valueArray [ index ] ;
192
+ }
193
+
194
+ if ( index != columnNameArray . length - 1 ) {
195
+ columnNames += "," ;
196
+ values += "," ;
197
+ }
198
+ }
199
+
200
+ sql += columnNames + ') ' + values + ');' ;
107
201
this . runSql ( sql , callback ) ;
108
202
} ,
109
203
@@ -118,28 +212,28 @@ var MysqlDriver = Base.extend({
118
212
return ;
119
213
}
120
214
121
- var sql = util . format ( 'DROP INDEX %s ON %s ' , indexName , tableName ) ;
215
+ var sql = util . format ( 'DROP INDEX `%s` ON `%s` ' , indexName , tableName ) ;
122
216
this . runSql ( sql , callback ) ;
123
217
} ,
124
218
125
219
renameColumn : function ( tableName , oldColumnName , newColumnName , callback ) {
126
220
var self = this , columnTypeSql = util . format ( "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '%s' AND COLUMN_NAME = '%s'" , tableName , oldColumnName ) ;
127
221
this . all ( columnTypeSql , function ( err , result ) {
128
222
var columnType = result [ 0 ] . COLUMN_TYPE ;
129
- var alterSql = util . format ( "ALTER TABLE %s CHANGE %s %s %s" , tableName , oldColumnName , newColumnName , columnType ) ;
223
+ var alterSql = util . format ( "ALTER TABLE `%s` CHANGE `%s` `%s` %s" , tableName , oldColumnName , newColumnName , columnType ) ;
130
224
self . runSql ( alterSql , callback ) ;
131
225
} ) ;
132
226
} ,
133
227
134
228
changeColumn : function ( tableName , columnName , columnSpec , callback ) {
135
229
var constraint = this . createColumnDef ( columnName , columnSpec ) ;
136
- var sql = util . format ( 'ALTER TABLE %s CHANGE COLUMN %s %s' , tableName , columnName , constraint ) ;
230
+ var sql = util . format ( 'ALTER TABLE `%s` CHANGE COLUMN `%s` %s' , tableName , columnName , constraint ) ;
137
231
this . runSql ( sql , callback ) ;
138
232
} ,
139
233
140
234
addMigrationRecord : function ( name , callback ) {
141
235
var formattedDate = moment ( new Date ( ) ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
142
- this . runSql ( 'INSERT INTO migrations (name, run_on) VALUES (?, ?)' , [ name , formattedDate ] , callback ) ;
236
+ this . runSql ( 'INSERT INTO migrations (` name`, ` run_on` ) VALUES (?, ?)' , [ name , formattedDate ] , callback ) ;
143
237
} ,
144
238
145
239
runSql : function ( ) {
0 commit comments