Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
Added support for PDO and raw MySQL drivers.
Browse files Browse the repository at this point in the history
  • Loading branch information
tenorviol committed Apr 1, 2010
1 parent d0eebd2 commit 6c5e1d2
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/Grubby/Mysql/Database.php
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 "''";
}
}
}
70 changes: 70 additions & 0 deletions src/Grubby/Mysql/Recordset.php
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 '';
}
}
31 changes: 31 additions & 0 deletions src/Grubby/Mysql/Result.php
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();
}
}
76 changes: 76 additions & 0 deletions src/Grubby/PDO/Database.php
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 "''";
}
}
}
66 changes: 66 additions & 0 deletions src/Grubby/PDO/Recordset.php
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();
}
}
31 changes: 31 additions & 0 deletions src/Grubby/PDO/Result.php
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();
}
}

0 comments on commit 6c5e1d2

Please sign in to comment.