adodb-lib.inc.php

来自「PhpWiki是sourceforge的一个开源项目」· PHP 代码 · 共 675 行 · 第 1/2 页

PHP
675
字号
		// Before returning the RecordSet, we set the pagination properties we need	if ($rsreturn) {		$rsreturn->rowsPerPage = $nrows;		$rsreturn->AbsolutePage($page);		$rsreturn->AtFirstPage($atfirstpage);		$rsreturn->AtLastPage($atlastpage);	}	return $rsreturn;}function _adodb_getupdatesql(&$zthis,&$rs, $arrFields,$forceUpdate=false,$magicq=false){		if (!$rs) {			printf(ADODB_BAD_RS,'GetUpdateSQL');			return false;		}			$fieldUpdatedCount = 0;		$arrFields = _array_change_key_case($arrFields);		$hasnumeric = isset($rs->fields[0]);		$setFields = '';				// Loop through all of the fields in the recordset		for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) {			// Get the field from the recordset			$field = $rs->FetchField($i);			// If the recordset field is one			// of the fields passed in then process.			$upperfname = strtoupper($field->name);			if (adodb_key_exists($upperfname,$arrFields)) {				// If the existing field value in the recordset				// is different from the value passed in then				// go ahead and append the field name and new value to				// the update query.								if ($hasnumeric) $val = $rs->fields[$i];				else if (isset($rs->fields[$upperfname])) $val = $rs->fields[$upperfname];				else if (isset($rs->fields[$field->name])) $val =  $rs->fields[$field->name];				else if (isset($rs->fields[strtolower($upperfname)])) $val =  $rs->fields[strtolower($upperfname)];				else $val = '';											if ($forceUpdate || strcmp($val, $arrFields[$upperfname])) {					// Set the counter for the number of fields that will be updated.					$fieldUpdatedCount++;					// Based on the datatype of the field					// Format the value properly for the database				$type = $rs->MetaType($field->type);										// is_null requires php 4.0.4				if ((defined('ADODB_FORCE_NULLS') && is_null($arrFields[$upperfname])) || 					$arrFields[$upperfname] === 'null') {					$setFields .= $field->name . " = null, ";				} else {					if ($type == 'null') {						$type = 'C';					}					//we do this so each driver can customize the sql for					//DB specific column types. 					//Oracle needs BLOB types to be handled with a returning clause					//postgres has special needs as well					$setFields .= _adodb_column_sql($zthis, 'U', $type, $upperfname,													  $arrFields, $magicq);				}			}		}	}		// If there were any modified fields then build the rest of the update query.		if ($fieldUpdatedCount > 0 || $forceUpdate) {					// Get the table name from the existing query.			preg_match("/FROM\s+".ADODB_TABLE_REGEX."/is", $rs->sql, $tableName);				// Get the full where clause excluding the word "WHERE" from			// the existing query.			preg_match('/\sWHERE\s(.*)/is', $rs->sql, $whereClause);						$discard = false;			// not a good hack, improvements?			if ($whereClause)				preg_match('/\s(LIMIT\s.*)/is', $whereClause[1], $discard);			else				$whereClause = array(false,false);							if ($discard)				$whereClause[1] = substr($whereClause[1], 0, strlen($whereClause[1]) - strlen($discard[1]));					$sql = 'UPDATE '.$tableName[1].' SET '.substr($setFields, 0, -2);		if (strlen($whereClause[1]) > 0) 			$sql .= ' WHERE '.$whereClause[1];		return $sql;		} else {			return false;	}}function adodb_key_exists($key, &$arr){	if (!defined('ADODB_FORCE_NULLS')) {		// the following is the old behaviour where null or empty fields are ignored		return (!empty($arr[$key])) || (isset($arr[$key]) && strlen($arr[$key])>0);	}	if (isset($arr[$key])) return true;	## null check below	if (ADODB_PHPVER >= 0x4010) return array_key_exists($key,$arr);	return false;}/** * There is a special case of this function for the oci8 driver. * The proper way to handle an insert w/ a blob in oracle requires * a returning clause with bind variables and a descriptor blob. *  *  */function _adodb_getinsertsql(&$zthis,&$rs,$arrFields,$magicq=false){	$tableName = '';	$values = '';	$fields = '';	$recordSet = null;	$arrFields = _array_change_key_case($arrFields);	$fieldInsertedCount = 0;		if (is_string($rs)) {		//ok we have a table name		//try and get the column info ourself.		$tableName = $rs;						//we need an object for the recordSet		//because we have to call MetaType.		//php can't do a $rsclass::MetaType()		$rsclass = $zthis->rsPrefix.$zthis->databaseType;		$recordSet =& new $rsclass(-1,$zthis->fetchMode);		$recordSet->connection = &$zthis;			$columns = $zthis->MetaColumns( $tableName );	} else if (is_subclass_of($rs, 'adorecordset')) {		for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) 			$columns[] = $rs->FetchField($i);		$recordSet =& $rs;		} else {		printf(ADODB_BAD_RS,'GetInsertSQL');		return false;	}	// Loop through all of the fields in the recordset	foreach( $columns as $field ) { 		$upperfname = strtoupper($field->name);		if (adodb_key_exists($upperfname,$arrFields)) {			// Set the counter for the number of fields that will be inserted.			$fieldInsertedCount++;			// Get the name of the fields to insert			$fields .= $field->name . ", ";					$type = $recordSet->MetaType($field->type);					if ((defined('ADODB_FORCE_NULLS') && is_null($arrFields[$upperfname])) || 				$arrFields[$upperfname] === 'null') {				$values  .= "null, ";			} else {				//we do this so each driver can customize the sql for				//DB specific column types. 				//Oracle needs BLOB types to be handled with a returning clause				//postgres has special needs as well				$values .= _adodb_column_sql($zthis, 'I', $type, $upperfname,											   $arrFields, $magicq);			}						}	}	// If there were any inserted fields then build the rest of the insert query.	if ($fieldInsertedCount <= 0)  return false;		// Get the table name from the existing query.	if (!$tableName) {		preg_match("/FROM\s+".ADODB_TABLE_REGEX."/is", $rs->sql, $tableName);			$tableName = $tableName[1];	}			// Strip off the comma and space on the end of both the fields	// and their values.	$fields = substr($fields, 0, -2);	$values = substr($values, 0, -2);	// Append the fields and their values to the insert query.	return 'INSERT INTO '.$tableName.' ( '.$fields.' ) VALUES ( '.$values.' )';}/** * This private method is used to help construct * the update/sql which is generated by GetInsertSQL and GetUpdateSQL. * It handles the string construction of 1 column -> sql string based on * the column type.  We want to do 'safe' handling of BLOBs *  * @param string the type of sql we are trying to create *                'I' or 'U'.  * @param string column data type from the db::MetaType() method   * @param string the column name * @param array the column value *  * @return string *  */function _adodb_column_sql_oci8(&$zthis,$action, $type, $fname, $arrFields, $magicq) {    $sql = '';        // Based on the datatype of the field    // Format the value properly for the database    switch($type) {    case 'B':        //in order to handle Blobs correctly, we need        //to do some magic for Oracle        //we need to create a new descriptor to handle         //this properly        if (!empty($zthis->hasReturningInto)) {            if ($action == 'I') {                $sql = 'empty_blob(), ';            } else {                $sql = $fname. '=empty_blob(), ';            }            //add the variable to the returning clause array            //so the user can build this later in            //case they want to add more to it            $zthis->_returningArray[$fname] = ':xx'.$fname.'xx';        } else {            //this is to maintain compatibility            //with older adodb versions.            $sql = _adodb_column_sql($zthis, $action, $type, $fname, $arrFields, $magicq,false);        }        break;    case "X":        //we need to do some more magic here for long variables        //to handle these correctly in oracle.        //create a safe bind var name        //to avoid conflicts w/ dupes.       if (!empty($zthis->hasReturningInto)) {            if ($action == 'I') {                $sql = ':xx'.$fname.'xx, ';                            } else {                $sql = $fname.'=:xx'.$fname.'xx, ';            }            //add the variable to the returning clause array            //so the user can build this later in            //case they want to add more to it            $zthis->_returningArray[$fname] = ':xx'.$fname.'xx';        } else {            //this is to maintain compatibility            //with older adodb versions.            $sql = _adodb_column_sql($zthis, $action, $type, $fname, $arrFields, $magicq,false);        }                    break;            default:        $sql = _adodb_column_sql($zthis, $action, $type, $fname, $arrFields, $magicq,false);        break;    }        return $sql;}    	function _adodb_column_sql(&$zthis, $action, $type, $fname, $arrFields, $magicq, $recurse=true) {	if ($recurse) {		switch($zthis->dataProvider)  {		case 'postgres':			if ($type == 'L') $type = 'C';			break;		case 'oci8':			return _adodb_column_sql_oci8($zthis, $action, $type, $fname, $arrFields, $magicq);					}	}		$sql = '';	switch($type) {		case "C":		case "X":		case 'B':			if ($action == 'I') {				$sql = $zthis->qstr($arrFields[$fname],$magicq) . ", ";			} else {				$sql .= $fname . "=" . $zthis->qstr($arrFields[$fname],$magicq) . ", ";			}		  break;		case "D":			if ($action == 'I') {				$sql = $zthis->DBDate($arrFields[$fname]) . ", ";			} else {				$sql .= $fname . "=" . $zthis->DBDate($arrFields[$fname]) . ", ";			}			break;		case "T":			if ($action == 'I') {				$sql = $zthis->DBTimeStamp($arrFields[$fname]) . ", ";			} else {				$sql .= $fname . "=" . $zthis->DBTimeStamp($arrFields[$fname]) . ", ";			}			break;		default:			$val = $arrFields[$fname];			if (empty($val)) $val = '0';			if ($action == 'I') {				$sql .= $val . ", ";			} else {				$sql .= $fname . "=" . $val  . ", ";			}			break;	}	return $sql;}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?