⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbo_source.php.svn-base

📁 j2me is based on j2mepolish, client & server for mobile application. server part
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
/** * Private method * * @return array */	function __mergeConditions(&$queryData, $assocData) {		if (isset($assocData['conditions']) && !empty($assocData['conditions'])) {			if (is_array($queryData['conditions'])) {				$queryData['conditions'] = array_merge((array)$assocData['conditions'], $queryData['conditions']);			} else {				if (!empty($queryData['conditions'])) {					$queryData['conditions'] = array($queryData['conditions']);					if (is_array($assocData['conditions'])) {						$queryData['conditions'] = array_merge($queryData['conditions'], $assocData['conditions']);					} else {						$queryData['conditions'][] = $assocData['conditions'];					}				} else {					$queryData['conditions'] = $assocData['conditions'];				}			}		}	}/** * Generates and executes an SQL UPDATE statement for given model, fields, and values. * * @param Model $model * @param array $fields * @param array $values * @return array */	function update(&$model, $fields = array(), $values = array()) {		$updates = array();		$combined = array_combine($fields, $values);		foreach($combined as $field => $value) {			if ($value === null) {				$updates[] = $this->name($field) . ' = NULL';			} else {				$updates[] = $this->name($field) . ' = ' . $this->value($value, $model->getColumnType($field));			}		}		$sql = 'UPDATE ' . $this->fullTableName($model);		$sql .= ' SET ' . join(',', $updates);		$sql .= ' WHERE ' . $this->name($model->primaryKey) . ' = ' . $this->value($model->getID(), $model->getColumnType($model->primaryKey));		return $this->execute($sql);	}/** * Generates and executes an SQL DELETE statement for given id on given model. * * @param Model $model * @param mixed $id Primary key id number to remove. * @return boolean Success */	function delete(&$model, $id = null) {		$_id = $model->id;		if ($id != null) {			$model->id = $id;		}		if (!is_array($model->id)) {			$model->id = array($model->id);		}		foreach($model->id as $id) {			$result = $this->execute('DELETE FROM ' . $this->fullTableName($model) . ' WHERE ' . $this->name($model->primaryKey) . ' = ' . $this->value($id));		}		if ($result) {			return true;		}		return false;	}/** * Returns a key formatted like a string Model.fieldname(i.e. Post.title, or Country.name) * * @param unknown_type $model * @param unknown_type $key * @param unknown_type $assoc * @return string */	function resolveKey($model, $key, $assoc = null) {		if ($assoc == null) {			$assoc = $model->name;		}		if (!strpos('.', $key)) {			return $this->name($model->name) . '.' . $this->name($key);		}		return $key;	}/** * Returns the column type of a given * * @param Model $model * @param string $field */	function getColumnType(&$model, $field) {		return $model->getColumnType($field);	}/** * Private helper method to remove query metadata in given data array. * * @param array $data */	function __scrubQueryData(&$data) {		if (!isset($data['conditions'])) {			$data['conditions'] = ' 1 = 1 ';		}		if (!isset($data['fields'])) {			$data['fields'] = '';		}		if (!isset($data['joins'])) {			$data['joins'] = array();		}		if (!isset($data['order'])) {			$data['order'] = '';		}		if (!isset($data['limit'])) {			$data['limit'] = '';		}		if (!isset($data['offset'])) {			$data['offset'] = null;		}	}/** * Generates the fields list of an SQL query. * * @param Model $model * @param string $alias Alias tablename * @param mixed $fields * @return array */	function fields(&$model, $alias, $fields) {		$resultMatch = null;		$build = true;		if (is_array($fields)) {			$fields = $fields;		} else {			if ($fields != null) {				preg_match_all('/(\\w*\\([\\s\\S]*?\\)[\.,\\s\\w]*?\\))([\\s\\S]*)/', $fields, $result, PREG_PATTERN_ORDER);				if(isset($result[1][0])){					$resultMatch = $result[1][0];					if(isset($result[2][0])){						$fields = $result[2][0];						if (preg_match('/AS/i', $fields)) {							$build = false;						}					}				}				if($build === true){					if (strpos($fields, ',')) {						$fields = explode(',', $fields);					} else {						$fields = array($fields);					}					$fields = array_map('trim', $fields);				}			} else {				foreach($model->_tableInfo->value as $field) {					$fields[] = $field['name'];				}			}		}		if($build === true){			$count = count($fields);			if ($count >= 1 && $fields[0] != '*') {				for($i = 0; $i < $count; $i++) {					if (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {						$prepend = '';						if (strpos($fields[$i], 'DISTINCT') !== false) {							$prepend   = 'DISTINCT ';							$fields[$i] = trim(r('DISTINCT', '', $fields[$i]));						}						$dot = strrpos($fields[$i], '.');						if ($dot === false) {							$fields[$i] = $prepend . $this->name($alias) . '.' . $this->name($fields[$i]);						} else {							$build = explode('.', $fields[$i]);							$fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]);						}					}				}			}		}		if($resultMatch != null){			if(is_string($fields)) {				$fields = array($resultMatch . $fields);			} else {				$fields = array_merge(array($resultMatch), $fields);			}		}		return $fields;	}/** * Creates a WHERE clause by parsing given conditions data. * * @param mixed $conditions Array or string of conditions * @return string SQL fragment */	function conditions($conditions) {		$clause = '';		if (!is_array($conditions)) {			if (!preg_match('/^WHERE\\x20|^GROUP\\x20BY\\x20|^HAVING\\x20|^ORDER\\x20BY\\x20/i', $conditions, $match)) {				$clause = ' WHERE ';			}		}		if (is_string($conditions)) {			if (trim($conditions) == '') {				$conditions = ' 1 = 1';			} else {				$start = null;				$end  = null;				if (!empty($this->startQuote)) {					$start = '\\\\' . $this->startQuote . '\\\\';				}				$end = $this->endQuote;				if (!empty($this->endQuote)) {					$end = '\\\\' . $this->endQuote . '\\\\';				}				preg_match_all('/(?:\'[^\'\\\]*(?:\\\.[^\'\\\]*)*\')|([a-z0-9_' . $start . $end . ']*\\.[a-z0-9_' . $start . $end . ']*)/i', $conditions, $match, PREG_PATTERN_ORDER);				if (isset($match['1']['0'])) {					$pregCount = count($match['1']);					for($i = 0; $i < $pregCount; $i++) {						if (!empty($match['1'][$i]) && !is_numeric($match['1'][$i])) {							$conditions = preg_replace('/^' . $match['0'][$i] . '/', ' '.$this->name($match['1'][$i]), $conditions);							if (strpos($conditions, '(' . $match['0'][$i]) === false) {								$conditions = preg_replace('/[^\w]' . $match['0'][$i] . '/', ' '.$this->name($match['1'][$i]), $conditions);							} else {								$conditions = preg_replace('/' . $match['0'][$i] . '/', ' '.$this->name($match['1'][$i]), $conditions);							}						}					}				}			}			return $clause . $conditions;		} else {			$clause = ' WHERE ';			$out   = $this->conditionKeysToString($conditions);			if (empty($out)) {				return $clause . ' (1 = 1)';			}			return $clause . ' (' . join(') AND (', $out) . ')';		}	}	function conditionKeysToString($conditions) {		$c = 0;		$data = null;		$out = array();		$bool = array('and', 'or', 'not', 'and not', 'or not', 'xor', '||', '&&');		$join = ' AND ';		foreach($conditions as $key => $value) {			if (in_array(strtolower(trim($key)), $bool)) {				$join = ' ' . strtoupper($key) . ' ';				$value = $this->conditionKeysToString($value);				if (strpos($join, 'NOT') !== false) {					$out[] = 'NOT (' . join(') ' . strtoupper($key) . ' (', $value) . ')';				} else {					$out[] = '(' . join(') ' . strtoupper($key) . ' (', $value) . ')';				}			} else {				if (is_array($value) && !empty($value)) {					$keys = array_keys($value);					if ($keys[0] === 0) {						$data = $this->name($key) . ' IN (';						if	(strpos($value[0], '-!') === 0){							$value[0] = str_replace('-!', '', $value[0]);							$data .= $value[0];							$data .= ')';						} else {							foreach($value as $valElement) {								$data .= $this->value($valElement) . ', ';							}							$data[strlen($data) - 2] = ')';						}					} else {						$out[] = '(' . join(') AND (', $this->conditionKeysToString($value)) . ')';					}				} elseif(is_numeric($key)) {					$data = ' ' . $value;				} elseif($value === null) {					$data = $this->name($key) . ' IS NULL';				} elseif($value === '') {					$data = $this->name($key) . " = ''";				} elseif(preg_match('/^([a-z]*\\([a-z0-9]*\\)\\x20?|(?:like\\x20)|(?:or\\x20)|(?:not\\x20)|(?:in\\x20)|(?:between\\x20)|(?:regexp\\x20)|[<> = !]{1,3}\\x20?)?(.*)/i', $value, $match)) {					if (preg_match('/(\\x20[\\w]*\\x20)/', $key, $regs)) {						$clause = $regs['1'];						$key = preg_replace('/' . $regs['1'] . '/', '', $key);					}					$mValue = trim($match['1']);					if (empty($match['1'])) {						$match['1'] = ' = ';					} elseif (empty($mValue)) {						$match['1'] = ' = ';						$match['2'] = $match['0'];					}					if (strpos($match['2'], '-!') === 0) {						$match['2'] = str_replace('-!', '', $match['2']);						$data = $this->name($key) . ' ' . $match['1'] . ' ' . $match['2'];					} else {						if ($match['2'] != '' && !is_numeric($match['2'])) {							$match['2'] = $this->value($match['2']);							$match['2'] = str_replace(' AND ', "' AND '", $match['2']);						}						$data = $this->name($key) . ' ' . $match['1'] . ' ' . $match['2'];					}				}				if ($data != null) {					$out[] = $data;				}			}			$c++;		}		return $out;	}/** * Returns a limit statement in the correct format for the particular database. * * @param int $limit Limit of results returned * @param int $offset Offset from which to start results * @return string SQL limit/offset statement */	function limit($limit, $offset = null) {		if ($limit) {			$rt = '';			if (!strpos(strtolower($limit), 'limit') || strpos(strtolower($limit), 'limit') === 0) {				$rt = ' LIMIT';			}			if ($offset) {				$rt .= ' ' . $offset . ',';			}			$rt .= ' ' . $limit;			return $rt;		}		return null;	}/** * Returns an ORDER BY clause as a string. * * @param string $key Field reference, as a key (i.e. Post.title) * @param string $direction Direction (ASC or DESC) * @return string ORDER BY clause */	function order($keys, $direction = 'ASC') {		if (is_string($keys) && strpos($keys, ',') && !preg_match('/\(.+\,.+\)/', $keys)) {			$keys = explode(',', $keys);			array_map('trim', $keys);		}		if (is_array($keys)) {			foreach($keys as $key => $val) {				if (is_numeric($key) && empty($val)) {					unset ($keys[$key]);				}			}		}		if (empty($keys) || (is_array($keys) && count($keys) && isset($keys[0]) && empty($keys[0]))) {			return '';		}		if (is_array($keys)) {			if (countdim($keys) > 1) {				$new = array();				foreach($keys as $val) {					$val = $this->order($val);					$new[] = $val;				}				$keys = $new;			}			foreach($keys as $key => $value) {				if (is_numeric($key)) {					$value = ltrim(r('ORDER BY ', '', $this->order($value)));					$key  = $value;					if (!preg_match('/\\x20ASC|\\x20DESC/i', $key)) {						$value = ' ' . $direction;					} else {						$value = '';					}				} else {					$value = ' ' . $value;				}				if (!preg_match('/^.+\\(.*\\)/', $key) && !strpos($key, ',')) {					$dir   = '';					$hasDir = preg_match('/\\x20ASC|\\x20DESC/i', $key, $dir);					if ($hasDir) {						$dir = $dir[0];						$key = preg_replace('/\\x20ASC|\\x20DESC/i', '', $key);					} else {						$dir = '';					}					$key = trim($this->name($key) . ' ' . $dir);				}				$order[] = $this->order($key . $value);			}			return ' ORDER BY ' . trim(r('ORDER BY', '', join(',', $order)));		} else {			$keys = preg_replace('/ORDER\\x20BY/i', '', $keys);			if (strpos($keys, '.')) {				preg_match_all('/([a-zA-Z0-9_]{1,})\\.([a-zA-Z0-9_]{1,})/', $keys, $result,									PREG_PATTERN_ORDER);				$pregCount = count($result['0']);				for($i = 0; $i < $pregCount; $i++) {					$keys = preg_replace('/' . $result['0'][$i] . '/', $this->name($result['0'][$i]), $keys);				}				if (preg_match('/\\x20ASC|\\x20DESC/i', $keys)) {					return ' ORDER BY ' . $keys;				} else {					return ' ORDER BY ' . $keys . ' ' . $direction;				}			} elseif(preg_match('/(\\x20ASC|\\x20DESC)/i', $keys, $match)) {				$direction = $match['1'];				$keys     = preg_replace('/' . $match['1'] . '/', '', $keys);				return ' ORDER BY ' . $keys . $direction;			} else {				$direction = ' ' . $direction;			}			return ' ORDER BY ' . $keys . $direction;		}	}/** * Disconnects database, kills the connection and says the connection is closed, * and if DEBUG is turned on, the log for this object is shown. * */	function close() {		if ($this->fullDebug) {			$this->showLog();		}		$this->disconnect();	}/** * To-be-overridden in subclasses. * */	function buildSchemaQuery($schema) {		die ("Implement in DBO");	}/** * Destructor. Closes connection to the database. * */	function __destruct() {		if ($this->__transactionStarted) {			$this->rollback();		}		$this->close();		parent::__destruct();	}/** * Checks if the specified table contains any record matching specified SQL * * @param Model $model Model to search * @param string $sql SQL WHERE clause (condition only, not the "WHERE" part) * @return boolean True if the table has a matching record, else false */	function hasAny($model, $sql) {		$sql = $this->conditions($sql);		$out = $this->one("SELECT COUNT(*) " . $this->alias . "count FROM " . $this->fullTableName($model) . ' ' . ($sql ? ' ' . $sql : 'WHERE 1 = 1'));		if (is_array($out)) {			return $out[0]['count'];		} else {			return false;		}	}/** * Translates between PHP boolean values and Database (faked) boolean values * * @param mixed $data Value to be translated * @return mixed Converted boolean value */	function boolean($data) {		if ($data === true || $data === false) {			if ($data === true) {				return 1;			}			return 0;		} else {			if (!empty($data)) {				return true;			}			return false;		}	}}?>

⌨️ 快捷键说明

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