mssql.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,425 行 · 第 1/4 页
PHP
1,425 行
// }}} // {{{ getBlobValue() /** * Convert a text value into a DBMS specific format that is suitable to * compose query statements. * * @param resource $prepared_query query handle from prepare() * @param $parameter * @param $blob * @return string text string that represents the given argument value in * a DBMS specific format. * @access public */ function getBlobValue($prepared_query, $parameter, $blob) { $value = "0x"; while(!$this->endOfLob($blob)) { if (MDB::isError($result = $this->readLob($blob, $data, $this->options['lob_buffer_length']))) { return($result); } $value.= Bin2Hex($data); } return($value); } // }}} // {{{ freeBlobValue() /** * free a binary large object * * @param resource $prepared_query query handle from prepare() * @param string $blob * @return MDB_OK * @access public */ function freeBlobValue($prepared_query, $blob) { unset($this->lobs[$blob]); return(MDB_OK); } // }}} // {{{ getBooleanValue() /** * Convert a text value into a DBMS specific format that is suitable to * compose query statements. * * @param string $value text string value that is intended to be converted. * @return string text string that represents the given argument value in * a DBMS specific format. * @access public */ function getBooleanValue($value) { return(($value === NULL) ? 'NULL' : $value); } // }}} // {{{ getFloatValue() /** * Convert a text value into a DBMS specific format that is suitable to * compose query statements. * * @param string $value text string value that is intended to be converted. * @return string text string that represents the given argument value in * a DBMS specific format. * @access public */ function getFloatValue($value) { return(($value === NULL) ? 'NULL' : $value); } // }}} // {{{ getDecimalValue() /** * Convert a text value into a DBMS specific format that is suitable to * compose query statements. * * @param string $value text string value that is intended to be converted. * @return string text string that represents the given argument value in * a DBMS specific format. * @access public */ function getDecimalValue($value) { return(($value === NULL) ? 'NULL' : $value); } // }}} // {{{ nextId() /** * returns the next free id of a sequence * * @param string $seq_name name of the sequence * @param boolean $ondemand when true the seqence is * automatic created, if it * not exists * * @return mixed MDB_Error or id * @access public */ function nextId($seq_name, $ondemand = TRUE) { $sequence_name = $this->getSequenceName($seq_name); $this->expectError(MDB_ERROR_NOSUCHTABLE); $result = $this->query("INSERT INTO $sequence_name DEFAULT VALUES"); $this->popExpect(); if ($ondemand && MDB::isError($result) && $result->getCode() == MDB_ERROR_NOSUCHTABLE) { // Since we are creating the sequence on demand // we know the first id = 1 so initialize the // sequence at 2 $result = $this->createSequence($seq_name, 2); if (MDB::isError($result)) { return($this->raiseError(MDB_ERROR, NULL, NULL, 'Next ID: on demand sequence could not be created')); } else { // First ID of a newly created sequence is 1 return(1); } } $value = $this->queryOne("SELECT @@IDENTITY FROM $sequence_name", 'integer'); if (MDB::isError($value)) { return($value); } $result = $this->query("DELETE FROM $sequence_name WHERE ".$this->options['sequence_col_name']." < $value"); if (MDB::isError($result)) { $this->warnings[] = 'nextID: could not delete previous sequence table values'; } return($value); } // }}} // {{{ fetchInto() /** * Fetch a row and insert the data into an existing array. * * @param resource $result result identifier * @param int $fetchmode how the array data should be indexed * @param int $rownum the row number to fetch * @return int data array on success, a MDB error on failure * @access public */ function fetchInto($result, $fetchmode = MDB_FETCHMODE_DEFAULT, $rownum = NULL) { $result_value = intval($result); if (is_null($rownum)) { ++$this->highest_fetched_row[$result_value]; } else { $this->highest_fetched_row[$result_value] = max($this->highest_fetched_row[$result_value], $rownum); if (isset($this->limits[$result_value])) { $rownum = $rownum + $this->limits[$result_value][0]; } if (!@mssql_data_seek($result, $rownum)) { return(NULL); } } if ($fetchmode == MDB_FETCHMODE_DEFAULT) { $fetchmode = $this->fetchmode; } if ($fetchmode & MDB_FETCHMODE_ASSOC) { $row = @mssql_fetch_assoc($result); if (is_array($row) && $this->options['optimize'] == 'portability') { $row = array_change_key_case($row, CASE_LOWER); } } else { $row = @mssql_fetch_row($result); } if (!$row) { if($this->options['autofree']) { $this->freeResult($result); } return(NULL); } if (isset($this->result_types[$result_value])) { $row = $this->convertResultRow($result, $row); } return($row); } // }}} // {{{ tableInfo() /** * 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 (MDB_TABLEINFO_ORDERTABLE or * MDB_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 MDB_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 MDB_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 MDB_TABLEINFO_ORDER * * you can combine MDB_TABLEINFO_ORDER and * MDB_TABLEINFO_ORDERTABLE with MDB_TABLEINFO_ORDER | * MDB_TABLEINFO_ORDERTABLE * or with MDB_TABLEINFO_FULL */ // if $result is a string, then we want information about a // table without a resultset if (is_string($result)) { if (!@mssql_select_db($this->database_name, $this->connection)) { return $this->mssqlRaiseError(); } $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); // We only support flags for tables $res[$i]['flags'] = is_string($result) ? $this->_mssql_field_flags($result, $res[$i]['name']) : ''; } } 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); // We only support flags for tables $res[$i]['flags'] = is_string($result) ? $this->_mssql_field_flags($result, $res[$i]['name']) : ''; if ($mode & MDB_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & MDB_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); } // }}} // {{{ _mssql_field_flags() /** * Get the flags for a field, currently only supports "isnullable" and "primary_key" * * @param string The table name * @param string The field * @access private */ function _mssql_field_flags($table, $column) { static $current_table = NULL; static $flags; // At the first call we discover the flags for all fields if ($table != $current_table) { $flags = array(); // find nullable fields $q_nulls = "SELECT syscolumns.name, syscolumns.isnullable FROM sysobjects INNER JOIN syscolumns ON sysobjects.id = syscolumns.id WHERE sysobjects.name ='$table' AND syscolumns.isnullable = 1"; $res = $this->query($q_nulls, NULL, FALSE); $res = $this->fetchAll($res, MDB_FETCHMODE_ASSOC); foreach ($res as $data) { if ($data['isnullable'] == 1) { $flags[$data['name']][] = 'isnullable'; } } // find primary keys $res2 = $this->query("EXEC SP_PKEYS[$table]", NULL, FALSE); $res2 = $this->fetchAll($res, MDB_FETCHMODE_ASSOC); foreach ($res2 as $data) { if (!empty($data['COLUMN_NAME'])) { $flags[$data['COLUMN_NAME']][] = 'primary_key'; } } $current_table = $table; } if (isset($flags[$column])) { return(implode(',', $flags[$column])); } return(''); } // }}}}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?