Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: 64b9396afe
...
compare: fbdb3fc1ab
  • 2 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
View
2  dbFacile.php
@@ -272,7 +272,7 @@ public function fetch($sql, $parameters = array()) {
public function fetchRow($sql = null, $parameters = array()) {
if($sql != null)
$this->execute($sql, $parameters);
- // not all resources look like resources
+ // not all results look like resources, so can't use is_resource()
if($this->result)
return $this->_fetchRow();
return null;
View
397 splitup/dbFacile.php
@@ -0,0 +1,397 @@
+<?php
+/*
+dbFacile - A Database abstraction that should have existed from the start
+Version 0.4.3
+See LICENSE for license details.
+*/
+
+abstract class dbFacile {
+ protected $connection; // handle to Database connection
+ protected $logFile;
+
+ // implement these methods to create driver subclass
+ public abstract function affectedRows();
+ //public function beginTransaction();
+ public abstract function close();
+ //public function commitTransaction();
+ public abstract function error();
+ public abstract function escapeString($string);
+ public abstract function lastID();
+ public abstract function numberRows($result);
+ //public abstract function open();
+ //public function quoteField($field);
+ public abstract function rewind($result);
+ //public function rollbackTransaction();
+
+ protected abstract function _fetch($result);
+ protected abstract function _fetchAll($result);
+ protected abstract function _fetchRow($result);
+ //protected abstract function _fields($table);
+ // Should return a result handle, or false
+ protected abstract function _query($sql);
+
+ public function __construct($handle = null) {
+ $this->connection = $handle;
+ }
+
+ /*
+ * Performs a query using the given string.
+ * Used by the other _query functions.
+ * */
+ public function execute($sql, $parameters = array()) {
+ $fullSql = $this->makeQuery($sql, $parameters);
+
+ /*
+ if($this->logFile)
+ $time_start = microtime(true);
+ */
+
+ $result = $this->_query($fullSql); // sets $this->result
+
+ /*
+ if($this->logFile) {
+ $time_end = microtime(true);
+ fwrite($this->logFile, date('Y-m-d H:i:s') . "\n" . $fullSql . "\n" . number_format($time_end - $time_start, 8) . " seconds\n\n");
+ }
+
+ if(!$this->result && (error_reporting() & 1))
+ trigger_error('dbFacile - Error in query: ' . $this->query . ' : ' . $this->_error(), E_USER_WARNING);
+ */
+
+ // I know getting a real true or false is handy,
+ // but returning the result handle gives more flexibility
+ // and honestly, many oof the convenience functions check result anyway, so just pass it to them
+ return $result;
+ }
+
+ /*
+ * Passed an array and a table name, it attempts to insert the data into the table.
+ * Check for boolean false to determine whether insert failed
+ * */
+ public function insert($data, $table) {
+ // quote character
+ // maybe pre-split these for use by makequery
+ $sql = 'insert into ' . $table . ' (`' . implode('`,`', array_keys($data)) . '`) values(?' . str_repeat(',?', sizeof($data)-1) . ')';
+
+ $this->beginTransaction();
+ $result = $this->execute($sql, $data);
+ if($result) {
+ $id = $this->lastID($result);
+ $this->commitTransaction();
+ return $id;
+ } else {
+ $this->rollbackTransaction();
+ return false;
+ }
+ }
+
+ /*
+ * Passed an array, table name, where clause, and placeholder parameters, it attempts to update a record.
+ * Returns the number of affected rows
+ * */
+ public function update($data, $table, $where = null, $parameters = array()) {
+ // need field name and placeholder value
+ // but how merge these field placeholders with actual $parameters array for the where clause
+ $sql = 'update ' . $table . ' set ';
+ // implode no looping
+ foreach($data as $key => $value) {
+ $sql .= $this->quoteField($key) . '=?,';
+ }
+ $sql = substr($sql, 0, -1); // strip off last comma
+
+ if($where) {
+ $sql .= ' where ' . $where;
+ $data = array_merge($data, $parameters);
+ }
+
+ $result = $this->execute($sql, $data);
+ return $this->affectedRows($result);
+ }
+
+ public function delete($table, $where = null, $parameters = array()) {
+ $sql = 'delete from ' . $table;
+ if($where) {
+ $sql .= ' where ' . $where;
+ }
+ $result = $this->execute($sql, $parameters);
+ return $this->affectedRows($result);
+ }
+
+ /*
+ * This is intended to be the method used for large result sets.
+ * It is intended to return an iterator, and act upon buffered data.
+ * */
+ public function fetch($sql, $parameters = array()) {
+ $result = $this->execute($sql, $parameters);
+ return $this->_fetch($result);
+ }
+
+ /*
+ * Fetches all of the rows where each is an associative array.
+ * Tries to use unbuffered queries to cut down on execution time and memory usage,
+ * but you'll only see a benefit with extremely large result sets.
+ * */
+ public function fetchAll($sql, $parameters = array()) {
+ $result = $this->execute($sql, $parameters, false);
+ if ($result)
+ return $this->_fetchAll($result);
+ return array();
+ }
+
+ /*
+ * Fetches the first call from the first row returned by the query
+ * */
+ public function fetchCell($sql, $parameters = array()) {
+ $result = $this->execute($sql, $parameters);
+ if ($result) {
+ return array_shift($this->_fetchRow($result)); // shift first field off first row
+ }
+ return null;
+ }
+
+ /*
+ * This method is quite different from fetchCell(), actually
+ * It fetches one cell from each row and places all the values in 1 array
+ * */
+ public function fetchColumn($sql, $parameters = array()) {
+ $result = $this->execute($sql, $parameters);
+ if($result) {
+ $cells = array();
+ foreach($this->_fetchAll($result) as $row) {
+ $cells[] = array_shift($row);
+ }
+ return $cells;
+ } else {
+ return array();
+ }
+ }
+
+ /*
+ * Should be passed a query that fetches two fields
+ * The first will become the array key
+ * The second the key's value
+ */
+ public function fetchKeyValue($sql, $parameters = array()) {
+ $result = $this->execute($sql, $parameters);
+ if(!$result) return array();
+
+ $data = array();
+ foreach($this->_fetchAll($result) as $row) {
+ $key = array_shift($row);
+ if(sizeof($row) == 1) { // if there were only 2 fields in the result
+ // use the second for the value
+ $data[ $key ] = array_shift($row);
+ } else { // if more than 2 fields were fetched
+ // use the array of the rest as the value
+ $data[ $key ] = $row;
+ }
+ }
+ return $data;
+ }
+
+ /*
+ * Like fetch(), accepts any number of arguments
+ * The first argument is an sprintf-ready query stringTypes
+ * */
+ public function fetchRow($sql = null, $parameters = array()) {
+ $result = $this->execute($sql, $parameters);
+ // not all results look like resources, so can't use is_resource()
+ if($result)
+ return $this->_fetchRow($result);
+ return null;
+ }
+
+
+ public function beginTransaction() {
+ // need to return true or false
+ $this->_query('begin');
+ }
+
+ public function commitTransaction() {
+ $this->_query('commit');
+ }
+
+ public function rollbackTransaction() {
+ $this->_query('rollback');
+ }
+
+
+ // Fill in question mark placeholders. No more named placeholders.
+ protected function makeQuery($sql, $parameters) {
+ // bypass extra logic if we have no parameters
+ if(sizeof($parameters) == 0)
+ return $sql;
+
+ $parts = explode('?', $sql);
+
+ // sizeof($sql) == sizeof($paramters) - 1
+ //var_dump($parts);var_dump($parameters);exit;
+
+ $query = '';
+ while(sizeof($parameters)) {
+ $query .= array_shift($parts);
+ //$query .= $this->escapeString( array_shift($parameters) );
+ $query .= "'" . $this->escapeString( array_shift($parameters) ) . "'";
+ }
+ $query .= array_shift($parts);
+ //var_dump($query);exit;
+ return $query;
+ }
+
+ /*
+ * This should be protected and overloadable by driver classes
+ */
+ private function prepareData($data) {
+ $values = array();
+
+ foreach($data as $key=>$value) {
+ // it's not right to worry about invalid fields in this method because we may be operating on fields
+ // that are aliases, or part of other tables through joins
+ //if(!in_array($key, $columns)) // skip invalid fields
+ // continue;
+ if($this->addQuotes)
+ $values[$key] = "'" . $this->_escapeString($value) . "'";
+ else
+ $values[$key] = $this->_escapeString($value);
+ }
+ return $values;
+ }
+
+ public function quoteField($field) {
+ return '`' . $field . '`';
+ }
+
+ public function quoteFields($fields) {
+ return array_map(array($this, 'quoteField'), $fields);
+ }
+}
+
+/*
+class dbFacile_pdo_postgresql extends dbFacile_pdo {
+ public function __construct($handle = null) {
+ parent::__construct($handle);
+ $this->schemaNameField = 'column_name';
+ $this->schemaTypeField = 'data_type';
+ }
+ protected function _open($database) {
+ $this->connection = new PDO("pgsql:host=$host;dbname=$database", $user, $pass");
+ }
+ protected function _schema($table) {
+ // getAttribute(PDO::DRIVER_NAME) to determine the sql to call
+ $this->execute('select column_name,split_part(data_type, \' \', 1) as data_type from information_schema.columns where table_name = \'' . $table . '\' order by ordinal_position');
+ return $this->_fetchAll();
+ }
+}
+*/
+/*
+class dbFacile_pdo_sqlite extends dbFacile_pdo {
+ public function __construct($handle = null) {
+ parent::__construct($handle);
+ $this->schemaNameField = 'name';
+ $this->schemaTypeField = 'type';
+ $this->schemaPrimaryKeyField = 'pk';
+ echo 'here';
+ }
+ protected function _open($database) {
+ $this->connection = new PDO('sqlite:' . $database);
+ }
+ protected function _schema($table) {
+ // getAttribute(PDO::DRIVER_NAME) to determine the sql to call
+ $this->execute('pragma table_info(' . $table. ')');
+ return $this->_fetchAll();
+ }
+}
+class dbFacile_pdo_sqlite2 extends dbFacile_pdo {
+ public function __construct($handle = null) {
+ parent::__construct($handle);
+ $this->schemaNameField = 'name';
+ $this->schemaTypeField = 'type';
+ $this->schemaPrimaryKeyField = 'pk';
+ }
+ protected function _open($database) {
+ $this->connection = new PDO("sqlite2:$database");
+ }
+ protected function _schema($table) {
+ // getAttribute(PDO::DRIVER_NAME) to determine the sql to call
+ $this->execute('pragma table_info(' . $table. ')');
+ return $this->_fetchAll();
+ }
+}
+
+abstract class dbFacile_pdo extends dbFacile {
+ protected $statement;
+ function __construct($handle = null) {
+ parent::__construct();
+ $this->addQuotes = false;
+ $this->schemaNameField = 'name';
+ $this->schemaTypeField = 'type';
+ if($handle != null)
+ $this->connection = $handle;
+ }
+ protected function _affectedRows() {
+ return $this->result->rowCount();
+ }
+ protected function _open($type, $database, $user, $pass, $host) {
+ $this->connection = new PDO("$type:host=$host;dbname=$database", $user, $pass);
+ }
+ protected function _query($sql, $buffered = true) {
+ return $this->connection->query($sql);
+ }
+ protected function _escapeString($string) {
+ return $this->connection->quote($string);
+ }
+ protected function _error() {
+ $e = $this->connection->errorInfo();
+ return $e[2];
+ }
+ protected function _fields($table) {
+ return array();
+ }
+ protected function _foreignKeys($table) {
+ return array();
+ }
+ protected function _numberRows() {
+ return true;
+ return $this->result->rowCount();
+ }
+ protected function _fetch() {
+ return $this->result;
+ }
+ protected function _fetchAll() {
+ return $this->result->fetchAll(PDO::FETCH_ASSOC);
+ }
+ protected function _fetchRow() {
+ return $this->result->fetch(PDO::FETCH_ASSOC);
+ }
+ protected function _lastID() {
+ return $this->connection->lastInsertId();
+ }
+ protected function _quoteField($field) {
+ return '`' . $field . '`';
+ }
+ protected function _rewind($result) {
+ reset($result);
+ }
+ protected function _schema($table) {
+ // getAttribute(PDO::DRIVER_NAME) to determine the sql to call
+ $this->execute('pragma table_info(' . $table. ')');
+ return $this->_fetchAll();
+ }
+ protected function _tables() {
+ return array();
+ }
+ public function beginTransaction() {
+ $this->connection->beginTransaction();
+ }
+ public function commitTransaction() {
+ $this->connection->commit();
+ }
+ public function rollbackTransaction() {
+ $this->connection->rollBack();
+ }
+ public function close() {
+ $this->connection = null;
+ }
+} // pdo
+
+*/
View
98 splitup/dbFacile_mssql.php
@@ -0,0 +1,98 @@
+<?php
+
+
+class dbFacile_mssql extends dbFacile {
+ public function beginTransaction() {
+ //mssql_query('begin', $this->connection);
+ }
+
+ public function commitTransaction() {
+ //mssql_query('commit', $this->connection);
+ }
+
+ public function close() {
+ mssql_close($this->connection);
+ }
+
+ public function rollbackTransaction() {
+ //mssql_query('rollback', $this->connection);
+ }
+
+ protected function _affectedRows() {
+ return mssql_rows_affected($this->connection);
+ }
+
+ protected function _error() {
+ return mssql_get_last_message();
+ }
+
+ protected function _escapeString($string) {
+ $s = stripslashes($string);
+ $s = str_replace( array("'", "\0"), array("''", '[NULL]'), $s);
+ return $s;
+ }
+
+ protected function _fetch() {
+ // use mysql_data_seek to get to row index
+ return $this->_fetchAll();
+ }
+
+ protected function _fetchAll() {
+ $data = array();
+ while($row = mssql_fetch_assoc($this->result)) {
+ $data[] = $row;
+ }
+ //mssql_free_result($this->result);
+ // rewind?
+ return $data;
+ }
+
+ protected function _fetchRow() {
+ return mssql_fetch_assoc($this->result);
+ }
+
+ protected function _fields($table) {
+ $this->execute('select COLUMN_NAME,DATA_TYPE from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=?', array($table), false);
+ return $this->_fetchAll();
+ }
+
+ protected function _foreignKeys($table) {
+ }
+
+ protected function _lastID() {
+ return $this->fetchCell('select scope_identity()');
+ }
+
+ protected function _open($database, $user, $password, $host) {
+ $this->connection = mssql_connect($host, $user, $password);
+ if($this->connection)
+ mssql_select_db($database, $this->connection);
+ //$this->buildSchema();
+ return $this->connection;
+ }
+
+ protected function _numberRows() {
+ return mssql_num_rows($this->result);
+ }
+
+ protected function _primaryKey($table) {
+ }
+
+ protected function _query($sql, $buffered = true) {
+ return mssql_query($sql, $this->connection);
+ }
+
+ protected function _quoteField($field) {
+ return $field;
+ }
+ protected function _quoteFields($fields) {
+ return $fields;
+ }
+
+ protected function _rewind($result) {
+ }
+
+ protected function _tables() {
+ }
+} // mssql
+
View
76 splitup/dbFacile_mysql.php
@@ -0,0 +1,76 @@
+<?php
+require_once('dbFacile.php');
+
+class dbFacile_mysql extends dbFacile {
+ public function affectedRows() {
+ return mysql_affected_rows($this->connection);
+ }
+
+ public function close() {
+ mysql_close($this->connection);
+ }
+
+ public function error() {
+ return mysql_error($this->connection);
+ }
+
+ public function escapeString($string) {
+ return mysql_real_escape_string($string);
+ }
+
+ public function lastID() {
+ return mysql_insert_id($this->connection);
+ }
+
+ public function numberRows($result) {
+ return mysql_num_rows($result);
+ }
+
+ public function open($database, $user, $password, $host, $charset) {
+ $this->database = $database;
+ // force opening a new link because we might be selecting a different database
+ $this->connection = mysql_connect($host, $user, $password, true);
+ if($this->connection) {
+ mysql_select_db($database, $this->connection);
+ if ($charset)
+ mysql_set_charset($charset, $this->connection);
+ }
+ //$this->buildSchema();
+ return $this->connection;
+ }
+
+ public function rewind($result) {
+ }
+
+
+ // Protected internals
+ protected function _fetch($result) {
+ // use mysql_data_seek to get to row index
+ return $this->_fetchAll($result);
+ }
+
+ protected function _fetchAll($result) {
+ $data = array();
+ while($row = mysql_fetch_assoc($result)) {
+ $data[] = $row;
+ }
+ //mysql_free_result($this->result);
+ return $data;
+ }
+
+ protected function _fetchRow($result) {
+ return mysql_fetch_assoc($result);
+ }
+
+ // user, password, database, host
+
+ protected function _query($sql, $buffered = true) {
+ if ($buffered)
+ return mysql_query($sql, $this->connection);
+ else {
+ return mysql_unbuffered_query($sql, $this->connection);
+ }
+ }
+
+} // mysql
+
View
84 splitup/dbFacile_mysqli.php
@@ -0,0 +1,84 @@
+<?php
+require_once('dbFacile.php');
+
+class dbFacile_mysqli extends dbFacile {
+ public function affectedRows() {
+ return mysql_affected_rows($this->connection);
+ }
+
+ public function beginTransaction() {
+ mysqli_autocommit($this->connection, false);
+ }
+
+ public function close() {
+ mysqli_close($this->connection);
+ }
+
+ public function commitTransaction() {
+ mysqli_commit($this->connection);
+ mysqli_autocommit($this->connection, true);
+ }
+
+ public function escapeString($string) {
+ return mysql_real_escape_string($string);
+ }
+
+ public function lastError() {
+ return mysqli_error($this->connection);
+ }
+
+ public function lastID() {
+ return mysql_insert_id($this->connection);
+ }
+ public function escapeString($string) {
+ return mysqli_real_escape_string($string);
+ }
+
+ public function lastID() {
+ return mysqli_insert_id($this->connection);
+ }
+
+ public function numberRows($result) {
+ if(mysqli_affected_rows($this->connection)) { // for insert, update, delete
+ $this->numberRecords = mysqli_affected_rows($this->connection);
+ } elseif(!is_bool($result)) { // for selects
+ $this->numberRecords = mysqli_num_rows($result);
+ } else { // will be boolean for create, drop, and other
+ $this->numberRecords = 0;
+ }
+ }
+
+ public function open($database, $user, $password, $host='localhost', $charset='utf-8') {
+ // force opening a new link because we might be selecting a different database
+ $this->connection = mysqli_connect($host, $user, $password, $database);
+ return $this->connection;
+ }
+
+ public function rewind($result) {
+ }
+
+ public function rollbackTransaction() {
+ mysqli_rollback($this->connection);
+ mysqli_autocommit($this->connection, true);
+ }
+
+
+ protected function _fetch($result) {
+ return $this->_fetchAll($result);
+ }
+ protected function _fetchAll($result) {
+ $data = array();
+ for($i = 0; $i < $this->numberRecords($result); $i++) {
+ $data[] = mysqli_fetch_assoc($result);
+ }
+ mysqli_free_result($result);
+ return $data;
+ }
+ protected function _fetchRow($result) {
+ return mysqli_fetch_assoc($result);
+ }
+ protected function _query($query) {
+ return mysqli_query($this->connection, $query);
+ }
+} // mysqli
+
View
77 splitup/dbFacile_postgresql.php
@@ -0,0 +1,77 @@
+<?php
+require_once('dbFacile.php');
+
+class dbFacile_postgresql extends dbFacile {
+ public function beginTransaction() {
+ $this->_query('begin');
+ }
+
+ public function close() {
+ pg_close($this->connection);
+ }
+
+ public function commitTransaction() {
+ $this->_query('commit');
+ }
+
+ // user, password, database, host
+ protected function open($database, $user, $password, $host) {
+ //die("host=$host dbname=$database user=$user");
+ $this->connection = pg_connect("host=$host dbname=$database port=5432 user=$user password=$password");
+ return $this->connection;
+ }
+
+ public function rollbackTransaction() {
+ $this->_query('rollback');
+ }
+
+ public function affectedRows($result) {
+ return pg_affected_rows($result);
+ }
+
+ public function lastError() {
+ return pg_last_error($this->connection);
+ }
+
+ public function escapeString($string) {
+ return pg_escape_string($string);
+ }
+
+
+ protected function _fetch($result) {
+ return $this->_fetchAll($result);
+ }
+
+ protected function _fetchAll($result) {
+ return $data;
+ $data = array();
+ while($row = pg_fetch_assoc($result)) {
+ $data[] = $row;
+ }
+ pg_free_result($result);
+ // rewind?
+ return $data;
+ }
+
+ protected function _fetchRow($result) {
+ return pg_fetch_assoc($result);
+ }
+
+ protected function _lastID($table) {
+ $sequence = $this->fetchCell("SELECT relname FROM pg_class WHERE relkind = 'S' AND relname LIKE '" . $table . "_%'");
+ if(strlen($sequence))
+ return $this->fetchCell('select last_value from ' . $sequence);
+ return 0;
+ }
+
+ protected function _numberRows($result) {
+ return pg_num_rows($result);
+ }
+
+ protected function _query($sql) {
+ // postgres has it's own param filling?
+ $sql = $this->makeQuery($sql, $parameters);
+ return pg_query($this->connection, $sql);
+ }
+} // postgresql
+
View
124 splitup/dbFacile_sqlite2.php
@@ -0,0 +1,124 @@
+<?php
+require_once('dbFacile.php');
+
+/*
+ * To create a new driver, implement the following:
+ * protected _open(...)
+ * protected _query($sql, $buffered)
+ * protected _escapeString
+ * protected _error
+ * protected _affectedRows
+ * protected _numberRows
+ * protected _fetch
+ * protected _fetchAll
+ * protected _fetchRow
+ * protected lastID
+ * protected _schema
+ * public beginTransaction
+ * public commitTransaction
+ * public rollbackTransaction
+ * public close
+ * */
+
+class dbFacile_sqlite2 extends dbFacile {
+ public function affectedRows() {
+ return sqlite_changes($this->connection);
+ }
+
+ public function beginTransaction() {
+ $thiss->_query('begin transaction');
+ }
+
+ public function close() {
+ sqlite_close($this->connection);
+ }
+
+ public function commitTransaction() {
+ $this->_query('commit transaction');
+ }
+
+ public function error() {
+ return sqlite_error_string(sqlite_last_error($this->connection));
+ }
+
+ public function escapeString($string) {
+ return sqlite_escape_string($string);
+ }
+
+ public function lastID() {
+ return sqlite_last_insert_rowid($this->connection);
+ }
+
+ public function numberRows($result) {
+ return sqlite_num_rows($result);
+ }
+
+ public function open($database) {
+ $this->connection = sqlite_open($database);
+ //$this->buildSchema();
+ return $this->connection;
+ }
+
+ public function quoteField($field) {
+ return '"' . $field . '"';
+ }
+
+ public function rewind($result) {
+ sqlite_rewind($result);
+ }
+
+ public function rollbackTransaction() {
+ $this->_query('rollback transaction');
+ }
+
+
+
+ protected function _fetch($result) {
+ return new dbFacile_sqlite_result($result);
+ }
+
+ protected function _fetchAll($result) {
+ $rows = sqlite_fetch_all($result, SQLITE_ASSOC);
+ // free result?
+ // rewind?
+ return $rows;
+ }
+
+ // when passed result
+ // returns next row
+ protected function _fetchRow($result) {
+ return sqlite_fetch_array($result, SQLITE_ASSOC);
+ }
+
+
+ protected function _query($sql) {
+ return sqlite_query($this->connection, $sql);
+ }
+
+} // sqlite
+
+
+
+/*
+class dbFacile_sqlite_result implements Iterator {
+ private $result;
+ public function __construct($r) {
+ $this->result = $r;
+ }
+ public function rewind() {
+ sqlite_rewind($this->result);
+ }
+ public function current() {
+ $a = sqlite_current($this->result, SQLITE_ASSOC);
+ return $a;
+ }
+ public function key() {
+ $a = sqlite_key($this->result);
+ return $a;
+ // getAttribute(PDO::DRIVER_NAME) to determine the sql to call
+ $this->execute('describe ' . $table);
+ return $this->_fetchAll();
+ }
+}
+
+*/
View
82 splitup/dbFacile_sqlite3.php
@@ -0,0 +1,82 @@
+<?php
+require_once('dbFacile.php');
+
+class dbFacile_sqlite3 extends dbFacile {
+ public function affectedRows() {
+ return $this->connection->changes();
+ }
+
+ public function beginTransaction() {
+ $this->_query('begin transaction');
+ }
+
+ public function close() {
+ $this->connection->close();
+ }
+
+ public function commitTransaction() {
+ $this->_query('commit transaction');
+ }
+
+ public function error() {
+ return $this->connection->lastErrorMsg();
+ }
+
+ public function escapeString($string) {
+ return $this->connection->escapeString($string);
+ }
+
+ public function lastID() {
+ return $this->connection->lastInsertRowID();
+ }
+
+ public function numberRows($result) {
+ $rows = $this->_fetchAll($result);
+ return sizeof($rows);
+ }
+
+ public function open($database) {
+ $this->connection = new SQLite3($database);
+ return $this->connection;
+ }
+
+ public function quoteField($field) {
+ return '"' . $field . '"';
+ }
+
+ public function rewind($result) {
+ $result->reset();
+ }
+
+ public function rollbackTransaction() {
+ $this->_query('rollback transaction');
+ }
+
+
+
+ protected function _fetch($result) {
+ return new dbFacile_sqlite3_result($result);
+ }
+
+ protected function _fetchAll($result) {
+ // loop
+ $rows = array();
+ while($row = $this->_fetchRow($result)) {
+ $rows[] = $row;
+ }
+ return $rows;
+ }
+
+ // when passed result
+ // returns next row
+ protected function _fetchRow($result) {
+ return $result->fetchArray(SQLITE3_ASSOC);
+ }
+
+
+ protected function _query($sql) {
+ return $this->connection->query($sql);
+ }
+
+} // sqlite3
+
View
117 tests/Main.php
@@ -0,0 +1,117 @@
+<?php
+include('../splitup/dbFacile_sqlite3.php');
+
+class Main extends PHPUnit_Framework_TestCase {
+ protected static $db;
+
+ protected $rows1 = array(
+ array('b' => 1, 'c' => 'aaa'),
+ array('b' => 2, 'c' => 'bbb'),
+ array('b' => 3, 'c' => 'ccc')
+ );
+ protected $rows2 = array(
+ array('id' => '1', 'name' => 'Hello')
+ );
+
+ public static function setUpBeforeClass() {
+ unlink('sqlite3.db');
+ $db = new dbFacile_sqlite3();
+ $db->open('sqlite3.db');
+
+ /*
+ $db->execute('create table test (b integer auto_increment, c text, primary key(b))');
+ $db->execute('create table test2 (id integer, name text, primary key (id,name))');
+ */
+
+ $db->execute('create table test (b integer primary key autoincrement, c text)');
+ $db->execute('create table test2 (id integer, name text, primary key (id,name))');
+ Main::$db = $db;
+ }
+
+ public function testInsertReportsKey() {
+ $db = Main::$db;
+ $row = $this->rows1[0];
+ unset($row['b']);
+ $a = $db->insert($row, 'test');
+ $this->assertEquals($a, 1);
+
+ $row = $this->rows1[1];
+ unset($row['b']);
+ $a = $db->insert($row, 'test');
+ $this->assertEquals($a, 2);
+
+ $row = $this->rows1[2];
+ unset($row['b']);
+ $a = $db->insert($row, 'test');
+ $this->assertEquals($a, 3);
+
+ // begin support for multi-field primary keys
+ $b = $db->insert( $this->rows2[0], 'test2');
+ $this->assertEquals($b, 1);
+ }
+
+ public function testFetchAll() {
+ $db = Main::$db;
+ $rows = $db->fetchAll('select * from test order by b');
+ foreach($rows as $i => $row) {
+ $this->assertEquals( $this->rows1[ $i ], $row);
+ }
+ }
+
+ public function testPlaceholders() {
+ $db = Main::$db;
+ $rows = $db->fetchAll('select * from test where b > ? order by b', array('1'));
+ foreach($rows as $i => $row) {
+ $this->assertEquals( $this->rows1[ $i+1 ], $row);
+ }
+ }
+
+ public function testFetchRow() {
+ $db = Main::$db;
+ $row = $db->fetchRow('select * from test where b = ?', array('2'));
+ $this->assertEquals( $this->rows1[ 1 ], $row);
+ }
+
+ public function testFetchCell() {
+ $db = Main::$db;
+ $row = $db->fetchCell('select b,c from test where b = ?', array('3'));
+ $this->assertEquals($row, 3);
+ }
+
+ public function testFetchColumn() {
+ $db = Main::$db;
+ $row = $db->fetchColumn('select b from test where b > 1 order by b');
+ $this->assertEquals($row, array(2,3));
+ }
+
+ public function testFetchKeyValue() {
+ $db = Main::$db;
+ $row = $db->fetchKeyValue('select b,c from test where b > 1 order by b');
+ $data = array(
+ 2 => 'bbb',
+ 3 => 'ccc'
+ );
+ $this->assertEquals($row, $data);
+ }
+
+ public function testUpdate() {
+ $db = Main::$db;
+ $data = array('c' => date('Y-m-d H:i:s'));
+ $db->update($data, 'test', 'b=?', array(3));
+
+ $row = $db->fetchRow('select b,c from test where b=3');
+ $data['b'] = 3;
+ $this->assertEquals($data, $row);
+ }
+
+ public function testInsert() {
+ $db = Main::$db;
+ $data = array('c' => 'new');
+ $db->insert($data, 'test');
+
+ $row = $db->fetchRow('select b,c from test where b=4');
+ $data['b'] = 4;
+ $this->assertEquals($data, $row);
+ }
+}
+

No commit comments for this range

Something went wrong with that request. Please try again.