Commit c6926580 authored by tommyknocker's avatar tommyknocker

PHP DocBlock update. Some small PSR-2 code refactoring.

parent 1c46e232
<?php <?php
/** /**
* MysqliDb Class * MysqliDb Class
* *
...@@ -13,112 +12,102 @@ ...@@ -13,112 +12,102 @@
* @link http://github.com/joshcam/PHP-MySQLi-Database-Class * @link http://github.com/joshcam/PHP-MySQLi-Database-Class
* @version 2.6-master * @version 2.6-master
*/ */
class MysqliDb class MysqliDb
{ {
/** /**
* Static instance of self * Static instance of self
*
* @var MysqliDb * @var MysqliDb
*/ */
protected static $_instance; protected static $_instance;
/** /**
* Table prefix * Table prefix
*
* @var string * @var string
*/ */
public static $prefix = ''; public static $prefix = '';
/** /**
* MySQLi instance * MySQLi instance
*
* @var mysqli * @var mysqli
*/ */
protected $_mysqli; protected $_mysqli;
/** /**
* The SQL query to be prepared and executed * The SQL query to be prepared and executed
*
* @var string * @var string
*/ */
protected $_query; protected $_query;
/** /**
* The previously executed SQL query * The previously executed SQL query
*
* @var string * @var string
*/ */
protected $_lastQuery; protected $_lastQuery;
/** /**
* The SQL query options required after SELECT, INSERT, UPDATE or DELETE * The SQL query options required after SELECT, INSERT, UPDATE or DELETE
*
* @var string * @var string
*/ */
protected $_queryOptions = array(); protected $_queryOptions = array();
/** /**
* An array that holds where joins * An array that holds where joins
*
* @var array * @var array
*/ */
protected $_join = array(); protected $_join = array();
/** /**
* An array that holds where conditions * An array that holds where conditions
*
* @var array * @var array
*/ */
protected $_where = array(); protected $_where = array();
/** /**
* An array that holds having conditions * An array that holds having conditions
*
* @var array * @var array
*/ */
protected $_having = array(); protected $_having = array();
/** /**
* Dynamic type list for order by condition value * Dynamic type list for order by condition value
* @var array
*/ */
protected $_orderBy = array(); protected $_orderBy = array();
/** /**
* Dynamic type list for group by condition value * Dynamic type list for group by condition value
* @var array
*/ */
protected $_groupBy = array(); protected $_groupBy = array();
/** /**
* Dynamic array that holds a combination of where condition/table data value types and parameter references * Dynamic array that holds a combination of where condition/table data value types and parameter references
*
* @var array * @var array
*/ */
protected $_bindParams = array(''); // Create the empty 0 index protected $_bindParams = array(''); // Create the empty 0 index
/** /**
* Variable which holds an amount of returned rows during get/getOne/select queries * Variable which holds an amount of returned rows during get/getOne/select queries
*
* @var string * @var string
*/ */
public $count = 0; public $count = 0;
/** /**
* Variable which holds an amount of returned rows during get/getOne/select queries with withTotalCount() * Variable which holds an amount of returned rows during get/getOne/select queries with withTotalCount()
*
* @var string * @var string
*/ */
public $totalCount = 0; public $totalCount = 0;
/** /**
* Variable which holds last statement error * Variable which holds last statement error
*
* @var string * @var string
*/ */
protected $_stmtError; protected $_stmtError;
/** /**
* Database credentials * Database credentials
*
* @var string * @var string
*/ */
protected $host; protected $host;
...@@ -130,46 +119,50 @@ class MysqliDb ...@@ -130,46 +119,50 @@ class MysqliDb
/** /**
* Is Subquery object * Is Subquery object
* * @var bool
*/ */
protected $isSubQuery = false; protected $isSubQuery = false;
/** /**
* Name of the auto increment column * Name of the auto increment column
* * @var int
*/ */
protected $_lastInsertId = null; protected $_lastInsertId = null;
/** /**
* Column names for update when using onDuplicate method * Column names for update when using onDuplicate method
* * @var array
*/ */
protected $_updateColumns = null; protected $_updateColumns = null;
/** /**
* Return type: 'Array' to return results as array, 'Object' as object * Return type: 'array' to return results as array, 'object' as object
* 'Json' as json string * 'json' as json string
*
* @var string * @var string
*/ */
public $returnType = 'Array'; public $returnType = 'array';
/** /**
* Should join() results be nested by table * Should join() results be nested by table
* @var boolean * @var bool
*/ */
protected $_nestJoin = false; protected $_nestJoin = false;
/**
* Table name (with prefix, if used)
* @var string
*/
private $_tableName = ''; private $_tableName = '';
/** /**
* FOR UPDATE flag * FOR UPDATE flag
* @var boolean * @var bool
*/ */
protected $_forUpdate = false; protected $_forUpdate = false;
/** /**
* LOCK IN SHARE MODE flag * LOCK IN SHARE MODE flag
* @var boolean * @var bool
*/ */
protected $_lockInShareMode = false; protected $_lockInShareMode = false;
...@@ -181,7 +174,6 @@ class MysqliDb ...@@ -181,7 +174,6 @@ class MysqliDb
/** /**
* Variables for query execution tracing * Variables for query execution tracing
*
*/ */
protected $traceStartQ; protected $traceStartQ;
protected $traceEnabled; protected $traceEnabled;
...@@ -194,6 +186,7 @@ class MysqliDb ...@@ -194,6 +186,7 @@ class MysqliDb
* @param string $password * @param string $password
* @param string $db * @param string $db
* @param int $port * @param int $port
* @param string $charset
*/ */
public function __construct($host = null, $username = null, $password = null, $db = null, $port = null, $charset = 'utf8') public function __construct($host = null, $username = null, $password = null, $db = null, $port = null, $charset = 'utf8')
{ {
...@@ -234,6 +227,7 @@ class MysqliDb ...@@ -234,6 +227,7 @@ class MysqliDb
* A method to connect to the database * A method to connect to the database
* *
* @throws Exception * @throws Exception
* @return void
*/ */
public function connect() public function connect()
{ {
...@@ -242,7 +236,7 @@ class MysqliDb ...@@ -242,7 +236,7 @@ class MysqliDb
} }
if (empty($this->host)) { if (empty($this->host)) {
throw new Exception('Mysql host is not set'); throw new Exception('MySQL host is not set');
} }
$this->_mysqli = new mysqli($this->host, $this->username, $this->password, $this->db, $this->port); $this->_mysqli = new mysqli($this->host, $this->username, $this->password, $this->db, $this->port);
...@@ -258,6 +252,8 @@ class MysqliDb ...@@ -258,6 +252,8 @@ class MysqliDb
/** /**
* A method to get mysqli object or create it in case needed * A method to get mysqli object or create it in case needed
*
* @return mysqli
*/ */
public function mysqli() public function mysqli()
{ {
...@@ -274,7 +270,7 @@ class MysqliDb ...@@ -274,7 +270,7 @@ class MysqliDb
* *
* @uses $db = MySqliDb::getInstance(); * @uses $db = MySqliDb::getInstance();
* *
* @return object Returns the current instance. * @return MysqliDb Returns the current instance.
*/ */
public static function getInstance() public static function getInstance()
{ {
...@@ -284,7 +280,7 @@ class MysqliDb ...@@ -284,7 +280,7 @@ class MysqliDb
/** /**
* Reset states after an execution * Reset states after an execution
* *
* @return object Returns the current instance. * @return MysqliDb Returns the current instance.
*/ */
protected function reset() protected function reset()
{ {
...@@ -300,7 +296,7 @@ class MysqliDb ...@@ -300,7 +296,7 @@ class MysqliDb
$this->_bindParams = array(''); // Create the empty 0 index $this->_bindParams = array(''); // Create the empty 0 index
$this->_query = null; $this->_query = null;
$this->_queryOptions = array(); $this->_queryOptions = array();
$this->returnType = 'Array'; $this->returnType = 'array';
$this->_nestJoin = false; $this->_nestJoin = false;
$this->_forUpdate = false; $this->_forUpdate = false;
$this->_lockInShareMode = false; $this->_lockInShareMode = false;
...@@ -311,36 +307,36 @@ class MysqliDb ...@@ -311,36 +307,36 @@ class MysqliDb
} }
/** /**
* Helper function to create dbObject with Json return type * Helper function to create dbObject with JSON return type
* *
* @return object * @return MysqliDb
*/ */
public function JsonBuilder() public function jsonBuilder()
{ {
$this->returnType = 'Json'; $this->returnType = 'json';
return $this; return $this;
} }
/** /**
* Helper function to create dbObject with Array return type * Helper function to create dbObject with array return type
* Added for consistency as thats default output type * Added for consistency as thats default output type
* *
* @return object * @return MysqliDb
*/ */
public function ArrayBuilder() public function arrayBuilder()
{ {
$this->returnType = 'Array'; $this->returnType = 'array';
return $this; return $this;
} }
/** /**
* Helper function to create dbObject with Object return type. * Helper function to create dbObject with object return type.
* *
* @return object * @return MysqliDb
*/ */
public function ObjectBuilder() public function objectBuilder()
{ {
$this->returnType = 'Object'; $this->returnType = 'object';
return $this; return $this;
} }
...@@ -348,7 +344,8 @@ class MysqliDb ...@@ -348,7 +344,8 @@ class MysqliDb
* Method to set a prefix * Method to set a prefix
* *
* @param string $prefix Contains a tableprefix * @param string $prefix Contains a tableprefix
* @return object *
* @return MysqliDb
*/ */
public function setPrefix($prefix = '') public function setPrefix($prefix = '')
{ {
...@@ -397,7 +394,7 @@ class MysqliDb ...@@ -397,7 +394,7 @@ class MysqliDb
* @param string $query User-provided query to execute. * @param string $query User-provided query to execute.
* @param array $bindParams Variables array to bind to the SQL statement. * @param array $bindParams Variables array to bind to the SQL statement.
* *
* @return array Contains the returned row from the query. * @return array|null Contains the returned row from the query.
*/ */
public function rawQueryOne($query, $bindParams = null) public function rawQueryOne($query, $bindParams = null)
{ {
...@@ -440,9 +437,10 @@ class MysqliDb ...@@ -440,9 +437,10 @@ class MysqliDb
} }
/** /**
* * A method to perform select query
*
* @param string $query Contains a user-provided select query. * @param string $query Contains a user-provided select query.
* @param integer|array $numRows Array to define SQL limit in format Array ($count, $offset) * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset)
* *
* @return array Contains the returned rows from the query. * @return array Contains the returned rows from the query.
*/ */
...@@ -463,7 +461,8 @@ class MysqliDb ...@@ -463,7 +461,8 @@ class MysqliDb
* *
* @uses $MySqliDb->setQueryOption('name'); * @uses $MySqliDb->setQueryOption('name');
* *
* @param string/array $options The optons name of the query. * @param string|array $options The optons name of the query.
*
* @throws Exception * @throws Exception
* @return MysqliDb * @return MysqliDb
*/ */
...@@ -512,8 +511,9 @@ class MysqliDb ...@@ -512,8 +511,9 @@ class MysqliDb
* A convenient SELECT * function. * A convenient SELECT * function.
* *
* @param string $tableName The name of the database table to work with. * @param string $tableName The name of the database table to work with.
* @param integer|array $numRows Array to define SQL limit in format Array ($count, $offset) * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset)
* or only $count * or only $count
* @param string $columns Desired columns
* *
* @return array Contains the returned rows from the select query. * @return array Contains the returned rows from the select query.
*/ */
...@@ -551,7 +551,8 @@ class MysqliDb ...@@ -551,7 +551,8 @@ class MysqliDb
* A convenient SELECT * function to get one record. * A convenient SELECT * function to get one record.
* *
* @param string $tableName The name of the database table to work with. * @param string $tableName The name of the database table to work with.
* * @param string $columns Desired columns
*
* @return array Contains the returned rows from the select query. * @return array Contains the returned rows from the select query.
*/ */
public function getOne($tableName, $columns = '*') public function getOne($tableName, $columns = '*')
...@@ -573,6 +574,7 @@ class MysqliDb ...@@ -573,6 +574,7 @@ class MysqliDb
* A convenient SELECT COLUMN function to get a single column value from one row * A convenient SELECT COLUMN function to get a single column value from one row
* *
* @param string $tableName The name of the database table to work with. * @param string $tableName The name of the database table to work with.
* @param string $column The desired column
* @param int $limit Limit of rows to select. Use null for unlimited..1 by default * @param int $limit Limit of rows to select. Use null for unlimited..1 by default
* *
* @return mixed Contains the value of a returned column / array of values * @return mixed Contains the value of a returned column / array of values
...@@ -602,10 +604,10 @@ class MysqliDb ...@@ -602,10 +604,10 @@ class MysqliDb
/** /**
* Insert method to add new row * Insert method to add new row
* *
* @param <string $tableName The name of the table. * @param string $tableName The name of the table.
* @param array $insertData Data containing information for inserting into the DB. * @param array $insertData Data containing information for inserting into the DB.
* *
* @return boolean Boolean indicating whether the insert query was completed succesfully. * @return bool Boolean indicating whether the insert query was completed succesfully.
*/ */
public function insert($tableName, $insertData) public function insert($tableName, $insertData)
{ {
...@@ -615,10 +617,10 @@ class MysqliDb ...@@ -615,10 +617,10 @@ class MysqliDb
/** /**
* Replace method to add new row * Replace method to add new row
* *
* @param <string $tableName The name of the table. * @param string $tableName The name of the table.
* @param array $insertData Data containing information for inserting into the DB. * @param array $insertData Data containing information for inserting into the DB.
* *
* @return boolean Boolean indicating whether the insert query was completed succesfully. * @return bool Boolean indicating whether the insert query was completed succesfully.
*/ */
public function replace($tableName, $insertData) public function replace($tableName, $insertData)
{ {
...@@ -645,7 +647,7 @@ class MysqliDb ...@@ -645,7 +647,7 @@ class MysqliDb
* @param string $tableName The name of the database table to work with. * @param string $tableName The name of the database table to work with.
* @param array $tableData Array of data to update the desired row. * @param array $tableData Array of data to update the desired row.
* *
* @return boolean * @return bool
*/ */
public function update($tableName, $tableData) public function update($tableName, $tableData)
{ {
...@@ -668,10 +670,10 @@ class MysqliDb ...@@ -668,10 +670,10 @@ class MysqliDb
* Delete query. Call the "where" method first. * Delete query. Call the "where" method first.
* *
* @param string $tableName The name of the database table to work with. * @param string $tableName The name of the database table to work with.
* @param integer|array $numRows Array to define SQL limit in format Array ($count, $offset) * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset)
* or only $count * or only $count
* *
* @return boolean Indicates success. 0 or 1. * @return bool Indicates success. 0 or 1.
*/ */
public function delete($tableName, $numRows = null) public function delete($tableName, $numRows = null)
{ {
...@@ -702,6 +704,8 @@ class MysqliDb ...@@ -702,6 +704,8 @@ class MysqliDb
* *
* @param string $whereProp The name of the database field. * @param string $whereProp The name of the database field.
* @param mixed $whereValue The value of the database field. * @param mixed $whereValue The value of the database field.
* @param string $operator Comparison operator. Default is =
* @param string $cond Condition of where statement (OR, AND)
* *
* @return MysqliDb * @return MysqliDb
*/ */
...@@ -717,7 +721,7 @@ class MysqliDb ...@@ -717,7 +721,7 @@ class MysqliDb
$cond = ''; $cond = '';
} }
$this->_where[] = Array($cond, $whereProp, $operator, $whereValue); $this->_where[] = array($cond, $whereProp, $operator, $whereValue);
return $this; return $this;
} }
...@@ -725,13 +729,15 @@ class MysqliDb ...@@ -725,13 +729,15 @@ class MysqliDb
* This function store update column's name and column name of the * This function store update column's name and column name of the
* autoincrement column * autoincrement column
* *
* @param Array Variable with values * @param array $updateColumns Variable with values
* @param String Variable value * @param string $lastInsertId Variable value
*
* @return MysqliDb
*/ */
public function onDuplicate($_updateColumns, $_lastInsertId = null) public function onDuplicate($updateColumns, $lastInsertId = null)
{ {
$this->_lastInsertId = $_lastInsertId; $this->_lastInsertId = $lastInsertId;
$this->_updateColumns = $_updateColumns; $this->_updateColumns = $updateColumns;
return $this; return $this;
} }
...@@ -742,6 +748,7 @@ class MysqliDb ...@@ -742,6 +748,7 @@ class MysqliDb
* *
* @param string $whereProp The name of the database field. * @param string $whereProp The name of the database field.
* @param mixed $whereValue The value of the database field. * @param mixed $whereValue The value of the database field.
* @param string $operator Comparison operator. Default is =
* *
* @return MysqliDb * @return MysqliDb
*/ */
...@@ -749,13 +756,15 @@ class MysqliDb ...@@ -749,13 +756,15 @@ class MysqliDb
{ {
return $this->where($whereProp, $whereValue, $operator, 'OR'); return $this->where($whereProp, $whereValue, $operator, 'OR');
} }
/*
/**
* This method allows you to specify multiple (method chaining optional) AND HAVING statements for SQL queries. * This method allows you to specify multiple (method chaining optional) AND HAVING statements for SQL queries.
* *
* @uses $MySqliDb->having('SUM(tags) > 10') * @uses $MySqliDb->having('SUM(tags) > 10')
* *
* @param string $havingProp The name of the database field. * @param string $havingProp The name of the database field.
* @param mixed $havingValue The value of the database field. * @param mixed $havingValue The value of the database field.
* @param string $operator Comparison operator. Default is =
* *
* @return MysqliDb * @return MysqliDb
*/ */
...@@ -763,10 +772,10 @@ class MysqliDb ...@@ -763,10 +772,10 @@ class MysqliDb
public function having($havingProp, $havingValue = null, $operator = null) public function having($havingProp, $havingValue = null, $operator = null)
{ {
if ($operator) { if ($operator) {
$havingValue = Array($operator => $havingValue); $havingValue = array($operator => $havingValue);
} }
$this->_having[] = Array("AND", $havingValue, $havingProp); $this->_having[] = array("AND", $havingValue, $havingProp);
return $this; return $this;
} }
...@@ -777,6 +786,7 @@ class MysqliDb ...@@ -777,6 +786,7 @@ class MysqliDb
* *
* @param string $havingProp The name of the database field. * @param string $havingProp The name of the database field.
* @param mixed $havingValue The value of the database field. * @param mixed $havingValue The value of the database field.
* @param string $operator Comparison operator. Default is =
* *
* @return MysqliDb * @return MysqliDb
*/ */
...@@ -798,6 +808,7 @@ class MysqliDb ...@@ -798,6 +808,7 @@ class MysqliDb
* @param string $joinTable The name of the table. * @param string $joinTable The name of the table.
* @param string $joinCondition the condition. * @param string $joinCondition the condition.
* @param string $joinType 'LEFT', 'INNER' etc. * @param string $joinType 'LEFT', 'INNER' etc.
*
* @throws Exception * @throws Exception
* @return MysqliDb * @return MysqliDb
*/ */
...@@ -826,6 +837,8 @@ class MysqliDb ...@@ -826,6 +837,8 @@ class MysqliDb
* *
* @param string $orderByField The name of the database field. * @param string $orderByField The name of the database field.
* @param string $orderByDirection Order direction. * @param string $orderByDirection Order direction.
* @param array $customFields Fieldset for ORDER BY FIELD() ordering
*
* @throws Exception * @throws Exception
* @return MysqliDb * @return MysqliDb
*/ */
...@@ -877,7 +890,7 @@ class MysqliDb ...@@ -877,7 +890,7 @@ class MysqliDb
/** /**
* This methods returns the ID of the last inserted item * This methods returns the ID of the last inserted item
* *
* @return integer The last inserted item ID. * @return int The last inserted item ID.
*/ */
public function getInsertId() public function getInsertId()
{ {
...@@ -957,12 +970,13 @@ class MysqliDb ...@@ -957,12 +970,13 @@ class MysqliDb
/** /**
* Helper function to add variables into bind parameters array in bulk * Helper function to add variables into bind parameters array in bulk
* *
* @param Array Variable with values * @param array $values Variable with values
*/ */
protected function _bindParams($values) protected function _bindParams($values)
{ {
foreach ($values as $value) foreach ($values as $value) {
$this->_bindParam($value); $this->_bindParam($value);
}
} }
/** /**
...@@ -970,7 +984,10 @@ class MysqliDb ...@@ -970,7 +984,10 @@ class MysqliDb
* its SQL part of the query according to operator in ' $operator ?' or * its SQL part of the query according to operator in ' $operator ?' or
* ' $operator ($subquery) ' formats * ' $operator ($subquery) ' formats
* *
* @param Array Variable with values * @param string $operator
* @param mixed $value Variable with values
*
* @return string
*/ */
protected function _buildPair($operator, $value) protected function _buildPair($operator, $value)
{ {
...@@ -988,10 +1005,11 @@ class MysqliDb ...@@ -988,10 +1005,11 @@ class MysqliDb
/** /**
* Internal function to build and execute INSERT/REPLACE calls * Internal function to build and execute INSERT/REPLACE calls
* *
* @param <string $tableName The name of the table. * @param string $tableName The name of the table.
* @param array $insertData Data containing information for inserting into the DB. * @param array $insertData Data containing information for inserting into the DB.
* @param string $operation Type of operation (INSERT, REPLACE)
* *
* @return boolean Boolean indicating whether the insert query was completed succesfully. * @return bool Boolean indicating whether the insert query was completed succesfully.
*/ */
private function _buildInsert($tableName, $insertData, $operation) private function _buildInsert($tableName, $insertData, $operation)
{ {
...@@ -1022,7 +1040,7 @@ class MysqliDb ...@@ -1022,7 +1040,7 @@ class MysqliDb
* any passed update data, and the desired rows. * any passed update data, and the desired rows.
* It then builds the SQL query. * It then builds the SQL query.
* *
* @param integer|array $numRows Array to define SQL limit in format Array ($count, $offset) * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset)
* or only $count * or only $count
* @param array $tableData Should contain an array of data for updating the database. * @param array $tableData Should contain an array of data for updating the database.
* *
...@@ -1038,6 +1056,7 @@ class MysqliDb ...@@ -1038,6 +1056,7 @@ class MysqliDb
$this->_buildOrderBy(); $this->_buildOrderBy();
$this->_buildLimit($numRows); $this->_buildLimit($numRows);
$this->_buildOnDuplicate($tableData); $this->_buildOnDuplicate($tableData);
if ($this->_forUpdate) { if ($this->_forUpdate) {
$this->_query .= ' FOR UPDATE'; $this->_query .= ' FOR UPDATE';
} }
...@@ -1116,7 +1135,7 @@ class MysqliDb ...@@ -1116,7 +1135,7 @@ class MysqliDb
$this->count = 0; $this->count = 0;
while ($stmt->fetch()) { while ($stmt->fetch()) {
if ($this->returnType == 'Object') { if ($this->returnType == 'object') {
$result = new stdClass (); $result = new stdClass ();
foreach ($row as $key => $val) { foreach ($row as $key => $val) {
if (is_array($val)) { if (is_array($val)) {
...@@ -1124,8 +1143,9 @@ class MysqliDb ...@@ -1124,8 +1143,9 @@ class MysqliDb
foreach ($val as $k => $v) { foreach ($val as $k => $v) {
$result->$key->$k = $v; $result->$key->$k = $v;
} }
} else } else {
$result->$key = $val; $result->$key = $val;
}
} }
} else { } else {
$result = array(); $result = array();
...@@ -1134,15 +1154,17 @@ class MysqliDb ...@@ -1134,15 +1154,17 @@ class MysqliDb
foreach ($val as $k => $v) { foreach ($val as $k => $v) {
$result[$key][$k] = $v; $result[$key][$k] = $v;
} }
} else } else {
$result[$key] = $val; $result[$key] = $val;
}
} }
} }
$this->count++; $this->count++;
if ($this->_mapKey) if ($this->_mapKey) {
$results[$row[$this->_mapKey]] = count($row) > 2 ? $result : end($result); $results[$row[$this->_mapKey]] = count($row) > 2 ? $result : end($result);
else } else {
array_push($results, $result); array_push($results, $result);
}
} }
if ($shouldStoreResult) { if ($shouldStoreResult) {
...@@ -1162,7 +1184,7 @@ class MysqliDb ...@@ -1162,7 +1184,7 @@ class MysqliDb
$this->totalCount = $totalCount[0]; $this->totalCount = $totalCount[0];
} }
if ($this->returnType == 'Json') { if ($this->returnType == 'json') {
return json_encode($results); return json_encode($results);
} }
...@@ -1171,6 +1193,8 @@ class MysqliDb ...@@ -1171,6 +1193,8 @@ class MysqliDb
/** /**
* Abstraction method that will build an JOIN part of the query * Abstraction method that will build an JOIN part of the query
*
* @return void
*/ */
protected function _buildJoin() protected function _buildJoin()
{ {
...@@ -1192,6 +1216,11 @@ class MysqliDb ...@@ -1192,6 +1216,11 @@ class MysqliDb
} }
/** /**
* Insert/Update query helper
*
* @param array $tableData
* @param array $tableColumns
* @param bool $isInsert INSERT operation flag
* *
* @throws Exception * @throws Exception
*/ */
...@@ -1231,10 +1260,11 @@ class MysqliDb ...@@ -1231,10 +1260,11 @@ class MysqliDb
} }
break; break;
case '[N]': case '[N]':
if ($val == null) if ($val == null) {
$this->_query .= "!" . $column . ", "; $this->_query .= "!" . $column . ", ";
else } else {
$this->_query .= "!" . $val . ", "; $this->_query .= "!" . $val . ", ";
}
break; break;
default: default:
throw new Exception("Wrong operation"); throw new Exception("Wrong operation");
...@@ -1246,12 +1276,12 @@ class MysqliDb ...@@ -1246,12 +1276,12 @@ class MysqliDb
/** /**
* Helper function to add variables into the query statement * Helper function to add variables into the query statement
* *
* @param Array Variable with values * @param array $tableData Variable with values
*/ */
protected function _buildOnDuplicate($tableData) protected function _buildOnDuplicate($tableData)
{ {
if (is_array($this->_updateColumns) && !empty($this->_updateColumns)) { if (is_array($this->_updateColumns) && !empty($this->_updateColumns)) {
$this->_query .= " on duplicate key update "; $this->_query .= " ON DUPLICATE KEY UPDATE ";
if ($this->_lastInsertId) { if ($this->_lastInsertId) {
$this->_query .= $this->_lastInsertId . "=LAST_INSERT_ID (" . $this->_lastInsertId . "), "; $this->_query .= $this->_lastInsertId . "=LAST_INSERT_ID (" . $this->_lastInsertId . "), ";
} }
...@@ -1261,8 +1291,9 @@ class MysqliDb ...@@ -1261,8 +1291,9 @@ class MysqliDb
if (is_numeric($key)) { if (is_numeric($key)) {
$this->_updateColumns[$val] = ''; $this->_updateColumns[$val] = '';
unset($this->_updateColumns[$key]); unset($this->_updateColumns[$key]);
} else } else {
$tableData[$key] = $val; $tableData[$key] = $val;
}
} }
$this->_buildDataPairs($tableData, array_keys($this->_updateColumns), false); $this->_buildDataPairs($tableData, array_keys($this->_updateColumns), false);
} }
...@@ -1270,6 +1301,8 @@ class MysqliDb ...@@ -1270,6 +1301,8 @@ class MysqliDb
/** /**
* Abstraction method that will build an INSERT or UPDATE part of the query * Abstraction method that will build an INSERT or UPDATE part of the query
*
* @param array $tableData
*/ */
protected function _buildInsertQuery($tableData) protected function _buildInsertQuery($tableData)
{ {
...@@ -1294,6 +1327,9 @@ class MysqliDb ...@@ -1294,6 +1327,9 @@ class MysqliDb
/** /**
* Abstraction method that will build the part of the WHERE conditions * Abstraction method that will build the part of the WHERE conditions
*
* @param string $operator
* @param array $conditions
*/ */
protected function _buildCondition($operator, &$conditions) protected function _buildCondition($operator, &$conditions)
{ {
...@@ -1346,6 +1382,7 @@ class MysqliDb ...@@ -1346,6 +1382,7 @@ class MysqliDb
/** /**
* Abstraction method that will build the GROUP BY part of the WHERE statement * Abstraction method that will build the GROUP BY part of the WHERE statement
* *
* @return void
*/ */
protected function _buildGroupBy() protected function _buildGroupBy()
{ {
...@@ -1365,6 +1402,7 @@ class MysqliDb ...@@ -1365,6 +1402,7 @@ class MysqliDb
/** /**
* Abstraction method that will build the LIMIT part of the WHERE statement * Abstraction method that will build the LIMIT part of the WHERE statement
* *
* @return void
*/ */
protected function _buildOrderBy() protected function _buildOrderBy()
{ {
...@@ -1387,8 +1425,10 @@ class MysqliDb ...@@ -1387,8 +1425,10 @@ class MysqliDb
/** /**
* Abstraction method that will build the LIMIT part of the WHERE statement * Abstraction method that will build the LIMIT part of the WHERE statement
* *
* @param integer|array $numRows Array to define SQL limit in format Array ($count, $offset) * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset)
* or only $count * or only $count
*
* @return void
*/ */
protected function _buildLimit($numRows) protected function _buildLimit($numRows)
{ {
...@@ -1424,6 +1464,8 @@ class MysqliDb ...@@ -1424,6 +1464,8 @@ class MysqliDb
/** /**
* Close connection * Close connection
*
* @return void
*/ */
public function __destruct() public function __destruct()
{ {
...@@ -1438,11 +1480,13 @@ class MysqliDb ...@@ -1438,11 +1480,13 @@ class MysqliDb
} }
/** /**
* Referenced data array is required by mysqli since PHP 5.3+
*
* @param array $arr * @param array $arr
* *
* @return array * @return array
*/ */
protected function refValues(Array &$arr) protected function refValues(array &$arr)
{ {
//Reference in the function arguments are required for HHVM to work //Reference in the function arguments are required for HHVM to work
//https://github.com/facebook/hhvm/issues/5155 //https://github.com/facebook/hhvm/issues/5155
...@@ -1459,8 +1503,9 @@ class MysqliDb ...@@ -1459,8 +1503,9 @@ class MysqliDb
/** /**
* Function to replace ? with variables from bind variable * Function to replace ? with variables from bind variable
*
* @param string $str * @param string $str
* @param Array $vals * @param array $vals
* *
* @return string * @return string
*/ */
...@@ -1531,16 +1576,17 @@ class MysqliDb ...@@ -1531,16 +1576,17 @@ class MysqliDb
$this->reset(); $this->reset();
return $val; return $val;
} }
/* Helper functions */ /* Helper functions */
/** /**
* Method returns generated interval function as a string * Method returns generated interval function as a string
* *
* @param string interval in the formats: * @param string $diff interval in the formats:
* "1", "-1d" or "- 1 day" -- For interval - 1 day * "1", "-1d" or "- 1 day" -- For interval - 1 day
* Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear * Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
* Default null; * Default null;
* @param string Initial date * @param string $func Initial date
* *
* @return string * @return string
*/ */
...@@ -1576,11 +1622,11 @@ class MysqliDb ...@@ -1576,11 +1622,11 @@ class MysqliDb
/** /**
* Method returns generated interval function as an insert/update function * Method returns generated interval function as an insert/update function
* *
* @param string interval in the formats: * @param string $diff interval in the formats:
* "1", "-1d" or "- 1 day" -- For interval - 1 day * "1", "-1d" or "- 1 day" -- For interval - 1 day
* Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear * Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
* Default null; * Default null;
* @param string Initial date * @param string $func Initial date
* *
* @return array * @return array
*/ */
...@@ -1591,8 +1637,11 @@ class MysqliDb ...@@ -1591,8 +1637,11 @@ class MysqliDb
/** /**
* Method generates incremental function call * Method generates incremental function call
* @param int increment by int or float. 1 by default *
* @param int $num increment by int or float. 1 by default
*
* @throws Exception * @throws Exception
* @return array
*/ */
public function inc($num = 1) public function inc($num = 1)
{ {
...@@ -1604,7 +1653,10 @@ class MysqliDb ...@@ -1604,7 +1653,10 @@ class MysqliDb
/** /**
* Method generates decrimental function call * Method generates decrimental function call
* @param int increment by int or float. 1 by default *
* @param int $num increment by int or float. 1 by default
*
* @return array
*/ */
public function dec($num = 1) public function dec($num = 1)
{ {
...@@ -1616,7 +1668,10 @@ class MysqliDb ...@@ -1616,7 +1668,10 @@ class MysqliDb
/** /**
* Method generates change boolean function call * Method generates change boolean function call
* @param string column name. null by default *
* @param string $col column name. null by default
*
* @return array
*/ */
public function not($col = null) public function not($col = null)
{ {
...@@ -1625,15 +1680,23 @@ class MysqliDb ...@@ -1625,15 +1680,23 @@ class MysqliDb
/** /**
* Method generates user defined function call * Method generates user defined function call
* @param string user function body *
* @param string $expr user function body
* @param array $bindParams
*
* @return array
*/ */
public function func($expr, $bindParams = null) public function func($expr, $bindParams = null)
{ {
return array("[F]" => Array($expr, $bindParams)); return array("[F]" => array($expr, $bindParams));
} }
/** /**
* Method creates new mysqlidb object for a subquery generation * Method creates new mysqlidb object for a subquery generation
*
* @param string $subQueryAlias
*
* @return MysqliDb
*/ */
public static function subQuery($subQueryAlias = "") public static function subQuery($subQueryAlias = "")
{ {
...@@ -1643,7 +1706,7 @@ class MysqliDb ...@@ -1643,7 +1706,7 @@ class MysqliDb
/** /**
* Method returns a copy of a mysqlidb subquery object * Method returns a copy of a mysqlidb subquery object
* *
* @param object new mysqlidb object * @return MysqliDb new mysqlidb object
*/ */
public function copy() public function copy()
{ {
...@@ -1710,7 +1773,9 @@ class MysqliDb ...@@ -1710,7 +1773,9 @@ class MysqliDb
* *
* @param bool $enabled Enable execution time tracking * @param bool $enabled Enable execution time tracking
* @param string $stripPrefix Prefix to strip from the path in exec log * @param string $stripPrefix Prefix to strip from the path in exec log
* */ *
* @return MysqliDb
*/
public function setTrace($enabled, $stripPrefix = null) public function setTrace($enabled, $stripPrefix = null)
{ {
$this->traceEnabled = $enabled; $this->traceEnabled = $enabled;
...@@ -1740,7 +1805,7 @@ class MysqliDb ...@@ -1740,7 +1805,7 @@ class MysqliDb
* *
* @param array $tables Table name or an Array of table names to check * @param array $tables Table name or an Array of table names to check
* *
* @returns boolean True if table exists * @return bool True if table exists
*/ */
public function tableExists($tables) public function tableExists($tables)
{ {
...@@ -1761,10 +1826,12 @@ class MysqliDb ...@@ -1761,10 +1826,12 @@ class MysqliDb
/** /**
* Return result as an associative array with $idField field value used as a record key * Return result as an associative array with $idField field value used as a record key
*
* Array Returns an array($k => $v) if get(.."param1, param2"), array ($k => array ($v, $v)) otherwise
*
* @param string $idField field name to use for a mapped element key
* *
* @param String $idField field name to use for a mapped element key * @return MysqliDb
*
* @return Array Returns an array($k => $v) if get(.."param1, param2"), array ($k => array ($v, $v)) otherwise
*/ */
public function map($idField) public function map($idField)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment