blob: 06ecb2da13322ed7d6377c39ab256f3d7ecc65c6 [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
11******************
12Function Reference
13******************
14
15Initializing the Utility Class
16==============================
17
18.. important:: In order to initialize the Utility class, your database
19 driver must already be running, since the utilities class relies on it.
20
21Load the Utility Class as follows::
22
23 $this->load->dbutil()
24
Andrey Andreeveaa60c72012-11-06 01:11:22 +020025You can also pass another database object to the DB Utility loader, in case
26the database you want to manage isn't the default one::
27
28 $this->myutil = $this->load->dbutil($this->other_db, TRUE);
29
30In the above example, we're passing a custom database object as the first
31parameter and then tell it to return the dbutil object, instead of
32assigning it directly to ``$this->dbutil``.
33
34.. note:: Both of the parameters can be used individually, just pass an empty
35 value as the first one if you wish to skip it.
36
37Once initialized you will access the methods using the ``$this->dbutil``
Derek Jones8ede1a22011-10-05 13:34:52 -050038object::
39
Andrey Andreeveaa60c72012-11-06 01:11:22 +020040 $this->dbutil->some_method()
Derek Jones8ede1a22011-10-05 13:34:52 -050041
Andrey Andreeveaa60c72012-11-06 01:11:22 +020042$this->dbutil->list_databases();
Derek Jones8ede1a22011-10-05 13:34:52 -050043================================
44
45Returns an array of database names::
46
Joseph Wensleyf24f4042011-10-06 22:53:29 -040047 $dbs = $this->dbutil->list_databases();
48
49 foreach ($dbs as $db)
50 {
51 echo $db;
52 }
Derek Jones8ede1a22011-10-05 13:34:52 -050053
54$this->dbutil->database_exists();
Andrey Andreeveaa60c72012-11-06 01:11:22 +020055=================================
Derek Jones8ede1a22011-10-05 13:34:52 -050056
57Sometimes it's helpful to know whether a particular database exists.
58Returns a boolean TRUE/FALSE. Usage example::
59
Joseph Wensleyf24f4042011-10-06 22:53:29 -040060 if ($this->dbutil->database_exists('database_name'))
61 {
62 // some code...
63 }
Derek Jones8ede1a22011-10-05 13:34:52 -050064
Andrey Andreeveaa60c72012-11-06 01:11:22 +020065.. note:: Replace *database_name* with the name of the table you are
66 looking for. This method is case sensitive.
Derek Jones8ede1a22011-10-05 13:34:52 -050067
68$this->dbutil->optimize_table('table_name');
Andrey Andreeveaa60c72012-11-06 01:11:22 +020069============================================
Derek Jones8ede1a22011-10-05 13:34:52 -050070
71Permits you to optimize a table using the table name specified in the
72first parameter. Returns TRUE/FALSE based on success or failure::
73
Joseph Wensleyf24f4042011-10-06 22:53:29 -040074 if ($this->dbutil->optimize_table('table_name'))
75 {
76 echo 'Success!';
77 }
Derek Jones8ede1a22011-10-05 13:34:52 -050078
Andrey Andreeveaa60c72012-11-06 01:11:22 +020079.. note:: Not all database platforms support table optimization. It is
80 mostly for use with MySQL.
Derek Jones8ede1a22011-10-05 13:34:52 -050081
82$this->dbutil->repair_table('table_name');
Andrey Andreeveaa60c72012-11-06 01:11:22 +020083==========================================
Derek Jones8ede1a22011-10-05 13:34:52 -050084
85Permits you to repair a table using the table name specified in the
86first parameter. Returns TRUE/FALSE based on success or failure::
87
Joseph Wensleyf24f4042011-10-06 22:53:29 -040088 if ($this->dbutil->repair_table('table_name'))
89 {
90 echo 'Success!';
91 }
Derek Jones8ede1a22011-10-05 13:34:52 -050092
93.. note:: Not all database platforms support table repairs.
94
95$this->dbutil->optimize_database();
96====================================
97
Derek Jones8ede1a22011-10-05 13:34:52 -050098Permits you to optimize the database your DB class is currently
99connected to. Returns an array containing the DB status messages or
100FALSE on failure.
101
102::
103
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400104 $result = $this->dbutil->optimize_database();
105
106 if ($result !== FALSE)
107 {
108 print_r($result);
109 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200111.. note:: Not all database platforms support table optimization. It
112 it is mostly for use with MySQL.
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200114$this->dbutil->csv_from_result($db_result);
115===========================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
117Permits you to generate a CSV file from a query result. The first
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200118parameter of the method must contain the result object from your
Derek Jones8ede1a22011-10-05 13:34:52 -0500119query. Example::
120
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400121 $this->load->dbutil();
122
123 $query = $this->db->query("SELECT * FROM mytable");
124
125 echo $this->dbutil->csv_from_result($query);
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
127The second, third, and fourth parameters allow you to set the delimiter
mrw7310e852012-03-26 11:25:26 -0300128newline, and enclosure characters respectively. By default commas are
Derek Jones8ede1a22011-10-05 13:34:52 -0500129used as the delimiter, "\n" is used as a new line, and a double-quote
130is used as the enclosure. Example::
131
132 $delimiter = ",";
133 $newline = "\r\n";
134 $enclosure = '"';
135
136 echo $this->dbutil->csv_from_result($query, $delimiter, $newline, $enclosure);
137
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200138.. important:: This method will NOT write the CSV file for you. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500139 simply creates the CSV layout. If you need to write the file
140 use the :doc:`File Helper <../helpers/file_helper>`.
141
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200142$this->dbutil->xml_from_result($db_result);
143===========================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
145Permits you to generate an XML file from a query result. The first
146parameter expects a query result object, the second may contain an
147optional array of config parameters. Example::
148
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400149 $this->load->dbutil();
150
151 $query = $this->db->query("SELECT * FROM mytable");
152
153 $config = array (
154 'root' => 'root',
155 'element' => 'element',
156 'newline' => "\n",
157 'tab' => "\t"
158 );
159
160 echo $this->dbutil->xml_from_result($query, $config);
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200162.. important:: This method will NOT write the XML file for you. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500163 simply creates the XML layout. If you need to write the file
164 use the :doc:`File Helper <../helpers/file_helper>`.
165
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200166$this->dbutil->backup();
167========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
169Permits you to backup your full database or individual tables. The
170backup data can be compressed in either Zip or Gzip format.
171
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200172.. note:: This feature is only available for MySQL and Interbase/Firebird databases.
Timothy Warrenab189e12012-02-22 10:34:23 -0500173
174.. note:: For Interbase/Firebird databases, the backup file name is the only parameter.
175
176 Eg. $this->dbutil->backup('db_backup_filename');
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
178.. note:: Due to the limited execution time and memory available to PHP,
179 backing up very large databases may not be possible. If your database is
180 very large you might need to backup directly from your SQL server via
181 the command line, or have your server admin do it for you if you do not
182 have root privileges.
183
184Usage Example
185-------------
186
187::
188
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400189 // Load the DB utility class
190 $this->load->dbutil();
191
192 // Backup your entire database and assign it to a variable
193 $backup =& $this->dbutil->backup();
194
195 // Load the file helper and write the file to your server
196 $this->load->helper('file');
197 write_file('/path/to/mybackup.gz', $backup);
198
199 // Load the download helper and send the file to your desktop
200 $this->load->helper('download');
201 force_download('mybackup.gz', $backup);
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
203Setting Backup Preferences
204--------------------------
205
206Backup preferences are set by submitting an array of values to the first
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200207parameter of the ``backup()`` method. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500208
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400209 $prefs = array(
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200210 'tables' => array('table1', 'table2'), // Array of tables to backup.
211 'ignore' => array(), // List of tables to omit from the backup
212 'format' => 'txt', // gzip, zip, txt
213 'filename' => 'mybackup.sql', // File name - NEEDED ONLY WITH ZIP FILES
214 'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file
215 'add_insert' => TRUE, // Whether to add INSERT data to backup file
216 'newline' => "\n" // Newline character used in backup file
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400217 );
218
219 $this->dbutil->backup($prefs);
Derek Jones8ede1a22011-10-05 13:34:52 -0500220
221Description of Backup Preferences
222---------------------------------
223
Joseph Wensley5b3ea1a2011-10-06 20:54:32 -0400224=============== ======================= ======================= ========================================================================
225Preference Default Value Options Description
226=============== ======================= ======================= ========================================================================
227**tables** empty array None An array of tables you want backed up. If left blank all tables will be
228 exported.
229**ignore** empty array None An array of tables you want the backup routine to ignore.
230**format** gzip gzip, zip, txt The file format of the export file.
231**filename** the current date/time None The name of the backed-up file. The name is needed only if you are using
232 zip compression.
233**add_drop** TRUE TRUE/FALSE Whether to include DROP TABLE statements in your SQL export file.
234**add_insert** TRUE TRUE/FALSE Whether to include INSERT statements in your SQL export file.
235**newline** "\\n" "\\n", "\\r", "\\r\\n" Type of newline to use in your SQL export file.
236=============== ======================= ======================= ========================================================================