blob: 371397d264797cdef6461e746c5e9bacbf20c957 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001####################
2Database Forge Class
3####################
4
Andrey Andreeveaa60c72012-11-06 01:11:22 +02005The Database Forge Class contains methods that help you manage your
Derek Jones8ede1a22011-10-05 13:34:52 -05006database.
7
8.. contents:: Table of Contents
James L Parry4828f892014-11-25 12:53:53 -08009 :depth: 3
Derek Jones8ede1a22011-10-05 13:34:52 -050010
11****************************
12Initializing the Forge Class
13****************************
14
15.. important:: In order to initialize the Forge class, your database
16 driver must already be running, since the forge class relies on it.
17
18Load the Forge Class as follows::
19
20 $this->load->dbforge()
21
Andrey Andreeveaa60c72012-11-06 01:11:22 +020022You can also pass another database object to the DB Forge loader, in case
23the database you want to manage isn't the default one::
24
25 $this->myforge = $this->load->dbforge($this->other_db, TRUE);
26
27In the above example, we're passing a custom database object as the first
28parameter and then tell it to return the dbforge object, instead of
29assigning it directly to ``$this->dbforge``.
30
31.. note:: Both of the parameters can be used individually, just pass an empty
32 value as the first one if you wish to skip it.
33
34Once initialized you will access the methods using the ``$this->dbforge``
Derek Jones8ede1a22011-10-05 13:34:52 -050035object::
36
Andrey Andreeveaa60c72012-11-06 01:11:22 +020037 $this->dbforge->some_method();
Derek Jones8ede1a22011-10-05 13:34:52 -050038
James L Parry4828f892014-11-25 12:53:53 -080039*******************************
40Creating and Dropping Databases
41*******************************
42
43**$this->dbforge->create_database('db_name')**
Derek Jones8ede1a22011-10-05 13:34:52 -050044
45Permits you to create the database specified in the first parameter.
46Returns TRUE/FALSE based on success or failure::
47
Joseph Wensleyf24f4042011-10-06 22:53:29 -040048 if ($this->dbforge->create_database('my_db'))
49 {
50 echo 'Database created!';
51 }
Derek Jones8ede1a22011-10-05 13:34:52 -050052
James L Parry4828f892014-11-25 12:53:53 -080053**$this->dbforge->drop_database('db_name')**
Derek Jones8ede1a22011-10-05 13:34:52 -050054
55Permits you to drop the database specified in the first parameter.
56Returns TRUE/FALSE based on success or failure::
57
Joseph Wensleyf24f4042011-10-06 22:53:29 -040058 if ($this->dbforge->drop_database('my_db'))
59 {
60 echo 'Database deleted!';
61 }
Derek Jones8ede1a22011-10-05 13:34:52 -050062
James L Parry4828f892014-11-25 12:53:53 -080063
Derek Jones8ede1a22011-10-05 13:34:52 -050064****************************
65Creating and Dropping Tables
66****************************
67
68There are several things you may wish to do when creating tables. Add
69fields, add keys to the table, alter columns. CodeIgniter provides a
70mechanism for this.
71
72Adding fields
73=============
74
75Fields are created via an associative array. Within the array you must
76include a 'type' key that relates to the datatype of the field. For
77example, INT, VARCHAR, TEXT, etc. Many datatypes (for example VARCHAR)
78also require a 'constraint' key.
79
80::
81
Joseph Wensleyf24f4042011-10-06 22:53:29 -040082 $fields = array(
83 'users' => array(
84 'type' => 'VARCHAR',
85 'constraint' => '100',
86 ),
87 );
88 // will translate to "users VARCHAR(100)" when the field is added.
Derek Jones8ede1a22011-10-05 13:34:52 -050089
90
91Additionally, the following key/values can be used:
92
93- unsigned/true : to generate "UNSIGNED" in the field definition.
94- default/value : to generate a default value in the field definition.
95- null/true : to generate "NULL" in the field definition. Without this,
96 the field will default to "NOT NULL".
97- auto_increment/true : generates an auto_increment flag on the
98 field. Note that the field type must be a type that supports this,
99 such as integer.
100
101::
102
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400103 $fields = array(
104 'blog_id' => array(
105 'type' => 'INT',
106 'constraint' => 5,
107 'unsigned' => TRUE,
108 'auto_increment' => TRUE
109 ),
110 'blog_title' => array(
111 'type' => 'VARCHAR',
112 'constraint' => '100',
113 ),
114 'blog_author' => array(
115 'type' =>'VARCHAR',
116 'constraint' => '100',
117 'default' => 'King of Town',
118 ),
119 'blog_description' => array(
120 'type' => 'TEXT',
121 'null' => TRUE,
122 ),
123 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
125
126After the fields have been defined, they can be added using
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200127``$this->dbforge->add_field($fields);`` followed by a call to the
128``create_table()`` method.
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
James L Parry4828f892014-11-25 12:53:53 -0800130**$this->dbforge->add_field()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200132The add fields method will accept the above array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
James L Parry4828f892014-11-25 12:53:53 -0800134
Derek Jones8ede1a22011-10-05 13:34:52 -0500135Passing strings as fields
136-------------------------
137
138If you know exactly how you want a field to be created, you can pass the
139string into the field definitions with add_field()
140
141::
142
143 $this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT 'default label'");
144
145
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400146.. note:: Multiple calls to add_field() are cumulative.
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
148Creating an id field
149--------------------
150
151There is a special exception for creating id fields. A field with type
152id will automatically be assinged as an INT(9) auto_incrementing
153Primary Key.
154
155::
156
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400157 $this->dbforge->add_field('id');
158 // gives id INT(9) NOT NULL AUTO_INCREMENT
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
160
161Adding Keys
162===========
163
164Generally speaking, you'll want your table to have Keys. This is
165accomplished with $this->dbforge->add_key('field'). An optional second
166parameter set to TRUE will make it a primary key. Note that add_key()
167must be followed by a call to create_table().
168
169Multiple column non-primary keys must be sent as an array. Sample output
170below is for MySQL.
171
172::
173
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400174 $this->dbforge->add_key('blog_id', TRUE);
175 // gives PRIMARY KEY `blog_id` (`blog_id`)
176
177 $this->dbforge->add_key('blog_id', TRUE);
178 $this->dbforge->add_key('site_id', TRUE);
179 // gives PRIMARY KEY `blog_id_site_id` (`blog_id`, `site_id`)
180
181 $this->dbforge->add_key('blog_name');
182 // gives KEY `blog_name` (`blog_name`)
183
184 $this->dbforge->add_key(array('blog_name', 'blog_label'));
185 // gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`)
Derek Jones8ede1a22011-10-05 13:34:52 -0500186
James L Parry4828f892014-11-25 12:53:53 -0800187
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
189Creating a table
190================
191
192After fields and keys have been declared, you can create a new table
193with
194
195::
196
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400197 $this->dbforge->create_table('table_name');
198 // gives CREATE TABLE table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500199
200
201An optional second parameter set to TRUE adds an "IF NOT EXISTS" clause
202into the definition
203
204::
205
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400206 $this->dbforge->create_table('table_name', TRUE);
207 // gives CREATE TABLE IF NOT EXISTS table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500208
Andrey Andreev27f798b2014-01-20 18:19:13 +0200209You could also pass optional table attributes, such as MySQL's ``ENGINE``::
210
211 $attributes = array('ENGINE' => 'InnoDB');
212 $this->dbforge->create_table('table_name', FALSE, $attributes);
213 // produces: CREATE TABLE `table_name` (...) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
214
215.. note:: Unless you specify the ``CHARACTER SET`` and/or ``COLLATE`` attributes,
216 ``create_table()`` will always add them with your configured *char_set*
217 and *dbcollat* values, as long as they are not empty (MySQL only).
Derek Jones8ede1a22011-10-05 13:34:52 -0500218
James L Parry4828f892014-11-25 12:53:53 -0800219
Derek Jones8ede1a22011-10-05 13:34:52 -0500220Dropping a table
221================
222
Andrey Andreeva287a342012-11-05 23:19:59 +0200223Execute a DROP TABLE statement and optionally add an IF EXISTS clause.
Derek Jones8ede1a22011-10-05 13:34:52 -0500224
225::
226
Andrey Andreeva287a342012-11-05 23:19:59 +0200227 // Produces: DROP TABLE table_name
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400228 $this->dbforge->drop_table('table_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500229
Andrey Andreeva287a342012-11-05 23:19:59 +0200230 // Produces: DROP TABLE IF EXISTS table_name
231 $this->dbforge->drop_table('table_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500232
James L Parry4828f892014-11-25 12:53:53 -0800233
Derek Jones8ede1a22011-10-05 13:34:52 -0500234Renaming a table
235================
236
237Executes a TABLE rename
238
239::
240
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400241 $this->dbforge->rename_table('old_table_name', 'new_table_name');
242 // gives ALTER TABLE old_table_name RENAME TO new_table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500243
James L Parry4828f892014-11-25 12:53:53 -0800244
Derek Jones8ede1a22011-10-05 13:34:52 -0500245
246****************
247Modifying Tables
248****************
249
James L Parry4828f892014-11-25 12:53:53 -0800250Adding a Column to a Table
251==========================
252
253**$this->dbforge->add_column()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500254
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200255The ``add_column()`` method is used to modify an existing table. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500256accepts the same field array as above, and can be used for an unlimited
257number of additional fields.
258
259::
260
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400261 $fields = array(
262 'preferences' => array('type' => 'TEXT')
263 );
264 $this->dbforge->add_column('table_name', $fields);
Andrey Andreevb67277b2012-11-12 12:51:14 +0200265 // Executes: ALTER TABLE table_name ADD preferences TEXT
266
267If you are using MySQL or CUBIRD, then you can take advantage of their
268AFTER and FIRST clauses to position the new column.
269
270Examples::
271
272 // Will place the new column after the `another_field` column:
273 $fields = array(
274 'preferences' => array('type' => 'TEXT', 'after' => 'another_field')
275 );
276
277 // Will place the new column at the start of the table definition:
278 $fields = array(
279 'preferences' => array('type' => 'TEXT', 'first' => TRUE)
280 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500281
James L Parry4828f892014-11-25 12:53:53 -0800282
283Dropping a Column From a Table
284==============================
285
286**$this->dbforge->drop_column()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500287
288Used to remove a column from a table.
289
290::
291
292 $this->dbforge->drop_column('table_name', 'column_to_drop');
293
294
James L Parry4828f892014-11-25 12:53:53 -0800295
296Modifying a Column in a Table
297=============================
298
299**$this->dbforge->modify_column()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500300
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200301The usage of this method is identical to ``add_column()``, except it
Derek Jones8ede1a22011-10-05 13:34:52 -0500302alters an existing column rather than adding a new one. In order to
303change the name you can add a "name" key into the field defining array.
304
305::
306
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400307 $fields = array(
308 'old_name' => array(
309 'name' => 'new_name',
310 'type' => 'TEXT',
311 ),
312 );
313 $this->dbforge->modify_column('table_name', $fields);
James L Parry4828f892014-11-25 12:53:53 -0800314 // gives ALTER TABLE table_name CHANGE old_name new_name TEXT
315
James L Parryd96616c2014-12-03 01:17:04 -0800316
317***************
318Class Reference
319***************
320
321.. class:: DB_forge
322
323 .. method:: __construct(&$db)
324
325 :param object $db: Database object
326 :returns: DB_forge object for the specified database
327 :rtype: DB_forge
328
329 Initializes a database forge.
330
331 .. method:: add_column($table = '', $field = array(), $_after = NULL)
332
333 :param string $table: Table name
334 :param array $field: Column definitions
335 :param string $_after: Column for AFTER clause (deprecated)
336 :returns: TRUE on success, FALSE on failure
337 :rtype: boolean
338
339 Add a column to a table. Usage: See `Adding a Column to a Table`_.
340
341 .. method:: add_field($field = '')
342
343 :param array $field: Field to add
344 :returns: DB_forge instance
345 :rtype: object
346
347 Add a field to the set that will be used to create a table. Usage: See `Adding fields`_.
348
349 .. method:: add_key($key = '', $primary = FALSE)
350
351 :param array $key: Name of a key field
352 :param boolean $primary: TRUE if this key is to be a primary key
353 :returns: DB_forge instance
354 :rtype: object
355
356 Specify a key field to be used to create a table. Usage: See `Adding Keys`_.
357
358 .. method:: create_database($db_name)
359
360 :param string $db_name: Name of the database to create
361 :returns: TRUE on success, FALSE on failure
362 :rtype: boolean
363
364 Create a new database. Usage: See `Creating and Dropping Databases`_.
365
366 .. method:: create_table($table = '', $if_not_exists = FALSE, array $attributes = array())
367
368 :param string $table: Name of the table to create
369 :param string $if_not_exists: TRUE to add an 'IF NOT EXISTS' clause
370 :param string $attributes: Associative array of table attributes
371 :returns: DB_driver on success, FALSE on failure
372 :rtype: mixed
373
374 Create a new table. Usage: See `Creating a table`_.
375
376 .. method:: drop_column($table = '', $column_name = '')
377
378 :param string $table: Table name
379 :param array $column_name: Column to drop
380 :returns: DB_driver on success, FALSE on failure
381 :rtype: mixed
382
383 Drop a column from a table. Usage: See `Dropping a Column From a Table`_.
384
385 .. method:: drop_database($db_name)
386
387 :param string $db_name: Name of the database to drop
388 :returns: TRUE on success, FALSE on failure
389 :rtype: boolean
390
391 Drop a database. Usage: See `Creating and Dropping Databases`_.
392
393 .. method:: drop_table($table_name, $if_exists = FALSE)
394
395 :param string $table: Name of the table to create
396 :param string $if_exists: TRUE to add an 'IF EXISTS' clause
397 :returns: DB_driver on success, FALSE on failure
398 :rtype: mixed
399
400 Drop a table. Usage: See `Dropping a table`_.
401
402 .. method:: modify_column($table = '', $field = array())
403
404 :param string $table: Table name
405 :param array $field: Column definitions
406 :returns: TRUE on success, FALSE on failure
407 :rtype: boolean
408
409 Modify a column in a table. Usage: See `Modifying a Column in a Table`_.
410
411 .. method:: rename_table($table_name, $new_table_name)
412
413 :param string $table: Name of the table
414 :param string $new_table_name: New name of the table
415 :returns: DB_driver on success, FALSE on failure
416 :rtype: mixed
417
418 Rename a table. Usage: See `Renaming a table`_.
419
420
James L Parry4828f892014-11-25 12:53:53 -0800421