Last active
December 16, 2015 18:09
-
-
Save AmyStephen/5475201 to your computer and use it in GitHub Desktop.
Database Interface without SQL Object Interaction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Database Interface | |
* | |
* @package Molajo | |
* @copyright 2013 Amy Stephen. All rights reserved. | |
* @license http://www.opensource.org/licenses/mit-license.html MIT License | |
*/ | |
namespace Molajo\Database\Api; | |
defined('MOLAJO') or die; | |
use Molajo\Database\Exception\DatabaseException; | |
/** | |
* Database Interface | |
* | |
* @package Molajo | |
* @license http://www.opensource.org/licenses/mit-license.html MIT License | |
* @copyright 2013 Amy Stephen. All rights reserved. | |
* @since 1.0 | |
*/ | |
interface DatabaseInterface | |
{ | |
/** | |
* Connect to the Database, passing through credentials and other data needed to secure a connection | |
* | |
* $options = array(); | |
* $options['db_type'] = $db_type; | |
* $options['db_host'] = $db_host; | |
* $options['db_port'] = $db_port; | |
* $options['db_socket'] = $db_socket; | |
* $options['db_user'] = $db_user; | |
* $options['db_password'] = $db_password; | |
* $options['db_name'] = $db; | |
* $options['db_prefix'] = $db_prefix; | |
* $options['process_plugins'] = $process_plugins; | |
* $options['select'] = true; | |
* | |
* try { | |
* $adapter_handler = $this->getAdapterHandler('Adaptername'); | |
* $this->adapter = $this->getAdapter($adapter_handler, $options); | |
* | |
* } catch (Exception $e) { | |
* throw new DatabaseException ('Database Adapter instantiation failed.' . $e->getMessage()); | |
* } | |
* | |
* @param array $options | |
* | |
* @return $this | |
* @since 1.0 | |
* @throws DatabaseException | |
*/ | |
public function connect($options = array()); | |
/** | |
* Retrieves the PHP date format compliant with the database driver | |
* | |
* $date_format = $this->adapter->getDateFormat(); | |
* | |
* @return string | |
* @since 1.0 | |
* @throws DatabaseException | |
*/ | |
public function getDateFormat(); | |
/** | |
* Returns a value for null date compliant with the database driver | |
* | |
* $null_date = $this->adapter->getNullDate(); | |
* | |
* @return string | |
* @since 1.0 | |
* @throws DatabaseException | |
*/ | |
public function getNullDate(); | |
/** | |
* Returns the value sent in, quoted for use in a database query | |
* | |
* $this->adapter->quote($string); | |
* | |
* @param string $value | |
* | |
* @return mixed | |
* @since 1.0 | |
* @throws DatabaseException | |
*/ | |
public function quote($value); | |
/** | |
* Returns the name sent in (ex. table name or column name), quoted for use in a database query | |
* | |
* $this->adapter->quoteName($field_name); | |
* | |
* @param string $name | |
* | |
* @return string | |
* @since 1.0 | |
* @throws DatabaseException | |
*/ | |
public function quoteName($name); | |
/** | |
* Escapes value sent in for use in a database query | |
* | |
* $this->adapter->escape($text); | |
* | |
* @param string $text | |
* | |
* @return string | |
* @since 1.0 | |
* @throws DatabaseException | |
*/ | |
public function escape($text); | |
/** | |
* Sends SQL to the database, returning a single data value as the result | |
* | |
* $results = $this->adapter->loadResult($sql); | |
* echo $results->title; | |
* | |
* @param string $sql | |
* | |
* @return object | |
* @since 1.0 | |
* @throws DatabaseException | |
*/ | |
public function loadResult($sql); | |
/** | |
* Sends the SQL request to the database, returning an array of rows, each row of which is an object. | |
* $Offset represents the row number in the result set from which to start. | |
* $Limit specifies the maximum number of rows to be returned. | |
* | |
* $results = $this->adapter->loadObjectList($sql, $offset, $limit); | |
* | |
* if (count($results) > 0) { | |
* foreach ($results as $row) { | |
* $title = $results->title; | |
* $author = $results->author; | |
* // etc. | |
* } | |
* } | |
* | |
* @param string $sql | |
* @param null|int $offset | |
* @param null|int $limit | |
* | |
* @return object | |
* @since 1.0 | |
* @throws DatabaseException | |
*/ | |
public function loadObjectList($sql, $offset = null, $limit = null); | |
/** | |
* Execute $sql as a database query. Can used to select data, create tables, insert, update, delete data, etc. | |
* | |
* $results = $this->adapter->execute($sql); | |
* | |
* @param null|string $sql | |
* | |
* @return object | |
* @since 1.0 | |
* @throws DatabaseException | |
*/ | |
public function execute($sql); | |
/** | |
* Returns the primary key following insert | |
* | |
* $sql = 'INSERT (x, y, z) INTO $__table VALUES (1, 2, 3)'; | |
* $results = $this->adapter->execute($sql); | |
* | |
* echo $this->adapter->getInsertId(); | |
* | |
* @return integer | |
* @since 1.0 | |
* @throws DatabaseException | |
*/ | |
public function getInsertId(); | |
/** | |
* Disconnects from Database and removes the database connection | |
* | |
* echo $this->adapter->disconnect(); | |
* | |
* @return $this | |
* @since 1.0 | |
* @throws DatabaseException | |
*/ | |
public function disconnect(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Relevant work from FIG membership:
Doctrine Connection has prepare, query, quote, exec, lastInsertId, beginTransaction, commit, rollBack, errorCode, ErrorInfo
zf2 Database Adapters. The ConnectionInterface has connect, isConnected, disconnect, beginTransaction, commit, rollback, execute, getLastGenerateValue
Laravel The ConnectionInterface has selectOne, select, insert, update, delete, statement, transaction
Lithium Abstract handler for databases encoding, error, execute, insertId, connect, disconnect, name (escaping), value (escaping), create, read, queryExport, update, delete,