/** * Returns information about a table or a result set * * @param object|string $result MDB2_result object from a query or a * string containing the name of a table. * While this also accepts a query result * resource identifier, this behavior is * deprecated. * @param int $mode a valid tableInfo mode * * @return array an associative array with the information requested. * A MDB2_Error object on failure. * * @see MDB2_Driver_Common::tableInfo() */ function tableInfo($result, $mode = null) { if (is_string($result)) { return parent::tableInfo($result, $mode); } $db =& $this->getDBInstance(); if (MDB2::isError($db)) { return $db; } $resource = MDB2::isResultCommon($result) ? $result->getResource() : $result; if (!is_resource($resource)) { return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 'Could not generate result resource', __FUNCTION__); } if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { if ($db->options['field_case'] == CASE_LOWER) { $case_func = 'strtolower'; } else { $case_func = 'strtoupper'; } } else { $case_func = 'strval'; } $count = @fbsql_num_fields($resource); $res = array(); if ($mode) { $res['num_fields'] = $count; } for ($i = 0; $i < $count; $i++) { $res[$i] = array('table' => $case_func(@fbsql_field_table($resource, $i)), 'name' => $case_func(@fbsql_field_name($resource, $i)), 'type' => @fbsql_field_type($resource, $i), 'length' => @fbsql_field_len($resource, $i), 'flags' => @fbsql_field_flags($resource, $i)); // todo: implement $db->datatype->mapNativeDatatype(); $res[$i]['mdb2type'] = $res[$i]['type']; if ($mode & MDB2_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & MDB2_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } return $res; }
function tableInfo($result, $mode = null) { $result = $this->_wrapResource($result); if (is_string($result) || MDB2::isResultCommon($result)) { $this->db_object->loadModule('Reverse'); return $this->db_object->reverse->tableInfo($result, $mode); } return $result->tableInfo($mode); }
/** * Returns information about a table or a result set * * NOTE: only supports 'table' and 'flags' if <var>$result</var> * is a table name. * * @param object|string $result MDB2_result object from a query or a * string containing the name of a table. * While this also accepts a query result * resource identifier, this behavior is * deprecated. * @param int $mode a valid tableInfo mode * * @return array an associative array with the information requested. * A MDB2_Error object on failure. * * @see MDB2_Driver_Common::tableInfo() */ function tableInfo($result, $mode = null) { if (is_string($result)) { return parent::tableInfo($result, $mode); } $db = $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } $resource = MDB2::isResultCommon($result) ? $result->getResource() : $result; if (!is_resource($resource)) { return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 'Could not generate result resource', __FUNCTION__); } if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { if ($db->options['field_case'] == CASE_LOWER) { $case_func = 'strtolower'; } else { $case_func = 'strtoupper'; } } else { $case_func = 'strval'; } $meta = @sqlsrv_field_metadata($resource); $count = count($meta); $res = array(); if ($mode) { $res['num_fields'] = $count; } $db->loadModule('Datatype', null, true); for ($i = 0; $i < $count; $i++) { $res[$i] = array('table' => '', 'name' => $case_func($meta[$i]['Name']), 'type' => $meta[$i]['Type'], 'length' => $meta[$i]['Size'], 'numeric_precision' => $meta[$i]['Precision'], 'numeric_scale' => $meta[$i]['Scale'], 'flags' => ''); $mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]); if (PEAR::isError($mdb2type_info)) { return $mdb2type_info; } $res[$i]['mdb2type'] = $mdb2type_info[0][0]; if ($mode & MDB2_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & MDB2_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } return $res; }
function testQuery() { if (!$this->methodExists($this->db, 'query')) { return; } $result = $this->standardQuery(); $this->assertTrue(MDB2::isResult($result), 'query: $result returned is not a resource'); $this->assertTrue(MDB2::isResultCommon($result), 'query: $result returned is not a resource'); }
/** * Tests that the MDB2::isResultCommon() method correctly identifies * common results. */ function test_isResultCommon() { $result = null; $obj = new MDB2_Result_Common($this->db, $result); $this->assertTrue(MDB2::isResultCommon($obj), 'isResultCommon'); $obj = null; $this->assertFalse(MDB2::isResultCommon($obj), 'isResultCommon'); }
/** * Execute the specified query, fetch all the rows of the result set into * a two dimensional array and then frees the result set. * * @param string $query the SELECT query statement to be executed. * @param array $types optional array argument that specifies a list of * expected datatypes of the result set columns, so that the eventual * conversions may be performed. The default list of datatypes is * empty, meaning that no conversion is performed. * @param int $fetchmode how the array data should be indexed * @param boolean $rekey if set to true, the $all will have the first * column as its first dimension * @param boolean $force_array used only when the query returns exactly * two columns. If true, the values of the returned array will be * one-element arrays instead of scalars. * @param boolean $group if true, the values of the returned array is * wrapped in another array. If the same key value (in the first * column) repeats itself, the values will be appended to this array * instead of overwriting the existing values. * @return mixed MDB2_OK or data array on success, a MDB2 error on failure * @access public */ function queryAll($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT, $rekey = false, $force_array = false, $group = false) { $result = $this->query($query, $types); if (!MDB2::isResultCommon($result)) { return $result; } $all = $result->fetchAll($fetchmode, $rekey, $force_array, $group); $result->free(); return $all; }
/** * Returns information about a table or a result set * * @param object|string $result MDB2_result object from a query or a * string containing the name of a table. * While this also accepts a query result * resource identifier, this behavior is * deprecated. * @param int $mode a valid tableInfo mode * * @return array an associative array with the information requested. * A MDB2_Error object on failure. * * @see MDB2_Driver_Common::setOption() */ function tableInfo($result, $mode = null) { if (is_string($result)) { return parent::tableInfo($result, $mode); } $db =& $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } $resource = MDB2::isResultCommon($result) ? $result->getResource() : $result; if (!is_object($resource)) { return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 'Could not generate result resource', __FUNCTION__); } if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { if ($db->options['field_case'] == CASE_LOWER) { $case_func = 'strtolower'; } else { $case_func = 'strtoupper'; } } else { $case_func = 'strval'; } $count = @mysqli_num_fields($resource); $res = array(); if ($mode) { $res['num_fields'] = $count; } $db->loadModule('Datatype', null, true); for ($i = 0; $i < $count; $i++) { $tmp = @mysqli_fetch_field($resource); $flags = ''; foreach ($this->flags as $const => $means) { if ($tmp->flags & $const) { $flags .= $means . ' '; } } if ($tmp->def) { $flags .= 'default_' . rawurlencode($tmp->def); } $flags = trim($flags); $res[$i] = array('table' => $case_func($tmp->table), 'name' => $case_func($tmp->name), 'type' => isset($this->types[$tmp->type]) ? $this->types[$tmp->type] : 'unknown', 'length' => $tmp->length, 'flags' => $flags); $mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]); if (PEAR::isError($mdb2type_info)) { return $mdb2type_info; } $res[$i]['mdb2type'] = $mdb2type_info[0][0]; if ($mode & MDB2_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & MDB2_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } return $res; }
/** * Fetch the entire result set of a query and return it as an * associative array using the first column as the key. * * If the result set contains more than two columns, the value * will be an array of the values from column 2-n. If the result * set contains only two columns, the returned value will be a * scalar with the value of the second column (unless forced to an * array with the $force_array parameter). A MDB2 error code is * returned on errors. If the result set contains fewer than two * columns, a MDB2_ERROR_TRUNCATED error is returned. * * For example, if the table 'mytable' contains: * * ID TEXT DATE * -------------------------------- * 1 'one' 944679408 * 2 'two' 944679408 * 3 'three' 944679408 * * Then the call getAssoc('SELECT id,text FROM mytable') returns: * array( * '1' => 'one', * '2' => 'two', * '3' => 'three', * ) * * ...while the call getAssoc('SELECT id,text,date FROM mytable') returns: * array( * '1' => array('one', '944679408'), * '2' => array('two', '944679408'), * '3' => array('three', '944679408') * ) * * If the more than one row occurs with the same value in the * first column, the last row overwrites all previous ones by * default. Use the $group parameter if you don't want to * overwrite like this. Example: * * getAssoc('SELECT category,id,name FROM mytable', null, null * MDB2_FETCHMODE_ASSOC, false, true) returns: * array( * '1' => array(array('id' => '4', 'name' => 'number four'), * array('id' => '6', 'name' => 'number six') * ), * '9' => array(array('id' => '4', 'name' => 'number four'), * array('id' => '6', 'name' => 'number six') * ) * ) * * Keep in mind that database functions in PHP usually return string * values for results regardless of the database's internal type. * * @param string the SQL query * @param array that contains the types of the columns in the result set * @param array if supplied, prepare/execute will be used * with this array as execute parameters * @param array that contains the types of the values defined in $params * @param bool $force_array used only when the query returns * exactly two columns. If TRUE, the values of the returned array * will be one-element arrays instead of scalars. * @param bool $group if TRUE, the values of the returned array * is wrapped in another array. If the same key value (in the first * column) repeats itself, the values will be appended to this array * instead of overwriting the existing values. * * @return array|MDB2_Error data on success, a MDB2 error on failure * @access public */ function getAssoc($query, $types = null, $params = array(), $param_types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT, $force_array = false, $group = false) { $db =& $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } settype($params, 'array'); if (empty($params)) { return $db->queryAll($query, $types, $fetchmode, true, $force_array, $group); } $stmt = $db->prepare($query, $param_types, $types); if (PEAR::isError($stmt)) { return $stmt; } $result = $stmt->execute($params); if (!MDB2::isResultCommon($result)) { return $result; } $all = $result->fetchAll($fetchmode, true, $force_array, $group); $stmt->free(); $result->free(); return $all; }
/** * list all users * * @return mixed array of user names on success, a MDB2 error on failure * @access public */ function listUsers() { $db = $this->getDBInstance(); if (MDB2::isError($db)) { return $db; } $query = 'SELECT usename FROM pg_user'; $result2 = $db->standaloneQuery($query, array('text'), false); if (!MDB2::isResultCommon($result2)) { return $result2; } $result = $result2->fetchCol(); $result2->free(); return $result; }
/** * Returns information about a table or a result set * * @param object|string $result MDB2_result object from a query or a * string containing the name of a table. * While this also accepts a query result * resource identifier, this behavior is * deprecated. * @param int $mode a valid tableInfo mode * * @return array an associative array with the information requested. * A MDB2_Error object on failure. * * @see MDB2_Driver_Common::tableInfo() */ function tableInfo($result, $mode = null) { $db =& $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } if (is_string($result)) { /* * Probably received a table name. * Create a result resource identifier. */ $connection = $db->getConnection(); if (PEAR::isError($connection)) { return $connection; } $id = @fbsql_list_fields($db->database_name, $result, $connection); $got_string = true; } elseif (MDB2::isResultCommon($result)) { /* * Probably received a result object. * Extract the result resource identifier. */ $id = $result->getResource(); $got_string = false; } else { /* * Probably received a result resource identifier. * Copy it. * Deprecated. Here for compatibility only. */ $id = $result; $got_string = false; } if (!is_resource($id)) { return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA); } if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { if ($db->options['field_case'] == CASE_LOWER) { $case_func = 'strtolower'; } else { $case_func = 'strtoupper'; } } else { $case_func = 'strval'; } $count = @fbsql_num_fields($id); $res = array(); if ($mode) { $res['num_fields'] = $count; } for ($i = 0; $i < $count; $i++) { $res[$i] = array('table' => $case_func(@fbsql_field_table($id, $i)), 'name' => $case_func(@fbsql_field_name($id, $i)), 'type' => @fbsql_field_type($id, $i), 'length' => @fbsql_field_len($id, $i), 'flags' => @fbsql_field_flags($id, $i)); // todo: implement $db->datatype->mapNativeDatatype(); $res[$i]['mdb2type'] = $res[$i]['type']; if ($mode & MDB2_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & MDB2_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } // free the result only if we were called on a table if ($got_string) { @fbsql_free_result($id); } return $res; }
/** * Fetch the entire result set of a query and return it as an * associative array using the first column as the key. * * If the result set contains more than two columns, the value * will be an array of the values from column 2-n. If the result * set contains only two columns, the returned value will be a * scalar with the value of the second column (unless forced to an * array with the $force_array parameter). A MDB error code is * returned on errors. If the result set contains fewer than two * columns, a MDB2_ERROR_TRUNCATED error is returned. * * For example, if the table 'mytable' contains: * * ID TEXT DATE * -------------------------------- * 1 'one' 944679408 * 2 'two' 944679408 * 3 'three' 944679408 * * Then the call getAssoc('SELECT id,text FROM mytable') returns: * array( * '1' => 'one', * '2' => 'two', * '3' => 'three', * ) * * ...while the call getAssoc('SELECT id,text,date FROM mytable') returns: * array( * '1' => array('one', '944679408'), * '2' => array('two', '944679408'), * '3' => array('three', '944679408') * ) * * If the more than one row occurs with the same value in the * first column, the last row overwrites all previous ones by * default. Use the $group parameter if you don't want to * overwrite like this. Example: * * getAssoc('SELECT category,id,name FROM mytable', null, null * MDB2_FETCHMODE_ASSOC, false, true) returns: * array( * '1' => array(array('id' => '4', 'name' => 'number four'), * array('id' => '6', 'name' => 'number six') * ), * '9' => array(array('id' => '4', 'name' => 'number four'), * array('id' => '6', 'name' => 'number six') * ) * ) * * Keep in mind that database functions in PHP usually return string * values for results regardless of the database's internal type. * * @param string $query the SQL query * @param array $types array that contains the types of the columns in * the result set * @param array $params array if supplied, prepare/execute will be used * with this array as execute parameters * @param array $param_types array that contains the types of the values * defined in $params * @param boolean $force_array used only when the query returns * exactly two columns. If TRUE, the values of the returned array * will be one-element arrays instead of scalars. * @param boolean $group if TRUE, the values of the returned array * is wrapped in another array. If the same key value (in the first * column) repeats itself, the values will be appended to this array * instead of overwriting the existing values. * @return array associative array with results from the query. * @access public */ function getAssoc($query, $types = null, $params = array(), $param_types = null, $fetchmode = MDB2_FETCHMODE_ORDERED, $force_array = false, $group = false) { $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; settype($params, 'array'); if (count($params) == 0) { return $db->queryAll($query, $types, $fetchmode, true, $force_array, $group); } $prepared_query = $db->prepare($query, $param_types); if (MDB2::isError($prepared_query)) { return $prepared_query; } $result = $db->executeParams($prepared_query, $types, $params); if (!MDB2::isResultCommon($result)) { return $result; } $all = $result->fetchAll($fetchmode, true, $force_array, $group); $db->freePrepared($prepared_query); $result->free(); return $all; }
/** * Tests that the MDB2::isResultCommon() method correctly identifies * common results. * @dataProvider provider */ public function test_isResultCommon($ci) { $this->manualSetUp($ci); $result = null; $obj = new MDB2_Result_Common($this->db, $result); $this->assertTrue(MDB2::isResultCommon($obj), 'isResultCommon'); $obj = null; $this->assertFalse(MDB2::isResultCommon($obj), 'isResultCommon'); }
/** * Returns information about a table or a result set * * NOTE: only supports 'table' and 'flags' if <var>$result</var> * is a table name. * * @param object|string $result MDB2_result object from a query or a * string containing the name of a table. * While this also accepts a query result * resource identifier, this behavior is * deprecated. * @param int $mode a valid tableInfo mode * * @return array an associative array with the information requested. * A MDB2_Error object on failure. * * @see MDB2_Driver_Common::tableInfo() */ function tableInfo($result, $mode = null) { $db =& $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } if (is_string($result)) { /* * Probably received a table name. * Create a result resource identifier. */ $query = 'SELECT TOP 0 * FROM ' . $db->quoteIdentifier($result); $id =& $db->_doQuery($query, false); if (PEAR::isError($id)) { return $id; } $got_string = true; } elseif (MDB2::isResultCommon($result)) { /* * Probably received a result object. * Extract the result resource identifier. */ $id = $result->getResource(); $got_string = false; } else { /* * Probably received a result resource identifier. * Copy it. * Deprecated. Here for compatibility only. */ $id = $result; $got_string = false; } if (!is_resource($id)) { return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA); } if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { if ($db->options['field_case'] == CASE_LOWER) { $case_func = 'strtolower'; } else { $case_func = 'strtoupper'; } } else { $case_func = 'strval'; } $count = @mssql_num_fields($id); $res = array(); if ($mode) { $res['num_fields'] = $count; } $db->loadModule('Datatype', null, true); for ($i = 0; $i < $count; $i++) { $res[$i] = array('table' => $got_string ? $case_func($result) : '', 'name' => $case_func(@mssql_field_name($id, $i)), 'type' => @mssql_field_type($id, $i), 'length' => @mssql_field_length($id, $i), 'flags' => $got_string ? $this->_mssql_field_flags($result, @mssql_field_name($id, $i)) : ''); $mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]); if (PEAR::isError($mdb2type_info)) { return $mdb2type_info; } $res[$i]['mdb2type'] = $mdb2type_info[0][0]; if ($mode & MDB2_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & MDB2_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } // free the result only if we were called on a table if ($got_string) { @mssql_free_result($id); } return $res; }
/** * Returns information about a table or a result set * * @param object|string $result MDB2_result object from a query or a * string containing the name of a table. * While this also accepts a query result * resource identifier, this behavior is * deprecated. * @param int $mode a valid tableInfo mode * * @return array an associative array with the information requested. * A MDB2_Error object on failure. * * @see MDB2_common::tableInfo() */ function tableInfo($result, $mode = null) { $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; if (is_string($result)) { /* * Probably received a table name. * Create a result resource identifier. */ $id = @mysql_list_fields($db->database_name, $result, $db->connection); $got_string = true; } elseif (MDB2::isResultCommon($result)) { /* * Probably received a result object. * Extract the result resource identifier. */ $id = $result->getResource(); $got_string = false; } else { /* * Probably received a result resource identifier. * Copy it. * Deprecated. Here for compatibility only. */ $id = $result; $got_string = false; } if (!is_resource($id)) { return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA); } if ($db->options['portability'] & MDB2_PORTABILITY_LOWERCASE) { $case_func = 'strtolower'; } else { $case_func = 'strval'; } $count = @mysql_num_fields($id); $res = array(); if ($mode) { $res['num_fields'] = $count; } for ($i = 0; $i < $count; $i++) { $res[$i] = array('table' => $case_func(@mysql_field_table($id, $i)), 'name' => $case_func(@mysql_field_name($id, $i)), 'type' => @mysql_field_type($id, $i), 'len' => @mysql_field_len($id, $i), 'flags' => @mysql_field_flags($id, $i)); if ($mode & MDB2_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & MDB2_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } // free the result only if we were called on a table if ($got_string) { @mysql_free_result($id); } return $res; }