Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DbPdo.class.php #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 130 additions & 17 deletions Extend/Driver/Db/DbPdo.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,45 @@ private function getAll() {
$this->numRows = count( $result );
return $result;
}

/**
* 获取informix数据表信息
* @access public
*/
public function ifxGetPrimaryInfo($tableid) {
$sql = "SELECT sc.colname,so.constrname,st.tabname,st.owner,sc.colno,
ABS(si.part1),ABS(si.part2),ABS(si.part3),ABS(si.part4), ABS(si.part5),ABS(si.part6),ABS(si.part7),
ABS(si.part8),ABS(si.part9),ABS(si.part10),ABS(si.part11),ABS(si.part12),ABS(si.part13),ABS(si.part14),
ABS(si.part15),ABS(si.part16)
FROM systables st, sysconstraints so, sysindexes si, syscolumns sc
WHERE st.tabid='$tableid'
AND st.tabid = so.tabid
AND so.constrtype = 'P'
AND so.idxname = si.idxname
AND sc.tabid = st.tabid
AND (sc.colno = ABS(si.part1)
OR sc.colno = ABS(si.part2)
OR sc.colno = ABS(si.part3)
OR sc.colno = ABS(si.part4)
OR sc.colno = ABS(si.part5)
OR sc.colno = ABS(si.part6)
OR sc.colno = ABS(si.part7)
OR sc.colno = ABS(si.part8)
OR sc.colno = ABS(si.part9)
OR sc.colno = ABS(si.part10)
OR sc.colno = ABS(si.part11)
OR sc.colno = ABS(si.part12)
OR sc.colno = ABS(si.part13)
OR sc.colno = ABS(si.part14)
OR sc.colno = ABS(si.part15)
OR sc.colno = ABS(si.part16))";
$stmt = $this->_linkID->query($sql);
$result = $stmt->fetchAll();
foreach($result as $key => $row) {
$ret[] = $row['colname'];
}
return $ret;
}

/**
* 取得数据表的字段信息
Expand All @@ -221,6 +260,13 @@ public function getFields($tableName) {
$sql = str_replace('%table%',$tableName,C('DB_DESCRIBE_TABLE_SQL'));
}else{
switch($this->dbType) {
case 'INFORMIX':
$sql = "SELECT DISTINCT t.owner, t.tabname, c.colname, c.colno, c.coltype, d.default, c.collength, t.tabid
FROM syscolumns c
JOIN systables t ON c.tabid = t.tabid
LEFT JOIN sysdefaults d ON c.tabid = d.tabid AND c.colno = d.colno
WHERE UPPER(t.tabname) = UPPER('$tableName') ORDER BY c.colno";
break;
case 'MSSQL':
case 'SQLSRV':
$sql = "SELECT column_name as 'Name', data_type as 'Type', column_default as 'Default', is_nullable as 'Null'
Expand Down Expand Up @@ -251,22 +297,78 @@ public function getFields($tableName) {
$sql = 'DESCRIBE '.$tableName;//备注: 驱动类不只针对mysql,不能加``
}
}
$result = $this->query($sql);
$info = array();
if($result) {
foreach ($result as $key => $val) {
$val = array_change_key_case($val);
$val['name'] = isset($val['name'])?$val['name']:"";
$val['type'] = isset($val['type'])?$val['type']:"";
$name = isset($val['field'])?$val['field']:$val['name'];
$info[$name] = array(
'name' => $name ,
'type' => $val['type'],
'notnull' => (bool)(((isset($val['null'])) && ($val['null'] === '')) || ((isset($val['notnull'])) && ($val['notnull'] === ''))), // not null is empty, null is yes
'default' => isset($val['default'])? $val['default'] :(isset($val['dflt_value'])?$val['dflt_value']:""),
'primary' => isset($val['dey'])?strtolower($val['dey']) == 'pri':(isset($val['pk'])?$val['pk']:false),
'autoinc' => isset($val['extra'])?strtolower($val['extra']) == 'auto_increment':(isset($val['key'])?$val['key']:false),
);
if($this->dbType == 'INFORMIX' ) {
$info = array();
$stmt = $this->_linkID->query($sql);
$result = $stmt->fetchAll();
if($result) {
/**
* The ordering of columns is defined by the query so we can map
* to variables to improve readability
*/
$tabschema = 0;
$tabname = 1;
$colname = 2;
$colno = 3;
$typename = 4;
$default = 5;
$length = 6;
$tabid = 7;

$primaryCols = null;
foreach ($result as $key => $row) {
$primary = false;
$primaryPosition = null;
if (empty($primaryCols)) {
$primaryCols = $this->ifxGetPrimaryInfo($row[$tabid]);
}
if (array_key_exists($row[$colno], $primaryCols)) {
$primary = true;
$primaryPosition = $primaryCols[$row[$colno]];
}

$identity = false;
if ($row[$typename] == 6 + 256 || $row[$typename] == 18 + 256) {
$identity = true;
}

$info[$row[$colname]] = array (
'SCHEMA_NAME' => $row[$tabschema],
'TABLE_NAME' => $row[$tabname],
'name' => $row[$colname],
'COLUMN_POSITION' => $row[$colno],
'type' => "",//$this->_getDataType($row['typename']),
'default' => $row[$default],
'notnull' => (bool) !($row[$typename] - 256 >= 0),
'LENGTH' => $row[$length],
'SCALE' => ($row[$typename] == 5 ? $row[$length]&255 : 0),
'PRECISION' => ($row[$typename] == 5 ? (int)($row[$length]/256) : 0),
'UNSIGNED' => false,
'primary' => $primary,
'PRIMARY_POSITION' => $primaryPosition,
'autoinc' => $identity
);
}
}
}
else {
$result = $this->query($sql);
$info = array();
if($result) {
foreach ($result as $key => $val) {
$val = array_change_key_case($val);
$val['name'] = isset($val['name'])?$val['name']:"";
$val['type'] = isset($val['type'])?$val['type']:"";
$name = isset($val['field'])?$val['field']:$val['name'];
$info[$name] = array(
'name' => $name ,
'type' => $val['type'],
'notnull' => (bool)(((isset($val['null'])) && ($val['null'] === '')) || ((isset($val['notnull'])) && ($val['notnull'] === ''))), // not null is empty, null is yes
'default' => isset($val['default'])? $val['default'] :(isset($val['dflt_value'])?$val['dflt_value']:""),
'primary' => isset($val['dey'])?strtolower($val['dey']) == 'pri':(isset($val['pk'])?$val['pk']:false),
'autoinc' => isset($val['extra'])?strtolower($val['extra']) == 'auto_increment':(isset($val['key'])?$val['key']:false),
);
}
}
}
return $info;
Expand Down Expand Up @@ -329,6 +431,15 @@ protected function parseLimit($limit) {
$limitStr = '';
if(!empty($limit)) {
switch($this->dbType){
case 'INFORMIX':
$limit = explode(',',$limit);
if(count($limit)>1) {
$limitStr .= ' SKIP '.$limit[0].' FIRST '.$limit[1].' ';
}
else{
$limitStr .= ' FIRST '.$limit[0].' ';
}
break;
case 'PGSQL':
case 'SQLITE':
$limit = explode(',',$limit);
Expand Down Expand Up @@ -410,6 +521,7 @@ public function error() {
*/
public function escapeString($str) {
switch($this->dbType) {
case 'INFORMIX':
case 'PGSQL':
case 'MSSQL':
case 'SQLSRV':
Expand All @@ -430,6 +542,7 @@ public function escapeString($str) {
*/
public function getLastInsertId() {
switch($this->dbType) {
case 'INFORMIX':
case 'PGSQL':
case 'SQLITE':
case 'MSSQL':
Expand All @@ -444,4 +557,4 @@ public function getLastInsertId() {
return $vo?$vo[0]["currval"]:0;
}
}
}
}