/** * Returns information about a table or a result set * * NOTE: doesn't support table name and flags if called from a db_result * * @param mixed $resource SQL Server result identifier or table name * @param int $mode A valid tableInfo mode (DB_TABLEINFO_ORDERTABLE or * DB_TABLEINFO_ORDER) * * @return array An array with all the information */ function tableInfo($result, $mode = null) { $count = 0; $id = 0; $res = array(); /* * depending on $mode, metadata returns the following values: * * - mode is false (default): * $result[]: * [0]["table"] table name * [0]["name"] field name * [0]["type"] field type * [0]["len"] field length * [0]["flags"] field flags * * - mode is DB_TABLEINFO_ORDER * $result[]: * ["num_fields"] number of metadata records * [0]["table"] table name * [0]["name"] field name * [0]["type"] field type * [0]["len"] field length * [0]["flags"] field flags * ["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['meta']['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, then we want information about a // table without a resultset if (is_string($result)) { $id = mssql_query("SELECT * FROM {$result}", $this->connection); if (empty($id)) { return $this->mssqlRaiseError(); } } else { // else we want information about a resultset $id = $result; if (empty($id)) { return $this->mssqlRaiseError(); } } $count = @mssql_num_fields($id); // made this IF due to performance (one if is faster than $count if's) if (empty($mode)) { for ($i = 0; $i < $count; $i++) { $res[$i]['table'] = is_string($result) ? $result : ''; $res[$i]['name'] = @mssql_field_name($id, $i); $res[$i]['type'] = @mssql_field_type($id, $i); $res[$i]['len'] = @mssql_field_length($id, $i); $res[$i]['flags'] = ''; } } else { // full $res['num_fields'] = $count; for ($i = 0; $i < $count; $i++) { $res[$i]['table'] = is_string($result) ? $result : ''; $res[$i]['name'] = @mssql_field_name($id, $i); $res[$i]['type'] = @mssql_field_type($id, $i); $res[$i]['len'] = @mssql_field_length($id, $i); $res[$i]['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; } } } // free the result only if we were called on a table if (is_string($result)) { @mssql_free_result($id); } 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. * * @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 (is_string($result)) { /* * Probably received a table name. * Create a result resource identifier. */ if (!@mssql_select_db($this->_db, $this->connection)) { return $this->mssqlRaiseError(DB_ERROR_NODBSELECTED); } $id = @mssql_query("SELECT * FROM {$result} WHERE 1=0", $this->connection); $got_string = true; } elseif (isset($result->result)) { /* * Probably received a result object. * Extract the result resource identifier. */ $id = $result->result; $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 $this->mssqlRaiseError(DB_ERROR_NEED_MORE_DATA); } if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) { $case_func = 'strtolower'; } else { $case_func = 'strval'; } $count = @mssql_num_fields($id); $res = array(); if ($mode) { $res['num_fields'] = $count; } for ($i = 0; $i < $count; $i++) { if ($got_string) { $flags = $this->_mssql_field_flags($result, @mssql_field_name($id, $i)); if (DB::isError($flags)) { return $flags; } } else { $flags = ''; } $res[$i] = array('table' => $got_string ? $case_func($result) : '', 'name' => $case_func(@mssql_field_name($id, $i)), 'type' => @mssql_field_type($id, $i), 'len' => @mssql_field_length($id, $i), 'flags' => $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; } } // 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. * * 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 * @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; } $id = @mssql_query("SELECT * FROM {$result} WHERE 1=0", $db->connection); $got_string = true; } else { /* * Probably received a result object. * Extract the result resource identifier. */ $id = $result->getResource(); if (empty($id)) { return $db->raiseError(); } $got_string = false; } if (!is_resource($id)) { return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA); } $count = @mssql_num_fields($id); // made this IF due to performance (one if is faster than $count if's) if (!$mode) { for ($i = 0; $i < $count; $i++) { $res[$i]['table'] = $got_string ? $case_func($result) : ''; $res[$i]['name'] = $case_func(@mssql_field_name($id, $i)); $res[$i]['type'] = @mssql_field_type($id, $i); $res[$i]['len'] = @mssql_field_length($id, $i); // We only support flags for tables $res[$i]['flags'] = $got_string ? $this->_mssql_field_flags($result, $res[$i]['name']) : ''; } } else { // full $res['num_fields'] = $count; for ($i = 0; $i < $count; $i++) { $res[$i]['table'] = $got_string ? $case_func($result) : ''; $res[$i]['name'] = $case_func(@mssql_field_name($id, $i)); $res[$i]['type'] = @mssql_field_type($id, $i); $res[$i]['len'] = @mssql_field_length($id, $i); // We only support flags for tables $res[$i]['flags'] = $got_string ? $this->_mssql_field_flags($result, $res[$i]['name']) : ''; 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 * * 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'; } $count = @mssql_num_fields($resource); $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(@mssql_field_name($resource, $i)), 'type' => @mssql_field_type($resource, $i), 'length' => @mssql_field_length($resource, $i), '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 query($query) { $query = trim($query); $query = str_replace('`', "'", $query); $query = str_replace('\\012', "\n", $query); $query = str_replace('\\015', "\r", $query); //$query = str_replace("\n",'',$query); //$query = str_replace("\r",'',$query); //$query = $this->escape($query); //$query = str_replace('"','\"',$query); //$query = str_replace("'","\'",$query); if (substr($query, 0, 7) == 'SELECT ') { dbug($query, 'SELECT : ORIGINAL QUERY ::::::: ', 'orange'); $query_arr = explode('LIMIT', $query); if ($query_arr[0] and $query_arr[1]) { $query_one = str_replace('SELECT ', '', $query_arr[0]); $top_arr = explode(' ', $query_arr[1]); if ($top_arr[1] && $top_arr[2]) { $query = 'SELECT TOP ' . $top_arr[2] . ' ' . $query_one; } else { $query = 'SELECT TOP ' . $top_arr[1] . ' ' . $query_one; } /* // TO BE DEVELOP //LIMIT 1, 3 SELECT * FROM ( SELECT TOP 3 * FROM (SELECT TOP (1+3) * FROM wp_posts WHERE 1=1 AND post_type = 'post' AND (post_status = 'publish' OR post_status = 'private') ORDER BY post_date DESC) AS table1 ORDER BY post_date ASC ) AS table2 ORDER BY post_date DESC */ //pre($query_arr); //exit(); } if (substr($query, 0, 23) == "SELECT TOP 1 comment_ID") { $query = str_replace(' = ', ' = ', $query); } /* DUMP DIRTY SWEPT TEST */ $query = str_replace('wp_posts.', '', $query); /* hacks for query; - SELECT * FROM wp_posts WHERE (post_type = 'page' AND post_status = 'publish') ORDER BY menu_order, post_title ASC - SELECT * FROM wp_posts WHERE 1=1 AND post_type = 'page' AND (post_status = 'publish' OR post_status = 'future' OR post_status = 'draft' OR post_status = 'pending' OR post_status = 'private') ORDER BY menu_order,post_title asc notes; somehow Wordpress developer create this 'post_title' date-type as 'text' in database. the post_title should be varchar since the amount of character in title is generally less then 255 char. and in ms-sql, text cannot be sorted. so this is a big mistake. haihhh... */ $pattern = '/post_title asc/is'; $replacement = 'cast(post_title as varchar(500)) ASC'; $query = preg_replace($pattern, $replacement, $query); /* hacks for query; - SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM wp_posts WHERE post_type = 'post' ORDER BY post_date DESC notes; this is one another hacks for SELECT DISTINCT call. The above query tries to sort by the column post_date. Because the keyword DISTINCT is also specified, column post_date must appear in the SELECT list expected return; - SELECT DISTINCT post_date, YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM wp_posts WHERE post_type = 'post' ORDER BY post_date DESC */ if (substr($query, 0, 15) == "SELECT DISTINCT") { $string = $query; $pattern = '/SELECT DISTINCT (.*)? ORDER BY (.*)? (asc|desc)/i'; $replacement = 'cast(post_title as varchar(500)) ASC'; preg_match_all($pattern, $string, $result); $order_by_col = $result[2][0]; $col_calls = $result[1][0]; $order_by_prop = $result[3][0]; $query = 'SELECT DISTINCT ' . $order_by_col . ', ' . $col_calls . ' ORDER BY ' . $order_by_col . ' ' . $order_by_prop; } /* hacks for query; - SELECT user_id FROM wp_usermeta WHERE meta_key = 'wp_user_level' AND meta_value != '0' notes; replace != into NOT <col_name> LIKE expected return; - SELECT user_id FROM wp_usermeta WHERE meta_key = 'wp_user_level' AND NOT meta_value LIKE '0' */ $pattern = "/ (?:[a-z][a-z0-9_]*)? \\!\\= /is"; preg_match_all($pattern, $query, $result); $result_arr = $result[0]; $query = str_replace('!=', '', $query); foreach ($result_arr as $result_item) { $result_item = str_replace('!=', '', $result_item); $result_item = trim($result_item); $query = str_replace($result_item, ' NOT ' . $result_item . ' LIKE ', $query); } /* hacks for; - SQL have 'GROUP BY' - SELECT DISTINCT post_date, YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC */ if (preg_match("/GROUP BY/i", $query)) { $pattern = '/SELECT (.*) GROUP BY (.*)? ORDER BY (.*)/i'; preg_match($pattern, $string, $output); $query0 = 'SELECT ' . $output[1] . ' ORDER BY ' . $output[3]; } $query = str_replace('SQL_CALC_FOUND_ROWS', ' ', $query); if (preg_match("/SELECT FOUND_ROWS()/i", $query)) { $last_query = $this->last_query; $last_query_one_arr = explode('TOP', $last_query); $last_query_two = trim($last_query_one_arr[1]); $last_query_three_arr = explode(' ', $last_query_two); $last_query_four = str_replace($last_query_three_arr[0], '', $last_query_two); $query = 'SELECT ' . $last_query_four; } dbug($query, 'SELECT : MODDED QUERY :::::::: ', 'orange'); } elseif (substr($query, 0, 7) == 'INSERT ') { dbug($query, 'INSERT : ORIGINAL QUERY ::::::::: ', 'purple'); $pattern = '/INSERT INTO (.*)?\\((.*)\\).*?VALUES.*?\\((.*)\\)/is'; preg_match_all($pattern, $query, $output); $insert_table = trim($output[1][0]); $insert_cols = trim($output[2][0]); $insert_values = trim($output[3][0]); /* STRIP QUOTE FROM COLS NAME */ $insert_cols = str_replace("'", '', $insert_cols); $query = $this->prepare($query); /* PROCESS THE $insert_values. NEED TO REPLACE THE INNER QUOTES */ preg_match_all("/([0-9]+|\\'(.*?)\\'[ ]*?),/is", $insert_values . ',', $output); $insert_values_arr = array(); //PREPARE THE NEW FRESH VALUES foreach ($output[0] as $insert_values_item) { if (substr(trim($insert_values_item), 0, 1) == "'") { $insert_values_item = substr($insert_values_item, 1); // TAKE OUT THE FIRST 1 CHAR, WHICH IS QUOTE } $insert_values_item = trim($insert_values_item); // TRIM THE WHITESPACE if (substr(trim($insert_values_item), -1) == ",") { $insert_values_item = substr($insert_values_item, 0, -1); // TAKE OUT THE LAST 1 CHAR, WHICH IS COMMA } if (substr(trim($insert_values_item), -1) == "'") { $insert_values_item = substr($insert_values_item, 0, -1); // TAKE OUT THE LAST 1 CHAR, WHICH IS QUOTE } $insert_values_item = str_replace("'", "''", $insert_values_item); // NOW WE PUT THE EXTRA QUOTE ON IT $insert_values_item = trim($insert_values_item); // TRIM THE WHITESPACE $insert_values_item = "'" . $insert_values_item . "'"; // WRAP THE VALUES WITH OUR SINGLE-QUOTE $insert_values_arr[] = $insert_values_item; } $insert_values = implode(',', $insert_values_arr); /* CONSTRUCT NEW INSERT CALL */ $query = 'INSERT INTO ' . $insert_table . ' (' . $insert_cols . ') VALUES (' . $insert_values . ');'; dbug($query, 'INSERT : MODDED QUERY :::::::::: ', 'purple'); } elseif (substr($query, 0, 7) == 'UPDATE ') { dbug($query, 'UPDATE : ORIGINAL QUERY ::::::::: ', 'blue'); $pattern = '/UPDATE ([A-Za-z0-9_-]+) SET (.*)? WHERE (.*)?/is'; preg_match_all($pattern, $query, $output); $update_table = trim($output[1][0]); $update_values = trim($output[2][0]); $update_where = trim($output[3][0]); preg_match_all("/(.*?)[ *]?=[ *]?(\\'(.*?)\\'|[0-9]+),/is", $update_values . ',', $output_vals); $update_vals_colname = $output_vals[1]; $update_vals_values = $output_vals[2]; $update_vals_colname_mod = array(); foreach ($update_vals_colname as $update_vals_colname_item) { $update_vals_colname_item = str_replace("'", '', $update_vals_colname_item); // GREEDY REPLACE ALL QUOTES. NO QUOTES ALLOWED IN COL NAME $update_vals_colname_item = trim($update_vals_colname_item); // TRIM THE WHITESPACE $update_vals_colname_mod[] = $update_vals_colname_item; } $update_vals_values_mod = array(); foreach ($update_vals_values as $update_vals_values_item) { if (substr(trim($update_vals_values_item), 0, 1) == "'") { $update_vals_values_item = substr($update_vals_values_item, 1); // TAKE OUT THE FIRST 1 CHAR, WHICH IS QUOTE } if (substr(trim($update_vals_values_item), -1) == "'") { $update_vals_values_item = substr($update_vals_values_item, 0, -1); // TAKE OUT THE LAST 1 CHAR, WHICH IS QUOTE } $update_vals_values_item = trim($update_vals_values_item); // TRIM THE WHITESPACE $update_vals_values_item = str_replace("'", "''", $update_vals_values_item); // ADD ADDITIONAL QUOTE TO ESCAPE IT IN MSSQL $update_vals_values_mod[] = $update_vals_values_item; } $update_values_modded = array_combine($update_vals_colname_mod, $update_vals_values_mod); $update_values = ''; $update_values_prepare_arr = ''; $update_values_item = ''; foreach ($update_values_modded as $update_values_item_colname => $update_values_item_value) { $update_values_prepare_arr[] = $update_values_item_colname . " = '" . $update_values_item_value . "'"; } $update_values = implode(',', $update_values_prepare_arr); /* CONSTRUCT NEW UPDATE CALL */ $query = 'UPDATE ' . $update_table . ' SET ' . $update_values . ' WHERE ' . $update_where . ';'; dbug($query, 'UPDATE : MODDED QUERY :::::::::: ', 'blue'); } elseif (substr($query, 0, 7) == 'DELETE ') { dbug($query, 'DELETE', 'orange'); } elseif (substr($query, 0, 7) == 'CREATE ') { $query_two = str_replace('auto_increment', 'IDENTITY(1,1)', $query); $query_two = str_replace('tinytext', 'text', $query_two); $query_two = str_replace('longtext', 'text', $query_two); $query_two = str_replace('mediumtext', 'text', $query_two); $query_two = str_replace('unsigned ', '', $query_two); $query_three = preg_replace('/bigint\\(\\d+\\)/i', 'int', $query_two); $query_three = preg_replace('/int\\(\\d+\\)/i', 'int', $query_three); $query_four_arr = explode('PRIMARY KEY', $query_three); $query_five = $query_four_arr[0] . ' '; $query_six = $query_four_arr[1]; $query_seven_arr = explode('),', $query_six); $query_eight_arr = explode('(', $query); $query_nine = trim($query_eight_arr[0]); $query_ten = str_replace($query_nine, '', $query_five); $table_name = trim(str_replace('CREATE TABLE', '', $query_nine)); $create_table_header = "\t\t\t\t\nIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[tretinoin_wpuser].[" . $table_name . "]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)\n DROP TABLE [tretinoin_wpuser].[" . $table_name . "]\nGO\nCREATE TABLE [tretinoin_wpuser].[" . $table_name . "]"; $create_table_header_simple = "CREATE TABLE [tretinoin_wpuser].[" . $table_name . "]\t\t\t\n\t\t\t\t\n\t\t\t\t"; $query = $create_table_header_simple . $query_ten . ' PRIMARY KEY ' . $query_seven_arr[0] . '))'; dbug($query, 'CREATE', 'purple'); } elseif (substr($query, 0, 5) == 'SHOW ') { $query = str_replace('SHOW TABLES;', '', $query); dbug($query, 'SHOW', 'yellow'); } else { dbug($query); } // For reg expressions $query = trim($query); // 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; // Perform the query via std mssql_query function.. if (substr($query, 0, 7) == 'SELECT ') { $this->result_check = @mssql_query($query, $this->dbh); } elseif (substr($query, 0, 7) == 'INSERT ') { } elseif (substr($query, 0, 7) == 'UPDATE ') { $this->result_check = @mssql_query($query, $this->dbh); } elseif (substr($query, 0, 7) == 'DELETE ') { } elseif (substr($query, 0, 7) == 'CREATE ') { } elseif (substr($query, 0, 5) == 'SHOW ') { } // Unfortunately, PHP fuctions for MS SQL currently don't offer a decent way // to retrieve errors from MS SQL // Make sure not to run a query between the actual query and this one ! $get_errorcode = "SELECT @@ERROR as errorcode"; $error_res = @mssql_query($get_errorcode, $this->dbh); $errorcode = @mssql_result($error_res, 0, "errorcode"); // ERROR LIST // 402 : The data types text and varchar are incompatible in the equal to operator. // 306 : The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator. if ($errorcode == '402') { $query_two = str_replace(' = ', ' LIKE ', $query); /* NEED MORE IMPROVEMENT HERE */ } else { $query_two = $query; } dbug($query, $errorcode, 'green'); $this->result = @mssql_query($query_two, $this->dbh); $this->num_queries++; // If there was an insert, delete or update see how many rows were affected // (Also, If there there was an insert take note of the last OID $query_type = array("insert", "delete", "update", "replace"); // loop through the above array foreach ($query_type as $word) { // This is true if the query starts with insert, delete or update if (preg_match("/^{$word}\\s+/i", $query)) { $this->rows_affected = @mssql_rows_affected($this->dbh); // This gets the insert ID if ($word == "insert" || $word == "replace") { $get_last_ident = "SELECT @@IDENTITY as id"; $last_res = @mssql_query($get_last_ident, $this->dbh); $this->insert_id = @mssql_result($last_res, 0, "id"); // If insert id then return it - true evaluation return $this->insert_id; } // Set to false if there was no insert id $this->result = false; } } if ($errorcode != 0) { // there is an error $this->print_error(); } else { // ======================================================= // Take note of column info $i = 0; while ($i < @mssql_num_fields($this->result)) { $this->col_info[$i]->name = @mssql_field_name($this->result, $i); $this->col_info[$i]->type = @mssql_field_type($this->result, $i); $this->col_info[$i]->size = @mssql_field_length($this->result, $i); $i++; } // ======================================================= // Store Query Results $i = 0; while ($row_arr = @mssql_fetch_array($this->result)) { $row = array_to_object($row_arr); $this->last_result[$i] = $row; // Store relults as an objects within main array $i++; } if ($i == 0) { $this->last_result = array(); } //pre($this->last_result); // Log number of rows the query returned $this->num_rows = $i; //} @mssql_free_result($this->result); // If this was a select.. if (preg_match("/^(select|show|desc)\\s+/i", $query)) { // If debug ALL queries $this->debug_all ? $this->debug() : null; // If there were results then return true for $db->query if ($i) { return true; } else { return false; } } else { // If debug ALL queries $this->debug_all ? $this->debug() : null; // Update insert etc. was good.. return true; } } }
function query($query) { //去掉查询语句的前后空格 $query = trim($query); //初始化返回值为0 $return_val = 0; //清空缓存.. $this->flush(); //记录此函数如何被调用,用于调试... $this->func_call = "\$db->query(\"{$query}\")"; //跟踪最后查询语句,用于调试.. $this->last_query = $query; //通过mysql_query函数执行查询操作.. $this->result = @mssql_query($query, $this->dbh); $this->num_queries++; //php现在还不支持从sqlserver服务器获取错误信息 #这里以后要进行讨论,改进。 $get_errorcode = "SELECT @@ERROR as errorcode"; $error_res = @mssql_query($get_errorcode, $this->dbh); $errorcode = @mssql_result($error_res, 0, "errorcode"); //执行insert, delete, update, replace操作 if (preg_match("/^(insert|delete|update|replace)\\s+/i", $query)) { $this->rows_affected = @mssql_rows_affected($this->dbh); $return_val = $this->rows_affected; //获取操作所影响的记录行数 if (preg_match("/^(insert|replace)\\s+/i", $query)) { $get_last_ident = "SELECT @@IDENTITY as id"; $last_res = @mssql_query($get_last_ident, $this->dbh); $this->insert_id = @mssql_result($last_res, 0, "id"); //$return_val = $this->insert_id; } } if ($errorcode != 0) { //如果有错误 $this->print_error(); } else { //获取字段信息 $i = 0; while ($i < @mssql_num_fields($this->result)) { $this->col_info[$i]->name = @mssql_field_name($this->result, $i); $this->col_info[$i]->type = @mssql_field_type($this->result, $i); $this->col_info[$i]->size = @mssql_field_length($this->result, $i); $i++; } //获取查询结果 $i = 0; while ($row = @mssql_fetch_object($this->result)) { //取得包含数组的结果对象 $this->last_result[$i] = $row; $i++; } //获取查询结果行数 $this->num_rows = $i; @mssql_free_result($this->result); //返回选中的结果行数 $return_val = $this->num_rows; } //是否显示所有的查询信息 $this->debug_all ? $this->debug() : null; return $return_val; }
function query($query) { // For reg expressions $query = trim($query); // 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; // Perform the query via std mssql_query function.. $this->result = @mssql_query($query, $this->dbh); $this->num_queries++; // Unfortunately, PHP fuctions for MS SQL currently don't offer a decent way // to retrieve errors from MS SQL // Make sure not to run a query between the actual query and this one ! $get_errorcode = "SELECT @@ERROR as errorcode"; $error_res = @mssql_query($get_errorcode, $this->dbh); $errorcode = @mssql_result($error_res, 0, "errorcode"); // If there was an insert, delete or update see how many rows were affected // (Also, If there there was an insert take note of the last OID $query_type = array("insert", "delete", "update", "replace"); // loop through the above array foreach ($query_type as $word) { // This is true if the query starts with insert, delete or update if (preg_match("/^{$word}\\s+/i", $query)) { $this->rows_affected = @mssql_rows_affected($this->dbh); // This gets the insert ID if ($word == "insert" || $word == "replace") { $get_last_ident = "SELECT @@IDENTITY as id"; $last_res = @mssql_query($get_last_ident, $this->dbh); $this->insert_id = @mssql_result($last_res, 0, "id"); // If insert id then return it - true evaluation return $this->insert_id; } // Set to false if there was no insert id $this->result = false; } } if ($errorcode != 0) { // there is an error $this->print_error(); } else { // ======================================================= // Take note of column info $i = 0; while ($i < @mssql_num_fields($this->result)) { $this->col_info[$i]->name = @mssql_field_name($this->result, $i); $this->col_info[$i]->type = @mssql_field_type($this->result, $i); $this->col_info[$i]->size = @mssql_field_length($this->result, $i); $i++; } // ======================================================= // Store Query Results $i = 0; while ($row = @mssql_fetch_object($this->result)) { // Store relults as an objects within main array $this->last_result[$i] = $row; $i++; } // Log number of rows the query returned $this->num_rows = $i; //} @mssql_free_result($this->result); // If this was a select.. if (preg_match("/^(select|show|desc)\\s+/i", $query)) { // If debug ALL queries $this->debug_all ? $this->debug() : null; // If there were results then return true for $db->query if ($i) { return true; } else { return false; } } else { // If debug ALL queries $this->debug_all ? $this->debug() : null; // Update insert etc. was good.. return true; } } }
public function __construct($result_id, $x, $value) { try { $this->name = @mssql_field_name($result_id, $x); //If can't read field name if (!$this->name) throw new TException(TCustomMessage::GetMessage(5008), 5008); $this->size = @mssql_field_length($result_id, $x); //If can't read field size if (!$this->size) throw new TException(TCustomMessage::GetMessage(5008), 5008); $this->type = @mssql_field_type($result_id, $x); //If can't read field type if (!$this->type) throw new TException(TCustomMessage::GetMessage(5008), 5008); $this->value = $value; } catch (TException $e) { //... } }
function FieldLength($result, $offset) { switch ($this->dbType) { case "mssql": $r = mssql_field_length($result, $offset); break; case "mysql": $r = mysql_field_len($result, $offset); break; case "pg": $r = pg_fieldsize($result, $offset); break; default: $r = False; break; } return $r; }
/** * 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; }
function vty_field_len($list,$i){ switch($this->vtAdi){ case 'mysql': return mysql_field_len($list,$i); break; case 'odbc': return odbc_field_len($list,$i); break; case 'mssql': return mssql_field_length($list,$i); break; case 'postgresql': pg_field_len($list,$i); break; } }