From 58bba6ea686b3e047c339fa70b903060c535c2d7 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sun, 19 Nov 2017 14:29:23 +0100 Subject: [PATCH] Rename app in SMSq and use composer --- .gitignore | 1 + composer.json | 6 + includes/config.inc.php | 6 +- includes/core.php | 5 +- includes/db.php | 1 - vendor/.gitignore | 1 + vendor/FluentPDO/BaseQuery.php | 361 --------------------------- vendor/FluentPDO/CommonQuery.php | 290 --------------------- vendor/FluentPDO/DeleteQuery.php | 94 ------- vendor/FluentPDO/FluentLiteral.php | 30 --- vendor/FluentPDO/FluentPDO.php | 164 ------------ vendor/FluentPDO/FluentStructure.php | 60 ----- vendor/FluentPDO/FluentUtils.php | 86 ------- vendor/FluentPDO/InsertQuery.php | 221 ---------------- vendor/FluentPDO/SelectQuery.php | 194 -------------- vendor/FluentPDO/UpdateQuery.php | 107 -------- 16 files changed, 12 insertions(+), 1615 deletions(-) create mode 100644 composer.json create mode 100644 vendor/.gitignore delete mode 100644 vendor/FluentPDO/BaseQuery.php delete mode 100644 vendor/FluentPDO/CommonQuery.php delete mode 100644 vendor/FluentPDO/DeleteQuery.php delete mode 100644 vendor/FluentPDO/FluentLiteral.php delete mode 100644 vendor/FluentPDO/FluentPDO.php delete mode 100644 vendor/FluentPDO/FluentStructure.php delete mode 100644 vendor/FluentPDO/FluentUtils.php delete mode 100644 vendor/FluentPDO/InsertQuery.php delete mode 100644 vendor/FluentPDO/SelectQuery.php delete mode 100644 vendor/FluentPDO/UpdateQuery.php diff --git a/.gitignore b/.gitignore index 4c5f88a..357fc74 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *~ .*.swp +composer.lock diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..932f998 --- /dev/null +++ b/composer.json @@ -0,0 +1,6 @@ +{ + "require": { + "corneltek/getoptionkit": "^2.6", + "envms/fluentpdo": "^1.1" + } +} diff --git a/includes/config.inc.php b/includes/config.inc.php index 0b11c67..da5de1b 100644 --- a/includes/config.inc.php +++ b/includes/config.inc.php @@ -18,9 +18,9 @@ $smsgw_ssl_verify=true; // Database // Example for MySQL : -$db_dsn="mysql:host=localhost;dbname=smsgw"; -$db_user="smsgw"; -$db_pwd="smsgw"; +$db_dsn="mysql:host=localhost;dbname=smsq"; +$db_user="smsq"; +$db_pwd="smsq"; $db_options=array(); // FluentPDO path diff --git a/includes/core.php b/includes/core.php index 8650bdd..c89ae45 100644 --- a/includes/core.php +++ b/includes/core.php @@ -11,10 +11,7 @@ set_include_path($root_dir_path.'/includes' . PATH_SEPARATOR . $root_dir_path.'/ require_once('config.inc.php'); -// Check $public_root_url end -if (substr($public_root_url, -1)=='/') { - $public_root_url=substr($public_root_url, 0, -1); -} +require $root_dir_path . '/vendor/autoload.php'; require_once('logging.php'); require_once('functions.php'); diff --git a/includes/db.php b/includes/db.php index 7a4ba6c..68b8dc3 100644 --- a/includes/db.php +++ b/includes/db.php @@ -1,7 +1,6 @@ debug = function ($q) { diff --git a/vendor/.gitignore b/vendor/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/vendor/.gitignore @@ -0,0 +1 @@ +* diff --git a/vendor/FluentPDO/BaseQuery.php b/vendor/FluentPDO/BaseQuery.php deleted file mode 100644 index ae1c4dd..0000000 --- a/vendor/FluentPDO/BaseQuery.php +++ /dev/null @@ -1,361 +0,0 @@ -fpdo = $fpdo; - $this->clauses = $clauses; - $this->initClauses(); - } - - /** - * Initialize statement and parameter clauses. - */ - private function initClauses() { - foreach ($this->clauses as $clause => $value) { - if ($value) { - $this->statements[$clause] = array(); - $this->parameters[$clause] = array(); - } else { - $this->statements[$clause] = null; - $this->parameters[$clause] = null; - } - } - } - - /** - * Add statement for all kind of clauses - * - * @param $clause - * @param $statement - * @param array $parameters - * - * @return $this - */ - protected function addStatement($clause, $statement, $parameters = array()) { - if ($statement === null) { - return $this->resetClause($clause); - } - // $statement !== null - if ($this->clauses[$clause]) { - if (is_array($statement)) { - $this->statements[$clause] = array_merge($this->statements[$clause], $statement); - } else { - $this->statements[$clause][] = $statement; - } - $this->parameters[$clause] = array_merge($this->parameters[$clause], $parameters); - } else { - $this->statements[$clause] = $statement; - $this->parameters[$clause] = $parameters; - } - - return $this; - } - - /** - * Remove all prev defined statements - * - * @param $clause - * - * @return $this - */ - protected function resetClause($clause) { - $this->statements[$clause] = null; - $this->parameters[$clause] = array(); - if (isset($this->clauses[$clause]) && $this->clauses[$clause]) { - $this->statements[$clause] = array(); - } - - return $this; - } - - /** - * Implements method from IteratorAggregate - * - * @return \PDOStatement - */ - public function getIterator() { - return $this->execute(); - } - - /** - * Execute query with earlier added parameters - * - * @return \PDOStatement - */ - public function execute() { - $query = $this->buildQuery(); - $parameters = $this->buildParameters(); - - $result = $this->fpdo->getPdo()->prepare($query); - - // At this point, $result is a PDOStatement instance, or false. - // PDO::prepare() does not reliably return errors. Some database drivers - // do not support prepared statements, and PHP emulates them. Postgresql - // does support prepared statements, but PHP does not call Postgresql's - // prepare function until we call PDOStatement::execute() below. - // If PDO::prepare() worked properly, this is where we would check - // for prepare errors, such as invalid SQL. - - if ($this->object !== false) { - if (class_exists($this->object)) { - $result->setFetchMode(PDO::FETCH_CLASS, $this->object); - } else { - $result->setFetchMode(PDO::FETCH_OBJ); - } - } elseif ($this->fpdo->getPdo()->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE) == PDO::FETCH_BOTH) { - $result->setFetchMode(PDO::FETCH_ASSOC); - } - - $time = microtime(true); - if ($result && $result->execute($parameters)) { - $this->time = microtime(true) - $time; - } else { - $result = false; - } - - $this->result = $result; - $this->debugger(); - - return $result; - } - - /** - * Echo/pass a debug string - */ - private function debugger() { - if ($this->fpdo->debug) { - if (!is_callable($this->fpdo->debug)) { - $backtrace = ''; - $query = $this->getQuery(); - $parameters = $this->getParameters(); - $debug = ''; - if ($parameters) { - $debug = '# parameters: ' . implode(', ', array_map(array($this, 'quote'), $parameters)) . "\n"; - } - $debug .= $query; - $pattern = '(^' . preg_quote(__DIR__) . '(\\.php$|[/\\\\]))'; // can be static - foreach (debug_backtrace() as $backtrace) { - if (isset($backtrace['file']) && !preg_match($pattern, $backtrace['file'])) { - // stop on first file outside FluentPDO source codes - break; - } - } - $time = sprintf('%0.3f', $this->time * 1000) . ' ms'; - $rows = ($this->result) ? $this->result->rowCount() : 0; - $finalString = "# $backtrace[file]:$backtrace[line] ($time; rows = $rows)\n$debug\n\n"; - if (is_resource(STDERR)) { // if STDERR is set, send there, otherwise just output the string - fwrite(STDERR, $finalString); - } - else { - echo $finalString; - } - } else { - call_user_func($this->fpdo->debug, $this); - } - } - } - - /** - * @return \PDO - */ - protected function getPDO() { - return $this->fpdo->getPdo(); - } - - /** - * @return \FluentStructure - */ - protected function getStructure() { - return $this->fpdo->getStructure(); - } - - /** - * Get PDOStatement result - * - * @return \PDOStatement - */ - public function getResult() { - return $this->result; - } - - /** - * Get time of execution - * - * @return float - */ - public function getTime() { - return $this->time; - } - - /** - * Get query parameters - * - * @return array - */ - public function getParameters() { - return $this->buildParameters(); - } - - /** - * Get query string - * - * @param bool $formatted - Return formatted query - * - * @return string - */ - public function getQuery($formatted = true) { - $query = $this->buildQuery(); - if ($formatted) { - $query = FluentUtils::formatQuery($query); - } - - return $query; - } - - /** - * Generate query - * - * @return string - * @throws Exception - */ - protected function buildQuery() { - $query = ''; - foreach ($this->clauses as $clause => $separator) { - if ($this->clauseNotEmpty($clause)) { - if (is_string($separator)) { - $query .= " $clause " . implode($separator, $this->statements[$clause]); - } elseif ($separator === null) { - $query .= " $clause " . $this->statements[$clause]; - } elseif (is_callable($separator)) { - $query .= call_user_func($separator); - } else { - throw new Exception("Clause '$clause' is incorrectly set to '$separator'."); - } - } - } - - return trim($query); - } - - /** - * @param $clause - * - * @return bool - */ - private function clauseNotEmpty($clause) { - if ($this->clauses[$clause]) { - return (boolean)count($this->statements[$clause]); - } else { - return (boolean)$this->statements[$clause]; - } - } - - /** - * @return array - */ - protected function buildParameters() { - $parameters = array(); - foreach ($this->parameters as $clauses) { - if (is_array($clauses)) { - foreach ($clauses as $value) { - if (is_array($value) && is_string(key($value)) && substr(key($value), 0, 1) == ':') { - // this is named params e.g. (':name' => 'Mark') - $parameters = array_merge($parameters, $value); - } else { - $parameters[] = $value; - } - } - } else { - if ($clauses) { - $parameters[] = $clauses; - } - } - } - - return $parameters; - } - - /** - * @param $value - * - * @return string - */ - protected function quote($value) { - if (!isset($value)) { - return "NULL"; - } - if (is_array($value)) { // (a, b) IN ((1, 2), (3, 4)) - return "(" . implode(", ", array_map(array($this, 'quote'), $value)) . ")"; - } - $value = $this->formatValue($value); - if (is_float($value)) { - return sprintf("%F", $value); // otherwise depends on setlocale() - } - if ($value === false) { - return "0"; - } - if (is_int($value) || $value instanceof FluentLiteral) { // number or SQL code - for example "NOW()" - return (string)$value; - } - - return $this->fpdo->getPdo()->quote($value); - } - - /** - * @param \DateTime $val - * - * @return string - */ - private function formatValue($val) { - if ($val instanceof DateTime) { - return $val->format("Y-m-d H:i:s"); //! may be driver specific - } - - return $val; - } - - /** - * Select an item as object - * - * @param boolean|object $object If set to true, items are returned as stdClass, otherwise a class - * name can be passed and a new instance of this class is return. - * Can be set to false to return items as an associative array. - * - * @return \BaseQuery - */ - public function asObject($object = true) { - $this->object = $object; - - return $this; - } - -} diff --git a/vendor/FluentPDO/CommonQuery.php b/vendor/FluentPDO/CommonQuery.php deleted file mode 100644 index 1c4f2ee..0000000 --- a/vendor/FluentPDO/CommonQuery.php +++ /dev/null @@ -1,290 +0,0 @@ -isSmartJoinEnabled = true; - - return $this; - } - - /** - * @return $this - */ - public function disableSmartJoin() { - $this->isSmartJoinEnabled = false; - - return $this; - } - - /** - * @return bool - */ - public function isSmartJoinEnabled() { - return $this->isSmartJoinEnabled; - } - - /** - * Add where condition, more calls appends with AND - * - * @param string $condition possibly containing ? or :name (PDO syntax) - * @param mixed $parameters array or a scalar value - * - * @return \SelectQuery - */ - public function where($condition, $parameters = array()) { - if ($condition === null) { - return $this->resetClause('WHERE'); - } - if (!$condition) { - return $this; - } - if (is_array($condition)) { // where(array("column1" => 1, "column2 > ?" => 2)) - foreach ($condition as $key => $val) { - $this->where($key, $val); - } - - return $this; - } - $args = func_get_args(); - if (count($args) == 1) { - return $this->addStatement('WHERE', $condition); - } - - // check that there are 2 arguments, a condition and a parameter value - // if the condition contains a parameter simply add them - // since its up to the user if it's valid sql or not - // Otherwise we're probably with just an identifier. So lets - // construct a new condition based on the passed parameter value. - if (count($args) == 2 && !preg_match('/(\?|:\w+)/i', $condition)) { - // condition is column only - if (is_null($parameters)) { - return $this->addStatement('WHERE', "$condition is NULL"); - } elseif ($args[1] === array()) { - return $this->addStatement('WHERE', 'FALSE'); - } elseif (is_array($args[1])) { - $in = $this->quote($args[1]); - - return $this->addStatement('WHERE', "$condition IN $in"); - } - $condition = "$condition = ?"; - } - array_shift($args); - - return $this->addStatement('WHERE', $condition, $args); - } - - /** - * @param $clause - * @param array $parameters - first is $statement followed by $parameters - * - * @return $this|\SelectQuery - */ - public function __call($clause, $parameters = array()) { - $clause = FluentUtils::toUpperWords($clause); - if ($clause == 'GROUP') { - $clause = 'GROUP BY'; - } - if ($clause == 'ORDER') { - $clause = 'ORDER BY'; - } - if ($clause == 'FOOT NOTE') { - $clause = "\n--"; - } - $statement = array_shift($parameters); - if (strpos($clause, 'JOIN') !== false) { - return $this->addJoinStatements($clause, $statement, $parameters); - } - - return $this->addStatement($clause, $statement, $parameters); - } - - /** - * @return string - */ - protected function getClauseJoin() { - return implode(' ', $this->statements['JOIN']); - } - - /** - * Statement can contain more tables (e.g. "table1.table2:table3:") - * - * @param $clause - * @param $statement - * @param array $parameters - * - * @return $this|\SelectQuery - */ - private function addJoinStatements($clause, $statement, $parameters = array()) { - if ($statement === null) { - $this->joins = array(); - - return $this->resetClause('JOIN'); - } - if (array_search(substr($statement, 0, -1), $this->joins) !== false) { - return $this; - } - - // match "tables AS alias" - preg_match('/`?([a-z_][a-z0-9_\.:]*)`?(\s+AS)?(\s+`?([a-z_][a-z0-9_]*)`?)?/i', $statement, $matches); - $joinAlias = ''; - $joinTable = ''; - if ($matches) { - $joinTable = $matches[1]; - if (isset($matches[4]) && !in_array(strtoupper($matches[4]), array('ON', 'USING'))) { - $joinAlias = $matches[4]; - } - } - - if (strpos(strtoupper($statement), ' ON ') || strpos(strtoupper($statement), ' USING')) { - if (!$joinAlias) { - $joinAlias = $joinTable; - } - if (in_array($joinAlias, $this->joins)) { - return $this; - } else { - $this->joins[] = $joinAlias; - $statement = " $clause $statement"; - - return $this->addStatement('JOIN', $statement, $parameters); - } - } - - // $joinTable is list of tables for join e.g.: table1.table2:table3.... - if (!in_array(substr($joinTable, -1), array('.', ':'))) { - $joinTable .= '.'; - } - - preg_match_all('/([a-z_][a-z0-9_]*[\.:]?)/i', $joinTable, $matches); - $mainTable = ''; - if (isset($this->statements['FROM'])) { - $mainTable = $this->statements['FROM']; - } elseif (isset($this->statements['UPDATE'])) { - $mainTable = $this->statements['UPDATE']; - } - $lastItem = array_pop($matches[1]); - array_push($matches[1], $lastItem); - foreach ($matches[1] as $joinItem) { - if ($mainTable == substr($joinItem, 0, -1)) { - continue; - } - - // use $joinAlias only for $lastItem - $alias = ''; - if ($joinItem == $lastItem) { - $alias = $joinAlias; - } - - $newJoin = $this->createJoinStatement($clause, $mainTable, $joinItem, $alias); - if ($newJoin) { - $this->addStatement('JOIN', $newJoin, $parameters); - } - $mainTable = $joinItem; - } - - return $this; - } - - /** - * Create join string - * - * @param $clause - * @param $mainTable - * @param $joinTable - * @param string $joinAlias - * - * @return string - */ - private function createJoinStatement($clause, $mainTable, $joinTable, $joinAlias = '') { - if (in_array(substr($mainTable, -1), array(':', '.'))) { - $mainTable = substr($mainTable, 0, -1); - } - $referenceDirection = substr($joinTable, -1); - $joinTable = substr($joinTable, 0, -1); - $asJoinAlias = ''; - if ($joinAlias) { - $asJoinAlias = " AS $joinAlias"; - } else { - $joinAlias = $joinTable; - } - if (in_array($joinAlias, $this->joins)) { - // if join exists don't create same again - return ''; - } else { - $this->joins[] = $joinAlias; - } - if ($referenceDirection == ':') { - // back reference - $primaryKey = $this->getStructure()->getPrimaryKey($mainTable); - $foreignKey = $this->getStructure()->getForeignKey($mainTable); - - return " $clause $joinTable$asJoinAlias ON $joinAlias.$foreignKey = $mainTable.$primaryKey"; - } else { - $primaryKey = $this->getStructure()->getPrimaryKey($joinTable); - $foreignKey = $this->getStructure()->getForeignKey($joinTable); - - return " $clause $joinTable$asJoinAlias ON $joinAlias.$primaryKey = $mainTable.$foreignKey"; - } - } - - /** - * @return string - */ - protected function buildQuery() { - // first create extra join from statements with columns with referenced tables - $statementsWithReferences = array('WHERE', 'SELECT', 'GROUP BY', 'ORDER BY'); - foreach ($statementsWithReferences as $clause) { - if (array_key_exists($clause, $this->statements)) { - $this->statements[$clause] = array_map(array($this, 'createUndefinedJoins'), $this->statements[$clause]); - } - } - - return parent::buildQuery(); - } - - /** - * Create undefined joins from statement with column with referenced tables - * - * @param string $statement - * - * @return string rewrited $statement (e.g. tab1.tab2:col => tab2.col) - */ - private function createUndefinedJoins($statement) { - if (!$this->isSmartJoinEnabled) { - return $statement; - } - - preg_match_all('/\\b([a-z_][a-z0-9_.:]*[.:])[a-z_]*/i', $statement, $matches); - foreach ($matches[1] as $join) { - if (!in_array(substr($join, 0, -1), $this->joins)) { - $this->addJoinStatements('LEFT JOIN', $join); - } - } - - // don't rewrite table from other databases - foreach ($this->joins as $join) { - if (strpos($join, '.') !== false && strpos($statement, $join) === 0) { - return $statement; - } - } - - // remove extra referenced tables (rewrite tab1.tab2:col => tab2.col) - $statement = preg_replace('/(?:\\b[a-z_][a-z0-9_.:]*[.:])?([a-z_][a-z0-9_]*)[.:]([a-z_*])/i', '\\1.\\2', $statement); - - return $statement; - } - -} diff --git a/vendor/FluentPDO/DeleteQuery.php b/vendor/FluentPDO/DeleteQuery.php deleted file mode 100644 index f3a133d..0000000 --- a/vendor/FluentPDO/DeleteQuery.php +++ /dev/null @@ -1,94 +0,0 @@ - array($this, 'getClauseDeleteFrom'), - 'DELETE' => array($this, 'getClauseDelete'), - 'FROM' => null, - 'JOIN' => array($this, 'getClauseJoin'), - 'WHERE' => ' AND ', - 'ORDER BY' => ', ', - 'LIMIT' => null, - ); - - parent::__construct($fpdo, $clauses); - - $this->statements['DELETE FROM'] = $table; - $this->statements['DELETE'] = $table; - } - - /** - * Forces delete operation to fail silently - * - * @return \DeleteQuery - */ - public function ignore() { - $this->ignore = true; - - return $this; - } - - /** - * @return string - */ - protected function buildQuery() { - if ($this->statements['FROM']) { - unset($this->clauses['DELETE FROM']); - } else { - unset($this->clauses['DELETE']); - } - - return parent::buildQuery(); - } - - /** - * Execute DELETE query - * - * @return bool - */ - public function execute() { - $result = parent::execute(); - if ($result) { - return $result->rowCount(); - } - - return false; - } - - /** - * @return string - */ - protected function getClauseDelete() { - return 'DELETE' . ($this->ignore ? " IGNORE" : '') . ' ' . $this->statements['DELETE']; - } - - /** - * @return string - */ - protected function getClauseDeleteFrom() { - return 'DELETE' . ($this->ignore ? " IGNORE" : '') . ' FROM ' . $this->statements['DELETE FROM']; - } - -} diff --git a/vendor/FluentPDO/FluentLiteral.php b/vendor/FluentPDO/FluentLiteral.php deleted file mode 100644 index ccfd199..0000000 --- a/vendor/FluentPDO/FluentLiteral.php +++ /dev/null @@ -1,30 +0,0 @@ -value = $value; - } - - /** - * Get literal value - * - * @return string - */ - function __toString() { - return $this->value; - } - -} diff --git a/vendor/FluentPDO/FluentPDO.php b/vendor/FluentPDO/FluentPDO.php deleted file mode 100644 index 02e83e9..0000000 --- a/vendor/FluentPDO/FluentPDO.php +++ /dev/null @@ -1,164 +0,0 @@ -pdo = $pdo; - if (!$structure) { - $structure = new FluentStructure(); - } - $this->structure = $structure; - } - - /** - * Create SELECT query from $table - * - * @param string $table - db table name - * @param integer $primaryKey - return one row by primary key - * - * @return \SelectQuery - */ - public function from($table, $primaryKey = null) { - $query = new SelectQuery($this, $table); - if ($primaryKey !== null) { - $tableTable = $query->getFromTable(); - $tableAlias = $query->getFromAlias(); - $primaryKeyName = $this->structure->getPrimaryKey($tableTable); - $query = $query->where("$tableAlias.$primaryKeyName", $primaryKey); - } - - return $query; - } - - /** - * Create INSERT INTO query - * - * @param string $table - * @param array $values - accepts one or multiple rows, @see docs - * - * @return \InsertQuery - */ - public function insertInto($table, $values = array()) { - $query = new InsertQuery($this, $table, $values); - - return $query; - } - - /** - * Create UPDATE query - * - * @param string $table - * @param array|string $set - * @param string $primaryKey - * - * @return \UpdateQuery - */ - public function update($table, $set = array(), $primaryKey = null) { - $query = new UpdateQuery($this, $table); - $query->set($set); - if ($primaryKey) { - $primaryKeyName = $this->getStructure()->getPrimaryKey($table); - $query = $query->where($primaryKeyName, $primaryKey); - } - - return $query; - } - - /** - * Create DELETE query - * - * @param string $table - * @param string $primaryKey delete only row by primary key - * - * @return \DeleteQuery - */ - public function delete($table, $primaryKey = null) { - $query = new DeleteQuery($this, $table); - if ($primaryKey) { - $primaryKeyName = $this->getStructure()->getPrimaryKey($table); - $query = $query->where($primaryKeyName, $primaryKey); - } - - return $query; - } - - /** - * Create DELETE FROM query - * - * @param string $table - * @param string $primaryKey - * - * @return \DeleteQuery - */ - public function deleteFrom($table, $primaryKey = null) { - $args = func_get_args(); - - return call_user_func_array(array($this, 'delete'), $args); - } - - /** - * @return \PDO - */ - public function getPdo() { - return $this->pdo; - } - - /** - * @return \FluentStructure - */ - public function getStructure() { - return $this->structure; - } - - /** - * Closes the \PDO connection to the database - * - * @return null - */ - public function close() { - $this->pdo = null; - } - -} diff --git a/vendor/FluentPDO/FluentStructure.php b/vendor/FluentPDO/FluentStructure.php deleted file mode 100644 index acac327..0000000 --- a/vendor/FluentPDO/FluentStructure.php +++ /dev/null @@ -1,60 +0,0 @@ -primaryKey = $primaryKey; - $this->foreignKey = $foreignKey; - } - - /** - * @param string $table - * - * @return string - */ - public function getPrimaryKey($table) { - return $this->key($this->primaryKey, $table); - } - - /** - * @param string $table - * - * @return string - */ - public function getForeignKey($table) { - return $this->key($this->foreignKey, $table); - } - - /** - * @param string|callback $key - * @param string $table - * - * @return string - */ - private function key($key, $table) { - if (is_callable($key)) { - return $key($table); - } - - return sprintf($key, $table); - } - -} diff --git a/vendor/FluentPDO/FluentUtils.php b/vendor/FluentPDO/FluentUtils.php deleted file mode 100644 index a4ae98b..0000000 --- a/vendor/FluentPDO/FluentUtils.php +++ /dev/null @@ -1,86 +0,0 @@ -getColumnMeta($i); $i++) - { - $type = $columnMeta['native_type']; - - switch($type) - { - case 'DECIMAL': - case 'TINY': - case 'SHORT': - case 'LONG': - case 'LONGLONG': - case 'INT24': - if(isset($rows[$columnMeta['name']])){ - $rows[$columnMeta['name']] = $rows[$columnMeta['name']] + 0; - }else{ - if(is_array($rows) || $rows instanceof Traversable){ - foreach($rows as &$row){ - if(isset($row[$columnMeta['name']])){ - $row[$columnMeta['name']] = $row[$columnMeta['name']] + 0; - } - } - } - } - break; - case 'DATETIME': - case 'DATE': - case 'TIMESTAMP': - // convert to date type? - break; - // default: keep as string - } - } - return $rows; - } - -} diff --git a/vendor/FluentPDO/InsertQuery.php b/vendor/FluentPDO/InsertQuery.php deleted file mode 100644 index d7f4552..0000000 --- a/vendor/FluentPDO/InsertQuery.php +++ /dev/null @@ -1,221 +0,0 @@ - array($this, 'getClauseInsertInto'), - 'VALUES' => array($this, 'getClauseValues'), - 'ON DUPLICATE KEY UPDATE' => array($this, 'getClauseOnDuplicateKeyUpdate'), - ); - parent::__construct($fpdo, $clauses); - - $this->statements['INSERT INTO'] = $table; - $this->values($values); - } - - /** - * Execute insert query - * - * @param mixed $sequence - * - * @return integer last inserted id or false - */ - public function execute($sequence = null) { - $result = parent::execute(); - if ($result) { - return $this->getPDO()->lastInsertId($sequence); - } - - return false; - } - - /** - * Add ON DUPLICATE KEY UPDATE - * - * @param array $values - * - * @return \InsertQuery - */ - public function onDuplicateKeyUpdate($values) { - $this->statements['ON DUPLICATE KEY UPDATE'] = array_merge( - $this->statements['ON DUPLICATE KEY UPDATE'], $values - ); - - return $this; - } - - /** - * Add VALUES - * - * @param $values - * - * @return \InsertQuery - * @throws Exception - */ - public function values($values) { - if (!is_array($values)) { - throw new Exception('Param VALUES for INSERT query must be array'); - } - $first = current($values); - if (is_string(key($values))) { - // is one row array - $this->addOneValue($values); - } elseif (is_array($first) && is_string(key($first))) { - // this is multi values - foreach ($values as $oneValue) { - $this->addOneValue($oneValue); - } - } - - return $this; - } - - /** - * Force insert operation to fail silently - * - * @return \InsertQuery - */ - public function ignore() { - $this->ignore = true; - - return $this; - } - - /** Force insert operation delay support - * - * @return \InsertQuery - */ - public function delayed() { - $this->delayed = true; - - return $this; - } - - /** - * @return string - */ - protected function getClauseInsertInto() { - return 'INSERT' . ($this->ignore ? " IGNORE" : '') . ($this->delayed ? " DELAYED" : '') . ' INTO ' . $this->statements['INSERT INTO']; - } - - /** - * @param $param - * - * @return string - */ - protected function parameterGetValue($param) { - return $param instanceof FluentLiteral ? (string)$param : '?'; - } - - /** - * @return string - */ - protected function getClauseValues() { - $valuesArray = array(); - foreach ($this->statements['VALUES'] as $rows) { - // literals should not be parametrized. - // They are commonly used to call engine functions or literals. - // Eg: NOW(), CURRENT_TIMESTAMP etc - $placeholders = array_map(array($this, 'parameterGetValue'), $rows); - $valuesArray[] = '(' . implode(', ', $placeholders) . ')'; - } - - $columns = implode(', ', $this->columns); - $values = implode(', ', $valuesArray); - - return " ($columns) VALUES $values"; - } - - /** - * Removes all FluentLiteral instances from the argument - * since they are not to be used as PDO parameters but rather injected directly into the query - * - * @param $statements - * - * @return array - */ - protected function filterLiterals($statements) { - $f = function ($item) { - return !$item instanceof FluentLiteral; - }; - - return array_map(function ($item) use ($f) { - if (is_array($item)) { - return array_filter($item, $f); - } - - return $item; - }, array_filter($statements, $f)); - } - - /** - * @return array - */ - protected function buildParameters() { - $this->parameters = array_merge( - $this->filterLiterals($this->statements['VALUES']), - $this->filterLiterals($this->statements['ON DUPLICATE KEY UPDATE']) - ); - - return parent::buildParameters(); - } - - /** - * @return string - */ - protected function getClauseOnDuplicateKeyUpdate() { - $result = array(); - foreach ($this->statements['ON DUPLICATE KEY UPDATE'] as $key => $value) { - $result[] = "$key = " . $this->parameterGetValue($value); - } - - return ' ON DUPLICATE KEY UPDATE ' . implode(', ', $result); - } - - /** - * @param array $oneValue - * - * @throws Exception - */ - private function addOneValue($oneValue) { - // check if all $keys are strings - foreach ($oneValue as $key => $value) { - if (!is_string($key)) { - throw new Exception('INSERT query: All keys of value array have to be strings.'); - } - } - if (!$this->firstValue) { - $this->firstValue = $oneValue; - } - if (!$this->columns) { - $this->columns = array_keys($oneValue); - } - if ($this->columns != array_keys($oneValue)) { - throw new Exception('INSERT query: All VALUES have to same keys (columns).'); - } - $this->statements['VALUES'][] = $oneValue; - } - -} diff --git a/vendor/FluentPDO/SelectQuery.php b/vendor/FluentPDO/SelectQuery.php deleted file mode 100644 index 3ded784..0000000 --- a/vendor/FluentPDO/SelectQuery.php +++ /dev/null @@ -1,194 +0,0 @@ - ', ', - 'FROM' => null, - 'JOIN' => array($this, 'getClauseJoin'), - 'WHERE' => ' AND ', - 'GROUP BY' => ',', - 'HAVING' => ' AND ', - 'ORDER BY' => ', ', - 'LIMIT' => null, - 'OFFSET' => null, - "\n--" => "\n--", - ); - parent::__construct($fpdo, $clauses); - - // initialize statements - $fromParts = explode(' ', $from); - $this->fromTable = reset($fromParts); - $this->fromAlias = end($fromParts); - - $this->statements['FROM'] = $from; - $this->statements['SELECT'][] = $this->fromAlias . '.*'; - $this->joins[] = $this->fromAlias; - - if(isset($fpdo->convertTypes) && $fpdo->convertTypes){ - $this->convertTypes = true; - } - } - - /** Return table name from FROM clause - * - * @internal - */ - public function getFromTable() { - return $this->fromTable; - } - - /** Return table alias from FROM clause - * - * @internal - */ - public function getFromAlias() { - return $this->fromAlias; - } - - /** - * Returns a single column - * - * @param int $columnNumber - * - * @return string - */ - public function fetchColumn($columnNumber = 0) { - if (($s = $this->execute()) !== false) { - return $s->fetchColumn($columnNumber); - } - - return $s; - } - - /** - * Fetch first row or column - * - * @param string $column column name or empty string for the whole row - * - * @return mixed string, array or false if there is no row - */ - public function fetch($column = '') { - $s = $this->execute(); - if ($s === false) { - return false; - } - $row = $s->fetch(); - - if($this->convertTypes){ - $row = FluentUtils::convertToNativeTypes($s,$row); - } - - if ($row && $column != '') { - if (is_object($row)) { - return $row->{$column}; - } else { - return $row[$column]; - } - } - - return $row; - } - - /** - * Fetch pairs - * - * @param $key - * @param $value - * @param $object - * - * @return array of fetched rows as pairs - */ - public function fetchPairs($key, $value, $object = false) { - if (($s = $this->select(null)->select("$key, $value")->asObject($object)->execute()) !== false) { - return $s->fetchAll(PDO::FETCH_KEY_PAIR); - } - - return $s; - } - - /** Fetch all row - * - * @param string $index specify index column - * @param string $selectOnly select columns which could be fetched - * - * @return array of fetched rows - */ - public function fetchAll($index = '', $selectOnly = '') { - if ($selectOnly) { - $this->select(null)->select($index . ', ' . $selectOnly); - } - if ($index) { - $data = array(); - foreach ($this as $row) { - if (is_object($row)) { - $data[$row->{$index}] = $row; - } else { - $data[$row[$index]] = $row; - } - } - - return $data; - } else { - if (($s = $this->execute()) !== false) { - if($this->convertTypes){ - return FluentUtils::convertToNativeTypes($s,$s->fetchAll()); - }else{ - return $s->fetchAll(); - } - } - - return $s; - } - } - - /** - * Countable interface doesn't break current \FluentPDO select query - * - * @return int - */ - public function count() { - $fpdo = clone $this; - - return (int)$fpdo->select(null)->select('COUNT(*)')->fetchColumn(); - } - - public function getIterator() { - if($this->convertTypes){ - return new ArrayIterator($this->fetchAll()); - }else{ - return $this->execute(); - } - } - -} diff --git a/vendor/FluentPDO/UpdateQuery.php b/vendor/FluentPDO/UpdateQuery.php deleted file mode 100644 index eff4731..0000000 --- a/vendor/FluentPDO/UpdateQuery.php +++ /dev/null @@ -1,107 +0,0 @@ - array($this, 'getClauseUpdate'), - 'JOIN' => array($this, 'getClauseJoin'), - 'SET' => array($this, 'getClauseSet'), - 'WHERE' => ' AND ', - 'ORDER BY' => ', ', - 'LIMIT' => null, - ); - parent::__construct($fpdo, $clauses); - - $this->statements['UPDATE'] = $table; - - $tableParts = explode(' ', $table); - $this->joins[] = end($tableParts); - } - - /** - * @param string|array $fieldOrArray - * @param bool|string $value - * - * @return $this - * @throws Exception - */ - public function set($fieldOrArray, $value = false) { - if (!$fieldOrArray) { - return $this; - } - if (is_string($fieldOrArray) && $value !== false) { - $this->statements['SET'][$fieldOrArray] = $value; - } else { - if (!is_array($fieldOrArray)) { - throw new Exception('You must pass a value, or provide the SET list as an associative array. column => value'); - } else { - foreach ($fieldOrArray as $field => $value) { - $this->statements['SET'][$field] = $value; - } - } - } - - return $this; - } - - /** - * Execute update query - * - * @param boolean $getResultAsPdoStatement true to return the pdo statement instead of row count - * - * @return int|boolean|\PDOStatement - */ - public function execute($getResultAsPdoStatement = false) { - $result = parent::execute(); - if ($getResultAsPdoStatement) { - return $result; - } - if ($result) { - return $result->rowCount(); - } - - return false; - } - - /** - * @return string - */ - protected function getClauseUpdate() { - return 'UPDATE ' . $this->statements['UPDATE']; - } - - /** - * @return string - */ - protected function getClauseSet() { - $setArray = array(); - foreach ($this->statements['SET'] as $field => $value) { - if ($value instanceof FluentLiteral) { - $setArray[] = $field . ' = ' . $value; - } else { - $setArray[] = $field . ' = ?'; - $this->parameters['SET'][$field] = $value; - } - } - - return ' SET ' . implode(', ', $setArray); - } - -}