blob: 2fa5239c8b1f6d66d7a8262faac4be5f3faaa2b8 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001######################
2Database Utility Class
3######################
4
Andrey Andreeveaa60c72012-11-06 01:11:22 +02005The Database Utility Class contains methods that help you manage your
Derek Jones8ede1a22011-10-05 13:34:52 -05006database.
7
James L Parry50d06492014-11-25 16:21:39 -08008.. contents::
9 :local:
10 :depth: 2
Derek Jones8ede1a22011-10-05 13:34:52 -050011
James L Parryee477c62014-11-25 16:15:18 -080012******************************
Derek Jones8ede1a22011-10-05 13:34:52 -050013Initializing the Utility Class
James L Parryee477c62014-11-25 16:15:18 -080014******************************
Derek Jones8ede1a22011-10-05 13:34:52 -050015
16.. important:: In order to initialize the Utility class, your database
17 driver must already be running, since the utilities class relies on it.
18
19Load the Utility Class as follows::
20
ftwbzhaoc836f2a2015-05-07 18:05:33 +080021 $this->load->dbutil();
Derek Jones8ede1a22011-10-05 13:34:52 -050022
Andrey Andreeveaa60c72012-11-06 01:11:22 +020023You can also pass another database object to the DB Utility loader, in case
24the database you want to manage isn't the default one::
25
26 $this->myutil = $this->load->dbutil($this->other_db, TRUE);
27
28In the above example, we're passing a custom database object as the first
29parameter and then tell it to return the dbutil object, instead of
30assigning it directly to ``$this->dbutil``.
31
32.. note:: Both of the parameters can be used individually, just pass an empty
33 value as the first one if you wish to skip it.
34
35Once initialized you will access the methods using the ``$this->dbutil``
Derek Jones8ede1a22011-10-05 13:34:52 -050036object::
37
ftwbzhaoc836f2a2015-05-07 18:05:33 +080038 $this->dbutil->some_method();
Derek Jones8ede1a22011-10-05 13:34:52 -050039
James L Parryee477c62014-11-25 16:15:18 -080040****************************
41Using the Database Utilities
42****************************
43
44Retrieve list of database names
Derek Jones8ede1a22011-10-05 13:34:52 -050045================================
46
47Returns an array of database names::
48
Joseph Wensleyf24f4042011-10-06 22:53:29 -040049 $dbs = $this->dbutil->list_databases();
Andrew Podner79494dd2012-12-19 14:15:41 -050050
Joseph Wensleyf24f4042011-10-06 22:53:29 -040051 foreach ($dbs as $db)
52 {
53 echo $db;
54 }
Derek Jones8ede1a22011-10-05 13:34:52 -050055
James L Parryee477c62014-11-25 16:15:18 -080056
57Determine If a Database Exists
58==============================
Derek Jones8ede1a22011-10-05 13:34:52 -050059
60Sometimes it's helpful to know whether a particular database exists.
61Returns a boolean TRUE/FALSE. Usage example::
62
Joseph Wensleyf24f4042011-10-06 22:53:29 -040063 if ($this->dbutil->database_exists('database_name'))
64 {
65 // some code...
66 }
Derek Jones8ede1a22011-10-05 13:34:52 -050067
Andrey Andreeveaa60c72012-11-06 01:11:22 +020068.. note:: Replace *database_name* with the name of the table you are
69 looking for. This method is case sensitive.
Derek Jones8ede1a22011-10-05 13:34:52 -050070
James L Parryee477c62014-11-25 16:15:18 -080071Optimize a Table
72================
Derek Jones8ede1a22011-10-05 13:34:52 -050073
74Permits you to optimize a table using the table name specified in the
75first parameter. Returns TRUE/FALSE based on success or failure::
76
Joseph Wensleyf24f4042011-10-06 22:53:29 -040077 if ($this->dbutil->optimize_table('table_name'))
78 {
79 echo 'Success!';
80 }
Derek Jones8ede1a22011-10-05 13:34:52 -050081
Andrey Andreeveaa60c72012-11-06 01:11:22 +020082.. note:: Not all database platforms support table optimization. It is
83 mostly for use with MySQL.
Derek Jones8ede1a22011-10-05 13:34:52 -050084
James L Parryee477c62014-11-25 16:15:18 -080085Repair a Table
86==============
Derek Jones8ede1a22011-10-05 13:34:52 -050087
88Permits you to repair a table using the table name specified in the
89first parameter. Returns TRUE/FALSE based on success or failure::
90
Joseph Wensleyf24f4042011-10-06 22:53:29 -040091 if ($this->dbutil->repair_table('table_name'))
92 {
93 echo 'Success!';
94 }
Derek Jones8ede1a22011-10-05 13:34:52 -050095
96.. note:: Not all database platforms support table repairs.
97
James L Parryee477c62014-11-25 16:15:18 -080098Optimize a Database
99===================
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
Derek Jones8ede1a22011-10-05 13:34:52 -0500101Permits you to optimize the database your DB class is currently
102connected to. Returns an array containing the DB status messages or
103FALSE on failure.
104
105::
106
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400107 $result = $this->dbutil->optimize_database();
Andrew Podner79494dd2012-12-19 14:15:41 -0500108
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400109 if ($result !== FALSE)
110 {
111 print_r($result);
112 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200114.. note:: Not all database platforms support table optimization. It
115 it is mostly for use with MySQL.
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
James L Parryee477c62014-11-25 16:15:18 -0800117Export a Query Result as a CSV File
118===================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
120Permits you to generate a CSV file from a query result. The first
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200121parameter of the method must contain the result object from your
Derek Jones8ede1a22011-10-05 13:34:52 -0500122query. Example::
123
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400124 $this->load->dbutil();
Andrew Podner79494dd2012-12-19 14:15:41 -0500125
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400126 $query = $this->db->query("SELECT * FROM mytable");
Andrew Podner79494dd2012-12-19 14:15:41 -0500127
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400128 echo $this->dbutil->csv_from_result($query);
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
130The second, third, and fourth parameters allow you to set the delimiter
mrw7310e852012-03-26 11:25:26 -0300131newline, and enclosure characters respectively. By default commas are
Derek Jones8ede1a22011-10-05 13:34:52 -0500132used as the delimiter, "\n" is used as a new line, and a double-quote
133is used as the enclosure. Example::
134
135 $delimiter = ",";
136 $newline = "\r\n";
137 $enclosure = '"';
138
139 echo $this->dbutil->csv_from_result($query, $delimiter, $newline, $enclosure);
140
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200141.. important:: This method will NOT write the CSV file for you. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500142 simply creates the CSV layout. If you need to write the file
143 use the :doc:`File Helper <../helpers/file_helper>`.
144
James L Parryee477c62014-11-25 16:15:18 -0800145Export a Query Result as an XML Document
146========================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
148Permits you to generate an XML file from a query result. The first
149parameter expects a query result object, the second may contain an
150optional array of config parameters. Example::
151
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400152 $this->load->dbutil();
Andrew Podner79494dd2012-12-19 14:15:41 -0500153
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400154 $query = $this->db->query("SELECT * FROM mytable");
Andrew Podner79494dd2012-12-19 14:15:41 -0500155
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400156 $config = array (
157 'root' => 'root',
158 'element' => 'element',
159 'newline' => "\n",
Andrew Podner79494dd2012-12-19 14:15:41 -0500160 'tab' => "\t"
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400161 );
Andrew Podner79494dd2012-12-19 14:15:41 -0500162
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400163 echo $this->dbutil->xml_from_result($query, $config);
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200165.. important:: This method will NOT write the XML file for you. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500166 simply creates the XML layout. If you need to write the file
167 use the :doc:`File Helper <../helpers/file_helper>`.
168
James L Parryee477c62014-11-25 16:15:18 -0800169********************
170Backup Your Database
171********************
172
173Database Backup Notes
174=====================
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
176Permits you to backup your full database or individual tables. The
177backup data can be compressed in either Zip or Gzip format.
178
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200179.. note:: This feature is only available for MySQL and Interbase/Firebird databases.
Timothy Warrenab189e12012-02-22 10:34:23 -0500180
181.. note:: For Interbase/Firebird databases, the backup file name is the only parameter.
Andrew Podner79494dd2012-12-19 14:15:41 -0500182
Andrey Andreev105749b2015-06-23 21:25:12 +0300183 $this->dbutil->backup('db_backup_filename');
Derek Jones8ede1a22011-10-05 13:34:52 -0500184
185.. note:: Due to the limited execution time and memory available to PHP,
186 backing up very large databases may not be possible. If your database is
187 very large you might need to backup directly from your SQL server via
188 the command line, or have your server admin do it for you if you do not
189 have root privileges.
190
191Usage Example
James L Parryee477c62014-11-25 16:15:18 -0800192=============
Derek Jones8ede1a22011-10-05 13:34:52 -0500193
194::
195
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400196 // Load the DB utility class
197 $this->load->dbutil();
Andrew Podner79494dd2012-12-19 14:15:41 -0500198
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400199 // Backup your entire database and assign it to a variable
Andrey Andreev105749b2015-06-23 21:25:12 +0300200 $backup = $this->dbutil->backup();
Andrew Podner79494dd2012-12-19 14:15:41 -0500201
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400202 // Load the file helper and write the file to your server
203 $this->load->helper('file');
204 write_file('/path/to/mybackup.gz', $backup);
Andrew Podner79494dd2012-12-19 14:15:41 -0500205
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400206 // Load the download helper and send the file to your desktop
207 $this->load->helper('download');
208 force_download('mybackup.gz', $backup);
Derek Jones8ede1a22011-10-05 13:34:52 -0500209
210Setting Backup Preferences
James L Parryee477c62014-11-25 16:15:18 -0800211==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500212
213Backup preferences are set by submitting an array of values to the first
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200214parameter of the ``backup()`` method. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500215
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400216 $prefs = array(
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200217 'tables' => array('table1', 'table2'), // Array of tables to backup.
218 'ignore' => array(), // List of tables to omit from the backup
219 'format' => 'txt', // gzip, zip, txt
220 'filename' => 'mybackup.sql', // File name - NEEDED ONLY WITH ZIP FILES
221 'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file
222 'add_insert' => TRUE, // Whether to add INSERT data to backup file
223 'newline' => "\n" // Newline character used in backup file
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400224 );
Andrew Podner79494dd2012-12-19 14:15:41 -0500225
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400226 $this->dbutil->backup($prefs);
Derek Jones8ede1a22011-10-05 13:34:52 -0500227
228Description of Backup Preferences
James L Parryee477c62014-11-25 16:15:18 -0800229=================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500230
Andrew Podner48512172012-12-20 07:56:19 -0500231======================= ======================= ======================= ========================================================================
232Preference Default Value Options Description
233======================= ======================= ======================= ========================================================================
234**tables** empty array None An array of tables you want backed up. If left blank all tables will be
235 exported.
236**ignore** empty array None An array of tables you want the backup routine to ignore.
237**format** gzip gzip, zip, txt The file format of the export file.
238**filename** the current date/time None The name of the backed-up file. The name is needed only if you are using
239 zip compression.
240**add_drop** TRUE TRUE/FALSE Whether to include DROP TABLE statements in your SQL export file.
241**add_insert** TRUE TRUE/FALSE Whether to include INSERT statements in your SQL export file.
242**newline** "\\n" "\\n", "\\r", "\\r\\n" Type of newline to use in your SQL export file.
243**foreign_key_checks** TRUE TRUE/FALSE Whether output should keep foreign key checks enabled.
James L Parryee477c62014-11-25 16:15:18 -0800244======================= ======================= ======================= ========================================================================
245
246***************
247Class Reference
248***************
249
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200250.. php:class:: CI_DB_utility
James L Parryee477c62014-11-25 16:15:18 -0800251
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200252 .. php:method:: backup([$params = array()])
James L Parryee477c62014-11-25 16:15:18 -0800253
Andrey Andreevb9061492014-12-04 16:33:24 +0200254 :param array $params: An associative array of options
Gabriel Potkány40bbd602015-02-04 14:06:47 +0100255 :returns: raw/(g)zipped SQL query string
Gabriel Potkánycea5fb72015-02-04 08:22:06 +0100256 :rtype: string
James L Parryee477c62014-11-25 16:15:18 -0800257
Andrey Andreevb9061492014-12-04 16:33:24 +0200258 Perform a database backup, per user preferences.
James L Parryee477c62014-11-25 16:15:18 -0800259
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200260 .. php:method:: database_exists($database_name)
James L Parryee477c62014-11-25 16:15:18 -0800261
Andrey Andreevb9061492014-12-04 16:33:24 +0200262 :param string $database_name: Database name
James L Parryee477c62014-11-25 16:15:18 -0800263 :returns: TRUE if the database exists, FALSE otherwise
Andrey Andreevb9061492014-12-04 16:33:24 +0200264 :rtype: bool
James L Parryee477c62014-11-25 16:15:18 -0800265
Andrey Andreevb9061492014-12-04 16:33:24 +0200266 Check for the existence of a database.
James L Parryee477c62014-11-25 16:15:18 -0800267
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200268 .. php:method:: list_databases()
James L Parryee477c62014-11-25 16:15:18 -0800269
270 :returns: Array of database names found
Andrey Andreevb9061492014-12-04 16:33:24 +0200271 :rtype: array
James L Parryee477c62014-11-25 16:15:18 -0800272
Andrey Andreevb9061492014-12-04 16:33:24 +0200273 Retrieve a list of all the database names.
James L Parryee477c62014-11-25 16:15:18 -0800274
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200275 .. php:method:: optimize_database()
James L Parryee477c62014-11-25 16:15:18 -0800276
Andrey Andreevb9061492014-12-04 16:33:24 +0200277 :returns: Array of optimization messages or FALSE on failure
278 :rtype: array
James L Parryee477c62014-11-25 16:15:18 -0800279
Andrey Andreevb9061492014-12-04 16:33:24 +0200280 Optimizes the database.
James L Parryee477c62014-11-25 16:15:18 -0800281
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200282 .. php:method:: optimize_table($table_name)
James L Parryee477c62014-11-25 16:15:18 -0800283
Andrey Andreevb9061492014-12-04 16:33:24 +0200284 :param string $table_name: Name of the table to optimize
285 :returns: Array of optimization messages or FALSE on failure
286 :rtype: array
James L Parryee477c62014-11-25 16:15:18 -0800287
Andrey Andreevb9061492014-12-04 16:33:24 +0200288 Optimizes a database table.
James L Parryee477c62014-11-25 16:15:18 -0800289
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200290 .. php:method:: repair_table($table_name)
James L Parryee477c62014-11-25 16:15:18 -0800291
Andrey Andreevb9061492014-12-04 16:33:24 +0200292 :param string $table_name: Name of the table to repair
293 :returns: Array of repair messages or FALSE on failure
294 :rtype: array
James L Parryee477c62014-11-25 16:15:18 -0800295
Andrey Andreevb9061492014-12-04 16:33:24 +0200296 Repairs a database table.
James L Parryee477c62014-11-25 16:15:18 -0800297
Mattias Hedmanec7372d2015-03-06 17:18:13 -0800298 .. php:method:: csv_from_result($query[, $delim = ','[, $newline = "\n"[, $enclosure = '"']]])
James L Parryee477c62014-11-25 16:15:18 -0800299
Andrey Andreevb9061492014-12-04 16:33:24 +0200300 :param object $query: A database result object
301 :param string $delim: The CSV field delimiter to use
302 :param string $newline: The newline character to use
303 :param string $enclosure: The enclosure delimiter to use
304 :returns: The generated CSV file as a string
305 :rtype: string
306
307 Translates a database result object into a CSV document.
308
Mattias Hedmanec7372d2015-03-06 17:18:13 -0800309 .. php:method:: xml_from_result($query[, $params = array()])
Andrey Andreevb9061492014-12-04 16:33:24 +0200310
311 :param object $query: A database result object
312 :param array $params: An associative array of preferences
313 :returns: The generated XML document as a string
314 :rtype: string
315
Mattias Hedmanec7372d2015-03-06 17:18:13 -0800316 Translates a database result object into an XML document.