Skip to content

Commit 11f96ea

Browse files
committed
docs: add missing methods in Database Utility Class
1 parent b555ce2 commit 11f96ea

File tree

8 files changed

+137
-0
lines changed

8 files changed

+137
-0
lines changed

user_guide_src/source/database/utilities.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,84 @@ parameter.
3030
Using the Database Utilities
3131
****************************
3232

33+
Retrieve List of Database Names
34+
================================
35+
36+
Returns an array of database names:
37+
38+
.. literalinclude:: utilities/004.php
39+
:lines: 2-
40+
41+
Determine If a Database Exists
42+
==============================
43+
44+
Sometimes it's helpful to know whether a particular database exists.
45+
Returns a boolean ``true``/``false``. Usage example:
46+
47+
.. literalinclude:: utilities/005.php
48+
:lines: 2-
49+
50+
.. note:: Replace ``database_name`` with the name of the database you are
51+
looking for. This method is case sensitive.
52+
53+
Optimize a Table
54+
================
55+
56+
Permits you to optimize a table using the table name specified in the
57+
first parameter. Returns ``true``/``false`` based on success or failure:
58+
59+
.. literalinclude:: utilities/006.php
60+
:lines: 2-
61+
62+
.. note:: Not all database platforms support table optimization. It is
63+
mostly for use with MySQL.
64+
65+
Repair a Table
66+
==============
67+
68+
Permits you to repair a table using the table name specified in the
69+
first parameter. Returns ``true``/``false`` based on success or failure:
70+
71+
.. literalinclude:: utilities/007.php
72+
:lines: 2-
73+
74+
.. note:: Not all database platforms support table repairs.
75+
76+
Optimize a Database
77+
===================
78+
79+
Permits you to optimize the database your DB class is currently
80+
connected to. Returns an array containing the DB status messages or
81+
``false`` on failure:
82+
83+
.. literalinclude:: utilities/008.php
84+
:lines: 2-
85+
86+
.. note:: Not all database platforms support database optimization. It
87+
it is mostly for use with MySQL.
88+
89+
Export a Query Result as a CSV File
90+
===================================
91+
92+
Permits you to generate a CSV file from a query result. The first
93+
parameter of the method must contain the result object from your
94+
query. Example:
95+
96+
.. literalinclude:: utilities/009.php
97+
:lines: 2-
98+
99+
The second, third, and fourth parameters allow you to set the delimiter
100+
newline, and enclosure characters respectively. By default commas are
101+
used as the delimiter, ``"\n"`` is used as a new line, and a double-quote
102+
is used as the enclosure. Example:
103+
104+
.. literalinclude:: utilities/010.php
105+
:lines: 2-
106+
107+
.. important:: This method 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 :php:func:`write_file()` helper.
110+
33111
Export a Query Result as an XML Document
34112
========================================
35113

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
$dbutil = \CodeIgniter\Database\Config::utils();
4+
5+
$dbs = $dbutil->listDatabases();
6+
7+
foreach ($dbs as $db) {
8+
echo $db;
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$dbutil = \CodeIgniter\Database\Config::utils();
4+
5+
if ($dbutil->databaseExists('database_name')) {
6+
// some code...
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$dbutil = \CodeIgniter\Database\Config::utils();
4+
5+
if ($dbutil->optimize_table('table_name')) {
6+
echo 'Success!';
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$dbutil = \CodeIgniter\Database\Config::utils();
4+
5+
if ($dbutil->repairTable('table_name')) {
6+
echo 'Success!';
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
$dbutil = \CodeIgniter\Database\Config::utils();
4+
5+
$result = $dbutil->optimizeDatabase();
6+
7+
if ($result !== false) {
8+
print_r($result);
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$db = db_connect();
4+
$dbutil = \CodeIgniter\Database\Config::utils();
5+
6+
$query = $db->query('SELECT * FROM mytable');
7+
8+
echo $dbutil->getCSVFromResult($query);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
$db = db_connect();
4+
$dbutil = \CodeIgniter\Database\Config::utils();
5+
6+
$query = $db->query('SELECT * FROM mytable');
7+
8+
$delimiter = ',';
9+
$newline = "\r\n";
10+
$enclosure = '"';
11+
12+
echo $dbutil->getCSVFromResult($query, $delimiter, $newline, $enclosure);

0 commit comments

Comments
 (0)