blob: 3805ffb879ecef0de2d351ff41e7a2226901d972 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001######################
2Database Utility Class
3######################
4
5The Database Utility Class contains functions that help you manage your
6database.
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
25Once initialized you will access the functions using the $this->dbutil
26object::
27
28 $this->dbutil->some_function()
29
30$this->dbutil->list_databases()
31================================
32
33Returns an array of database names::
34
Joseph Wensleyf24f4042011-10-06 22:53:29 -040035 $dbs = $this->dbutil->list_databases();
36
37 foreach ($dbs as $db)
38 {
39 echo $db;
40 }
Derek Jones8ede1a22011-10-05 13:34:52 -050041
42$this->dbutil->database_exists();
43==================================
44
45Sometimes it's helpful to know whether a particular database exists.
46Returns a boolean TRUE/FALSE. Usage example::
47
Joseph Wensleyf24f4042011-10-06 22:53:29 -040048 if ($this->dbutil->database_exists('database_name'))
49 {
50 // some code...
51 }
Derek Jones8ede1a22011-10-05 13:34:52 -050052
53Note: Replace *database_name* with the name of the table you are
54looking for. This function is case sensitive.
55
56$this->dbutil->optimize_table('table_name');
57==============================================
58
59.. note:: This features is only available for MySQL/MySQLi databases.
60
61Permits you to optimize a table using the table name specified in the
62first parameter. Returns TRUE/FALSE based on success or failure::
63
Joseph Wensleyf24f4042011-10-06 22:53:29 -040064 if ($this->dbutil->optimize_table('table_name'))
65 {
66 echo 'Success!';
67 }
Derek Jones8ede1a22011-10-05 13:34:52 -050068
69.. note:: Not all database platforms support table optimization.
70
71$this->dbutil->repair_table('table_name');
72============================================
73
74.. note:: This features is only available for MySQL/MySQLi databases.
75
76Permits you to repair a table using the table name specified in the
77first parameter. Returns TRUE/FALSE based on success or failure::
78
Joseph Wensleyf24f4042011-10-06 22:53:29 -040079 if ($this->dbutil->repair_table('table_name'))
80 {
81 echo 'Success!';
82 }
Derek Jones8ede1a22011-10-05 13:34:52 -050083
84.. note:: Not all database platforms support table repairs.
85
86$this->dbutil->optimize_database();
87====================================
88
89.. note:: This features is only available for MySQL/MySQLi databases.
90
91Permits you to optimize the database your DB class is currently
92connected to. Returns an array containing the DB status messages or
93FALSE on failure.
94
95::
96
Joseph Wensleyf24f4042011-10-06 22:53:29 -040097 $result = $this->dbutil->optimize_database();
98
99 if ($result !== FALSE)
100 {
101 print_r($result);
102 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
104.. note:: Not all database platforms support table optimization.
105
106$this->dbutil->csv_from_result($db_result)
107=============================================
108
109Permits you to generate a CSV file from a query result. The first
110parameter of the function must contain the result object from your
111query. Example::
112
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400113 $this->load->dbutil();
114
115 $query = $this->db->query("SELECT * FROM mytable");
116
117 echo $this->dbutil->csv_from_result($query);
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
119The second, third, and fourth parameters allow you to set the delimiter
120newline, and enclosure characters respectively. By default tabs are
121used as the delimiter, "\n" is used as a new line, and a double-quote
122is used as the enclosure. Example::
123
124 $delimiter = ",";
125 $newline = "\r\n";
126 $enclosure = '"';
127
128 echo $this->dbutil->csv_from_result($query, $delimiter, $newline, $enclosure);
129
130.. important:: This function will NOT write the CSV file for you. It
131 simply creates the CSV layout. If you need to write the file
132 use the :doc:`File Helper <../helpers/file_helper>`.
133
134$this->dbutil->xml_from_result($db_result)
135=============================================
136
137Permits you to generate an XML file from a query result. The first
138parameter expects a query result object, the second may contain an
139optional array of config parameters. Example::
140
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400141 $this->load->dbutil();
142
143 $query = $this->db->query("SELECT * FROM mytable");
144
145 $config = array (
146 'root' => 'root',
147 'element' => 'element',
148 'newline' => "\n",
149 'tab' => "\t"
150 );
151
152 echo $this->dbutil->xml_from_result($query, $config);
Derek Jones8ede1a22011-10-05 13:34:52 -0500153
154.. important:: This function will NOT write the XML file for you. It
155 simply creates the XML layout. If you need to write the file
156 use the :doc:`File Helper <../helpers/file_helper>`.
157
158$this->dbutil->backup()
159=======================
160
161Permits you to backup your full database or individual tables. The
162backup data can be compressed in either Zip or Gzip format.
163
Timothy Warrenab189e12012-02-22 10:34:23 -0500164.. note:: This features is only available for MySQL and Interbase/Firebird databases.
165
166.. note:: For Interbase/Firebird databases, the backup file name is the only parameter.
167
168 Eg. $this->dbutil->backup('db_backup_filename');
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
170.. note:: Due to the limited execution time and memory available to PHP,
171 backing up very large databases may not be possible. If your database is
172 very large you might need to backup directly from your SQL server via
173 the command line, or have your server admin do it for you if you do not
174 have root privileges.
175
176Usage Example
177-------------
178
179::
180
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400181 // Load the DB utility class
182 $this->load->dbutil();
183
184 // Backup your entire database and assign it to a variable
185 $backup =& $this->dbutil->backup();
186
187 // Load the file helper and write the file to your server
188 $this->load->helper('file');
189 write_file('/path/to/mybackup.gz', $backup);
190
191 // Load the download helper and send the file to your desktop
192 $this->load->helper('download');
193 force_download('mybackup.gz', $backup);
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
195Setting Backup Preferences
196--------------------------
197
198Backup preferences are set by submitting an array of values to the first
199parameter of the backup function. Example::
200
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400201 $prefs = array(
202 'tables' => array('table1', 'table2'), // Array of tables to backup.
203 'ignore' => array(), // List of tables to omit from the backup
204 'format' => 'txt', // gzip, zip, txt
205 'filename' => 'mybackup.sql', // File name - NEEDED ONLY WITH ZIP FILES
206 'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file
207 'add_insert' => TRUE, // Whether to add INSERT data to backup file
208 'newline' => "\n" // Newline character used in backup file
209 );
210
211 $this->dbutil->backup($prefs);
Derek Jones8ede1a22011-10-05 13:34:52 -0500212
213Description of Backup Preferences
214---------------------------------
215
Joseph Wensley5b3ea1a2011-10-06 20:54:32 -0400216=============== ======================= ======================= ========================================================================
217Preference Default Value Options Description
218=============== ======================= ======================= ========================================================================
219**tables** empty array None An array of tables you want backed up. If left blank all tables will be
220 exported.
221**ignore** empty array None An array of tables you want the backup routine to ignore.
222**format** gzip gzip, zip, txt The file format of the export file.
223**filename** the current date/time None The name of the backed-up file. The name is needed only if you are using
224 zip compression.
225**add_drop** TRUE TRUE/FALSE Whether to include DROP TABLE statements in your SQL export file.
226**add_insert** TRUE TRUE/FALSE Whether to include INSERT statements in your SQL export file.
227**newline** "\\n" "\\n", "\\r", "\\r\\n" Type of newline to use in your SQL export file.
228=============== ======================= ======================= ========================================================================