/** * Returns information about a table or a result set * * NOTE: only supports 'table' and 'flags' if <var>$result</var> * is a table name. * * NOTE: flags won't contain index information. * * @param object|string $result DB_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 DB_Error object on failure. * * @see DB_common::tableInfo() */ function tableInfo($result, $mode = null) { if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) { $case_func = 'strtolower'; } else { $case_func = 'strval'; } $res = array(); if (is_string($result)) { /* * Probably received a table name. * Create a result resource identifier. */ $result = strtoupper($result); $q_fields = 'SELECT column_name, data_type, data_length, ' . 'nullable ' . 'FROM user_tab_columns ' . "WHERE table_name='{$result}' ORDER BY column_id"; $this->last_query = $q_fields; if (!($stmt = @OCIParse($this->connection, $q_fields))) { return $this->oci8RaiseError(DB_ERROR_NEED_MORE_DATA); } if (!@OCIExecute($stmt, OCI_DEFAULT)) { return $this->oci8RaiseError($stmt); } $i = 0; while (@OCIFetch($stmt)) { $res[$i] = array('table' => $case_func($result), 'name' => $case_func(@OCIResult($stmt, 1)), 'type' => @OCIResult($stmt, 2), 'len' => @OCIResult($stmt, 3), 'flags' => @OCIResult($stmt, 4) == 'N' ? 'not_null' : ''); if ($mode & DB_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & DB_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } $i++; } if ($mode) { $res['num_fields'] = $i; } @OCIFreeStatement($stmt); } else { if (isset($result->result)) { /* * Probably received a result object. * Extract the result resource identifier. */ $result = $result->result; } $res = array(); if ($result === $this->last_stmt) { $count = @OCINumCols($result); if ($mode) { $res['num_fields'] = $count; } for ($i = 0; $i < $count; $i++) { $res[$i] = array('table' => '', 'name' => $case_func(@OCIColumnName($result, $i + 1)), 'type' => @OCIColumnType($result, $i + 1), 'len' => @OCIColumnSize($result, $i + 1), 'flags' => ''); if ($mode & DB_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & DB_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } } else { return $this->raiseError(DB_ERROR_NOT_CAPABLE); } } return $res; }
function tableInfo($result, $mode = null) { $count = 0; $res = array(); /* * depending on $mode, metadata returns the following values: * * - mode is false (default): * $res[]: * [0]["table"] table name * [0]["name"] field name * [0]["type"] field type * [0]["len"] field length * [0]["nullable"] field can be null (boolean) * [0]["format"] field precision if NUMBER * [0]["default"] field default value * * - mode is DB_TABLEINFO_ORDER * $res[]: * ["num_fields"] number of fields * [0]["table"] table name * [0]["name"] field name * [0]["type"] field type * [0]["len"] field length * [0]["nullable"] field can be null (boolean) * [0]["format"] field precision if NUMBER * [0]["default"] field default value * ['order'][field name] index of field named "field name" * The last one is used, if you have a field name, but no index. * Test: if (isset($result['order']['myfield'])) { ... * * - mode is DB_TABLEINFO_ORDERTABLE * the same as above. but additionally * ["ordertable"][table name][field name] index of field * named "field name" * * this is, because if you have fields from different * tables with the same field name * they override each * other with DB_TABLEINFO_ORDER * * you can combine DB_TABLEINFO_ORDER and * DB_TABLEINFO_ORDERTABLE with DB_TABLEINFO_ORDER | * DB_TABLEINFO_ORDERTABLE * or with DB_TABLEINFO_FULL */ // if $result is a string, we collect info for a table only if (is_string($result)) { $result = strtoupper($result); $q_fields = "select column_name, data_type, data_length, data_precision,\n nullable, data_default from user_tab_columns\n where table_name='{$result}' order by column_id"; if (!($stmt = OCIParse($this->connection, $q_fields))) { return $this->oci8RaiseError(); } if (!OCIExecute($stmt, OCI_DEFAULT)) { return $this->oci8RaiseError($stmt); } while (OCIFetch($stmt)) { $res[$count]['table'] = $result; $res[$count]['name'] = @OCIResult($stmt, 1); $res[$count]['type'] = @OCIResult($stmt, 2); $res[$count]['len'] = @OCIResult($stmt, 3); $res[$count]['format'] = @OCIResult($stmt, 4); $res[$count]['nullable'] = @OCIResult($stmt, 5) == 'Y' ? true : false; $res[$count]['default'] = @OCIResult($stmt, 6); if ($mode & DB_TABLEINFO_ORDER) { $res['order'][$res[$count]['name']] = $count; } if ($mode & DB_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$count]['table']][$res[$count]['name']] = $count; } $count++; } $res['num_fields'] = $count; @OCIFreeStatement($stmt); } else { // else we want information about a resultset if ($result === $this->last_stmt) { $count = @OCINumCols($result); for ($i = 0; $i < $count; $i++) { $res[$i]['name'] = @OCIColumnName($result, $i + 1); $res[$i]['type'] = @OCIColumnType($result, $i + 1); $res[$i]['len'] = @OCIColumnSize($result, $i + 1); $q_fields = "select table_name, data_precision, nullable, data_default from user_tab_columns where column_name='" . $res[$i]['name'] . "'"; if (!($stmt = OCIParse($this->connection, $q_fields))) { return $this->oci8RaiseError(); } if (!OCIExecute($stmt, OCI_DEFAULT)) { return $this->oci8RaiseError($stmt); } OCIFetch($stmt); $res[$i]['table'] = OCIResult($stmt, 1); $res[$i]['format'] = OCIResult($stmt, 2); $res[$i]['nullable'] = OCIResult($stmt, 3) == 'Y' ? true : false; $res[$i]['default'] = OCIResult($stmt, 4); OCIFreeStatement($stmt); if ($mode & DB_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & DB_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } $res['num_fields'] = $count; } else { return $this->raiseError(DB_ERROR_NOT_CAPABLE); } } return $res; }
/** * Returns information about a table or a result set * * NOTE: only supports 'table' and 'flags' if <var>$result</var> * is a table name. * * NOTE: flags won't contain index information. * * @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'; } $count = @OCINumCols($resource); $res = array(); if ($mode) { $res['num_fields'] = $count; } $db->loadModule('Datatype', null, true); for ($i = 0; $i < $count; $i++) { $column = array('table' => '', 'name' => $case_func(@OCIColumnName($resource, $i + 1)), 'type' => @OCIColumnType($resource, $i + 1), 'length' => @OCIColumnSize($resource, $i + 1), 'flags' => ''); $res[$i] = $column; $res[$i]['mdb2type'] = $db->datatype->mapNativeDatatype($res[$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; } } return $res; }
/** * Returns information about a table or a result set. * * NOTE: only supports 'table' and 'flags' if <var>$result</var> * is a table name. * * NOTE: flags won't contain index information. * * @param object|string $result MDB2_result object from a query or a * string containing the name of a table * @param int $mode a valid tableInfo mode * @return array an associative array with the information requested * or an error object if something is wrong * @access public * @internal * @see MDB2_Driver_Common::tableInfo() */ function tableInfo($result, $mode = null) { $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; if ($db->options['portability'] & MDB2_PORTABILITY_LOWERCASE) { $case_func = 'strtolower'; } else { $case_func = 'strval'; } if (is_string($result)) { /* * Probably received a table name. * Create a result resource identifier. */ if (MDB2::isError($connect = $db->connect())) { return $connect; } $result = strtoupper($result); $q_fields = 'SELECT column_name, data_type, data_length, ' . 'nullable ' . 'FROM user_tab_columns ' . "WHERE table_name='{$result}' ORDER BY column_id"; $db->last_query = $q_fields; if (!($stmt = @OCIParse($db->connection, $q_fields))) { return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA); } if (!@OCIExecute($stmt, OCI_DEFAULT)) { return $db->raiseError($stmt); } $i = 0; while (@OCIFetch($stmt)) { $res[$i]['table'] = $case_func($result); $res[$i]['name'] = $case_func(@OCIResult($stmt, 1)); $res[$i]['type'] = @OCIResult($stmt, 2); $res[$i]['len'] = @OCIResult($stmt, 3); $res[$i]['flags'] = @OCIResult($stmt, 4) == 'N' ? 'not_null' : ''; 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; } $i++; } if ($mode) { $res['num_fields'] = $i; } @OCIFreeStatement($stmt); } else { /* * Probably received a result object. * Extract the result resource identifier. */ $id = $result->getResource(); if (empty($id)) { return $db->raiseError(); } # if ($result === $db->last_stmt) { $count = @OCINumCols($id); for ($i = 0; $i < $count; $i++) { $res[$i]['table'] = ''; $res[$i]['name'] = $case_func(@OCIColumnName($id, $i + 1)); $res[$i]['type'] = @OCIColumnType($id, $i + 1); $res[$i]['len'] = @OCIColumnSize($id, $i + 1); $res[$i]['flags'] = ''; 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; } } if ($mode) { $res['num_fields'] = $i; } # } else { # return $db->raiseError(MDB2_ERROR_NOT_CAPABLE); # } } return $res; }
function query($query) { $return_value = 0; // Flush cached values.. $this->flush(); // Log how the function was called $this->func_call = "\$db->query(\"{$query}\")"; // Keep track of the last query for debug.. $this->last_query = $query; $this->count(true, true); // Use core file cache function if ($cache = $this->get_cache($query)) { return $cache; } // If there is no existing database connection then try to connect if (!isset($this->dbh) || !$this->dbh) { $this->connect($this->dbuser, $this->dbpassword, $this->dbname); } // Parses the query and returns a statement.. if (!($stmt = OCIParse($this->dbh, $query))) { $error = OCIError($this->dbh); $this->register_error($error["message"]); $this->show_errors ? trigger_error($error["message"], E_USER_WARNING) : null; return false; } elseif (!($this->result = OCIExecute($stmt))) { $error = OCIError($stmt); $this->register_error($error["message"]); $this->show_errors ? trigger_error($error["message"], E_USER_WARNING) : null; return false; } // If query was an insert $is_insert = false; if (preg_match('/^(insert|delete|update|create) /i', $query)) { $is_insert = true; // num afected rows $return_value = $this->rows_affected = @OCIRowCount($stmt); } else { // Get column information if ($num_cols = @OCINumCols($stmt)) { // Fetch the column meta data for ($i = 1; $i <= $num_cols; $i++) { $this->col_info[$i - 1]->name = @OCIColumnName($stmt, $i); $this->col_info[$i - 1]->type = @OCIColumnType($stmt, $i); $this->col_info[$i - 1]->size = @OCIColumnSize($stmt, $i); } } // If there are any results then get them if ($this->num_rows = @OCIFetchStatement($stmt, $results)) { // Convert results into object orientated results.. // Due to Oracle strange return structure - loop through columns foreach ($results as $col_title => $col_contents) { $row_num = 0; // then - loop through rows foreach ($col_contents as $col_content) { $this->last_result[$row_num]->{$col_title} = $col_content; $row_num++; } } } // num result rows $return_value = $this->num_rows; } // disk caching of queries $this->store_cache($query, $is_insert); // If debug ALL queries $this->trace || $this->debug_all ? $this->debug() : null; return $return_value; }
function query($query) { // For reg expressions $query = trim($query); $return_value = 0; // Flush cached values.. $this->flush(); // Log how the function was called $this->func_call = "\$db->query(\"{$query}\")"; // Keep track of the last query for debug.. $this->last_query = $query; // Parses the query and returns a statement.. if (!($stmt = OCIParse($this->dbh, $query))) { $this->print_error("Last Query", $query); } elseif (!($this->result = OCIExecute($stmt))) { $this->print_error("Last Query", $query); } $this->num_queries++; // If query was an insert if (preg_match('/^(insert|delete|update|create)\\s+/i', $query)) { // num afected rows $return_value = $this->rows_affected = OCIRowCount($stmt); } else { // Get column information if ($num_cols = @OCINumCols($stmt)) { // Fetch the column meta data for ($i = 1; $i <= $num_cols; $i++) { $this->col_info[$i - 1]->name = OCIColumnName($stmt, $i); $this->col_info[$i - 1]->type = OCIColumnType($stmt, $i); $this->col_info[$i - 1]->size = OCIColumnSize($stmt, $i); } } // If there are any results then get them if ($this->num_rows = @OCIFetchStatement($stmt, $results)) { // Convert results into object orientated results.. // Due to Oracle strange return structure - loop through columns foreach ($results as $col_title => $col_contents) { $row_num = 0; // then - loop through rows foreach ($col_contents as $col_content) { $this->last_result[$row_num]->{$col_title} = $col_content; $row_num++; } } } // num result rows $return_value = $this->num_rows; } // If debug ALL queries $this->trace || $this->debug_all ? $this->debug() : null; return $return_value; }
/** returns type of field */ function fieldsize($field, $stmt = FALSE) { if (!$stmt) { $stmt = $this->stmt; } return @OCIColumnSize($stmt, $field); }
function query($query) { //去掉查询语句的前后空格 $query = trim($query); $return_value = 0; //清空缓存.. $this->flush(); //记录此函数如何被调用,用于调试... $this->func_call = "\$db->query(\"{$query}\")"; //跟踪最后查询语句,用于调试.. $this->last_query = $query; //解析查询语句 if (!($stmt = OCIParse($this->dbh, $query))) { $this->print_error(); } elseif (!($this->result = OCIExecute($stmt))) { $this->print_error(); } $this->num_queries++; //执行insert, delete, update, replace操作 if (preg_match('/^(insert|delete|update|create)\\s+/i', $query)) { //获取操作所影响的记录行数 $return_value = $this->rows_affected = OCIRowCount($stmt); } else { //获取字段信息 if ($num_cols = @OCINumCols($stmt)) { for ($i = 1; $i <= $num_cols; $i++) { $this->col_info[$i - 1]->name = OCIColumnName($stmt, $i); $this->col_info[$i - 1]->type = OCIColumnType($stmt, $i); $this->col_info[$i - 1]->size = OCIColumnSize($stmt, $i); } } //获取查询结果 if ($this->num_rows = @OCIFetchStatement($stmt, $results)) { //将结果集转变成对象,因为oracle的返回结果比较奇怪:) foreach ($results as $col_title => $col_contents) { $row_num = 0; //循环所有的行 foreach ($col_contents as $col_content) { $this->last_result[$row_num]->{$col_title} = $col_content; $row_num++; } } } //获取查询结果行数 $return_value = $this->num_rows; } //是否显示所有的查询信息 $this->debug_all ? $this->debug() : null; return $return_value; }