blob: f1f5e4a0111652c49ba3f8a4d33d1c1178e29280 [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
8.. contents:: Table of Contents
9
10
James L Parryee477c62014-11-25 16:15:18 -080011******************************
Derek Jones8ede1a22011-10-05 13:34:52 -050012Initializing the Utility Class
James L Parryee477c62014-11-25 16:15:18 -080013******************************
Derek Jones8ede1a22011-10-05 13:34:52 -050014
15.. important:: In order to initialize the Utility class, your database
16 driver must already be running, since the utilities class relies on it.
17
18Load the Utility Class as follows::
19
20 $this->load->dbutil()
21
Andrey Andreeveaa60c72012-11-06 01:11:22 +020022You can also pass another database object to the DB Utility loader, in case
23the database you want to manage isn't the default one::
24
25 $this->myutil = $this->load->dbutil($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 dbutil object, instead of
29assigning it directly to ``$this->dbutil``.
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->dbutil``
Derek Jones8ede1a22011-10-05 13:34:52 -050035object::
36
Andrey Andreeveaa60c72012-11-06 01:11:22 +020037 $this->dbutil->some_method()
Derek Jones8ede1a22011-10-05 13:34:52 -050038
James L Parryee477c62014-11-25 16:15:18 -080039****************************
40Using the Database Utilities
41****************************
42
43Retrieve list of database names
Derek Jones8ede1a22011-10-05 13:34:52 -050044================================
45
46Returns an array of database names::
47
Joseph Wensleyf24f4042011-10-06 22:53:29 -040048 $dbs = $this->dbutil->list_databases();
Andrew Podner79494dd2012-12-19 14:15:41 -050049
Joseph Wensleyf24f4042011-10-06 22:53:29 -040050 foreach ($dbs as $db)
51 {
52 echo $db;
53 }
Derek Jones8ede1a22011-10-05 13:34:52 -050054
James L Parryee477c62014-11-25 16:15:18 -080055
56Determine If a Database Exists
57==============================
Derek Jones8ede1a22011-10-05 13:34:52 -050058
59Sometimes it's helpful to know whether a particular database exists.
60Returns a boolean TRUE/FALSE. Usage example::
61
Joseph Wensleyf24f4042011-10-06 22:53:29 -040062 if ($this->dbutil->database_exists('database_name'))
63 {
64 // some code...
65 }
Derek Jones8ede1a22011-10-05 13:34:52 -050066
Andrey Andreeveaa60c72012-11-06 01:11:22 +020067.. note:: Replace *database_name* with the name of the table you are
68 looking for. This method is case sensitive.
Derek Jones8ede1a22011-10-05 13:34:52 -050069
James L Parryee477c62014-11-25 16:15:18 -080070Optimize a Table
71================
Derek Jones8ede1a22011-10-05 13:34:52 -050072
73Permits you to optimize a table using the table name specified in the
74first parameter. Returns TRUE/FALSE based on success or failure::
75
Joseph Wensleyf24f4042011-10-06 22:53:29 -040076 if ($this->dbutil->optimize_table('table_name'))
77 {
78 echo 'Success!';
79 }
Derek Jones8ede1a22011-10-05 13:34:52 -050080
Andrey Andreeveaa60c72012-11-06 01:11:22 +020081.. note:: Not all database platforms support table optimization. It is
82 mostly for use with MySQL.
Derek Jones8ede1a22011-10-05 13:34:52 -050083
James L Parryee477c62014-11-25 16:15:18 -080084Repair a Table
85==============
Derek Jones8ede1a22011-10-05 13:34:52 -050086
87Permits you to repair a table using the table name specified in the
88first parameter. Returns TRUE/FALSE based on success or failure::
89
Joseph Wensleyf24f4042011-10-06 22:53:29 -040090 if ($this->dbutil->repair_table('table_name'))
91 {
92 echo 'Success!';
93 }
Derek Jones8ede1a22011-10-05 13:34:52 -050094
95.. note:: Not all database platforms support table repairs.
96
James L Parryee477c62014-11-25 16:15:18 -080097Optimize a Database
98===================
Derek Jones8ede1a22011-10-05 13:34:52 -050099
Derek Jones8ede1a22011-10-05 13:34:52 -0500100Permits you to optimize the database your DB class is currently
101connected to. Returns an array containing the DB status messages or
102FALSE on failure.
103
104::
105
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400106 $result = $this->dbutil->optimize_database();
Andrew Podner79494dd2012-12-19 14:15:41 -0500107
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400108 if ($result !== FALSE)
109 {
110 print_r($result);
111 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200113.. note:: Not all database platforms support table optimization. It
114 it is mostly for use with MySQL.
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
James L Parryee477c62014-11-25 16:15:18 -0800116Export a Query Result as a CSV File
117===================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
119Permits you to generate a CSV file from a query result. The first
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200120parameter of the method must contain the result object from your
Derek Jones8ede1a22011-10-05 13:34:52 -0500121query. Example::
122
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400123 $this->load->dbutil();
Andrew Podner79494dd2012-12-19 14:15:41 -0500124
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400125 $query = $this->db->query("SELECT * FROM mytable");
Andrew Podner79494dd2012-12-19 14:15:41 -0500126
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400127 echo $this->dbutil->csv_from_result($query);
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
129The second, third, and fourth parameters allow you to set the delimiter
mrw7310e852012-03-26 11:25:26 -0300130newline, and enclosure characters respectively. By default commas are
Derek Jones8ede1a22011-10-05 13:34:52 -0500131used as the delimiter, "\n" is used as a new line, and a double-quote
132is used as the enclosure. Example::
133
134 $delimiter = ",";
135 $newline = "\r\n";
136 $enclosure = '"';
137
138 echo $this->dbutil->csv_from_result($query, $delimiter, $newline, $enclosure);
139
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200140.. important:: This method will NOT write the CSV file for you. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500141 simply creates the CSV layout. If you need to write the file
142 use the :doc:`File Helper <../helpers/file_helper>`.
143
James L Parryee477c62014-11-25 16:15:18 -0800144Export a Query Result as an XML Document
145========================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
147Permits you to generate an XML file from a query result. The first
148parameter expects a query result object, the second may contain an
149optional array of config parameters. Example::
150
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400151 $this->load->dbutil();
Andrew Podner79494dd2012-12-19 14:15:41 -0500152
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400153 $query = $this->db->query("SELECT * FROM mytable");
Andrew Podner79494dd2012-12-19 14:15:41 -0500154
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400155 $config = array (
156 'root' => 'root',
157 'element' => 'element',
158 'newline' => "\n",
Andrew Podner79494dd2012-12-19 14:15:41 -0500159 'tab' => "\t"
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400160 );
Andrew Podner79494dd2012-12-19 14:15:41 -0500161
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400162 echo $this->dbutil->xml_from_result($query, $config);
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200164.. important:: This method will NOT write the XML file for you. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500165 simply creates the XML layout. If you need to write the file
166 use the :doc:`File Helper <../helpers/file_helper>`.
167
James L Parryee477c62014-11-25 16:15:18 -0800168********************
169Backup Your Database
170********************
171
172Database Backup Notes
173=====================
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
175Permits you to backup your full database or individual tables. The
176backup data can be compressed in either Zip or Gzip format.
177
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200178.. note:: This feature is only available for MySQL and Interbase/Firebird databases.
Timothy Warrenab189e12012-02-22 10:34:23 -0500179
180.. note:: For Interbase/Firebird databases, the backup file name is the only parameter.
Andrew Podner79494dd2012-12-19 14:15:41 -0500181
Timothy Warrenab189e12012-02-22 10:34:23 -0500182 Eg. $this->dbutil->backup('db_backup_filename');
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
184.. note:: Due to the limited execution time and memory available to PHP,
185 backing up very large databases may not be possible. If your database is
186 very large you might need to backup directly from your SQL server via
187 the command line, or have your server admin do it for you if you do not
188 have root privileges.
189
190Usage Example
James L Parryee477c62014-11-25 16:15:18 -0800191=============
Derek Jones8ede1a22011-10-05 13:34:52 -0500192
193::
194
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400195 // Load the DB utility class
196 $this->load->dbutil();
Andrew Podner79494dd2012-12-19 14:15:41 -0500197
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400198 // Backup your entire database and assign it to a variable
199 $backup =& $this->dbutil->backup();
Andrew Podner79494dd2012-12-19 14:15:41 -0500200
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400201 // Load the file helper and write the file to your server
202 $this->load->helper('file');
203 write_file('/path/to/mybackup.gz', $backup);
Andrew Podner79494dd2012-12-19 14:15:41 -0500204
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400205 // Load the download helper and send the file to your desktop
206 $this->load->helper('download');
207 force_download('mybackup.gz', $backup);
Derek Jones8ede1a22011-10-05 13:34:52 -0500208
209Setting Backup Preferences
James L Parryee477c62014-11-25 16:15:18 -0800210==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500211
212Backup preferences are set by submitting an array of values to the first
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200213parameter of the ``backup()`` method. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500214
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400215 $prefs = array(
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200216 'tables' => array('table1', 'table2'), // Array of tables to backup.
217 'ignore' => array(), // List of tables to omit from the backup
218 'format' => 'txt', // gzip, zip, txt
219 'filename' => 'mybackup.sql', // File name - NEEDED ONLY WITH ZIP FILES
220 'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file
221 'add_insert' => TRUE, // Whether to add INSERT data to backup file
222 'newline' => "\n" // Newline character used in backup file
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400223 );
Andrew Podner79494dd2012-12-19 14:15:41 -0500224
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400225 $this->dbutil->backup($prefs);
Derek Jones8ede1a22011-10-05 13:34:52 -0500226
227Description of Backup Preferences
James L Parryee477c62014-11-25 16:15:18 -0800228=================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500229
Andrew Podner48512172012-12-20 07:56:19 -0500230======================= ======================= ======================= ========================================================================
231Preference Default Value Options Description
232======================= ======================= ======================= ========================================================================
233**tables** empty array None An array of tables you want backed up. If left blank all tables will be
234 exported.
235**ignore** empty array None An array of tables you want the backup routine to ignore.
236**format** gzip gzip, zip, txt The file format of the export file.
237**filename** the current date/time None The name of the backed-up file. The name is needed only if you are using
238 zip compression.
239**add_drop** TRUE TRUE/FALSE Whether to include DROP TABLE statements in your SQL export file.
240**add_insert** TRUE TRUE/FALSE Whether to include INSERT statements in your SQL export file.
241**newline** "\\n" "\\n", "\\r", "\\r\\n" Type of newline to use in your SQL export file.
242**foreign_key_checks** TRUE TRUE/FALSE Whether output should keep foreign key checks enabled.
James L Parryee477c62014-11-25 16:15:18 -0800243======================= ======================= ======================= ========================================================================
244
245***************
246Class Reference
247***************
248
249.. class:: DB_utility
250
251 .. method:: backup($params)
252
253 :param array $params: associative array of backup preferences
254 :rtype: void
255
256 Perform a database backup, per user preferences
257
258 .. method:: csv_from_results($query, $delim = ',', $newline = "\n", $enclosure = '"')
259
260 :param object $query: DB_result with data to backup
261 :param string $delim: Delimniter character for the CSV file, default is ','
262 :param string $newline: Character to use for newlines, default is "\n"
263 :param string $enclosure: Delimiter used for enclosure, default is '"'
264 :returns: The generated CSV file as a string
265
266 .. method:: database_exists($database_name)
267
268 :param string $database_name: name of the database to check for
269 :returns: TRUE if the database exists, FALSE otherwise
270
271 Perform a database backup, per user preferences
272
273 .. method:: database_exists($database_name)
274
275 :param string $database_name: name of the database to check for
276 :returns: TRUE if the database exists, FALSE otherwise
277
278 Check for the existence of a database
279
280 .. method:: list_databases()
281
282 :returns: Array of database names found
283
284 Retrieve all the database names
285
286 .. method:: optimize_database()
287
288 :returns: Array of optimization messages, FALSE on failure
289
290 Optimizes a database
291
292 .. method:: optimize_table($table_name)
293
294 :param string $table_name: Name of the table to optimize
295 :returns: Array of optimization messages, FALSE on failure
296
297 Optimizes a database table
298
299 .. method:: repair_table($table_name)
300
301 :param string $table_name: Name of the table to repair
302 :returns: Array of repair messages, FALSE on failure
303
304 Repairs a database table
305
306 .. method:: xml_from_results($query, $params)
307
308 :param object $query: DB_result with data to backup
309 :param array $params: Associative array of preferences
310 :returns: The generated XML document as a string