blob: ab7d6a14971129533ce4a825fda2afcbc1e6832b [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
35 $dbs = $this->dbutil->list_databases(); foreach ($dbs as $db) {     echo $db; }
36
37$this->dbutil->database_exists();
38==================================
39
40Sometimes it's helpful to know whether a particular database exists.
41Returns a boolean TRUE/FALSE. Usage example::
42
43 if ($this->dbutil->database_exists('database_name')) {    // some code... }
44
45Note: Replace *database_name* with the name of the table you are
46looking for. This function is case sensitive.
47
48$this->dbutil->optimize_table('table_name');
49==============================================
50
51.. note:: This features is only available for MySQL/MySQLi databases.
52
53Permits you to optimize a table using the table name specified in the
54first parameter. Returns TRUE/FALSE based on success or failure::
55
56 if ($this->dbutil->optimize_table('table_name')) {     echo 'Success!'; }
57
58.. note:: Not all database platforms support table optimization.
59
60$this->dbutil->repair_table('table_name');
61============================================
62
63.. note:: This features is only available for MySQL/MySQLi databases.
64
65Permits you to repair a table using the table name specified in the
66first parameter. Returns TRUE/FALSE based on success or failure::
67
68 if ($this->dbutil->repair_table('table_name')) {     echo 'Success!'; }
69
70.. note:: Not all database platforms support table repairs.
71
72$this->dbutil->optimize_database();
73====================================
74
75.. note:: This features is only available for MySQL/MySQLi databases.
76
77Permits you to optimize the database your DB class is currently
78connected to. Returns an array containing the DB status messages or
79FALSE on failure.
80
81::
82
83 $result = $this->dbutil->optimize_database(); if ($result !== FALSE) {     print_r($result); }
84
85.. note:: Not all database platforms support table optimization.
86
87$this->dbutil->csv_from_result($db_result)
88=============================================
89
90Permits you to generate a CSV file from a query result. The first
91parameter of the function must contain the result object from your
92query. Example::
93
94 $this->load->dbutil(); $query = $this->db->query("SELECT * FROM mytable"); echo $this->dbutil->csv_from_result($query);
95
96The second, third, and fourth parameters allow you to set the delimiter
97newline, and enclosure characters respectively. By default tabs are
98used as the delimiter, "\n" is used as a new line, and a double-quote
99is used as the enclosure. Example::
100
101 $delimiter = ",";
102 $newline = "\r\n";
103 $enclosure = '"';
104
105 echo $this->dbutil->csv_from_result($query, $delimiter, $newline, $enclosure);
106
107.. important:: This function will NOT write the CSV file for you. It
108 simply creates the CSV layout. If you need to write the file
109 use the :doc:`File Helper <../helpers/file_helper>`.
110
111$this->dbutil->xml_from_result($db_result)
112=============================================
113
114Permits you to generate an XML file from a query result. The first
115parameter expects a query result object, the second may contain an
116optional array of config parameters. Example::
117
118 $this->load->dbutil(); $query = $this->db->query("SELECT * FROM mytable"); $config = array (                   'root'    => 'root',                   'element' => 'element',                   'newline' => "\n",                   'tab'    => "\t"                 ); echo $this->dbutil->xml_from_result($query, $config);
119
120.. important:: This function will NOT write the XML file for you. It
121 simply creates the XML layout. If you need to write the file
122 use the :doc:`File Helper <../helpers/file_helper>`.
123
124$this->dbutil->backup()
125=======================
126
127Permits you to backup your full database or individual tables. The
128backup data can be compressed in either Zip or Gzip format.
129
130.. note:: This features is only available for MySQL databases.
131
132.. note:: Due to the limited execution time and memory available to PHP,
133 backing up very large databases may not be possible. If your database is
134 very large you might need to backup directly from your SQL server via
135 the command line, or have your server admin do it for you if you do not
136 have root privileges.
137
138Usage Example
139-------------
140
141::
142
143 // Load the DB utility class $this->load->dbutil(); // Backup your entire database and assign it to a variable $backup =& $this->dbutil->backup(); // Load the file helper and write the file to your server $this->load->helper('file'); write_file('/path/to/mybackup.gz', $backup); // Load the download helper and send the file to your desktop $this->load->helper('download'); force_download('mybackup.gz', $backup);
144
145Setting Backup Preferences
146--------------------------
147
148Backup preferences are set by submitting an array of values to the first
149parameter of the backup function. Example::
150
151 $prefs = array(                 'tables'      => array('table1', 'table2'),  // Array of tables to backup.                 'ignore'      => array(),           // List of tables to omit from the backup                 'format'      => 'txt',             // gzip, zip, txt                 'filename'    => 'mybackup.sql',    // File name - NEEDED ONLY WITH ZIP FILES                 'add_drop'    => TRUE,              // Whether to add DROP TABLE statements to backup file                 'add_insert'  => TRUE,              // Whether to add INSERT data to backup file                 'newline'     => "\n"               // Newline character used in backup file               ); $this->dbutil->backup($prefs);
152
153Description of Backup Preferences
154---------------------------------
155
Joseph Wensley5b3ea1a2011-10-06 20:54:32 -0400156=============== ======================= ======================= ========================================================================
157Preference Default Value Options Description
158=============== ======================= ======================= ========================================================================
159**tables** empty array None An array of tables you want backed up. If left blank all tables will be
160 exported.
161**ignore** empty array None An array of tables you want the backup routine to ignore.
162**format** gzip gzip, zip, txt The file format of the export file.
163**filename** the current date/time None The name of the backed-up file. The name is needed only if you are using
164 zip compression.
165**add_drop** TRUE TRUE/FALSE Whether to include DROP TABLE statements in your SQL export file.
166**add_insert** TRUE TRUE/FALSE Whether to include INSERT statements in your SQL export file.
167**newline** "\\n" "\\n", "\\r", "\\r\\n" Type of newline to use in your SQL export file.
168=============== ======================= ======================= ========================================================================