5
5
*/
6
6
class MIDASUpgrade
7
7
{
8
-
9
8
protected $ db ;
9
+ protected $ dbtype ;
10
10
11
11
/**
12
12
* @method public __construct()
@@ -21,6 +21,7 @@ public function __construct($db, $module)
21
21
{
22
22
$ this ->loadModuleElements ();
23
23
}
24
+ $ this ->dbtype = Zend_Registry::get ('configDatabase ' )->database ->adapter ;
24
25
} // end __construct()
25
26
26
27
/** preUpgrade called before the upgrade*/
@@ -148,5 +149,155 @@ public function loadElements()
148
149
}
149
150
}
150
151
}//end loadElements
151
- } //end class MIDASDatabasePdo
152
+
153
+
154
+ /**
155
+ * @method public AddTableField()
156
+ * Add a field to a table
157
+ */
158
+ function AddTableField ($ table ,$ field ,$ mySQLType ,$ pgSqlType ,$ default )
159
+ {
160
+ $ sql = '' ;
161
+ if ($ default !== false )
162
+ {
163
+ $ sql = " DEFAULT ' " .$ default ."' " ;
164
+ }
165
+
166
+ if ($ this ->dbtype == "PDO_PGSQL " )
167
+ {
168
+ $ this ->db ->query ("ALTER TABLE \"" .$ table ."\" ADD \"" .$ field ."\" " .$ pgSqlType .$ sql );
169
+ }
170
+ else
171
+ {
172
+ $ this ->db ->query ("ALTER TABLE " .$ table ." ADD " .$ field ." " .$ mySQLType .$ sql );
173
+ }
174
+ }
175
+
176
+ /**
177
+ * @method public RemoveTableField()
178
+ * Remove a field from a table
179
+ */
180
+ function RemoveTableField ($ table ,$ field )
181
+ {
182
+ if ($ this ->dbtype == "PDO_PGSQL " )
183
+ {
184
+ $ this ->db ->query ("ALTER TABLE \"" .$ table ."\" DROP COLUMN \"" .$ field ."\"" );
185
+ }
186
+ else
187
+ {
188
+ $ this ->db ->query ("ALTER TABLE " .$ table ." DROP " .$ field );
189
+ }
190
+ }
191
+
192
+ /**
193
+ * @method public RenameTableField()
194
+ * Rename a field from a table
195
+ */
196
+ function RenameTableField ($ table ,$ field ,$ newfield ,$ mySQLType ,$ pgSqlType ,$ default )
197
+ {
198
+ if ($ this ->dbtype == "PDO_PGSQL " )
199
+ {
200
+ $ this ->db ->query ("ALTER TABLE \"" .$ table ."\" RENAME \"" .$ field ."\" TO \"" .$ newfield ."\"" );
201
+ $ this ->db ->query ("ALTER TABLE \"" .$ table ."\" ALTER COLUMN \"" .$ newfield ."\" TYPE " .$ pgSqlType );
202
+ $ this ->db ->query ("ALTER TABLE \"" .$ table ."\" ALTER COLUMN \"" .$ newfield ."\" SET DEFAULT " .$ default );
203
+ }
204
+ else
205
+ {
206
+ $ this ->db ->query ("ALTER TABLE " .$ table ." CHANGE " .$ field ." " .$ newfield ." " .$ mySQLType ." DEFAULT ' " .$ default ."' " );
207
+ }
208
+ }
209
+
210
+ /**
211
+ * @method public CheckIndexExists()
212
+ * Check if the index exists.
213
+ * Only works for MySQL
214
+ */
215
+ function CheckIndexExists ($ table ,$ field )
216
+ {
217
+ if ($ this ->dbtype == "PDO_MYSQL " )
218
+ {
219
+ $ rowset = $ this ->db ->fetchAll ("SHOW INDEX FROM " .$ tablename );
220
+ foreach ($ rowset as $ index_array )
221
+ {
222
+ if ($ index_array ['Column_name ' ] == $ columnname )
223
+ {
224
+ return true ;
225
+ }
226
+ }
227
+ }
228
+ return false ;
229
+ } // end CheckIndexExists()
230
+
231
+ /**
232
+ * @method public AddTableIndex()
233
+ * Add an index to a table
234
+ */
235
+ function AddTableIndex ($ table ,$ field )
236
+ {
237
+ if (!$ this ->CheckIndexExists ($ table ,$ field ))
238
+ {
239
+ if ($ this ->dbtype == "PDO_PGSQL " )
240
+ {
241
+ @$ this ->db ->query ("CREATE INDEX " .$ table ."_ " .$ field ."_idx ON \"" .$ table ."\" ( \"" .$ field ."\") " );
242
+ }
243
+ else
244
+ {
245
+ $ this ->db ->query ("ALTER TABLE " .$ table ." ADD INDEX ( " .$ field ." ) " );
246
+ }
247
+ }
248
+ }
249
+
250
+ /**
251
+ * @method public RemoveTableIndex()
252
+ * Remove an index from a table
253
+ */
254
+ function RemoveTableIndex ($ table ,$ field )
255
+ {
256
+ if ($ this ->CheckIndexExists ($ table ,$ field ))
257
+ {
258
+ if ($ this ->dbtype == "PDO_PGSQL " )
259
+ {
260
+ $ this ->db ->query ("DROP INDEX " .$ table ."_ " .$ field ."_idx " );
261
+ }
262
+ else
263
+ {
264
+ $ this ->db ->query ("ALTER TABLE " .$ table ." DROP INDEX " .$ field );
265
+ }
266
+ }
267
+ }
268
+
269
+ /**
270
+ * @method public AddTablePrimaryKey()
271
+ * Add a primary key to a table
272
+ */
273
+ function AddTablePrimaryKey ($ table ,$ field )
274
+ {
275
+ if ($ this ->dbtype == "PDO_PGSQL " )
276
+ {
277
+ $ this ->db ->query ("ALTER TABLE \"" .$ table ."\" ADD PRIMARY KEY ( \"" .$ field ."\") " );
278
+ }
279
+ else
280
+ {
281
+ $ this ->db ->query ("ALTER TABLE " .$ table ." ADD PRIMARY KEY ( " .$ field ." ) " );
282
+ }
283
+ }
284
+
285
+ /**
286
+ * @method public RemoveTablePrimaryKey()
287
+ * Remove a primary key from a table
288
+ */
289
+ function RemoveTablePrimaryKey ($ table )
290
+ {
291
+ if ($ this ->dbtype == "PDO_PGSQL " )
292
+ {
293
+ $ this ->db ->query ("ALTER TABLE \"" .$ table ."\" DROP CONSTRAINT \"value_pkey \"" );
294
+ $ this ->db ->query ("ALTER TABLE \"" .$ table ."\" DROP CONSTRAINT \"" .$ table ."_pkey \"" );
295
+ }
296
+ else
297
+ {
298
+ $ this ->db ->query ("ALTER TABLE " .$ table ." DROP PRIMARY KEY " );
299
+ }
300
+ }
301
+
302
+ } //end class MIDASUpgrade
152
303
?>
0 commit comments