Skip to content

Instantly share code, notes, and snippets.

@sturdy5
Created July 14, 2014 20:37
Show Gist options
  • Save sturdy5/9f7f07539c2f980161b7 to your computer and use it in GitHub Desktop.
Save sturdy5/9f7f07539c2f980161b7 to your computer and use it in GitHub Desktop.
Attempting to write a class that can be used to abstract the database that is being used within a php application. Currently supports mysqli and sqlite.
<?php
class DatabaseUtility {
/**
* Opens the connection to the database and returns the database handler.
*
* @param $db_server - the name of the server that hosts the database
* @param $db_user - the name of the user to use to access the database
* @param $db_password - the password for the user to access the database
* @param $db_database - the name of the database to connect to
*/
public function open($db_server, $db_user, $db_password, $db_database) {
echo 'use the real implementation instead of the base class';
}
/**
* Queries the database using the query parameter and returns the results
*
* @param $db_handle - the handle for the database connection
* @param $query - the query to execute
*/
public function query($db_handle, $query) {
echo 'use the real implementation instead of the base class';
}
/**
* Inserts data into the database.
*
* @param $db_handle - the handle for the database connection
* @param $statement - the SQL statement to execute
*/
public function insert($db_handle, $statement) {
echo 'use the real implementation instead of the base class';
}
/**
* Gets the last inserted rows id.
*
* @param $db_handle - the handle for the database connection
* @returns the last inserted rows id
*/
public function getInsertId($db_handle) {
echo 'use the real implementation instead of the base class';
}
/**
* Gets the last error message for the most recent function call to succeed or fail.
*
* @param $db_handle - the handle for the database
* @returns the last error message for the most recent function call to succeed or fail
*/
public function getError($db_handle) {
echo 'use the real implementation instead of the base class';
}
/**
* Fetches the next row that is represented in the result object.
*
* @param $result - the result of a query call
* @returns the next row in the result
*/
public function getRow($result) {
echo 'use the real implementation instead of the base class';
}
/**
* Frees the result set
*
* @param $result - the result set to free
*/
public function freeResult($result) {
echo 'use the real implementation instead of the base class';
}
/**
* Gets the number of rows that were affected by the most recent function call.
*
* @param $result - the result set to check
* @returns the number of rows affected
*/
public function numberOfRowsAffected($result) {
echo 'use the real implementation instead of the base class';
}
}
class MySQLDatabaseUtility extends DatabaseUtility {
/**
* Opens the connection to the database and returns the database handler.
*
* @param $db_server - the name of the server that hosts the database
* @param $db_user - the name of the user to use to access the database
* @param $db_password - the password for the user to access the database
* @param $db_database - the name of the database to connect to
*/
public function open($db_server, $db_user, $db_password, $db_database) {
$handle = new mysqli($db_server, $db_user, $db_password, $db_database);
if ($handle->connect_errno) {
die("Could not connect to the database - " . $handle->connect_errno);
}
return $handle;
}
/**
* Queries the database using the query parameter and returns the results
*
* @param $db_handle - the handle for the database connection
* @param $query - the query to execute
*/
public function query($db_handle, $query) {
return $db_handle->query($query);
}
/**
* Inserts data into the database.
*
* @param $db_handle - the handle for the database connection
* @param $statement - the SQL statement to execute
*/
public function insert($db_handle, $statement) {
$db_handle->query($statement);
}
/**
* Gets the last inserted rows id.
*
* @param $db_handle - the handle for the database connection
* @returns the last inserted rows id
*/
public function getInsertId($db_handle) {
return $db_handle->insert_id;
}
/**
* Gets the last error message for the most recent function call to succeed or fail.
*
* @param $db_handle - the handle for the database
* @returns the last error message for the most recent function call to succeed or fail
*/
public function getError($db_handle) {
return $db_handle->error;
}
/**
* Fetches the next row that is represented in the result object.
*
* @param $result - the result of a query call
* @returns the next row in the result
*/
public function getRow($result) {
return $result->fetch_array();
}
/**
* Frees the result set
*
* @param $result - the result set to free
*/
public function freeResult($result) {
$result->free();
}
/**
* Gets the number of rows that were affected by the most recent function call.
*
* @param $result - the result set to check
* @returns the number of rows affected
*/
public function numberOfRowsAffected($result) {
return $result->num_rows;
}
}
class SQLiteDatabaseUtility extends DatabaseUtility {
/**
* Opens the connection to the database and returns the database handler.
*
* @param $db_server - not used
* @param $db_user - not used
* @param $db_password - not used
* @param $db_database - the location of the database to connect to
*/
public function open($db_server, $db_user, $db_password, $db_database) {
$handle = sqlite_open($db_database, 0666, $error);
if (!$handle) {
die("Could not connect to the database - " . $error);
}
return $handle;
}
/**
* Queries the database using the query parameter and returns the results
*
* @param $db_handle - the handle for the database connection
* @param $query - the query to execute
*/
public function query($db_handle, $query) {
return sqlite_query($db_handle, $query);
}
/**
* Inserts data into the database.
*
* @param $db_handle - the handle for the database connection
* @param $statement - the SQL statement to execute
*/
public function insert($db_handle, $statement) {
sqlite_exec($db_handle, $statement);
}
/**
* Gets the last inserted rows id.
*
* @param $db_handle - the handle for the database connection
* @returns the last inserted rows id
*/
public function getInsertId($db_handle) {
return sqlite_last_insert_rowid($db_handle);
}
/**
* Gets the last error message for the most recent function call to succeed or fail.
*
* @param $db_handle - the handle for the database
* @returns the last error message for the most recent function call to succeed or fail
*/
public function getError($db_handle) {
$code = sqlite_last_error($db_handle);
return sqlite_error_string($code);
}
/**
* Fetches the next row that is represented in the result object.
*
* @param $result - the result of a query call
* @returns the next row in the result
*/
public function getRow($result) {
return sqlite_fetch_array($result);
}
/**
* Frees the result set
*
* @param $result - the result set to free
*/
public function freeResult($result) {
// no op for sqlite
}
/**
* Gets the number of rows that were affected by the most recent function call.
*
* @param $result - the result set to check
* @returns the number of rows affected
*/
public function numberOfRowsAffected($result) {
return sqlite_num_rows($result);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment