Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Commenting and whitespace.

  • Loading branch information...
commit a8ebaa2f68fa6e4f93e5b3537d352ee1f6082e33 1 parent 6caec75
@tenorviol authored
View
10 src/AbstractRecord.php
@@ -1,4 +1,14 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
abstract class AbstractRecord implements IteratorAggregate {
View
124 src/Grubby/DB/Database.php
@@ -1,64 +1,74 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
* Grubby database using the DB abstraction layer
*/
class Grubby_DB_Database extends Grubby_Database {
- private $dsn;
- private $options;
- private $connection = null;
-
- /**
- * Creates a new Grubby database.
- */
- public function __construct($dsn, $options = null) {
- $this->dsn = $dsn;
- $this->options = $options;
- }
-
- /**
- * Returns a database connection.
- */
- public function getConnection() {
- if ($this->connection === null) {
- $connect = DB::connect($this->dsn, $this->options);
- if (PEAR::isError($connect)) {
- throw new Grubby_Exception('No database connection available: '.$connect->getMessage());
- }
- $this->connection = $connect;
- }
- return $this->connection;
- }
-
- /**
- * Executes a SQL query against the database.
- * Use for UPDATE, INSERT, DELETE.
- * @return Grubby_DB_Result
- */
- public function executeImpl($sql) {
- $connection = $this->getConnection();
- $result = $connection->query($sql);
- return new Grubby_DB_Result($result, $connection);
- }
-
- /**
- * Runs a SQL query against the database.
- * Use for SELECT.
- * @return false or a new DB_Result object.
- */
- public function queryImpl($sql) {
- $connection = $this->getConnection();
- $result = $connection->query($sql);
- return new Grubby_DB_Recordset($this, $result);
- }
-
- public function formatString($s) {
- if (is_null($s)) {
- return 'NULL';
- } elseif ($s) {
- return $this->getConnection()->quoteSmart(strval($s));
- } else {
- return "''";
- }
- }
+ private $dsn;
+ private $options;
+ private $connection = null;
+
+ /**
+ * Creates a new Grubby database.
+ */
+ public function __construct($dsn, $options = null) {
+ $this->dsn = $dsn;
+ $this->options = $options;
+ }
+
+ /**
+ * Returns a database connection.
+ */
+ public function getConnection() {
+ if ($this->connection === null) {
+ $connect = DB::connect($this->dsn, $this->options);
+ if (PEAR::isError($connect)) {
+ throw new Grubby_Exception('No database connection available: '.$connect->getMessage());
+ }
+ $this->connection = $connect;
+ }
+ return $this->connection;
+ }
+
+ /**
+ * Executes a SQL query against the database.
+ * Use for UPDATE, INSERT, DELETE.
+ * @return Grubby_DB_Result
+ */
+ public function executeImpl($sql) {
+ $connection = $this->getConnection();
+ $result = $connection->query($sql);
+ return new Grubby_DB_Result($result, $connection);
+ }
+
+ /**
+ * Runs a SQL query against the database.
+ * Use for SELECT.
+ * @return false or a new DB_Result object.
+ */
+ public function queryImpl($sql) {
+ $connection = $this->getConnection();
+ $result = $connection->query($sql);
+ return new Grubby_DB_Recordset($this, $result);
+ }
+
+ public function formatString($s) {
+ if (is_null($s)) {
+ return 'NULL';
+ } elseif ($s) {
+ return $this->getConnection()->quoteSmart(strval($s));
+ } else {
+ return "''";
+ }
+ }
}
View
140 src/Grubby/DB/Recordset.php
@@ -1,72 +1,82 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
*
*/
class Grubby_DB_Recordset extends Grubby_Recordset {
-
- /**
- * Creates a new recordset resource handler.
- * @param result mixed result of a call to DB::query
- */
- public function __construct($database, $result) {
- $this->database = $database;
- if ($result instanceof DB_Error) {
- $this->error = $result;
- } else {
- $this->recordset = $result;
- }
- }
-
- /**
- * Returns the next row in the resultset or null if eof has been reached.
- * @return mixed
- */
- public function fetch() {
- $row = $this->recordset->fetchRow(DB_FETCHMODE_ASSOC);
- if ($row) {
- return $this->unmarshal($row);
- } else {
- return null;
- }
- }
-
- /**
- * Returns all remaining rows in the result set in an ordered array.
- * @return array
- */
- public function fetchAll() {
- $all = array();
- while ($row = $this->recordset->fetchRow(DB_FETCHMODE_ASSOC)) {
- $all[] = $row;
- }
- foreach ($all as $key => $row) {
- $all[$key] = $this->unmarshal($row);
- }
- return $all;
- }
-
- /**
- * Returns all members of a particular column of the result set as an ordered array.
- * @return array
- */
- public function fetchColumn($column) {
- return $this->recordset->fetchCol($column);
- }
-
- /**
- * True if the query resulted in an error.
- * @return boolean
- */
- public function error() {
- return isset($this->error);
- }
-
- /**
- * Available when error() is true.
- * @return string
- */
- public function errorMessage() {
- return $this->error->getMessage();
- }
+
+ /**
+ * Creates a new recordset resource handler.
+ * @param result mixed result of a call to DB::query
+ */
+ public function __construct($database, $result) {
+ $this->database = $database;
+ if ($result instanceof DB_Error) {
+ $this->error = $result;
+ } else {
+ $this->recordset = $result;
+ }
+ }
+
+ /**
+ * Returns the next row in the resultset or null if eof has been reached.
+ * @return mixed
+ */
+ public function fetch() {
+ $row = $this->recordset->fetchRow(DB_FETCHMODE_ASSOC);
+ if ($row) {
+ return $this->unmarshal($row);
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Returns all remaining rows in the result set in an ordered array.
+ * @return array
+ */
+ public function fetchAll() {
+ $all = array();
+ while ($row = $this->recordset->fetchRow(DB_FETCHMODE_ASSOC)) {
+ $all[] = $row;
+ }
+ foreach ($all as $key => $row) {
+ $all[$key] = $this->unmarshal($row);
+ }
+ return $all;
+ }
+
+ /**
+ * Returns all members of a particular column of the result set as an ordered array.
+ * @return array
+ */
+ public function fetchColumn($column) {
+ return $this->recordset->fetchCol($column);
+ }
+
+ /**
+ * True if the query resulted in an error.
+ * @return boolean
+ */
+ public function error() {
+ return isset($this->error);
+ }
+
+ /**
+ * Available when error() is true.
+ * @return string
+ */
+ public function errorMessage() {
+ return $this->error->getMessage();
+ }
}
View
66 src/Grubby/DB/Result.php
@@ -1,35 +1,45 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
* Result object returned by a GrubbyDBDatabase execute function call.
*/
class Grubby_DB_Result extends Grubby_Result {
-
- /**
- * Creates an DB execute result object.
- * @param result mixed result of a call to DB::exec
- */
- public function __construct($result, $connection) {
- if ($result instanceof DB_Error) {
- $this->error = $result;
- } else {
- $this->affected_rows = $connection->affectedRows();
- }
- }
-
- /**
- * True if the query resulted in an error.
- * @return boolean
- */
- public function error() {
- return isset($this->error);
- }
-
- /**
- * Available when error() is true.
- * @return string
- */
- public function errorMessage() {
- return $this->error->getMessage();
- }
+
+ /**
+ * Creates an DB execute result object.
+ * @param result mixed result of a call to DB::exec
+ */
+ public function __construct($result, $connection) {
+ if ($result instanceof DB_Error) {
+ $this->error = $result;
+ } else {
+ $this->affected_rows = $connection->affectedRows();
+ }
+ }
+
+ /**
+ * True if the query resulted in an error.
+ * @return boolean
+ */
+ public function error() {
+ return isset($this->error);
+ }
+
+ /**
+ * Available when error() is true.
+ * @return string
+ */
+ public function errorMessage() {
+ return $this->error->getMessage();
+ }
}
View
342 src/Grubby/Database.php
@@ -1,4 +1,14 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
define('GRUBBY_INT', 1);
define('GRUBBY_STRING', 2);
@@ -14,7 +24,7 @@
* A relational database.
*/
abstract class Grubby_Database {
-
+
public $time = 0;
public $logger = null;
@@ -25,169 +35,169 @@ public function log($message, $level) {
}
}
- /**
- * Run a SQL statement that manipulates data against this database.
- * I.e. CREATE, UPDATE, DELETE
- * @retun GrubbyResult
- */
- public function execute($sql) {
- $this->log('Executing: '.$sql, LOG_DEBUG);
- $start = microtime(true);
-
- $result = $this->executeImpl($sql);
-
- $time = microtime(true) - $start;
- $this->time += $time;
-
- if ($result->error()) {
- $this->log('Error: '.$result->errorMessage(), LOG_ERR);
- }
- $this->log('Time: '.number_format($time, 4)." secs", LOG_DEBUG);
-
- if ($result->error()) {
- throw new Grubby_Exception($result->errorMessage());
- }
-
- return $result;
- }
-
- /**
- * Run a SQL query against the database.
- * I.e. SELECT
- * @return GrubbyRecordset
- */
- public function query($sql) {
- $this->log('Querying: '.$sql, LOG_DEBUG);
-
- $start = microtime(true);
-
- $result = $this->queryImpl($sql);
-
- $time = microtime(true) - $start;
- $this->time += $time;
-
- if ($result->error()) {
- $this->log('Error: '.$result->errorMessage(), LOG_ERR);
- }
- $this->log('Time: '.number_format($time, 4)." secs", LOG_DEBUG);
-
- if ($result->error()) {
- throw new Grubby_Exception($result->errorMessage());
- }
-
- return $result;
- }
-
- /**
- * @return mixed
- */
- public function lastInsertID() {
- $result = $this->query('SELECT LAST_INSERT_ID() AS last_id')->fetch();
- return $result['last_id'];
- }
-
- /**
- *
- * @param $value mixed
- * @param $type string
- * @return string
- */
- public function format($value, $type = GRUBBY_STRING) {
- switch ($type) {
- case GRUBBY_INT:
- return $this->formatInt($value);
- break;
- case GRUBBY_STRING:
- case null:
- return $this->formatString($value);
- break;
- case GRUBBY_DATETIME:
- throw new Grubby_Exception('Datetime formatting unimplemented.');
- break;
- default:
- throw new Grubby_Exception('Unknown format type: '.$type);
- }
- }
-
- /**
- * Escapes and delimits a string for insertion into sql intended for this database.
- * If $string is null this function returns the sql empty string "''".
- * If $string contains another datatype it will be converted to a string by way of the strval function.
- * @param $string mixed
- * @return string
- */
- public abstract function formatString($string);
-
- /**
- * Prepares an integer for insertion into a sql statement,
- * making sure it is an string encoded integer.
- * @param $int mixed
- * @return string
- */
- public function formatInt($int) {
- if (is_int($int)) {
- return (string)$int;
- } elseif (is_float($int)) {
- return (string)round($int);
- } else {
- $int = strval($int);
- if (is_numeric($int)) {
- return $int;
- } else {
- return 0;
- }
- }
- }
-
- /**
- * Replaces wildcards (?) with formatted wildcard values.
- * Coded to MySQL's version of SQL.
- * TODO: Generalize for all database (accept '''' from SQL Server)
- * TODO: Allow ? in column identifiers delimited by ` or cross-database equivalents
- */
- public function replaceWildcards($sql, $wildcards, $types = array()) {
- if (is_scalar($wildcards)) {
- $wildcards = array($wildcards);
- }
- if (is_scalar($types)) {
- $types = array($types);
- }
- $return = '';
- $len = strlen($sql);
- $wc = 0;
- $marker = 0;
- $i = 0;
- while ($i < $len) {
- if ($sql[$i] == '?') {
- if (!isset($wildcards[$wc])) {
- throw new Grubby_Exception('Too few wildcards for filter expression.');
- }
- $return .= substr($sql, $marker, $i-$marker);
- $return .= $this->format($wildcards[$wc], @$types[$wc]);
- $marker = $i+1;
- $wc++;
- } elseif ($sql[$i] == '\'') { // string starting, skip to end
- $end = false;
- while (!$end) {
- $i++;
- if ($i == $len) {
- throw new Grubby_Exception('Unterminated string in filter expression, "'.$sql.'"');
- } elseif ($sql[$i] == '\\') {
- $i++;
- } elseif ($sql[$i] == '\'') {
- $end = true;
- }
- }
- }
- $i++;
- }
- $wildcard_count = count($wildcards);
- if ($wc < $wildcard_count && ($wildcard_count > 1 || $wildcards[0])) {
- throw new Grubby_Exception('Too many wildcards for filter expression.');
- }
- if ($marker < $len) {
- $return .= substr($sql, $marker);
- }
- return $return;
- }
+ /**
+ * Run a SQL statement that manipulates data against this database.
+ * I.e. CREATE, UPDATE, DELETE
+ * @retun GrubbyResult
+ */
+ public function execute($sql) {
+ $this->log('Executing: '.$sql, LOG_DEBUG);
+ $start = microtime(true);
+
+ $result = $this->executeImpl($sql);
+
+ $time = microtime(true) - $start;
+ $this->time += $time;
+
+ if ($result->error()) {
+ $this->log('Error: '.$result->errorMessage(), LOG_ERR);
+ }
+ $this->log('Time: '.number_format($time, 4)." secs", LOG_DEBUG);
+
+ if ($result->error()) {
+ throw new Grubby_Exception($result->errorMessage());
+ }
+
+ return $result;
+ }
+
+ /**
+ * Run a SQL query against the database.
+ * I.e. SELECT
+ * @return GrubbyRecordset
+ */
+ public function query($sql) {
+ $this->log('Querying: '.$sql, LOG_DEBUG);
+
+ $start = microtime(true);
+
+ $result = $this->queryImpl($sql);
+
+ $time = microtime(true) - $start;
+ $this->time += $time;
+
+ if ($result->error()) {
+ $this->log('Error: '.$result->errorMessage(), LOG_ERR);
+ }
+ $this->log('Time: '.number_format($time, 4)." secs", LOG_DEBUG);
+
+ if ($result->error()) {
+ throw new Grubby_Exception($result->errorMessage());
+ }
+
+ return $result;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function lastInsertID() {
+ $result = $this->query('SELECT LAST_INSERT_ID() AS last_id')->fetch();
+ return $result['last_id'];
+ }
+
+ /**
+ *
+ * @param $value mixed
+ * @param $type string
+ * @return string
+ */
+ public function format($value, $type = GRUBBY_STRING) {
+ switch ($type) {
+ case GRUBBY_INT:
+ return $this->formatInt($value);
+ break;
+ case GRUBBY_STRING:
+ case null:
+ return $this->formatString($value);
+ break;
+ case GRUBBY_DATETIME:
+ throw new Grubby_Exception('Datetime formatting unimplemented.');
+ break;
+ default:
+ throw new Grubby_Exception('Unknown format type: '.$type);
+ }
+ }
+
+ /**
+ * Escapes and delimits a string for insertion into sql intended for this database.
+ * If $string is null this function returns the sql empty string "''".
+ * If $string contains another datatype it will be converted to a string by way of the strval function.
+ * @param $string mixed
+ * @return string
+ */
+ public abstract function formatString($string);
+
+ /**
+ * Prepares an integer for insertion into a sql statement,
+ * making sure it is an string encoded integer.
+ * @param $int mixed
+ * @return string
+ */
+ public function formatInt($int) {
+ if (is_int($int)) {
+ return (string)$int;
+ } elseif (is_float($int)) {
+ return (string)round($int);
+ } else {
+ $int = strval($int);
+ if (is_numeric($int)) {
+ return $int;
+ } else {
+ return 0;
+ }
+ }
+ }
+
+ /**
+ * Replaces wildcards (?) with formatted wildcard values.
+ * Coded to MySQL's version of SQL.
+ * TODO: Generalize for all database (accept '''' from SQL Server)
+ * TODO: Allow ? in column identifiers delimited by ` or cross-database equivalents
+ */
+ public function replaceWildcards($sql, $wildcards, $types = array()) {
+ if (is_scalar($wildcards)) {
+ $wildcards = array($wildcards);
+ }
+ if (is_scalar($types)) {
+ $types = array($types);
+ }
+ $return = '';
+ $len = strlen($sql);
+ $wc = 0;
+ $marker = 0;
+ $i = 0;
+ while ($i < $len) {
+ if ($sql[$i] == '?') {
+ if (!isset($wildcards[$wc])) {
+ throw new Grubby_Exception('Too few wildcards for filter expression.');
+ }
+ $return .= substr($sql, $marker, $i-$marker);
+ $return .= $this->format($wildcards[$wc], @$types[$wc]);
+ $marker = $i+1;
+ $wc++;
+ } elseif ($sql[$i] == '\'') { // string starting, skip to end
+ $end = false;
+ while (!$end) {
+ $i++;
+ if ($i == $len) {
+ throw new Grubby_Exception('Unterminated string in filter expression, "'.$sql.'"');
+ } elseif ($sql[$i] == '\\') {
+ $i++;
+ } elseif ($sql[$i] == '\'') {
+ $end = true;
+ }
+ }
+ }
+ $i++;
+ }
+ $wildcard_count = count($wildcards);
+ if ($wc < $wildcard_count && ($wildcard_count > 1 || $wildcards[0])) {
+ throw new Grubby_Exception('Too many wildcards for filter expression.');
+ }
+ if ($marker < $len) {
+ $return .= substr($sql, $marker);
+ }
+ return $return;
+ }
}
View
10 src/Grubby/Exception.php
@@ -1,4 +1,14 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
* When something goes wrong, a grubby exception will be thrown.
View
172 src/Grubby/Filter.php
@@ -1,88 +1,98 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
* Defines the generally used filter, employed by read, delete, filter, and not.
*/
class Grubby_Filter {
- private $filter;
- private $table;
- private $expression = null;
- private $empty = null;
-
- public function __construct($filter) {
- $this->filter = $filter;
- }
-
- /**
- *
- * @param $table
- * @return unknown_type
- */
- public function setTable($table) {
- $this->table = $table;
- }
-
- /**
- *
- * @return boolean
- */
- public function emptySet() {
- if ($this->expression === null) {
- $this->buildExpression();
- }
- return $this->empty;
- }
-
- /**
- *
- * @return string
- */
- public function getExpression() {
- if ($this->expression === null) {
- $this->buildExpression();
- }
- return $this->expression;
- }
-
- /**
- *
- */
- private function buildExpression() {
- if ($this->filter === false) {
- $this->empty = false;
- $this->expression = 'FALSE';
- } elseif ($this->filter === true) {
- $this->empty = false;
- $this->expression = 'TRUE';
- } elseif (empty($this->filter)) {
- // empty set
- $this->empty = true;
- $this->expression = 'FALSE';
- } elseif (is_array($this->filter)) {
- // expression of field=>value equalities
- $this->empty = false;
- $where = array();
- foreach ($this->filter as $field => $value) {
- if (is_int($field)) {
- throw new Grubby_Exception('Filter arrays can only contain column=>value pairings.');
- } elseif (is_null($value)) {
- $where[] = $field.' IS NULL';
- } elseif (is_array($value)) {
- throw new Grubby_Exception('Array filter values not implemented yet.');
- } else {
- $where[] = $field.'='.$this->table->formatFieldValue($field, $value);
- }
- }
- $this->expression = implode(' AND ', $where);
- } else {
- // scalar; match against the primary key
- $pk = $this->table->primaryKey();
- if (is_scalar($pk)) {
- $this->expression = $pk.'='.$this->table->formatFieldValue($pk, $this->filter);
- } else {
- throw new Grubby_Exception('Multi-column primary keys cannot use shorthand pk notation.');
- }
- $this->empty = false;
- }
- }
+ private $filter;
+ private $table;
+ private $expression = null;
+ private $empty = null;
+
+ public function __construct($filter) {
+ $this->filter = $filter;
+ }
+
+ /**
+ *
+ * @param $table
+ * @return unknown_type
+ */
+ public function setTable($table) {
+ $this->table = $table;
+ }
+
+ /**
+ *
+ * @return boolean
+ */
+ public function emptySet() {
+ if ($this->expression === null) {
+ $this->buildExpression();
+ }
+ return $this->empty;
+ }
+
+ /**
+ *
+ * @return string
+ */
+ public function getExpression() {
+ if ($this->expression === null) {
+ $this->buildExpression();
+ }
+ return $this->expression;
+ }
+
+ /**
+ *
+ */
+ private function buildExpression() {
+ if ($this->filter === false) {
+ $this->empty = false;
+ $this->expression = 'FALSE';
+ } elseif ($this->filter === true) {
+ $this->empty = false;
+ $this->expression = 'TRUE';
+ } elseif (empty($this->filter)) {
+ // empty set
+ $this->empty = true;
+ $this->expression = 'FALSE';
+ } elseif (is_array($this->filter)) {
+ // expression of field=>value equalities
+ $this->empty = false;
+ $where = array();
+ foreach ($this->filter as $field => $value) {
+ if (is_int($field)) {
+ throw new Grubby_Exception('Filter arrays can only contain column=>value pairings.');
+ } elseif (is_null($value)) {
+ $where[] = $field.' IS NULL';
+ } elseif (is_array($value)) {
+ throw new Grubby_Exception('Array filter values not implemented yet.');
+ } else {
+ $where[] = $field.'='.$this->table->formatFieldValue($field, $value);
+ }
+ }
+ $this->expression = implode(' AND ', $where);
+ } else {
+ // scalar; match against the primary key
+ $pk = $this->table->primaryKey();
+ if (is_scalar($pk)) {
+ $this->expression = $pk.'='.$this->table->formatFieldValue($pk, $this->filter);
+ } else {
+ throw new Grubby_Exception('Multi-column primary keys cannot use shorthand pk notation.');
+ }
+ $this->empty = false;
+ }
+ }
}
View
124 src/Grubby/MDB2/Database.php
@@ -1,64 +1,74 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
* GrubbyMDB2 : Grubby database using the MDB2 abstraction layer
*/
class Grubby_MDB2_Database extends Grubby_Database {
- private $dsn;
- private $options;
-
- /**
- * Creates a new Grubby database.
- */
- public function __construct($dsn, $options = null) {
- $this->dsn = $dsn;
- $this->options = $options;
- }
-
- /**
- * Returns a database connection.
- */
- public function getConnection() {
- $connection = MDB2::singleton($this->dsn, $this->options);
- if (PEAR::isError($connection)) {
- throw new Grubby_Exception('No database connection available: '.$connection->getMessage());
- }
- return $connection;
- }
-
- /**
- * Executes a SQL query against the database.
- * Use for UPDATE, INSERT, DELETE.
- * @return Grubby_MDB2_Result
- */
- public function executeImpl($sql) {
- $connection = $this->getConnection();
- $result = $connection->exec($sql);
- return new Grubby_MDB2_Result($result);
- }
-
- /**
- * Runs a SQL query against the database.
- * Use for SELECT.
- * @return false or a new MDB2_Result object.
- */
- public function queryImpl($sql) {
- $connection = $this->getConnection();
- $result = $connection->query($sql);
- return new Grubby_MDB2_Recordset($result);
- }
-
- public function lastInsertID() {
- return $this->getConnection()->lastInsertID();
- }
-
- public function formatString($s) {
- if (is_null($s)) {
- return 'NULL';
- } elseif ($s) {
- return $this->getConnection()->quote($s, 'text');
- } else {
- return "''";
- }
- }
+ private $dsn;
+ private $options;
+
+ /**
+ * Creates a new Grubby database.
+ */
+ public function __construct($dsn, $options = null) {
+ $this->dsn = $dsn;
+ $this->options = $options;
+ }
+
+ /**
+ * Returns a database connection.
+ */
+ public function getConnection() {
+ $connection = MDB2::singleton($this->dsn, $this->options);
+ if (PEAR::isError($connection)) {
+ throw new Grubby_Exception('No database connection available: '.$connection->getMessage());
+ }
+ return $connection;
+ }
+
+ /**
+ * Executes a SQL query against the database.
+ * Use for UPDATE, INSERT, DELETE.
+ * @return Grubby_MDB2_Result
+ */
+ public function executeImpl($sql) {
+ $connection = $this->getConnection();
+ $result = $connection->exec($sql);
+ return new Grubby_MDB2_Result($result);
+ }
+
+ /**
+ * Runs a SQL query against the database.
+ * Use for SELECT.
+ * @return false or a new MDB2_Result object.
+ */
+ public function queryImpl($sql) {
+ $connection = $this->getConnection();
+ $result = $connection->query($sql);
+ return new Grubby_MDB2_Recordset($result);
+ }
+
+ public function lastInsertID() {
+ return $this->getConnection()->lastInsertID();
+ }
+
+ public function formatString($s) {
+ if (is_null($s)) {
+ return 'NULL';
+ } elseif ($s) {
+ return $this->getConnection()->quote($s, 'text');
+ } else {
+ return "''";
+ }
+ }
}
View
132 src/Grubby/MDB2/Recordset.php
@@ -1,68 +1,78 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/*
*
*/
class Grubby_MDB2_Recordset extends Grubby_Recordset {
-
- /**
- * Creates a new recordset resource handler.
- * @param result mixed result of a call to MDB2::query
- */
- public function __construct($result) {
- if ($result instanceof MDB2_Error) {
- $this->error = $result;
- } else {
- $this->recordset = $result;
- }
- }
-
- /**
- * Returns the next row in the resultset or null if eof has been reached.
- * @return mixed
- */
- public function fetch() {
- $row = $this->recordset->fetchRow(MDB2_FETCHMODE_ASSOC);
- if ($row) {
- return $this->unmarshal($row);
- } else {
- return null;
- }
- }
-
- /**
- * Returns all remaining rows in the result set in an ordered array.
- * @return array
- */
- public function fetchAll() {
- $all = $this->recordset->fetchAll(MDB2_FETCHMODE_ASSOC);
- foreach ($all as $key => $row) {
- $all[$key] = $this->unmarshal($row);
- }
- return $all;
- }
-
- /**
- * Returns all members of a particular column of the result set as an ordered array.
- * @return array
- */
- public function fetchColumn($column) {
- return $this->recordset->fetchCol($column);
- }
-
- /**
- * True if the query resulted in an error.
- * @return boolean
- */
- public function error() {
- return isset($this->error);
- }
-
- /**
- * Available when error() is true.
- * @return string
- */
- public function errorMessage() {
- return $this->error->getMessage();
- }
+
+ /**
+ * Creates a new recordset resource handler.
+ * @param result mixed result of a call to MDB2::query
+ */
+ public function __construct($result) {
+ if ($result instanceof MDB2_Error) {
+ $this->error = $result;
+ } else {
+ $this->recordset = $result;
+ }
+ }
+
+ /**
+ * Returns the next row in the resultset or null if eof has been reached.
+ * @return mixed
+ */
+ public function fetch() {
+ $row = $this->recordset->fetchRow(MDB2_FETCHMODE_ASSOC);
+ if ($row) {
+ return $this->unmarshal($row);
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Returns all remaining rows in the result set in an ordered array.
+ * @return array
+ */
+ public function fetchAll() {
+ $all = $this->recordset->fetchAll(MDB2_FETCHMODE_ASSOC);
+ foreach ($all as $key => $row) {
+ $all[$key] = $this->unmarshal($row);
+ }
+ return $all;
+ }
+
+ /**
+ * Returns all members of a particular column of the result set as an ordered array.
+ * @return array
+ */
+ public function fetchColumn($column) {
+ return $this->recordset->fetchCol($column);
+ }
+
+ /**
+ * True if the query resulted in an error.
+ * @return boolean
+ */
+ public function error() {
+ return isset($this->error);
+ }
+
+ /**
+ * Available when error() is true.
+ * @return string
+ */
+ public function errorMessage() {
+ return $this->error->getMessage();
+ }
}
View
66 src/Grubby/MDB2/Result.php
@@ -1,35 +1,45 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
* Result object returned by a GrubbyMDB2Database execute function call.
*/
class Grubby_MDB2_Result extends Grubby_Result {
-
- /**
- * Creates an MDB2 execute result object.
- * @param result mixed result of a call to MDB2::exec
- */
- public function __construct($result) {
- if ($result instanceof MDB2_Error) {
- $this->error = $result;
- } else {
- $this->affected_rows = $result;
- }
- }
-
- /**
- * True if the query resulted in an error.
- * @return boolean
- */
- public function error() {
- return isset($this->error);
- }
-
- /**
- * Available when error() is true.
- * @return string
- */
- public function errorMessage() {
- return $this->error->getMessage();
- }
+
+ /**
+ * Creates an MDB2 execute result object.
+ * @param result mixed result of a call to MDB2::exec
+ */
+ public function __construct($result) {
+ if ($result instanceof MDB2_Error) {
+ $this->error = $result;
+ } else {
+ $this->affected_rows = $result;
+ }
+ }
+
+ /**
+ * True if the query resulted in an error.
+ * @return boolean
+ */
+ public function error() {
+ return isset($this->error);
+ }
+
+ /**
+ * Available when error() is true.
+ * @return string
+ */
+ public function errorMessage() {
+ return $this->error->getMessage();
+ }
}
View
140 src/Grubby/Mysql/Database.php
@@ -1,73 +1,83 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
* Grubby database using the DB abstraction layer
*/
class Grubby_Mysql_Database extends Grubby_Database {
- private $server;
- private $username;
- private $password;
- private $database;
- private $client_flags;
+ private $server;
+ private $username;
+ private $password;
+ private $database;
+ private $client_flags;
private $connection = null;
-
- /**
- * Creates a new Grubby database.
- */
- public function __construct($server, $username, $password, $database, $client_flags = 0) {
- $this->server = $server;
- $this->username = $username;
- $this->password = $password;
- $this->database = $database;
- $this->client_flags = $client_flags;
- }
-
- /**
- * Returns a database connection.
- */
- public function getConnection() {
- if ($this->connection === null) {
- $this->connection = mysql_connect($this->server, $this->username, $this->password, $this->client_flags);
- mysql_select_db($this->database, $this->connection);
- }
- return $this->connection;
- }
-
- /**
- * Executes a SQL query against the database.
- * Use for UPDATE, INSERT, DELETE.
- * @return Grubby_DB_Result
- */
- public function executeImpl($sql) {
- $connection = $this->getConnection();
- $result = mysql_query($sql, $connection);
- if (!$result) {
- throw new Grubby_Exception(mysql_error($connection));
- }
- return new Grubby_Mysql_Result($connection, $result);
- }
-
- /**
- * Runs a SQL query against the database.
- * Use for SELECT.
- * @return false or a new DB_Result object.
- */
- public function queryImpl($sql) {
- $connection = $this->getConnection();
- $result = mysql_query($sql, $connection);
- if (!$result) {
- throw new Grubby_Exception(mysql_error($connection));
- }
- return new Grubby_Mysql_Recordset($this, $result);
- }
-
- public function formatString($s) {
- if ($s === null) {
- return 'NULL';
- } elseif ($s) {
- return "'".mysql_real_escape_string($s, $this->getConnection())."'";
- } else {
- return "''";
- }
- }
+
+ /**
+ * Creates a new Grubby database.
+ */
+ public function __construct($server, $username, $password, $database, $client_flags = 0) {
+ $this->server = $server;
+ $this->username = $username;
+ $this->password = $password;
+ $this->database = $database;
+ $this->client_flags = $client_flags;
+ }
+
+ /**
+ * Returns a database connection.
+ */
+ public function getConnection() {
+ if ($this->connection === null) {
+ $this->connection = mysql_connect($this->server, $this->username, $this->password, $this->client_flags);
+ mysql_select_db($this->database, $this->connection);
+ }
+ return $this->connection;
+ }
+
+ /**
+ * Executes a SQL query against the database.
+ * Use for UPDATE, INSERT, DELETE.
+ * @return Grubby_DB_Result
+ */
+ public function executeImpl($sql) {
+ $connection = $this->getConnection();
+ $result = mysql_query($sql, $connection);
+ if (!$result) {
+ throw new Grubby_Exception(mysql_error($connection));
+ }
+ return new Grubby_Mysql_Result($connection, $result);
+ }
+
+ /**
+ * Runs a SQL query against the database.
+ * Use for SELECT.
+ * @return false or a new DB_Result object.
+ */
+ public function queryImpl($sql) {
+ $connection = $this->getConnection();
+ $result = mysql_query($sql, $connection);
+ if (!$result) {
+ throw new Grubby_Exception(mysql_error($connection));
+ }
+ return new Grubby_Mysql_Recordset($this, $result);
+ }
+
+ public function formatString($s) {
+ if ($s === null) {
+ return 'NULL';
+ } elseif ($s) {
+ return "'".mysql_real_escape_string($s, $this->getConnection())."'";
+ } else {
+ return "''";
+ }
+ }
}
View
132 src/Grubby/Mysql/Recordset.php
@@ -1,68 +1,78 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
*
*/
class Grubby_Mysql_Recordset extends Grubby_Recordset {
-
- /**
- * Creates a new recordset resource handler.
- * @param result mixed result of a call to DB::query
- */
- public function __construct($database, $result) {
- $this->database = $database;
- $this->recordset = $result;
- }
-
- /**
- * Returns the next row in the resultset or null if eof has been reached.
- * @return mixed
- */
- public function fetch() {
- $row = mysql_fetch_assoc($this->recordset);
- if ($row) {
- return $this->unmarshal($row);
- } else {
- return null;
- }
- }
-
- /**
- * Returns all remaining rows in the result set in an ordered array.
- * @return array
- */
- public function fetchAll() {
- $all = array();
- while ($row = mysql_fetch_assoc($this->recordset)) {
- $all[] = $row;
- }
- foreach ($all as $key => $row) {
- $all[$key] = $this->unmarshal($row);
- }
- return $all;
- }
-
- /**
- * Returns all members of a particular column of the result set as an ordered array.
- * @return array
- */
- public function fetchColumn($column) {
- return $this->recordset->fetchCol($column);
- }
-
- /**
- * True if the query resulted in an error.
- * @return boolean
- */
- public function error() {
- return isset($this->error);
- }
-
- /**
- * Available when error() is true.
- * @return string
- */
- public function errorMessage() {
- return '';
- }
+
+ /**
+ * Creates a new recordset resource handler.
+ * @param result mixed result of a call to DB::query
+ */
+ public function __construct($database, $result) {
+ $this->database = $database;
+ $this->recordset = $result;
+ }
+
+ /**
+ * Returns the next row in the resultset or null if eof has been reached.
+ * @return mixed
+ */
+ public function fetch() {
+ $row = mysql_fetch_assoc($this->recordset);
+ if ($row) {
+ return $this->unmarshal($row);
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Returns all remaining rows in the result set in an ordered array.
+ * @return array
+ */
+ public function fetchAll() {
+ $all = array();
+ while ($row = mysql_fetch_assoc($this->recordset)) {
+ $all[] = $row;
+ }
+ foreach ($all as $key => $row) {
+ $all[$key] = $this->unmarshal($row);
+ }
+ return $all;
+ }
+
+ /**
+ * Returns all members of a particular column of the result set as an ordered array.
+ * @return array
+ */
+ public function fetchColumn($column) {
+ return $this->recordset->fetchCol($column);
+ }
+
+ /**
+ * True if the query resulted in an error.
+ * @return boolean
+ */
+ public function error() {
+ return isset($this->error);
+ }
+
+ /**
+ * Available when error() is true.
+ * @return string
+ */
+ public function errorMessage() {
+ return '';
+ }
}
View
58 src/Grubby/Mysql/Result.php
@@ -1,31 +1,41 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
* Result object returned by a GrubbyDBDatabase execute function call.
*/
class Grubby_Mysql_Result extends Grubby_Result {
-
- /**
- * Creates an DB execute result object.
- * @param result mixed result of a call to DB::exec
- */
- public function __construct($connection, $result) {
- $this->affected_rows = mysql_affected_rows($connection);
- }
-
- /**
- * True if the query resulted in an error.
- * @return boolean
- */
- public function error() {
- return '';
- }
-
- /**
- * Available when error() is true.
- * @return string
- */
- public function errorMessage() {
- return $this->error->getMessage();
- }
+
+ /**
+ * Creates an DB execute result object.
+ * @param result mixed result of a call to DB::exec
+ */
+ public function __construct($connection, $result) {
+ $this->affected_rows = mysql_affected_rows($connection);
+ }
+
+ /**
+ * True if the query resulted in an error.
+ * @return boolean
+ */
+ public function error() {
+ return '';
+ }
+
+ /**
+ * Available when error() is true.
+ * @return string
+ */
+ public function errorMessage() {
+ return $this->error->getMessage();
+ }
}
View
16 src/Grubby/NotFilter.php
@@ -1,7 +1,17 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
class Grubby_NotFilter extends Grubby_Filter {
- public function getExpression() {
- return 'NOT ('.parent::getExpression().')';
- }
+ public function getExpression() {
+ return 'NOT ('.parent::getExpression().')';
+ }
}
View
148 src/Grubby/PDO/Database.php
@@ -1,76 +1,86 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
* GrubbyMDB2 : Grubby database using the MDB2 abstraction layer
*/
class Grubby_PDO_Database extends Grubby_Database {
- private $dsn;
- private $username;
- private $password;
- private $options;
- private $pdo = null;
-
- /**
- * Creates a new Grubby database.
- */
- public function __construct($dsn, $username, $password, $options = null) {
- $this->dsn = $dsn;
- $this->username = $username;
- $this->password = $password;
- $this->options = $options;
- }
-
- /**
- * Returns a database connection.
- */
- public function getConnection() {
- if ($this->pdo === null) {
- $this->pdo = new PDO($this->dsn, $this->username, $this->password, $this->options);
- }
- return $this->pdo;
- }
-
- /**
- * Executes a SQL query against the database.
- * Use for UPDATE, INSERT, DELETE.
- * @return Grubby_MDB2_Result
- */
- public function executeImpl($sql) {
- $pdo = $this->getConnection();
- $result = $pdo->exec($sql);
- if ($result === false) {
- $info = $pdo->errorInfo();
- throw new Grubby_Exception(json_encode($info));
- }
- return new Grubby_PDO_Result($result);
- }
-
- /**
- * Runs a SQL query against the database.
- * Use for SELECT.
- * @return false or a new MDB2_Result object.
- */
- public function queryImpl($sql) {
- $pdo = $this->getConnection();
- $result = $pdo->query($sql);
- if ($result === false) {
- $info = $pdo->errorInfo();
- throw new Grubby_Exception(json_encode($info));
- }
- return new Grubby_PDO_Recordset($result);
- }
-
- public function lastInsertID() {
- return $this->getConnection()->lastInsertId();
- }
-
- public function formatString($s) {
- if (is_null($s)) {
- return 'NULL';
- } elseif ($s) {
- return $this->getConnection()->quote($s, 'text');
- } else {
- return "''";
- }
- }
+ private $dsn;
+ private $username;
+ private $password;
+ private $options;
+ private $pdo = null;
+
+ /**
+ * Creates a new Grubby database.
+ */
+ public function __construct($dsn, $username, $password, $options = null) {
+ $this->dsn = $dsn;
+ $this->username = $username;
+ $this->password = $password;
+ $this->options = $options;
+ }
+
+ /**
+ * Returns a database connection.
+ */
+ public function getConnection() {
+ if ($this->pdo === null) {
+ $this->pdo = new PDO($this->dsn, $this->username, $this->password, $this->options);
+ }
+ return $this->pdo;
+ }
+
+ /**
+ * Executes a SQL query against the database.
+ * Use for UPDATE, INSERT, DELETE.
+ * @return Grubby_MDB2_Result
+ */
+ public function executeImpl($sql) {
+ $pdo = $this->getConnection();
+ $result = $pdo->exec($sql);
+ if ($result === false) {
+ $info = $pdo->errorInfo();
+ throw new Grubby_Exception(json_encode($info));
+ }
+ return new Grubby_PDO_Result($result);
+ }
+
+ /**
+ * Runs a SQL query against the database.
+ * Use for SELECT.
+ * @return false or a new MDB2_Result object.
+ */
+ public function queryImpl($sql) {
+ $pdo = $this->getConnection();
+ $result = $pdo->query($sql);
+ if ($result === false) {
+ $info = $pdo->errorInfo();
+ throw new Grubby_Exception(json_encode($info));
+ }
+ return new Grubby_PDO_Recordset($result);
+ }
+
+ public function lastInsertID() {
+ return $this->getConnection()->lastInsertId();
+ }
+
+ public function formatString($s) {
+ if (is_null($s)) {
+ return 'NULL';
+ } elseif ($s) {
+ return $this->getConnection()->quote($s, 'text');
+ } else {
+ return "''";
+ }
+ }
}
View
124 src/Grubby/PDO/Recordset.php
@@ -1,64 +1,74 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/*
*
*/
class Grubby_PDO_Recordset extends Grubby_Recordset {
-
- /**
- * Creates a new recordset resource handler.
- * @param result mixed result of a call to MDB2::query
- */
- public function __construct($result) {
- $this->recordset = $result;
- }
-
- /**
- * Returns the next row in the resultset or null if eof has been reached.
- * @return mixed
- */
- public function fetch() {
- $row = $this->recordset->fetch(PDO::FETCH_ASSOC);
- if ($row) {
- return $this->unmarshal($row);
- } else {
- return null;
- }
- }
-
- /**
- * Returns all remaining rows in the result set in an ordered array.
- * @return array
- */
- public function fetchAll() {
- $all = $this->recordset->fetchAll(PDO::FETCH_ASSOC);
- foreach ($all as $key => $row) {
- $all[$key] = $this->unmarshal($row);
- }
- return $all;
- }
-
- /**
- * Returns all members of a particular column of the result set as an ordered array.
- * @return array
- */
- public function fetchColumn($column) {
- return $this->recordset->fetchCol($column);
- }
-
- /**
- * True if the query resulted in an error.
- * @return boolean
- */
- public function error() {
- return isset($this->error);
- }
-
- /**
- * Available when error() is true.
- * @return string
- */
- public function errorMessage() {
- return $this->error->getMessage();
- }
+
+ /**
+ * Creates a new recordset resource handler.
+ * @param result mixed result of a call to MDB2::query
+ */
+ public function __construct($result) {
+ $this->recordset = $result;
+ }
+
+ /**
+ * Returns the next row in the resultset or null if eof has been reached.
+ * @return mixed
+ */
+ public function fetch() {
+ $row = $this->recordset->fetch(PDO::FETCH_ASSOC);
+ if ($row) {
+ return $this->unmarshal($row);
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Returns all remaining rows in the result set in an ordered array.
+ * @return array
+ */
+ public function fetchAll() {
+ $all = $this->recordset->fetchAll(PDO::FETCH_ASSOC);
+ foreach ($all as $key => $row) {
+ $all[$key] = $this->unmarshal($row);
+ }
+ return $all;
+ }
+
+ /**
+ * Returns all members of a particular column of the result set as an ordered array.
+ * @return array
+ */
+ public function fetchColumn($column) {
+ return $this->recordset->fetchCol($column);
+ }
+
+ /**
+ * True if the query resulted in an error.
+ * @return boolean
+ */
+ public function error() {
+ return isset($this->error);
+ }
+
+ /**
+ * Available when error() is true.
+ * @return string
+ */
+ public function errorMessage() {
+ return $this->error->getMessage();
+ }
}
View
58 src/Grubby/PDO/Result.php
@@ -1,31 +1,41 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
* Result object returned by a GrubbyMDB2Database execute function call.
*/
class Grubby_PDO_Result extends Grubby_Result {
-
- /**
- * Creates an MDB2 execute result object.
- * @param result mixed result of a call to MDB2::exec
- */
- public function __construct($result) {
- $this->affected_rows = $result;
- }
-
- /**
- * True if the query resulted in an error.
- * @return boolean
- */
- public function error() {
- return isset($this->error);
- }
-
- /**
- * Available when error() is true.
- * @return string
- */
- public function errorMessage() {
- return $this->error->getMessage();
- }
+
+ /**
+ * Creates an MDB2 execute result object.
+ * @param result mixed result of a call to MDB2::exec
+ */
+ public function __construct($result) {
+ $this->affected_rows = $result;
+ }
+
+ /**
+ * True if the query resulted in an error.
+ * @return boolean
+ */
+ public function error() {
+ return isset($this->error);
+ }
+
+ /**
+ * Available when error() is true.
+ * @return string
+ */
+ public function errorMessage() {
+ return $this->error->getMessage();
+ }
}
View
320 src/Grubby/Query.php
@@ -1,4 +1,14 @@
<?php
+/**
+ * Grubby : Quick and dirty CRUD operations
+ * http://grubbycrud.com/
+ *
+ * Version: @version@
+ * Date: @date@
+ *
+ * Copyright (c) @year@ Christopher Johnson
+ * Licensed under the MIT license (see LICENSE file).
+ */
/**
* Allows for CRUD operations.
@@ -6,159 +16,159 @@
* This is the base object of all GrubbyTable type queries.
*/
class Grubby_Query {
- private $parent;
-
- /**
- * Create a new GrubbyQuery beholden to a parent query.
- * The top level query should be a GrubbyTable, which will
- * actually perform the ultimate CRUD operation requested.
- * GrubbyQuery objects are chained by using the helper functions
- * provided here: all(), sort(), etc.
- */
- public function __construct($parent) {
- $this->parent = $parent;
- }
-
- /**
- * Adds a row of data to the table.
- * @param $data array
- * @return GrubbyResult
- */
- public function create($data) {
- return $this->crudImpl(array('create'=>$data));
- }
-
- /**
- * Reads data from the query following all of the rules that have been chained onto the end.
- * @param $filter int|string|array
- * @return GrubbyRecordset
- */
- public function read($filter = true) {
- return $this->crudImpl(array('read'=>$filter));
- }
-
- /**
- * Update a record in this database table.
- * Batch updates can be performed only after all() has been chained onto the query.
- * @return GrubbyResult
- */
- public function update($data) {
- return $this->crudImpl(array('update'=>$data));
- }
-
- /**
- * Delete a record from this database table.
- * Bulk deletions can be performed only after all() has been chained onto the query.
- * @return GrubbyResult
- */
- public function delete($filter = true) {
- return $this->crudImpl(array('delete'=>$filter));
- }
-
- /**
- * Count records in this query, accounting for all filters, etc.
- * @return int
- */
- public function count($filter = true) {
- $result = $this->fields(array('tally'=>'COUNT(*)'))->read($filter);
- $row = $result->fetch();
- return $row['tally'];
- }
-
- /**
- * $query can have information attached to it as the update tree is traversed.
- * See GrubbyTable::updateImpl for information on the $query array.
- * @return GrubbyResult|GrubbyRecordset
- */
- protected function crudImpl($query) {
- return $this->parent->crudImpl($query);
- }
-
- /**
- * Reduce the set of returned records to those matching the filter equality.
- * @param $filter int|string|array
- * @return GrubbyQuery
- */
- public function filter($filter) {
- return new Grubby_Query_Modifier($this, array('filters'=>new Grubby_Filter($filter)));
- }
-
- /**
- * Reduce the set of returned records to those matching the custom filter.
- * @param $expression string SQL boolean expression
- * @param $wildcards string|array replaces ? in the expression
- * @return GrubbyQuery
- */
- public function filterExpression($expression, $wildcards = null) {
- return new Grubby_Query_Modifier($this, array('expressions'=>array($expression, $wildcards)));
- }
-
- /**
- * Reduce the set of returned records to those not matching the filter equality.
- * @param $filter int|string|array
- * @return GrubbyQuery
- */
- public function not($filter) {
- return new Grubby_Query_Modifier($this, array('filters'=>new Grubby_NotFilter($filter)));
- }
-
- /**
- * Only take a certain sub-set of the records.
- * @param $offset int
- * @param $count int
- * @return GrubbyQuery
- */
- public function slice($offset, $count) {
- return new Grubby_Query_Modifier($this, array('slice'=>array('offset'=>$offset, 'limit'=>$count)));
- }
-
- /**
- * Changes the sort order of the query.
- * As queries can have only one sort order, later sorts will overwrite previous sorts.
- * @param $order string|array the field(s) to sort by
- * @return GrubbyQuery
- */
- public function sort($order) {
- return new Grubby_Query_Modifier($this, array('sort'=>$order));
- }
-
- /**
- * Batch updates and bulk deletions are disallowed by default.
- * Calling all() at the end of a query chain overrides that.
- * Note that further chaining the query removes the all() capability.
- *
- * Example:
- * // the former works, the latter throws an exception
- * $table->constrain('id < 10')->all()->delete();
- * $table->all()->constrain('id < 10')->delete();
- *
- * @return GrubbyQuery
- */
- public function all() {
- return new Grubby_Query_All($this);
- }
-
- /**
- * Changes the field set being queried.
- * Later field sets totally override earlier ones.
- * @param $fields string|array
- * @return GrubbyQuery
- */
- public function fields($fields) {
- return new Grubby_Query_Modifier($this, array('fields'=>$fields));
- }
-
- /**
- * Aggregates certain fields together.
- * Later aggregates override earlier ones.
- * @param $fields string|array
- * @return GrubbyQuery
- */
- public function aggregate($fields) {
- return new Grubby_Query_Modifier($this, array('aggregate'=>$fields));
- }
-
- public function join($query, $on) {
- return new Grubby_Query_Modifier($this, array('join'=>array($query, $on)));
- }
+ private $parent;
+
+ /**
+ * Create a new GrubbyQuery beholden to a parent query.
+ * The top level query should be a GrubbyTable, which will
+ * actually perform the ultimate CRUD operation requested.
+ * GrubbyQuery objects are chained by using the helper functions
+ * provided here: all(), sort(), etc.
+ */
+ public function __construct($parent) {
+ $this->parent = $parent;
+ }
+
+ /**
+ * Adds a row of data to the table.
+ * @param $data array
+ * @return GrubbyResult
+ */
+ public function create($data) {
+ return $this->crudImpl(array('create'=>$data));
+ }
+
+ /**
+ * Reads data from the query following all of the rules that have been chained onto the end.
+ * @param $filter int|string|array
+ * @return GrubbyRecordset
+ */
+ public function read($filter = true) {
+ return $this->crudImpl(array('read'=>$filter));
+ }
+
+ /**
+ * Update a record in this database table.
+ * Batch updates can be performed only after all() has been chained onto the query.
+ * @return GrubbyResult