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