Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add DBUtils missing methods #8277

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions user_guide_src/source/database/utilities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,73 @@ parameter.
Using the Database Utilities
****************************

Retrieve List of Database Names
================================

Returns an array of database names:

.. literalinclude:: utilities/004.php
:lines: 2-

Determine If a Database Exists
==============================

Sometimes it's helpful to know whether a particular database exists.
Returns a boolean ``true``/``false``. Usage example:

.. literalinclude:: utilities/005.php
:lines: 2-

.. note:: Replace ``database_name`` with the name of the database you are
looking for. This method is case sensitive.

Optimize a Table
================

Permits you to optimize a table using the table name specified in the
first parameter. Returns ``true``/``false`` based on success or failure:

.. literalinclude:: utilities/006.php
:lines: 2-

.. note:: Not all database platforms support table optimization. It is
mostly for use with MySQL.

Optimize a Database
===================

Permits you to optimize the database your DB class is currently
connected to. Returns an array containing the DB status messages or
``false`` on failure:

.. literalinclude:: utilities/008.php
:lines: 2-

.. note:: Not all database platforms support database optimization. It
it is mostly for use with MySQL.

Export a Query Result as a CSV File
===================================

Permits you to generate a CSV file from a query result. The first
parameter of the method must contain the result object from your
query. Example:

.. literalinclude:: utilities/009.php
:lines: 2-

The second, third, and fourth parameters allow you to set the delimiter
newline, and enclosure characters respectively. By default commas are
used as the delimiter, ``"\n"`` is used as a new line, and a double-quote
is used as the enclosure. Example:

.. literalinclude:: utilities/010.php
:lines: 2-

.. important:: This method will NOT write the CSV file for you. It
simply creates the CSV layout. If you need to write the file
use the :php:func:`write_file()` helper.

Export a Query Result as an XML Document
========================================

Expand Down
9 changes: 9 additions & 0 deletions user_guide_src/source/database/utilities/004.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$dbutil = \Config\Database::utils();

$dbs = $dbutil->listDatabases();

foreach ($dbs as $db) {
echo $db;
}
7 changes: 7 additions & 0 deletions user_guide_src/source/database/utilities/005.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$dbutil = \Config\Database::utils();

if ($dbutil->databaseExists('database_name')) {
// some code...
}
7 changes: 7 additions & 0 deletions user_guide_src/source/database/utilities/006.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$dbutil = \Config\Database::utils();

if ($dbutil->optimizeTable('table_name')) {
echo 'Success!';
}
9 changes: 9 additions & 0 deletions user_guide_src/source/database/utilities/008.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$dbutil = \Config\Database::utils();

$result = $dbutil->optimizeDatabase();

if ($result !== false) {
print_r($result);
}
8 changes: 8 additions & 0 deletions user_guide_src/source/database/utilities/009.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$db = db_connect();
$dbutil = \Config\Database::utils();

$query = $db->query('SELECT * FROM mytable');

echo $dbutil->getCSVFromResult($query);
12 changes: 12 additions & 0 deletions user_guide_src/source/database/utilities/010.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$db = db_connect();
$dbutil = \Config\Database::utils();

$query = $db->query('SELECT * FROM mytable');

$delimiter = ',';
$newline = "\r\n";
$enclosure = '"';

echo $dbutil->getCSVFromResult($query, $delimiter, $newline, $enclosure);