Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ###################### |
| 2 | Database Utility Class |
| 3 | ###################### |
| 4 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 5 | The Database Utility Class contains methods that help you manage your |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 6 | database. |
| 7 | |
| 8 | .. contents:: Table of Contents |
| 9 | |
| 10 | |
| 11 | ****************** |
| 12 | Function Reference |
| 13 | ****************** |
| 14 | |
| 15 | Initializing 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 | |
| 21 | Load the Utility Class as follows:: |
| 22 | |
| 23 | $this->load->dbutil() |
| 24 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 25 | You can also pass another database object to the DB Utility loader, in case |
| 26 | the database you want to manage isn't the default one:: |
| 27 | |
| 28 | $this->myutil = $this->load->dbutil($this->other_db, TRUE); |
| 29 | |
| 30 | In the above example, we're passing a custom database object as the first |
| 31 | parameter and then tell it to return the dbutil object, instead of |
| 32 | assigning 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 | |
| 37 | Once initialized you will access the methods using the ``$this->dbutil`` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | object:: |
| 39 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 40 | $this->dbutil->some_method() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 41 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 42 | $this->dbutil->list_databases(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | ================================ |
| 44 | |
| 45 | Returns an array of database names:: |
| 46 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 47 | $dbs = $this->dbutil->list_databases(); |
| 48 | |
| 49 | foreach ($dbs as $db) |
| 50 | { |
| 51 | echo $db; |
| 52 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | |
| 54 | $this->dbutil->database_exists(); |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 55 | ================================= |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 56 | |
| 57 | Sometimes it's helpful to know whether a particular database exists. |
| 58 | Returns a boolean TRUE/FALSE. Usage example:: |
| 59 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 60 | if ($this->dbutil->database_exists('database_name')) |
| 61 | { |
| 62 | // some code... |
| 63 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 65 | .. note:: Replace *database_name* with the name of the table you are |
| 66 | looking for. This method is case sensitive. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 67 | |
| 68 | $this->dbutil->optimize_table('table_name'); |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 69 | ============================================ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 70 | |
| 71 | Permits you to optimize a table using the table name specified in the |
| 72 | first parameter. Returns TRUE/FALSE based on success or failure:: |
| 73 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 74 | if ($this->dbutil->optimize_table('table_name')) |
| 75 | { |
| 76 | echo 'Success!'; |
| 77 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 78 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 79 | .. note:: Not all database platforms support table optimization. It is |
| 80 | mostly for use with MySQL. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 81 | |
| 82 | $this->dbutil->repair_table('table_name'); |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 83 | ========================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
| 85 | Permits you to repair a table using the table name specified in the |
| 86 | first parameter. Returns TRUE/FALSE based on success or failure:: |
| 87 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 88 | if ($this->dbutil->repair_table('table_name')) |
| 89 | { |
| 90 | echo 'Success!'; |
| 91 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | |
| 93 | .. note:: Not all database platforms support table repairs. |
| 94 | |
| 95 | $this->dbutil->optimize_database(); |
| 96 | ==================================== |
| 97 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 98 | Permits you to optimize the database your DB class is currently |
| 99 | connected to. Returns an array containing the DB status messages or |
| 100 | FALSE on failure. |
| 101 | |
| 102 | :: |
| 103 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 104 | $result = $this->dbutil->optimize_database(); |
| 105 | |
| 106 | if ($result !== FALSE) |
| 107 | { |
| 108 | print_r($result); |
| 109 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 111 | .. note:: Not all database platforms support table optimization. It |
| 112 | it is mostly for use with MySQL. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 113 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 114 | $this->dbutil->csv_from_result($db_result); |
| 115 | =========================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 116 | |
| 117 | Permits you to generate a CSV file from a query result. The first |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 118 | parameter of the method must contain the result object from your |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 119 | query. Example:: |
| 120 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 121 | $this->load->dbutil(); |
| 122 | |
| 123 | $query = $this->db->query("SELECT * FROM mytable"); |
| 124 | |
| 125 | echo $this->dbutil->csv_from_result($query); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | |
| 127 | The second, third, and fourth parameters allow you to set the delimiter |
mrw | 7310e85 | 2012-03-26 11:25:26 -0300 | [diff] [blame] | 128 | newline, and enclosure characters respectively. By default commas are |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 129 | used as the delimiter, "\n" is used as a new line, and a double-quote |
| 130 | is 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 Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 138 | .. important:: This method will NOT write the CSV file for you. It |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | simply creates the CSV layout. If you need to write the file |
| 140 | use the :doc:`File Helper <../helpers/file_helper>`. |
| 141 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 142 | $this->dbutil->xml_from_result($db_result); |
| 143 | =========================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 144 | |
| 145 | Permits you to generate an XML file from a query result. The first |
| 146 | parameter expects a query result object, the second may contain an |
| 147 | optional array of config parameters. Example:: |
| 148 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 149 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 161 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 162 | .. important:: This method will NOT write the XML file for you. It |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | simply creates the XML layout. If you need to write the file |
| 164 | use the :doc:`File Helper <../helpers/file_helper>`. |
| 165 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 166 | $this->dbutil->backup(); |
| 167 | ======================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 168 | |
| 169 | Permits you to backup your full database or individual tables. The |
| 170 | backup data can be compressed in either Zip or Gzip format. |
| 171 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 172 | .. note:: This feature is only available for MySQL and Interbase/Firebird databases. |
Timothy Warren | ab189e1 | 2012-02-22 10:34:23 -0500 | [diff] [blame] | 173 | |
| 174 | .. note:: For Interbase/Firebird databases, the backup file name is the only parameter. |
| 175 | |
| 176 | Eg. $this->dbutil->backup('db_backup_filename'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 177 | |
| 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 | |
| 184 | Usage Example |
| 185 | ------------- |
| 186 | |
| 187 | :: |
| 188 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 189 | // 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 202 | |
| 203 | Setting Backup Preferences |
| 204 | -------------------------- |
| 205 | |
| 206 | Backup preferences are set by submitting an array of values to the first |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 207 | parameter of the ``backup()`` method. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 208 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 209 | $prefs = array( |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 210 | '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 Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 217 | ); |
| 218 | |
| 219 | $this->dbutil->backup($prefs); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 220 | |
| 221 | Description of Backup Preferences |
| 222 | --------------------------------- |
| 223 | |
Joseph Wensley | 5b3ea1a | 2011-10-06 20:54:32 -0400 | [diff] [blame] | 224 | =============== ======================= ======================= ======================================================================== |
| 225 | Preference 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 | =============== ======================= ======================= ======================================================================== |