diff options
author | Frédéric Guillot <fred@kanboard.net> | 2014-11-04 21:33:05 -0500 |
---|---|---|
committer | Frédéric Guillot <fred@kanboard.net> | 2014-11-04 21:33:05 -0500 |
commit | 135b921db75da5995eab7e36393ecd4d2b0bc66f (patch) | |
tree | 46efc60fcf1f9d5c57ab1fb9418c2acfbda0698a /vendor/PicoDb | |
parent | 850645dd6b22f5b495d1680e0b49540e0ebf9bd3 (diff) |
Switch to composer
Diffstat (limited to 'vendor/PicoDb')
-rw-r--r-- | vendor/PicoDb/Database.php | 155 | ||||
-rw-r--r-- | vendor/PicoDb/Drivers/Mysql.php | 75 | ||||
-rw-r--r-- | vendor/PicoDb/Drivers/Postgres.php | 73 | ||||
-rw-r--r-- | vendor/PicoDb/Drivers/Sqlite.php | 56 | ||||
-rw-r--r-- | vendor/PicoDb/Schema.php | 54 | ||||
-rw-r--r-- | vendor/PicoDb/Table.php | 444 |
6 files changed, 0 insertions, 857 deletions
diff --git a/vendor/PicoDb/Database.php b/vendor/PicoDb/Database.php deleted file mode 100644 index c09d8a92..00000000 --- a/vendor/PicoDb/Database.php +++ /dev/null @@ -1,155 +0,0 @@ -<?php - -namespace PicoDb; - -class Database -{ - private static $instances = array(); - private $logs = array(); - private $pdo; - - - public function __construct(array $settings) - { - if (! isset($settings['driver'])) { - throw new \LogicException('You must define a database driver.'); - } - - switch ($settings['driver']) { - - case 'sqlite': - require_once __DIR__.'/Drivers/Sqlite.php'; - $this->pdo = new Sqlite($settings); - break; - - case 'mysql': - require_once __DIR__.'/Drivers/Mysql.php'; - $this->pdo = new Mysql($settings); - break; - - case 'postgres': - require_once __DIR__.'/Drivers/Postgres.php'; - $this->pdo = new Postgres($settings); - break; - - default: - throw new \LogicException('This database driver is not supported.'); - } - - $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); - } - - - public static function bootstrap($name, \Closure $callback) - { - self::$instances[$name] = $callback; - } - - - public static function get($name) - { - if (! isset(self::$instances[$name])) { - throw new \LogicException('No database instance created with that name.'); - } - - if (is_callable(self::$instances[$name])) { - self::$instances[$name] = call_user_func(self::$instances[$name]); - } - - return self::$instances[$name]; - } - - - public function setLogMessage($message) - { - $this->logs[] = $message; - } - - - public function getLogMessages() - { - return $this->logs; - } - - - public function getConnection() - { - return $this->pdo; - } - - - public function closeConnection() - { - $this->pdo = null; - } - - - public function escapeIdentifier($value) - { - // Do not escape custom query - if (strpos($value, '.') !== false || strpos($value, ' ') !== false) { - return $value; - } - - return $this->pdo->escapeIdentifier($value); - } - - - public function execute($sql, array $values = array()) - { - try { - - $this->setLogMessage($sql); - $this->setLogMessage(implode(', ', $values)); - - $rq = $this->pdo->prepare($sql); - $rq->execute($values); - - return $rq; - } - catch (\PDOException $e) { - - if ($this->pdo->inTransaction()) $this->pdo->rollback(); - $this->setLogMessage($e->getMessage()); - return false; - } - } - - - public function startTransaction() - { - if (! $this->pdo->inTransaction()) { - $this->pdo->beginTransaction(); - } - } - - - public function closeTransaction() - { - if ($this->pdo->inTransaction()) { - $this->pdo->commit(); - } - } - - - public function cancelTransaction() - { - if ($this->pdo->inTransaction()) { - $this->pdo->rollback(); - } - } - - - public function table($table_name) - { - require_once __DIR__.'/Table.php'; - return new Table($this, $table_name); - } - - - public function schema() - { - require_once __DIR__.'/Schema.php'; - return new Schema($this); - } -}
\ No newline at end of file diff --git a/vendor/PicoDb/Drivers/Mysql.php b/vendor/PicoDb/Drivers/Mysql.php deleted file mode 100644 index 96148a1c..00000000 --- a/vendor/PicoDb/Drivers/Mysql.php +++ /dev/null @@ -1,75 +0,0 @@ -<?php - -namespace PicoDb; - -class Mysql extends \PDO { - - private $schema_table = 'schema_version'; - - - public function __construct(array $settings) - { - $required_atttributes = array( - 'hostname', - 'username', - 'password', - 'database', - 'charset', - ); - - foreach ($required_atttributes as $attribute) { - if (! isset($settings[$attribute])) { - throw new \LogicException('This configuration parameter is missing: "'.$attribute.'"'); - } - } - - $dsn = 'mysql:host='.$settings['hostname'].';dbname='.$settings['database']; - $options = array( - \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES '.$settings['charset'] - ); - - parent::__construct($dsn, $settings['username'], $settings['password'], $options); - - if (isset($settings['schema_table'])) { - $this->schema_table = $settings['schema_table']; - } - } - - - public function getSchemaVersion() - { - $this->exec("CREATE TABLE IF NOT EXISTS `".$this->schema_table."` (`version` INT DEFAULT '0')"); - - $rq = $this->prepare('SELECT `version` FROM `'.$this->schema_table.'`'); - $rq->execute(); - $result = $rq->fetch(\PDO::FETCH_ASSOC); - - if (isset($result['version'])) { - return (int) $result['version']; - } - else { - $this->exec('INSERT INTO `'.$this->schema_table.'` VALUES(0)'); - } - - return 0; - } - - - public function setSchemaVersion($version) - { - $rq = $this->prepare('UPDATE `'.$this->schema_table.'` SET `version`=?'); - $rq->execute(array($version)); - } - - - public function getLastId() - { - return $this->lastInsertId(); - } - - - public function escapeIdentifier($value) - { - return '`'.$value.'`'; - } -}
\ No newline at end of file diff --git a/vendor/PicoDb/Drivers/Postgres.php b/vendor/PicoDb/Drivers/Postgres.php deleted file mode 100644 index 641727f3..00000000 --- a/vendor/PicoDb/Drivers/Postgres.php +++ /dev/null @@ -1,73 +0,0 @@ -<?php - -namespace PicoDb; - -class Postgres extends \PDO { - - private $schema_table = 'schema_version'; - - - public function __construct(array $settings) - { - $required_atttributes = array( - 'hostname', - 'username', - 'password', - 'database', - ); - - foreach ($required_atttributes as $attribute) { - if (! isset($settings[$attribute])) { - throw new \LogicException('This configuration parameter is missing: "'.$attribute.'"'); - } - } - - $dsn = 'pgsql:host='.$settings['hostname'].';dbname='.$settings['database']; - - parent::__construct($dsn, $settings['username'], $settings['password']); - - if (isset($settings['schema_table'])) { - $this->schema_table = $settings['schema_table']; - } - } - - - public function getSchemaVersion() - { - $this->exec("CREATE TABLE IF NOT EXISTS ".$this->schema_table." (version SMALLINT DEFAULT 0)"); - - $rq = $this->prepare('SELECT version FROM '.$this->schema_table.''); - $rq->execute(); - $result = $rq->fetch(\PDO::FETCH_ASSOC); - - if (isset($result['version'])) { - return (int) $result['version']; - } - else { - $this->exec('INSERT INTO '.$this->schema_table.' VALUES(0)'); - } - - return 0; - } - - - public function setSchemaVersion($version) - { - $rq = $this->prepare('UPDATE '.$this->schema_table.' SET version=?'); - $rq->execute(array($version)); - } - - - public function getLastId() - { - $rq = $this->prepare('SELECT LASTVAL()'); - $rq->execute(); - return $rq->fetchColumn(); - } - - - public function escapeIdentifier($value) - { - return $value; - } -}
\ No newline at end of file diff --git a/vendor/PicoDb/Drivers/Sqlite.php b/vendor/PicoDb/Drivers/Sqlite.php deleted file mode 100644 index 38c823ae..00000000 --- a/vendor/PicoDb/Drivers/Sqlite.php +++ /dev/null @@ -1,56 +0,0 @@ -<?php - -namespace PicoDb; - -class Sqlite extends \PDO { - - - public function __construct(array $settings) - { - $required_atttributes = array( - 'filename', - ); - - foreach ($required_atttributes as $attribute) { - if (! isset($settings[$attribute])) { - throw new \LogicException('This configuration parameter is missing: "'.$attribute.'"'); - } - } - - parent::__construct('sqlite:'.$settings['filename']); - - $this->exec('PRAGMA foreign_keys = ON'); - } - - - public function getSchemaVersion() - { - $rq = $this->prepare('PRAGMA user_version'); - $rq->execute(); - $result = $rq->fetch(\PDO::FETCH_ASSOC); - - if (isset($result['user_version'])) { - return (int) $result['user_version']; - } - - return 0; - } - - - public function setSchemaVersion($version) - { - $this->exec('PRAGMA user_version='.$version); - } - - - public function getLastId() - { - return $this->lastInsertId(); - } - - - public function escapeIdentifier($value) - { - return '"'.$value.'"'; - } -}
\ No newline at end of file diff --git a/vendor/PicoDb/Schema.php b/vendor/PicoDb/Schema.php deleted file mode 100644 index a054ac09..00000000 --- a/vendor/PicoDb/Schema.php +++ /dev/null @@ -1,54 +0,0 @@ -<?php - -namespace PicoDb; - -class Schema -{ - protected $db = null; - - - public function __construct(Database $db) - { - $this->db = $db; - } - - - public function check($last_version = 1) - { - $current_version = $this->db->getConnection()->getSchemaVersion(); - - if ($current_version < $last_version) { - return $this->migrateTo($current_version, $last_version); - } - - return true; - } - - - public function migrateTo($current_version, $next_version) - { - try { - - $this->db->startTransaction(); - - for ($i = $current_version + 1; $i <= $next_version; $i++) { - - $function_name = '\Schema\version_'.$i; - - if (function_exists($function_name)) { - call_user_func($function_name, $this->db->getConnection()); - $this->db->getConnection()->setSchemaVersion($i); - } - } - - $this->db->closeTransaction(); - } - catch (\PDOException $e) { - $this->db->setLogMessage($function_name.' => '.$e->getMessage()); - $this->db->cancelTransaction(); - return false; - } - - return true; - } -}
\ No newline at end of file diff --git a/vendor/PicoDb/Table.php b/vendor/PicoDb/Table.php deleted file mode 100644 index 9c6bf4f9..00000000 --- a/vendor/PicoDb/Table.php +++ /dev/null @@ -1,444 +0,0 @@ -<?php - -namespace PicoDb; - -class Table -{ - const SORT_ASC = 'ASC'; - const SORT_DESC = 'DESC'; - - private $table_name = ''; - private $sql_limit = ''; - private $sql_offset = ''; - private $sql_order = ''; - private $joins = array(); - private $conditions = array(); - private $or_conditions = array(); - private $is_or_condition = false; - private $columns = array(); - private $values = array(); - private $distinct = false; - private $group_by = array(); - - private $db; - - - public function __construct(Database $db, $table_name) - { - $this->db = $db; - $this->table_name = $table_name; - - return $this; - } - - - public function save(array $data) - { - if (! empty($this->conditions)) { - - return $this->update($data); - } - else { - - return $this->insert($data); - } - } - - /** - * Update - * - * Note: Do not use `rowCount()` the behaviour is different across drivers - */ - public function update(array $data) - { - $columns = array(); - $values = array(); - - foreach ($data as $column => $value) { - - $columns[] = $this->db->escapeIdentifier($column).'=?'; - $values[] = $value; - } - - foreach ($this->values as $value) { - - $values[] = $value; - } - - $sql = sprintf( - 'UPDATE %s SET %s %s', - $this->db->escapeIdentifier($this->table_name), - implode(', ', $columns), - $this->conditions() - ); - - $result = $this->db->execute($sql, $values); - - return $result !== false; - } - - - public function insert(array $data) - { - $columns = array(); - - foreach ($data as $column => $value) { - - $columns[] = $this->db->escapeIdentifier($column); - } - - $sql = sprintf( - 'INSERT INTO %s (%s) VALUES (%s)', - $this->db->escapeIdentifier($this->table_name), - implode(', ', $columns), - implode(', ', array_fill(0, count($data), '?')) - ); - - return false !== $this->db->execute($sql, array_values($data)); - } - - - public function remove() - { - $sql = sprintf( - 'DELETE FROM %s %s', - $this->db->escapeIdentifier($this->table_name), - $this->conditions() - ); - - $result = $this->db->execute($sql, $this->values); - - return $result !== false && $result->rowCount() > 0; - } - - - public function listing($key, $value) - { - $this->columns($key, $value); - - $listing = array(); - $results = $this->findAll(); - - if ($results) { - - foreach ($results as $result) { - - $listing[$result[$key]] = $result[$value]; - } - } - - return $listing; - } - - - public function findAll() - { - $rq = $this->db->execute($this->buildSelectQuery(), $this->values); - if (false === $rq) return false; - - return $rq->fetchAll(\PDO::FETCH_ASSOC); - } - - - public function findAllByColumn($column) - { - $this->columns = array($column); - $rq = $this->db->execute($this->buildSelectQuery(), $this->values); - if (false === $rq) return false; - - return $rq->fetchAll(\PDO::FETCH_COLUMN, 0); - } - - - public function findOne() - { - $this->limit(1); - $result = $this->findAll(); - - return isset($result[0]) ? $result[0] : null; - } - - - public function findOneColumn($column) - { - $this->limit(1); - $this->columns = array($column); - - $rq = $this->db->execute($this->buildSelectQuery(), $this->values); - if (false === $rq) return false; - - return $rq->fetchColumn(); - } - - - public function buildSelectQuery() - { - foreach ($this->columns as $key => $value) { - $this->columns[$key] = $this->db->escapeIdentifier($value); - } - - return sprintf( - 'SELECT %s %s FROM %s %s %s %s %s %s %s', - $this->distinct ? 'DISTINCT' : '', - empty($this->columns) ? '*' : implode(', ', $this->columns), - $this->db->escapeIdentifier($this->table_name), - implode(' ', $this->joins), - $this->conditions(), - empty($this->group_by) ? '' : 'GROUP BY '.implode(', ', $this->group_by), - $this->sql_order, - $this->sql_limit, - $this->sql_offset - ); - } - - - public function count() - { - $sql = sprintf( - 'SELECT COUNT(*) FROM %s'.$this->conditions().$this->sql_order.$this->sql_limit.$this->sql_offset, - $this->db->escapeIdentifier($this->table_name) - ); - - $rq = $this->db->execute($sql, $this->values); - if (false === $rq) return false; - - $result = $rq->fetchColumn(); - return $result ? (int) $result : 0; - } - - - public function join($table, $foreign_column, $local_column) - { - $this->joins[] = sprintf( - 'LEFT JOIN %s ON %s=%s', - $this->db->escapeIdentifier($table), - $this->db->escapeIdentifier($table).'.'.$this->db->escapeIdentifier($foreign_column), - $this->db->escapeIdentifier($this->table_name).'.'.$this->db->escapeIdentifier($local_column) - ); - - return $this; - } - - - public function conditions() - { - if (! empty($this->conditions)) { - - return ' WHERE '.implode(' AND ', $this->conditions); - } - else { - - return ''; - } - } - - - public function addCondition($sql) - { - if ($this->is_or_condition) { - - $this->or_conditions[] = $sql; - } - else { - - $this->conditions[] = $sql; - } - } - - - public function beginOr() - { - $this->is_or_condition = true; - $this->or_conditions = array(); - - return $this; - } - - - public function closeOr() - { - $this->is_or_condition = false; - - if (! empty($this->or_conditions)) { - - $this->conditions[] = '('.implode(' OR ', $this->or_conditions).')'; - } - - return $this; - } - - - public function orderBy($column, $order = self::SORT_ASC) - { - $order = strtoupper($order); - $order = $order === self::SORT_ASC || $order === self::SORT_DESC ? $order : self::SORT_ASC; - - if ($this->sql_order === '') { - $this->sql_order = ' ORDER BY '.$this->db->escapeIdentifier($column).' '.$order; - } - else { - $this->sql_order .= ', '.$this->db->escapeIdentifier($column).' '.$order; - } - - return $this; - } - - - public function asc($column) - { - if ($this->sql_order === '') { - $this->sql_order = ' ORDER BY '.$this->db->escapeIdentifier($column).' '.self::SORT_ASC; - } - else { - $this->sql_order .= ', '.$this->db->escapeIdentifier($column).' '.self::SORT_ASC; - } - - return $this; - } - - - public function desc($column) - { - if ($this->sql_order === '') { - $this->sql_order = ' ORDER BY '.$this->db->escapeIdentifier($column).' '.self::SORT_DESC; - } - else { - $this->sql_order .= ', '.$this->db->escapeIdentifier($column).' '.self::SORT_DESC; - } - - return $this; - } - - - public function limit($value) - { - if (! is_null($value)) $this->sql_limit = ' LIMIT '.(int) $value; - return $this; - } - - - public function offset($value) - { - if (! is_null($value)) $this->sql_offset = ' OFFSET '.(int) $value; - return $this; - } - - - public function groupBy() - { - $this->group_by = \func_get_args(); - return $this; - } - - - public function columns() - { - $this->columns = \func_get_args(); - return $this; - } - - - public function distinct() - { - $this->columns = \func_get_args(); - $this->distinct = true; - return $this; - } - - - public function __call($name, array $arguments) - { - $column = $arguments[0]; - $sql = ''; - - switch (strtolower($name)) { - - case 'in': - if (isset($arguments[1]) && is_array($arguments[1]) && ! empty($arguments[1])) { - - $sql = sprintf( - '%s IN (%s)', - $this->db->escapeIdentifier($column), - implode(', ', array_fill(0, count($arguments[1]), '?')) - ); - } - break; - - case 'notin': - if (isset($arguments[1]) && is_array($arguments[1]) && ! empty($arguments[1])) { - - $sql = sprintf( - '%s NOT IN (%s)', - $this->db->escapeIdentifier($column), - implode(', ', array_fill(0, count($arguments[1]), '?')) - ); - } - break; - - case 'like': - $sql = sprintf('%s LIKE ?', $this->db->escapeIdentifier($column)); - break; - - case 'eq': - case 'equal': - case 'equals': - $sql = sprintf('%s = ?', $this->db->escapeIdentifier($column)); - break; - - case 'neq': - case 'notequal': - case 'notequals': - $sql = sprintf('%s != ?', $this->db->escapeIdentifier($column)); - break; - - case 'gt': - case 'greaterthan': - $sql = sprintf('%s > ?', $this->db->escapeIdentifier($column)); - break; - - case 'lt': - case 'lowerthan': - $sql = sprintf('%s < ?', $this->db->escapeIdentifier($column)); - break; - - case 'gte': - case 'greaterthanorequals': - $sql = sprintf('%s >= ?', $this->db->escapeIdentifier($column)); - break; - - case 'lte': - case 'lowerthanorequals': - $sql = sprintf('%s <= ?', $this->db->escapeIdentifier($column)); - break; - - case 'isnull': - $sql = sprintf('%s IS NULL', $this->db->escapeIdentifier($column)); - break; - - case 'notnull': - $sql = sprintf('%s IS NOT NULL', $this->db->escapeIdentifier($column)); - break; - } - - if ($sql !== '') { - - $this->addCondition($sql); - - if (isset($arguments[1])) { - - if (is_array($arguments[1])) { - - foreach ($arguments[1] as $value) { - $this->values[] = $value; - } - } - else { - - $this->values[] = $arguments[1]; - } - } - } - - return $this; - } -} |