Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ###################### |
| 2 | Database Utility Class |
| 3 | ###################### |
| 4 | |
| 5 | The Database Utility Class contains functions that help you manage your |
| 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 | |
| 25 | Once initialized you will access the functions using the $this->dbutil |
| 26 | object:: |
| 27 | |
| 28 | $this->dbutil->some_function() |
| 29 | |
| 30 | $this->dbutil->list_databases() |
| 31 | ================================ |
| 32 | |
| 33 | Returns an array of database names:: |
| 34 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 35 | $dbs = $this->dbutil->list_databases(); |
| 36 | |
| 37 | foreach ($dbs as $db) |
| 38 | { |
| 39 | echo $db; |
| 40 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 41 | |
| 42 | $this->dbutil->database_exists(); |
| 43 | ================================== |
| 44 | |
| 45 | Sometimes it's helpful to know whether a particular database exists. |
| 46 | Returns a boolean TRUE/FALSE. Usage example:: |
| 47 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 48 | if ($this->dbutil->database_exists('database_name')) |
| 49 | { |
| 50 | // some code... |
| 51 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | |
| 53 | Note: Replace *database_name* with the name of the table you are |
| 54 | looking 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 | |
| 61 | Permits you to optimize a table using the table name specified in the |
| 62 | first parameter. Returns TRUE/FALSE based on success or failure:: |
| 63 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 64 | if ($this->dbutil->optimize_table('table_name')) |
| 65 | { |
| 66 | echo 'Success!'; |
| 67 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 68 | |
| 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 | |
| 76 | Permits you to repair a table using the table name specified in the |
| 77 | first parameter. Returns TRUE/FALSE based on success or failure:: |
| 78 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 79 | if ($this->dbutil->repair_table('table_name')) |
| 80 | { |
| 81 | echo 'Success!'; |
| 82 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | |
| 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 | |
| 91 | Permits you to optimize the database your DB class is currently |
| 92 | connected to. Returns an array containing the DB status messages or |
| 93 | FALSE on failure. |
| 94 | |
| 95 | :: |
| 96 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 97 | $result = $this->dbutil->optimize_database(); |
| 98 | |
| 99 | if ($result !== FALSE) |
| 100 | { |
| 101 | print_r($result); |
| 102 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 103 | |
| 104 | .. note:: Not all database platforms support table optimization. |
| 105 | |
| 106 | $this->dbutil->csv_from_result($db_result) |
| 107 | ============================================= |
| 108 | |
| 109 | Permits you to generate a CSV file from a query result. The first |
| 110 | parameter of the function must contain the result object from your |
| 111 | query. Example:: |
| 112 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 113 | $this->load->dbutil(); |
| 114 | |
| 115 | $query = $this->db->query("SELECT * FROM mytable"); |
| 116 | |
| 117 | echo $this->dbutil->csv_from_result($query); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | |
| 119 | The second, third, and fourth parameters allow you to set the delimiter |
| 120 | newline, and enclosure characters respectively. By default tabs are |
| 121 | used as the delimiter, "\n" is used as a new line, and a double-quote |
| 122 | is 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 | |
| 137 | Permits you to generate an XML file from a query result. The first |
| 138 | parameter expects a query result object, the second may contain an |
| 139 | optional array of config parameters. Example:: |
| 140 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 141 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 153 | |
| 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 | |
| 161 | Permits you to backup your full database or individual tables. The |
| 162 | backup data can be compressed in either Zip or Gzip format. |
| 163 | |
| 164 | .. note:: This features is only available for MySQL databases. |
| 165 | |
| 166 | .. note:: Due to the limited execution time and memory available to PHP, |
| 167 | backing up very large databases may not be possible. If your database is |
| 168 | very large you might need to backup directly from your SQL server via |
| 169 | the command line, or have your server admin do it for you if you do not |
| 170 | have root privileges. |
| 171 | |
| 172 | Usage Example |
| 173 | ------------- |
| 174 | |
| 175 | :: |
| 176 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 177 | // Load the DB utility class |
| 178 | $this->load->dbutil(); |
| 179 | |
| 180 | // Backup your entire database and assign it to a variable |
| 181 | $backup =& $this->dbutil->backup(); |
| 182 | |
| 183 | // Load the file helper and write the file to your server |
| 184 | $this->load->helper('file'); |
| 185 | write_file('/path/to/mybackup.gz', $backup); |
| 186 | |
| 187 | // Load the download helper and send the file to your desktop |
| 188 | $this->load->helper('download'); |
| 189 | force_download('mybackup.gz', $backup); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 190 | |
| 191 | Setting Backup Preferences |
| 192 | -------------------------- |
| 193 | |
| 194 | Backup preferences are set by submitting an array of values to the first |
| 195 | parameter of the backup function. Example:: |
| 196 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 197 | $prefs = array( |
| 198 | 'tables' => array('table1', 'table2'), // Array of tables to backup. |
| 199 | 'ignore' => array(), // List of tables to omit from the backup |
| 200 | 'format' => 'txt', // gzip, zip, txt |
| 201 | 'filename' => 'mybackup.sql', // File name - NEEDED ONLY WITH ZIP FILES |
| 202 | 'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file |
| 203 | 'add_insert' => TRUE, // Whether to add INSERT data to backup file |
| 204 | 'newline' => "\n" // Newline character used in backup file |
| 205 | ); |
| 206 | |
| 207 | $this->dbutil->backup($prefs); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 208 | |
| 209 | Description of Backup Preferences |
| 210 | --------------------------------- |
| 211 | |
Joseph Wensley | 5b3ea1a | 2011-10-06 20:54:32 -0400 | [diff] [blame] | 212 | =============== ======================= ======================= ======================================================================== |
| 213 | Preference Default Value Options Description |
| 214 | =============== ======================= ======================= ======================================================================== |
| 215 | **tables** empty array None An array of tables you want backed up. If left blank all tables will be |
| 216 | exported. |
| 217 | **ignore** empty array None An array of tables you want the backup routine to ignore. |
| 218 | **format** gzip gzip, zip, txt The file format of the export file. |
| 219 | **filename** the current date/time None The name of the backed-up file. The name is needed only if you are using |
| 220 | zip compression. |
| 221 | **add_drop** TRUE TRUE/FALSE Whether to include DROP TABLE statements in your SQL export file. |
| 222 | **add_insert** TRUE TRUE/FALSE Whether to include INSERT statements in your SQL export file. |
| 223 | **newline** "\\n" "\\n", "\\r", "\\r\\n" Type of newline to use in your SQL export file. |
| 224 | =============== ======================= ======================= ======================================================================== |