DB
class is the root manager class that makes it easy to connect to the database and perform several
operations smartly. In spoova, there are two different ways by which a database connection can be made
before operations can be performed. Connections can either be made manually or by default through the use
of a default database configuration file.
DB
and DBHandler
classes, we can easily set up a new connection using the syntax
below:
<?php use \spoova\mi\core\classes\DB; $dbm = (new DB(MiSQL | PDO)); // initialize database class // connect and return database operations handler class $dbh = $dbm->openDB(DBNAME, DBUSER, DBPASS, DBPORT, DBSERVER, DBSOCKET);
DB
was first initilized in the first line.
This database class supports two type of connection which are MySQL denoted as "MiSQL" and "PDO".
The default connection is PDO. Hence, we can change this default by simply overiding the default
option as shown below:
$dbm = (new DB('MiSQL')); // using mysql connection
$dbh = $dbm->openDB(DBNAME, DBUSER, DBPASS, DBPORT, DBSERVER);
openDB()
method of the database class tries to create a new connection. This connection, if successful,
it will return the DBHandler
class which will make it possible to perform database operations.
icore/dbconfig.php
file. This file contains connection parameters defined for offline and online environments.
Depending on the project environment, spoova internal picks its suitable connection parameters. The connection parameters
can be easily configured manually from the mentioned file or by running simple terminal commands like below:
php mi config:dboffline dbname dbuser dbpass dbport dbserver dbsocket
php mi config:dbonline dbname dbuser dbpass dbport dbserver dbsocket
DBHandler
class will automatically pick
its connection parameters when initialized. Easily, we can start a new connection as shown below:
<?php use \spoova\mi\core\classes\DB; $dbm = (new DB); // initialize database class $dbh = $dbm->openDB(); // start automatic connection
openDB()
method of the database class tries to create a new connection using default configuration parameters.
This connection, if successful, it will return the DBHandler
class which will make it possible to perform database operations.
openDB
method. A sample of this is displayed below:
$dbm = new DB;
$dbh = $dbm->open('', DBUSER, DBPASS, DBSERVER, DBPORT); //basic connection
DBNAME
was not specified. This type of connection will only connect to the database without trying to select
any database name.
$dbm = new DB;
$dbh = $dbm->open(DBNAME); //overide DBNAME only
$dbh($dbc = (new DB)->openDB(); if($dbc->active()){ //run this if database is connected } else { vdump($dbc->error(true)); // display connection error }
$dbh($dbc = (new DB)->openDB(); if($dbc->active()){ if( $dbh->isConnected() ) { //database name is connected and selected }else{ vdump($dbc->error(true)); // display connection error } } else { vdump($dbc->error(true)); // display connection error }
isConnected()
will never return true if an error occurs. This
means that we can shorten the line of code as shown below:
$dbh($dbc = (new DB)->openDB(); if( $dbh->isConnected() ) { //database name is connected and selected }else{ vdump($dbc->error(true)); // display connection error }
$dbh
will be referred to as $db
since it is
the database handler class that handles majority of the database crud operations.
$db = (new DB)->openDB();
if($db->table_exists('table_name')) {
//run this code ...
}
$db = (new DB)->openDB();
if($db->column_exists('table_name', 'column_name')) {
//run this code ...
}
$db = (new DB)->openDB();
if($db->addColumn([table_name => column_name], type, pipe, definition, default)) {
where:
table_name : name of table where column will be added
column_name : name of column to be added
type : type of column e.g ( decimal(2,5); varchar(200), e.t.c)
pipe : FIRST | AFTER FIELDNAME (After can be replaced with a pipe e.g "|Email" means AFTER Email )
definition : field definition (e.g NOT NULL, UNIQUE)
default : field default value.
}
1970-01-01 00:00:00
as the default datetime which
still translates as zero.$db = (new DB)->openDB(); if($db->drop(true)) { //currently connected database dropped successfully! } if($db->drop('table_name', true)) { //selected table_name of current database dropped successfully! } if($db->drop('table_name', 'column_name')) { //relative column dropped successfully }
error_exists()
and error()
method must be supplied
an argument of true
in order to function as expected. However DBStatus::err()
will still return
the last error encountered. Errors are discussed below.
DBStatus
helps to keep track of last executed
sql queries and error responses.