diff --git a/MysqliDb.php b/MysqliDb.php index 6c50789a..2907d65e 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -9,7 +9,7 @@ * @author Alexander V. Butenko * @copyright Copyright (c) 2010 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License - * @link http://github.com/joshcam/PHP-MySQLi-Database-Class + * @link http://github.com/joshcam/PHP-MySQLi-Database-Class * @version 2.6-master */ @@ -150,7 +150,7 @@ class MysqliDb /** * Table name (with prefix, if used) - * @var string + * @var string */ private $_tableName = ''; @@ -239,7 +239,7 @@ public function __construct($host = null, $username = null, $password = null, $d /** * A method to connect to the database - * + * * @throws Exception * @return void */ @@ -266,7 +266,7 @@ public function connect() /** * A method to get mysqli object or create it in case needed - * + * * @return mysqli */ public function mysqli() @@ -358,7 +358,7 @@ public function objectBuilder() * Method to set a prefix * * @param string $prefix Contains a tableprefix - * + * * @return MysqliDb */ public function setPrefix($prefix = '') @@ -452,7 +452,7 @@ public function rawQueryValue($query, $bindParams = null) /** * A method to perform select query - * + * * @param string $query Contains a user-provided select query. * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset) * @@ -470,13 +470,103 @@ public function query($query, $numRows = null) return $res; } + /** + * A method to execute a procedure + * + * @param String $name MySql's procedure's name + * @param Array $inputs params of the procedure + * @param Array $outputs params of output of the procedure + * + * @return Array Contains the result and outputs of the procedure + */ + public function callProcedure( $name, $inputs = [], $outputs = [] ) + { + $inputs = $this->_validParams( $inputs ); + $params = array_merge( $inputs, $outputs ); + $query = "CALL `$name` (". implode( ',',$params ) .')'; + $return = [ 'result' => [], 'outputs' => [] ]; + + // Execute Procedure + if (! $this->mysqli()->multi_query( $query ) ) { + $this->_stmtError = $this->mysqli()->error; + throw new Exception( $this->_stmtError ); + } + + // Get Procedure's Resulset + while( $this->mysqli()->more_results() ) { + + $this->mysqli()->next_result(); + if ( $result = $this->mysqli()->store_result() ) { + + $queryData = []; + while ( $fetch = $result->fetch_assoc() ) { + $queryData[] = $fetch; + } + $return['result'] = array_merge( $return['result'], $queryData ); + $result->free(); + } + } + + // Get Procedure's outputs + if ( count( $outputs ) > 0 ) { + $query = 'SELECT '.implode( ',', $outputs ); + $rQuery = $this->query( $query ); + $return['outputs'] = isset( $rQuery[0] ) ? $rQuery[0] : $return['outputs']; + } + + return $return; + } + + /** + * A method to execute a function + * + * @param String $name MySql's function's name + * @param Array $inputs params of the function + * + * @return Array Contains the result of the function + */ + public function callFunction( $name, $inputs = [] ) { + $inputs = $this->_validParams( $inputs ); + $return = []; + + // Execute Function... + $query = "SELECT `$name` (". implode( ',',$inputs ) .') AS `return`'; + $rQuery = $this->query( $query ); + $return = isset( $rQuery[0] ) ? $rQuery[0] : $return; + + return $return; + } + + /** + * Function to validate the params of a procedure|function + * + * @param array $params params + * @return [type] [description] + */ + private function _validParams( $params = [] ) { + if (! is_array( $params ) ) { + $this->reset(); + throw new Exception('Procedure|Function params cant be empty!'); + } + + if ( empty( $params ) ) { + return $params; + } + + foreach ( $params as $key => &$param ) { + $param = "'". $this->mysqli()->real_escape_string( $param ) ."'"; + } + + return $params; + } + /** * This method allows you to specify multiple (method chaining optional) options for SQL queries. * * @uses $MySqliDb->setQueryOption('name'); * * @param string|array $options The optons name of the query. - * + * * @throws Exception * @return MysqliDb */ @@ -566,7 +656,7 @@ public function get($tableName, $numRows = null, $columns = '*') * * @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. */ public function getOne($tableName, $columns = '*') @@ -588,7 +678,7 @@ public function getOne($tableName, $columns = '*') * 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 $column The desired column + * @param string $column The desired column * @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 @@ -746,7 +836,7 @@ public function where($whereProp, $whereValue = 'DBNULL', $operator = '=', $cond * * @param array $updateColumns Variable with values * @param string $lastInsertId Variable value - * + * * @return MysqliDb */ public function onDuplicate($updateColumns, $lastInsertId = null) @@ -771,7 +861,7 @@ public function orWhere($whereProp, $whereValue = 'DBNULL', $operator = '=') { return $this->where($whereProp, $whereValue, $operator, 'OR'); } - + /** * This method allows you to specify multiple (method chaining optional) AND HAVING statements for SQL queries. * @@ -824,7 +914,7 @@ public function orHaving($havingProp, $havingValue = null, $operator = null) * @param string $joinTable The name of the table. * @param string $joinCondition the condition. * @param string $joinType 'LEFT', 'INNER' etc. - * + * * @throws Exception * @return MysqliDb */ @@ -854,7 +944,7 @@ public function join($joinTable, $joinCondition, $joinType = '') * @param string $orderByField The name of the database field. * @param string $orderByDirection Order direction. * @param array $customFields Fieldset for ORDER BY FIELD() ordering - * + * * @throws Exception * @return MysqliDb */ @@ -1002,7 +1092,7 @@ protected function _bindParams($values) * * @param string $operator * @param mixed $value Variable with values - * + * * @return string */ protected function _buildPair($operator, $value) @@ -1077,7 +1167,7 @@ protected function _buildQuery($numRows = null, $tableData = null) $this->_buildOrderBy(); $this->_buildLimit($numRows); $this->_buildOnDuplicate($tableData); - + if ($this->_forUpdate) { $this->_query .= ' FOR UPDATE'; } @@ -1214,7 +1304,7 @@ protected function _dynamicBindResults(mysqli_stmt $stmt) /** * Abstraction method that will build an JOIN part of the query - * + * * @return void */ protected function _buildJoin() @@ -1232,7 +1322,7 @@ protected function _buildJoin() $joinStr = $joinTable; } - $this->_query .= " " . $joinType . " JOIN " . $joinStr . + $this->_query .= " " . $joinType . " JOIN " . $joinStr . (false !== stripos($joinCondition, 'using') ? " " : " on ") . $joinCondition; } @@ -1240,11 +1330,11 @@ protected function _buildJoin() /** * Insert/Update query helper - * + * * @param array $tableData * @param array $tableColumns * @param bool $isInsert INSERT operation flag - * + * * @throws Exception */ public function _buildDataPairs($tableData, $tableColumns, $isInsert) @@ -1324,7 +1414,7 @@ protected function _buildOnDuplicate($tableData) /** * Abstraction method that will build an INSERT or UPDATE part of the query - * + * * @param array $tableData */ protected function _buildInsertQuery($tableData) @@ -1352,7 +1442,7 @@ protected function _buildInsertQuery($tableData) /** * Abstraction method that will build the part of the WHERE conditions - * + * * @param string $operator * @param array $conditions */ @@ -1452,7 +1542,7 @@ protected function _buildOrderBy() * * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset) * or only $count - * + * * @return void */ protected function _buildLimit($numRows) @@ -1491,7 +1581,7 @@ protected function _prepareQuery() /** * Close connection - * + * * @return void */ public function __destruct() @@ -1508,7 +1598,7 @@ public function __destruct() /** * Referenced data array is required by mysqli since PHP 5.3+ - * + * * @param array $arr * * @return array @@ -1530,7 +1620,7 @@ protected function refValues(array &$arr) /** * Function to replace ? with variables from bind variable - * + * * @param string $str * @param array $vals * @@ -1603,7 +1693,7 @@ public function getSubQuery() $this->reset(); return $val; } - + /* Helper functions */ /** @@ -1664,9 +1754,9 @@ public function now($diff = null, $func = "NOW()") /** * Method generates incremental function call - * + * * @param int $num increment by int or float. 1 by default - * + * * @throws Exception * @return array */ @@ -1680,9 +1770,9 @@ public function inc($num = 1) /** * Method generates decrimental function call - * + * * @param int $num increment by int or float. 1 by default - * + * * @return array */ public function dec($num = 1) @@ -1695,9 +1785,9 @@ public function dec($num = 1) /** * Method generates change boolean function call - * + * * @param string $col column name. null by default - * + * * @return array */ public function not($col = null) @@ -1707,10 +1797,10 @@ public function not($col = null) /** * Method generates user defined function call - * + * * @param string $expr user function body * @param array $bindParams - * + * * @return array */ public function func($expr, $bindParams = null) @@ -1720,9 +1810,9 @@ public function func($expr, $bindParams = null) /** * Method creates new mysqlidb object for a subquery generation - * + * * @param string $subQueryAlias - * + * * @return MysqliDb */ public static function subQuery($subQueryAlias = "") @@ -1802,7 +1892,7 @@ public function _transaction_status_check() * * @param bool $enabled Enable execution time tracking * @param string $stripPrefix Prefix to strip from the path in exec log - * + * * @return MysqliDb */ public function setTrace($enabled, $stripPrefix = null) @@ -1855,9 +1945,9 @@ public function tableExists($tables) /** * 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 * * @return MysqliDb