blob: a9bfbddfbc128be054e1c856d22201fdce997978 [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
12
James L Parryee477c62014-11-25 16:15:18 -080013******************************
Derek Jones8ede1a22011-10-05 13:34:52 -050014Initializing the Utility Class
James L Parryee477c62014-11-25 16:15:18 -080015******************************
Derek Jones8ede1a22011-10-05 13:34:52 -050016
17.. important:: In order to initialize the Utility class, your database
18 driver must already be running, since the utilities class relies on it.
19
20Load the Utility Class as follows::
21
22 $this->load->dbutil()
23
Andrey Andreeveaa60c72012-11-06 01:11:22 +020024You can also pass another database object to the DB Utility loader, in case
25the database you want to manage isn't the default one::
26
27 $this->myutil = $this->load->dbutil($this->other_db, TRUE);
28
29In the above example, we're passing a custom database object as the first
30parameter and then tell it to return the dbutil object, instead of
31assigning it directly to ``$this->dbutil``.
32
33.. note:: Both of the parameters can be used individually, just pass an empty
34 value as the first one if you wish to skip it.
35
36Once initialized you will access the methods using the ``$this->dbutil``
Derek Jones8ede1a22011-10-05 13:34:52 -050037object::
38
Andrey Andreeveaa60c72012-11-06 01:11:22 +020039 $this->dbutil->some_method()
Derek Jones8ede1a22011-10-05 13:34:52 -050040
James L Parryee477c62014-11-25 16:15:18 -080041****************************
42Using the Database Utilities
43****************************
44
45Retrieve list of database names
Derek Jones8ede1a22011-10-05 13:34:52 -050046================================
47
48Returns an array of database names::
49
Joseph Wensleyf24f4042011-10-06 22:53:29 -040050 $dbs = $this->dbutil->list_databases();
Andrew Podner79494dd2012-12-19 14:15:41 -050051
Joseph Wensleyf24f4042011-10-06 22:53:29 -040052 foreach ($dbs as $db)
53 {
54 echo $db;
55 }
Derek Jones8ede1a22011-10-05 13:34:52 -050056
James L Parryee477c62014-11-25 16:15:18 -080057
58Determine If a Database Exists
59==============================
Derek Jones8ede1a22011-10-05 13:34:52 -050060
61Sometimes it's helpful to know whether a particular database exists.
62Returns a boolean TRUE/FALSE. Usage example::
63
Joseph Wensleyf24f4042011-10-06 22:53:29 -040064 if ($this->dbutil->database_exists('database_name'))
65 {
66 // some code...
67 }
Derek Jones8ede1a22011-10-05 13:34:52 -050068
Andrey Andreeveaa60c72012-11-06 01:11:22 +020069.. note:: Replace *database_name* with the name of the table you are
70 looking for. This method is case sensitive.
Derek Jones8ede1a22011-10-05 13:34:52 -050071
James L Parryee477c62014-11-25 16:15:18 -080072Optimize a Table
73================
Derek Jones8ede1a22011-10-05 13:34:52 -050074
75Permits you to optimize a table using the table name specified in the
76first parameter. Returns TRUE/FALSE based on success or failure::
77
Joseph Wensleyf24f4042011-10-06 22:53:29 -040078 if ($this->dbutil->optimize_table('table_name'))
79 {
80 echo 'Success!';
81 }
Derek Jones8ede1a22011-10-05 13:34:52 -050082
Andrey Andreeveaa60c72012-11-06 01:11:22 +020083.. note:: Not all database platforms support table optimization. It is
84 mostly for use with MySQL.
Derek Jones8ede1a22011-10-05 13:34:52 -050085
James L Parryee477c62014-11-25 16:15:18 -080086Repair a Table
87==============
Derek Jones8ede1a22011-10-05 13:34:52 -050088
89Permits you to repair a table using the table name specified in the
90first parameter. Returns TRUE/FALSE based on success or failure::
91
Joseph Wensleyf24f4042011-10-06 22:53:29 -040092 if ($this->dbutil->repair_table('table_name'))
93 {
94 echo 'Success!';
95 }
Derek Jones8ede1a22011-10-05 13:34:52 -050096
97.. note:: Not all database platforms support table repairs.
98
James L Parryee477c62014-11-25 16:15:18 -080099Optimize a Database
100===================
Derek Jones8ede1a22011-10-05 13:34:52 -0500101
Derek Jones8ede1a22011-10-05 13:34:52 -0500102Permits you to optimize the database your DB class is currently
103connected to. Returns an array containing the DB status messages or
104FALSE on failure.
105
106::
107
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400108 $result = $this->dbutil->optimize_database();
Andrew Podner79494dd2012-12-19 14:15:41 -0500109
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400110 if ($result !== FALSE)
111 {
112 print_r($result);
113 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200115.. note:: Not all database platforms support table optimization. It
116 it is mostly for use with MySQL.
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
James L Parryee477c62014-11-25 16:15:18 -0800118Export a Query Result as a CSV File
119===================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
121Permits you to generate a CSV file from a query result. The first
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200122parameter of the method must contain the result object from your
Derek Jones8ede1a22011-10-05 13:34:52 -0500123query. Example::
124
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400125 $this->load->dbutil();
Andrew Podner79494dd2012-12-19 14:15:41 -0500126
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400127 $query = $this->db->query("SELECT * FROM mytable");
Andrew Podner79494dd2012-12-19 14:15:41 -0500128
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400129 echo $this->dbutil->csv_from_result($query);
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
131The second, third, and fourth parameters allow you to set the delimiter
mrw7310e852012-03-26 11:25:26 -0300132newline, and enclosure characters respectively. By default commas are
Derek Jones8ede1a22011-10-05 13:34:52 -0500133used as the delimiter, "\n" is used as a new line, and a double-quote
134is used as the enclosure. Example::
135
136 $delimiter = ",";
137 $newline = "\r\n";
138 $enclosure = '"';
139
140 echo $this->dbutil->csv_from_result($query, $delimiter, $newline, $enclosure);
141
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200142.. important:: This method will NOT write the CSV file for you. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500143 simply creates the CSV layout. If you need to write the file
144 use the :doc:`File Helper <../helpers/file_helper>`.
145
James L Parryee477c62014-11-25 16:15:18 -0800146Export a Query Result as an XML Document
147========================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500148
149Permits you to generate an XML file from a query result. The first
150parameter expects a query result object, the second may contain an
151optional array of config parameters. Example::
152
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400153 $this->load->dbutil();
Andrew Podner79494dd2012-12-19 14:15:41 -0500154
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400155 $query = $this->db->query("SELECT * FROM mytable");
Andrew Podner79494dd2012-12-19 14:15:41 -0500156
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400157 $config = array (
158 'root' => 'root',
159 'element' => 'element',
160 'newline' => "\n",
Andrew Podner79494dd2012-12-19 14:15:41 -0500161 'tab' => "\t"
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400162 );
Andrew Podner79494dd2012-12-19 14:15:41 -0500163
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400164 echo $this->dbutil->xml_from_result($query, $config);
Derek Jones8ede1a22011-10-05 13:34:52 -0500165
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200166.. important:: This method will NOT write the XML file for you. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500167 simply creates the XML layout. If you need to write the file
168 use the :doc:`File Helper <../helpers/file_helper>`.
169
James L Parryee477c62014-11-25 16:15:18 -0800170********************
171Backup Your Database
172********************
173
174Database Backup Notes
175=====================
Derek Jones8ede1a22011-10-05 13:34:52 -0500176
177Permits you to backup your full database or individual tables. The
178backup data can be compressed in either Zip or Gzip format.
179
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200180.. note:: This feature is only available for MySQL and Interbase/Firebird databases.
Timothy Warrenab189e12012-02-22 10:34:23 -0500181
182.. note:: For Interbase/Firebird databases, the backup file name is the only parameter.
Andrew Podner79494dd2012-12-19 14:15:41 -0500183
Timothy Warrenab189e12012-02-22 10:34:23 -0500184 Eg. $this->dbutil->backup('db_backup_filename');
Derek Jones8ede1a22011-10-05 13:34:52 -0500185
186.. note:: Due to the limited execution time and memory available to PHP,
187 backing up very large databases may not be possible. If your database is
188 very large you might need to backup directly from your SQL server via
189 the command line, or have your server admin do it for you if you do not
190 have root privileges.
191
192Usage Example
James L Parryee477c62014-11-25 16:15:18 -0800193=============
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
195::
196
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400197 // Load the DB utility class
198 $this->load->dbutil();
Andrew Podner79494dd2012-12-19 14:15:41 -0500199
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400200 // Backup your entire database and assign it to a variable
201 $backup =& $this->dbutil->backup();
Andrew Podner79494dd2012-12-19 14:15:41 -0500202
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400203 // Load the file helper and write the file to your server
204 $this->load->helper('file');
205 write_file('/path/to/mybackup.gz', $backup);
Andrew Podner79494dd2012-12-19 14:15:41 -0500206
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400207 // Load the download helper and send the file to your desktop
208 $this->load->helper('download');
209 force_download('mybackup.gz', $backup);
Derek Jones8ede1a22011-10-05 13:34:52 -0500210
211Setting Backup Preferences
James L Parryee477c62014-11-25 16:15:18 -0800212==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
214Backup preferences are set by submitting an array of values to the first
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200215parameter of the ``backup()`` method. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500216
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400217 $prefs = array(
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200218 'tables' => array('table1', 'table2'), // Array of tables to backup.
219 'ignore' => array(), // List of tables to omit from the backup
220 'format' => 'txt', // gzip, zip, txt
221 'filename' => 'mybackup.sql', // File name - NEEDED ONLY WITH ZIP FILES
222 'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file
223 'add_insert' => TRUE, // Whether to add INSERT data to backup file
224 'newline' => "\n" // Newline character used in backup file
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400225 );
Andrew Podner79494dd2012-12-19 14:15:41 -0500226
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400227 $this->dbutil->backup($prefs);
Derek Jones8ede1a22011-10-05 13:34:52 -0500228
229Description of Backup Preferences
James L Parryee477c62014-11-25 16:15:18 -0800230=================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500231
Andrew Podner48512172012-12-20 07:56:19 -0500232======================= ======================= ======================= ========================================================================
233Preference Default Value Options Description
234======================= ======================= ======================= ========================================================================
235**tables** empty array None An array of tables you want backed up. If left blank all tables will be
236 exported.
237**ignore** empty array None An array of tables you want the backup routine to ignore.
238**format** gzip gzip, zip, txt The file format of the export file.
239**filename** the current date/time None The name of the backed-up file. The name is needed only if you are using
240 zip compression.
241**add_drop** TRUE TRUE/FALSE Whether to include DROP TABLE statements in your SQL export file.
242**add_insert** TRUE TRUE/FALSE Whether to include INSERT statements in your SQL export file.
243**newline** "\\n" "\\n", "\\r", "\\r\\n" Type of newline to use in your SQL export file.
244**foreign_key_checks** TRUE TRUE/FALSE Whether output should keep foreign key checks enabled.
James L Parryee477c62014-11-25 16:15:18 -0800245======================= ======================= ======================= ========================================================================
246
247***************
248Class Reference
249***************
250
251.. class:: DB_utility
252
253 .. method:: backup($params)
254
255 :param array $params: associative array of backup preferences
256 :rtype: void
257
258 Perform a database backup, per user preferences
259
260 .. method:: csv_from_results($query, $delim = ',', $newline = "\n", $enclosure = '"')
261
262 :param object $query: DB_result with data to backup
263 :param string $delim: Delimniter character for the CSV file, default is ','
264 :param string $newline: Character to use for newlines, default is "\n"
265 :param string $enclosure: Delimiter used for enclosure, default is '"'
266 :returns: The generated CSV file as a string
267
268 .. method:: database_exists($database_name)
269
270 :param string $database_name: name of the database to check for
271 :returns: TRUE if the database exists, FALSE otherwise
272
273 Perform a database backup, per user preferences
274
275 .. method:: database_exists($database_name)
276
277 :param string $database_name: name of the database to check for
278 :returns: TRUE if the database exists, FALSE otherwise
279
280 Check for the existence of a database
281
282 .. method:: list_databases()
283
284 :returns: Array of database names found
285
286 Retrieve all the database names
287
288 .. method:: optimize_database()
289
290 :returns: Array of optimization messages, FALSE on failure
291
292 Optimizes a database
293
294 .. method:: optimize_table($table_name)
295
296 :param string $table_name: Name of the table to optimize
297 :returns: Array of optimization messages, FALSE on failure
298
299 Optimizes a database table
300
301 .. method:: repair_table($table_name)
302
303 :param string $table_name: Name of the table to repair
304 :returns: Array of repair messages, FALSE on failure
305
306 Repairs a database table
307
308 .. method:: xml_from_results($query, $params)
309
310 :param object $query: DB_result with data to backup
311 :param array $params: Associative array of preferences
312 :returns: The generated XML document as a string