This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for PDO and raw MySQL drivers.
- Loading branch information
Showing
6 changed files
with
347 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/** | ||
* 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 $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 "''"; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
/** | ||
* | ||
*/ | ||
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; | ||
} | ||
if ($this->object_type) { | ||
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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/** | ||
* 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(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/** | ||
* 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 "''"; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* | ||
*/ | ||
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); | ||
if ($this->object_type) { | ||
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(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/** | ||
* 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(); | ||
} | ||
} |