Skip to content

Commit

Permalink
PhalconAdapter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tzatrepalek-inwk committed Feb 25, 2019
1 parent 08ead01 commit 4d60ce0
Showing 1 changed file with 61 additions and 12 deletions.
73 changes: 61 additions & 12 deletions src/Bridges/Phalcon/PhalconAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,111 @@ class PhalconAdapter implements IDbal
/** @var Pdo */
private $conn;


/**
* @param Pdo $connection
*/
public function __construct(Pdo $connection)
{
$this->conn = $connection;
$this->conn->connect();
}


/**
* @param string $sql
* @return array list of rows represented by assoc. arrays
*/
public function query($sql)
{
return $this->conn->fetchAll($sql);
}


/**
* @param string $sql
* @return int number of affected rows
*/
public function exec($sql)
{
$this->conn->execute($sql);
$this->conn->affectedRows();
return $this->conn->affectedRows();
}


public function escapeString($value)
/**
* @param string $value
* @return string escaped string wrapped in quotes
*/
public function escapeString($value)
{
return $this->conn->escapeString($value);
}


/**
* @param int $value
* @return string
*/
public function escapeInt($value)
{
return (string)(int)$value;
}


/**
* @param bool $value
* @return string
* @throws ExecutionException
*/
public function escapeBool($value)
{
return (string)(int)$value;
}
if ($this->conn->getType() === self::TYPE_MYSQL) {
return (string)(int)$value;
}

if ($this->conn->getType() === self::TYPE_PGSQL) {
return (bool)$value ? 'TRUE' : 'FALSE';
}

throw new ExecutionException('Unsupported type of \Phalcon\Db\Adapter\Pdo driver.');
}

/**
* @param DateTime $value
* @return string
* @throws ExecutionException
*/
public function escapeDateTime(DateTime $value)
{
return $this->conn->escapeString($this->formatDateTime($value));
}


/**
* @param string $value
* @return string
* @throws ExecutionException
*/
public function escapeIdentifier($value)
{
return $this->conn->escapeIdentifier($value);
if ($this->conn->getType() === self::TYPE_MYSQL) {
return '`' . $value . '`';
}

if ($this->conn->getType() === self::TYPE_PGSQL) {
return '"' . $value . '"';
}

throw new ExecutionException('Unsupported type of \Phalcon\Db\Adapter\Pdo driver.');
}

/**
* @param DateTime $value
* @return string
* @throws ExecutionException
*/
private function formatDateTime(DateTime $value)
{
if (in_array($this->conn->getType(), [self::TYPE_MYSQL, self::TYPE_PGSQL], true)) {
return $value->format('Y-m-d H:i:s');
} else {
throw new ExecutionException('Unsupported type of \Phalcon\Db\Adapter\Pdo driver.');
}

throw new ExecutionException('Unsupported type of \Phalcon\Db\Adapter\Pdo driver.');
}
}

0 comments on commit 4d60ce0

Please sign in to comment.