改进数据库驱动的getFields方法 支持指定别名的情况 修正model类一处错误

This commit is contained in:
ThinkPHP
2013-04-30 10:45:38 +08:00
parent 7cfdde0ab7
commit 64f7e1d3ca
7 changed files with 8 additions and 3 deletions

View File

@@ -430,7 +430,7 @@ abstract class Driver {
if(!is_numeric($table)) if(!is_numeric($table))
$array[] = $this->parseKey($table).' '.$this->parseKey($alias); $array[] = $this->parseKey($table).' '.$this->parseKey($alias);
else else
$array[] = $this->parseKey($table); $array[] = $this->parseKey($alias);
} }
$tables = $array; $tables = $array;
}elseif(is_string($tables)){ }elseif(is_string($tables)){
@@ -563,7 +563,7 @@ abstract class Driver {
} }
}else { }else {
//对字符串类型字段采用模糊匹配 //对字符串类型字段采用模糊匹配
if($this->conf['db_like_fields'] && preg_match('/('.$this->conf['db_like_fields'].')/i',$key)) { if($this->config['db_like_fields'] && preg_match('/('.$this->config['db_like_fields'].')/i',$key)) {
$val = '%'.$val.'%'; $val = '%'.$val.'%';
$whereStr .= $key.' LIKE '.$this->parseValue($val); $whereStr .= $key.' LIKE '.$this->parseValue($val);
}else { }else {

View File

@@ -42,6 +42,7 @@ class Mysql extends Driver{
*/ */
public function getFields($tableName) { public function getFields($tableName) {
$this->initConnect(true); $this->initConnect(true);
list($tableName) = explode(' ', $tableName);
$sql = 'SHOW COLUMNS FROM `'.$tableName.'`'; $sql = 'SHOW COLUMNS FROM `'.$tableName.'`';
$result = $this->query($sql); $result = $this->query($sql);
$info = []; $info = [];

View File

@@ -80,6 +80,7 @@ class Oracle extends Driver{
* @access public * @access public
*/ */
public function getFields($tableName) { public function getFields($tableName) {
list($tableName) = explode(' ', $tableName);
$result = $this->query("select a.column_name,data_type,decode(nullable,'Y',0,1) notnull,data_default,decode(a.column_name,b.column_name,1,0) pk " $result = $this->query("select a.column_name,data_type,decode(nullable,'Y',0,1) notnull,data_default,decode(a.column_name,b.column_name,1,0) pk "
."from user_tab_columns a,(select column_name from user_constraints c,user_cons_columns col " ."from user_tab_columns a,(select column_name from user_constraints c,user_cons_columns col "
."where c.constraint_name=col.constraint_name and c.constraint_type='P'and c.table_name='".strtoupper($tableName) ."where c.constraint_name=col.constraint_name and c.constraint_type='P'and c.table_name='".strtoupper($tableName)

View File

@@ -37,6 +37,7 @@ class Pgsql extends Driver{
* @return array * @return array
*/ */
public function getFields($tableName) { public function getFields($tableName) {
list($tableName) = explode(' ', $tableName);
$result = $this->query('select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg('.$tableName.');'); $result = $this->query('select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg('.$tableName.');');
$info = []; $info = [];
if($result){ if($result){

View File

@@ -34,6 +34,7 @@ class Sqlite extends Driver {
* @return array * @return array
*/ */
public function getFields($tableName) { public function getFields($tableName) {
list($tableName) = explode(' ', $tableName);
$result = $this->query('PRAGMA table_info( '.$tableName.' )'); $result = $this->query('PRAGMA table_info( '.$tableName.' )');
$info = []; $info = [];
if($result){ if($result){

View File

@@ -46,6 +46,7 @@ class Sqlsrv extends Driver{
* @return array * @return array
*/ */
public function getFields($tableName) { public function getFields($tableName) {
list($tableName) = explode(' ', $tableName);
$result = $this->query("SELECT column_name, data_type, column_default, is_nullable $result = $this->query("SELECT column_name, data_type, column_default, is_nullable
FROM information_schema.tables AS t FROM information_schema.tables AS t
JOIN information_schema.columns AS c JOIN information_schema.columns AS c

View File

@@ -396,7 +396,7 @@ class Model {
$options['model'] = $this->name; $options['model'] = $this->name;
if(isset($options['table'])) {// 动态指定表名 if(isset($options['table'])) {// 动态指定表名
$fields = $this->db->getFields($this->options['table']); $fields = $this->db->getFields($options['table']);
$fields = $fields?array_keys($fields):false; $fields = $fields?array_keys($fields):false;
}else{ }else{
$options['table'] = $this->getTableName(); $options['table'] = $this->getTableName();