Skip to content

Commit

Permalink
Explicitly specify Akiban does not support savepoints. Flesh out a fe…
Browse files Browse the repository at this point in the history
…w methods in Connection and Statement classes so queries can be sent to Akiban now. This allows basic unit tests to pass. Will likely modify this implementation in the near future.
  • Loading branch information
posulliv committed Aug 14, 2012
1 parent a9513da commit e8d0f49
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 14 deletions.
14 changes: 7 additions & 7 deletions lib/Doctrine/DBAL/Driver/AkibanSrv/AkibanSrvConnection.php
Expand Up @@ -34,7 +34,7 @@ class AkibanSrvConnection implements \Doctrine\DBAL\Driver\Connection
/**
* @var resource
*/
protected $dbh;
protected $_dbh;

/**
* Create a Connection to an Akiban Server Database using
Expand All @@ -46,8 +46,8 @@ class AkibanSrvConnection implements \Doctrine\DBAL\Driver\Connection
*/
public function __construct($connectionString)
{
$this->dbh = pg_connect($connectionString);
if ( ! $this->dbh ) {
$this->_dbh = pg_connect($connectionString);
if ( ! $this->_dbh ) {
throw AkibanSrvException::fromErrorString("Failed to connect to Akiban Server.");
}
}
Expand All @@ -60,7 +60,7 @@ public function __construct($connectionString)
*/
public function prepare($prepareString)
{
return new AkibanSrvStatement($this->dbh, $prepareString, $this);
return new AkibanSrvStatement($this->_dbh, $prepareString, $this);
}

/**
Expand All @@ -71,7 +71,7 @@ public function query()
{
$args = func_get_args();
$sql = $args[0];
$stmt = $this->prepare($sql);
$stmt = new AkibanSrvStatement($this->_dbh, $sql, $this);
$stmt->execute();
return $stmt;
}
Expand Down Expand Up @@ -146,11 +146,11 @@ public function rollBack()
public function errorCode()
{
// TODO - this returns error message, not error code
return pg_last_error($this->dbh);
return pg_last_error($this->_dbh);
}

public function errorInfo()
{
return pg_last_error($this->dbh);
return pg_last_error($this->_dbh);
}
}
66 changes: 61 additions & 5 deletions lib/Doctrine/DBAL/Driver/AkibanSrv/AkibanSrvStatement.php
Expand Up @@ -45,18 +45,61 @@ class AkibanSrvStatement implements IteratorAggregate, Statement
*/
private $_statement;

/**
* randomly generated name for this statement.
*/
private $_statementName;

/**
* query results
*/
private $_results;

/**
* Akiban Server connection object.
*
* @var resource
*/
private $_conn;

private $_parameters = array();

public function __construct($dbh, $statement, AkibanSrvConnection $conn)
{
$this->_statement = $statement;
$this->_statement = $this->convertPositionalToNumberedParameters($statement);
$this->_dbh = $dbh;
$this->_conn = $conn;
// generate a random name for this statement
$this->_statementName = "my_query";
$this->_results = false;
//pg_prepare($this->_dbh, $this->_statementName, $this->_statement);
}

/**
* Convert positional (?) into numbered parameters ($<num>).
*
* The PostgreSQL client libraries do not support positional parameters, hence
* this method converts all positional parameters into numbered parameters.
*/
private function convertPositionalToNumberedParameters($statement)
{
$count = 1;
$inLiteral = false;
$stmtLen = strlen($statement);
for ($i = 0; $i < $stmtLen; $i++) {
if ($statement[$i] == '?' && ! $inLiteral) {
$param = "$" . $count;
$len = strlen($param);
$statement = substr_replace($statement, $param, $i, 1);
$i += $len - 1;
$stmtLen = strlen($statement);
++$count;
} else if ($statement[$i] == "'" || $statement[$i] == '"') {
$inLiteral = ! $inLiteral;
}
}

return $statement;
}

/**
Expand All @@ -72,7 +115,7 @@ public function bindValue($param, $value, $type = null)
*/
public function bindParam($column, &$variable, $type = null, $length = null)
{
// TODO
$this->_parameters[] = $variable;
}

public function closeCursor()
Expand Down Expand Up @@ -109,7 +152,17 @@ public function errorInfo()
*/
public function execute($params = null)
{
// TODO
if (is_null($params)) {
$args = array();
}
if (empty($this->_parameters) && is_null($params)) {
$this->_results = pg_query($this->_dbh, $this->_statement);
} else if (empty($this->_parameters) && ! is_null($params)) {
$this->_results = pg_query_params($this->_dbh, $this->_statement, $params);
} else {
$this->_results = pg_query_params($this->_dbh, $this->_statement, $this->_parameters);
}
return $this->_results;
}

/**
Expand Down Expand Up @@ -141,7 +194,7 @@ public function fetch($fetchMode = null)
*/
public function fetchAll($fetchMode = null)
{
// TODO
return pg_fetch_all($this->_results);
}

/**
Expand All @@ -157,7 +210,10 @@ public function fetchColumn($columnIndex = 0)
*/
public function rowCount()
{
// TODO
if ($this->_results) {
return pg_affected_rows($this->_results);
}
return 0;
}
}

25 changes: 23 additions & 2 deletions lib/Doctrine/DBAL/Platforms/AkibanServerPlatform.php
Expand Up @@ -140,6 +140,27 @@ public function prefersSequences()
return true;
}

/**
* Whether the platform supports savepoints.
*
* @return boolean
*/
public function supportsSavepoints()
{
return false;
}

/**
* Whether the platform supports releasing savepoints.
*
* @return boolean
*/
public function supportsReleaseSavepoints()
{
return $this->supportsSavepoints();
}


public function getListDatabasesSQL()
{
return "SELECT schema_name FROM information_schema.schemata";
Expand Down Expand Up @@ -227,7 +248,7 @@ public function getCreateDatabaseSQL($name)
*/
public function getDropDatabaseSQL($name)
{
return "DROP SCHEMA " . $name . " CASCADE";
return "DROP SCHEMA IF EXISTS " . $name . " CASCADE";
}

/**
Expand Down Expand Up @@ -531,7 +552,7 @@ public function getEmptyIdentityInsertSQL($quotedTableName, $quotedIdentifierCol
*/
public function getTruncateTableSQL($tableName, $cascade = false)
{
return 'TRUNCATE '.$tableName.' '.(($cascade)?'CASCADE':'');
return 'TRUNCATE TABLE ' . $tableName . ' ' . (($cascade) ? 'CASCADE' : '');
}

protected function initializeDoctrineTypeMappings()
Expand Down

0 comments on commit e8d0f49

Please sign in to comment.