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 | |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 11 | ****************************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 12 | Initializing the Utility Class |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 13 | ****************************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 14 | |
| 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 | |
| 18 | Load the Utility Class as follows:: |
| 19 | |
| 20 | $this->load->dbutil() |
| 21 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 22 | You can also pass another database object to the DB Utility loader, in case |
| 23 | the database you want to manage isn't the default one:: |
| 24 | |
| 25 | $this->myutil = $this->load->dbutil($this->other_db, TRUE); |
| 26 | |
| 27 | In the above example, we're passing a custom database object as the first |
| 28 | parameter and then tell it to return the dbutil object, instead of |
| 29 | assigning 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 | |
| 34 | Once initialized you will access the methods using the ``$this->dbutil`` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | object:: |
| 36 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 37 | $this->dbutil->some_method() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 39 | **************************** |
| 40 | Using the Database Utilities |
| 41 | **************************** |
| 42 | |
| 43 | Retrieve list of database names |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | ================================ |
| 45 | |
| 46 | Returns an array of database names:: |
| 47 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 48 | $dbs = $this->dbutil->list_databases(); |
Andrew Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 49 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 50 | foreach ($dbs as $db) |
| 51 | { |
| 52 | echo $db; |
| 53 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 55 | |
| 56 | Determine If a Database Exists |
| 57 | ============================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 58 | |
| 59 | Sometimes it's helpful to know whether a particular database exists. |
| 60 | Returns a boolean TRUE/FALSE. Usage example:: |
| 61 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 62 | if ($this->dbutil->database_exists('database_name')) |
| 63 | { |
| 64 | // some code... |
| 65 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 67 | .. note:: Replace *database_name* with the name of the table you are |
| 68 | looking for. This method is case sensitive. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 69 | |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 70 | Optimize a Table |
| 71 | ================ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | |
| 73 | Permits you to optimize a table using the table name specified in the |
| 74 | first parameter. Returns TRUE/FALSE based on success or failure:: |
| 75 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 76 | if ($this->dbutil->optimize_table('table_name')) |
| 77 | { |
| 78 | echo 'Success!'; |
| 79 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 81 | .. note:: Not all database platforms support table optimization. It is |
| 82 | mostly for use with MySQL. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 84 | Repair a Table |
| 85 | ============== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | |
| 87 | Permits you to repair a table using the table name specified in the |
| 88 | first parameter. Returns TRUE/FALSE based on success or failure:: |
| 89 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 90 | if ($this->dbutil->repair_table('table_name')) |
| 91 | { |
| 92 | echo 'Success!'; |
| 93 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | |
| 95 | .. note:: Not all database platforms support table repairs. |
| 96 | |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 97 | Optimize a Database |
| 98 | =================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 99 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | Permits you to optimize the database your DB class is currently |
| 101 | connected to. Returns an array containing the DB status messages or |
| 102 | FALSE on failure. |
| 103 | |
| 104 | :: |
| 105 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 106 | $result = $this->dbutil->optimize_database(); |
Andrew Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 107 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 108 | if ($result !== FALSE) |
| 109 | { |
| 110 | print_r($result); |
| 111 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 113 | .. note:: Not all database platforms support table optimization. It |
| 114 | it is mostly for use with MySQL. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 115 | |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 116 | Export a Query Result as a CSV File |
| 117 | =================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | |
| 119 | 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] | 120 | parameter of the method must contain the result object from your |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | query. Example:: |
| 122 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 123 | $this->load->dbutil(); |
Andrew Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 124 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 125 | $query = $this->db->query("SELECT * FROM mytable"); |
Andrew Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 126 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 127 | echo $this->dbutil->csv_from_result($query); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | |
| 129 | The second, third, and fourth parameters allow you to set the delimiter |
mrw | 7310e85 | 2012-03-26 11:25:26 -0300 | [diff] [blame] | 130 | newline, and enclosure characters respectively. By default commas are |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | used as the delimiter, "\n" is used as a new line, and a double-quote |
| 132 | is 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 Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 140 | .. important:: This method will NOT write the CSV file for you. It |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 141 | simply creates the CSV layout. If you need to write the file |
| 142 | use the :doc:`File Helper <../helpers/file_helper>`. |
| 143 | |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 144 | Export a Query Result as an XML Document |
| 145 | ======================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 146 | |
| 147 | Permits you to generate an XML file from a query result. The first |
| 148 | parameter expects a query result object, the second may contain an |
| 149 | optional array of config parameters. Example:: |
| 150 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 151 | $this->load->dbutil(); |
Andrew Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 152 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 153 | $query = $this->db->query("SELECT * FROM mytable"); |
Andrew Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 154 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 155 | $config = array ( |
| 156 | 'root' => 'root', |
| 157 | 'element' => 'element', |
| 158 | 'newline' => "\n", |
Andrew Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 159 | 'tab' => "\t" |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 160 | ); |
Andrew Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 161 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 162 | echo $this->dbutil->xml_from_result($query, $config); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 164 | .. important:: This method will NOT write the XML file for you. It |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 165 | simply creates the XML layout. If you need to write the file |
| 166 | use the :doc:`File Helper <../helpers/file_helper>`. |
| 167 | |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 168 | ******************** |
| 169 | Backup Your Database |
| 170 | ******************** |
| 171 | |
| 172 | Database Backup Notes |
| 173 | ===================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 174 | |
| 175 | Permits you to backup your full database or individual tables. The |
| 176 | backup data can be compressed in either Zip or Gzip format. |
| 177 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 178 | .. note:: This feature is only available for MySQL and Interbase/Firebird databases. |
Timothy Warren | ab189e1 | 2012-02-22 10:34:23 -0500 | [diff] [blame] | 179 | |
| 180 | .. note:: For Interbase/Firebird databases, the backup file name is the only parameter. |
Andrew Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 181 | |
Timothy Warren | ab189e1 | 2012-02-22 10:34:23 -0500 | [diff] [blame] | 182 | Eg. $this->dbutil->backup('db_backup_filename'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 183 | |
| 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 | |
| 190 | Usage Example |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 191 | ============= |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 192 | |
| 193 | :: |
| 194 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 195 | // Load the DB utility class |
| 196 | $this->load->dbutil(); |
Andrew Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 197 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 198 | // Backup your entire database and assign it to a variable |
| 199 | $backup =& $this->dbutil->backup(); |
Andrew Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 200 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 201 | // 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 Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 204 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 205 | // Load the download helper and send the file to your desktop |
| 206 | $this->load->helper('download'); |
| 207 | force_download('mybackup.gz', $backup); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 208 | |
| 209 | Setting Backup Preferences |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 210 | ========================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 211 | |
| 212 | 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] | 213 | parameter of the ``backup()`` method. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 214 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 215 | $prefs = array( |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 216 | '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 Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 223 | ); |
Andrew Podner | 79494dd | 2012-12-19 14:15:41 -0500 | [diff] [blame] | 224 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 225 | $this->dbutil->backup($prefs); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 226 | |
| 227 | Description of Backup Preferences |
James L Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 228 | ================================= |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 229 | |
Andrew Podner | 4851217 | 2012-12-20 07:56:19 -0500 | [diff] [blame] | 230 | ======================= ======================= ======================= ======================================================================== |
| 231 | Preference 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 Parry | ee477c6 | 2014-11-25 16:15:18 -0800 | [diff] [blame^] | 243 | ======================= ======================= ======================= ======================================================================== |
| 244 | |
| 245 | *************** |
| 246 | Class 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 |