Abstract.php

浏览该文件的文档。
00001 <?php
00003 // FleaPHP Framework
00004 //
00005 // Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
00006 //
00007 // 许可协议,请查看源代码中附带的 LICENSE.txt 文件,
00008 // 或者访问 http://www.fleaphp.org/ 获得详细信息。
00010 
00020 // {{{ constants
00024 define('DBO_PARAM_QM',          '?');
00028 define('DBO_PARAM_CL_NAMED',    ':');
00032 define('DBO_PARAM_DL_SEQUENCE', '$');
00036 define('DBO_PARAM_AT_NAMED',    '@');
00037 
00045 class FLEA_Db_Driver_Abstract
00046 {
00050     var $TRUE_VALUE  = 1;
00051     var $FALSE_VALUE = 0;
00052     var $NULL_VALUE = 'NULL';
00053 
00057     var $NEXT_ID_SQL    = null;
00058     var $CREATE_SEQ_SQL = null;
00059     var $INIT_SEQ_SQL   = null;
00060     var $DROP_SEQ_SQL   = null;
00061 
00065     var $META_COLUMNS_SQL = null;
00066 
00072     var $PARAM_STYLE = DBO_PARAM_QM;
00073 
00079     var $HAS_INSERT_ID  = false;
00080 
00086     var $HAS_AFFECTED_ROWS = false;
00087 
00093     var $HAS_TRANSACTION = false;
00094 
00100     var $HAS_SAVEPOINT = false;
00101 
00107     var $RESULT_FIELD_NAME_LOWER = false;
00108 
00114     var $dsn = null;
00115 
00121     var $conn = null;
00122 
00128     var $log = array();
00129 
00135     var $querycount = 0;
00136 
00142     var $lasterr = null;
00143 
00149     var $lasterrcode = null;
00150 
00156     var $_insertId = null;
00157 
00163     var $_transCount = 0;
00164 
00170     var $_hasFailedQuery = false;
00171 
00177     var $_savepointStack = array();
00178 
00184     function FLEA_Db_Driver_Abstract($dsn = null)
00185     {
00186         $tmp = (array)$dsn;
00187         unset($tmp['password']);
00188         $this->dsn = $dsn;
00189         $this->enableLog = FLEA::getAppInf('logEnabled');
00190         if (!function_exists('log_message')) {
00191             $this->enableLog = false;
00192         }
00193     }
00194 
00202     function connect($dsn = false)
00203     {
00204     }
00205 
00209     function close()
00210     {
00211         $this->conn = null;
00212         $this->lasterr = null;
00213         $this->lasterrcode = null;
00214         $this->_insertId = null;
00215         $this->_transCount = 0;
00216         $this->_transCommit = true;
00217     }
00218 
00226     function selectDb($database)
00227     {
00228     }
00229 
00239     function execute($sql, $inputarr = null, $throw = true)
00240     {
00241     }
00242 
00250     function qstr($value)
00251     {
00252     }
00253 
00262     function setValueByType($value, $type)
00263     {
00275         switch (strtoupper($type)) {
00276         case 'I':
00277             return (int)$value;
00278         case 'N':
00279             return (float)$value;
00280         case 'L':
00281             return (bool)$value;
00282         default:
00283             return $value;
00284         }
00285     }
00286 
00295     function qtable($tableName, $schema = null)
00296     {
00297     }
00298 
00308     function qfield($fieldName, $tableName = null, $schema = null)
00309     {
00310     }
00311 
00322     function qfields($fields, $tableName = null, $schema = null, $returnArray = false)
00323     {
00324         if (!is_array($fields)) {
00325             $fields = explode(',', $fields);
00326             $fields = array_map('trim', $fields);
00327         }
00328         $return = array();
00329         foreach ($fields as $fieldName) {
00330             $return[] = $this->qfield($fieldName, $tableName, $schema);
00331         }
00332         return $returnArray ? $return : implode(', ', $return);
00333     }
00334 
00343     function nextId($seqName = 'sdbo_seq', $startValue = 1)
00344     {
00345         $getNextIdSql = sprintf($this->NEXT_ID_SQL, $seqName);
00346         $result = $this->execute($getNextIdSql, null, false);
00347         if (!$result) {
00348             if (!$this->createSeq($seqName, $startValue)) { return false; }
00349             $result = $this->execute($getNextIdSql);
00350             if (!$result) { return false; }
00351         }
00352 
00353         if ($this->HAS_INSERT_ID) {
00354             return $this->_insertId();
00355         } else {
00356             $row = $this->fetchRow($result);
00357             $this->freeRes($result);
00358             $nextId = reset($row);
00359             $this->_insertId = $nextId;
00360             return $nextId;
00361         }
00362     }
00363 
00372     function createSeq($seqName = 'sdbo_seq', $startValue = 1)
00373     {
00374         if ($this->execute(sprintf($this->CREATE_SEQ_SQL, $seqName))) {
00375             return $this->execute(sprintf($this->INIT_SEQ_SQL, $seqName, $startValue - 1));
00376         } else {
00377             return false;
00378         }
00379     }
00380 
00388     function dropSeq($seqName = 'sdbo_seq')
00389     {
00390         return $this->execute(sprintf($this->DROP_SEQ_SQL, $seqName));
00391     }
00392 
00398     function insertId()
00399     {
00400         return $this->HAS_INSERT_ID ? $this->_insertId() : $this->_insertId;
00401     }
00402 
00408     function affectedRows()
00409     {
00410         return $this->HAS_AFFECTED_ROWS ? $this->_affectedRows() : false;
00411     }
00412 
00420     function fetchRow($res)
00421     {
00422     }
00423 
00431     function fetchAssoc($res)
00432     {
00433     }
00434 
00442     function freeRes($res)
00443     {
00444     }
00445 
00455     function selectLimit($sql, $length = null, $offset = null)
00456     {
00457     }
00458 
00469     function getAllWithFieldRefs($sql, $field, & $fieldValues, & $reference)
00470     {
00471         $res = is_resource($sql) ? $sql : $this->execute($sql);
00472         $fieldValues = array();
00473         $reference = array();
00474         $offset = 0;
00475         $data = array();
00476 
00477         while ($row = $this->fetchAssoc($res)) {
00478             $fieldValue = $row[$field];
00479             unset($row[$field]);
00480             $data[$offset] = $row;
00481             $fieldValues[$offset] = $fieldValue;
00482             $reference[$fieldValue] =& $data[$offset];
00483             $offset++;
00484         }
00485         $this->freeRes($res);
00486         return $data;
00487     }
00488 
00499     function assemble($sql, & $assocRowset, $mappingName, $oneToOne, $refKeyName, $limit = null)
00500     {
00501         if (is_resource($sql)) {
00502             $res = $sql;
00503         } else {
00504             if (!is_null($limit)) {
00505                 if (is_array($limit)) {
00506                     list($length, $offset) = $limit;
00507                 } else {
00508                     $length = $limit;
00509                     $offset = 0;
00510                 }
00511                 $res = $this->selectLimit($sql, $length, $offset);
00512             } else {
00513                 $res = $this->execute($sql);
00514             }
00515         }
00516 
00517         if ($oneToOne) {
00518             // 一对一组装数据
00519             while ($row = $this->fetchAssoc($res)) {
00520                 $rkv = $row[$refKeyName];
00521                 unset($row[$refKeyName]);
00522                 $assocRowset[$rkv][$mappingName] = $row;
00523             }
00524         } else {
00525             // 一对多组装数据
00526             while ($row = $this->fetchAssoc($res)) {
00527                 $rkv = $row[$refKeyName];
00528                 unset($row[$refKeyName]);
00529                 $assocRowset[$rkv][$mappingName][] = $row;
00530             }
00531         }
00532         $this->freeRes($res);
00533     }
00534 
00542     function & getAll($sql)
00543     {
00544         $res = is_resource($sql) ? $sql : $this->execute($sql);
00545         $rowset = array();
00546         while ($row = $this->fetchAssoc($res)) {
00547             $rowset[] = $row;
00548         }
00549         $this->freeRes($res);
00550         return $rowset;
00551     }
00552 
00560     function getOne($sql)
00561     {
00562         $res = is_resource($sql) ? $sql : $this->execute($sql);
00563         $row = $this->fetchRow($res);
00564         $this->freeRes($res);
00565         return isset($row[0]) ? $row[0] : null;
00566     }
00567 
00575     function & getRow($sql)
00576     {
00577         $res = is_resource($sql) ? $sql : $this->execute($sql);
00578         $row = $this->fetchAssoc($res);
00579         $this->freeRes($res);
00580         return $row;
00581     }
00582 
00591     function & getCol($sql, $col = 0)
00592     {
00593         $res = is_resource($sql) ? $sql : $this->execute($sql);
00594         $data = array();
00595         while ($row = $this->fetchRow($res)) {
00596             $data[] = $row[$col];
00597         }
00598         $this->freeRes($res);
00599         return $data;
00600     }
00601 
00613     function & getAllGroupBy($sql, & $groupBy)
00614     {
00615         if (is_resource($sql)) {
00616             $res = $sql;
00617         } else {
00618             $res = $this->execute($sql);
00619         }
00620         $data = array();
00621         $row = $this->fetchAssoc($res);
00622         if ($row != false) {
00623             if ($groupBy === true) {
00624                 $groupBy = key($row);
00625             }
00626             do {
00627                 $rkv = $row[$groupBy];
00628                 unset($row[$groupBy]);
00629                 $data[$rkv][] = $row;
00630             } while ($row = $this->fetchAssoc($res));
00631         }
00632         $this->freeRes($res);
00633         return $data;
00634     }
00635 
00660     function & metaColumns($table)
00661     {
00662     }
00663 
00672     function metaTables($pattern = null, $schema = null)
00673     {
00674     }
00675 
00681     function dbTimeStamp($timestamp)
00682     {
00683         return date('Y-m-d H:i:s', $timestamp);
00684     }
00685 
00689     function startTrans()
00690     {
00691         if (!$this->HAS_TRANSACTION) { return false; }
00692         if ($this->_transCount == 0) {
00693             $this->_startTrans();
00694             $this->_hasFailedQuery = false;
00695         }
00696         $this->_transCount++;
00697         if ($this->_transCount > 1 && $this->HAS_SAVEPOINT) {
00698             $savepoint = 'savepoint_' . $this->_transCount;
00699             $this->execute("SAVEPOINT {$savepoint}");
00700             array_push($this->_savepointStack, $savepoint);
00701         }
00702     }
00703 
00712     function completeTrans($commitOnNoErrors = true)
00713     {
00714         if (!$this->HAS_TRANSACTION) { return false; }
00715         if ($this->_transCount == 0) { return; }
00716         $this->_transCount--;
00717         if ($this->_transCount > 0 && $this->HAS_SAVEPOINT) {
00718             $savepoint = array_pop($this->_savepointStack);
00719             if ($this->_hasFailedQuery || $commitOnNoErrors == false) {
00720                 $this->execute("ROLLBACK TO SAVEPOINT {$savepoint}");
00721             }
00722         } else {
00723             $this->_completeTrans($commitOnNoErrors);
00724         }
00725     }
00726 
00730     function failTrans()
00731     {
00732         $this->_hasFailedQuery = true;
00733     }
00734 
00738     function hasFailedTrans()
00739     {
00740         return $this->HAS_TRANSACTION ? $this->_hasFailedQuery : false;
00741     }
00742 
00751     function bind($sql, & $inputarr)
00752     {
00753         $arr = explode('?', $sql);
00754         $sql = array_shift($arr);
00755         foreach ($inputarr as $value) {
00756             if (isset($arr[0])) {
00757                 $sql .= $this->qstr($value) . array_shift($arr);
00758             }
00759         }
00760         return $sql;
00761     }
00762 
00772     function getInsertSQL(& $row, $table, $schema = null)
00773     {
00774         list($holders, $values) = $this->getPlaceholder($row);
00775         $holders = implode(',', $holders);
00776         $fields = $this->qfields(array_keys($values));
00777         $table = $this->qtable($table, $schema);
00778         $sql = "INSERT INTO {$table} ({$fields}) VALUES ({$holders})";
00779         return $sql;
00780     }
00781 
00782     function getUpdateSQL(& $row, $pk, $table, $schema = null)
00783     {
00784         $pkv = $row[$pk];
00785         unset($row[$pk]);
00786         list($pairs, $values) = $this->getPlaceholderPair($row);
00787         $row[$pk] = $pkv;
00788         $pairs = implode(',', $pairs);
00789         $table = $this->qtable($table, $schema);
00790         $pk = $this->qfield($pk);
00791         $sql = "UPDATE {$table} SET {$pairs} WHERE {$pk} = " . $this->qstr($pkv);
00792         return $sql;
00793     }
00794 
00803     function getPlaceholder(& $inputarr, $fields = null)
00804     {
00805         $holders = array();
00806         $values = array();
00807         if (is_array($fields)) {
00808             $fields = array_change_key_case(array_flip($fields), CASE_LOWER);
00809             foreach (array_keys($inputarr) as $key) {
00810                 if (!isset($fields[strtolower($key)])) { continue; }
00811                 if ($this->PARAM_STYLE == DBO_PARAM_QM) {
00812                     $holders[] = $this->PARAM_STYLE;
00813                 } else {
00814                     $holders[] = $this->PARAM_STYLE . $key;
00815                 }
00816                 $values[$key] =& $inputarr[$key];
00817             }
00818         } else {
00819             foreach (array_keys($inputarr) as $key) {
00820                 if ($this->PARAM_STYLE == DBO_PARAM_QM) {
00821                     $holders[] = $this->PARAM_STYLE;
00822                 } else {
00823                     $holders[] = $this->PARAM_STYLE . $key;
00824                 }
00825                 $values[$key] =& $inputarr[$key];
00826             }
00827         }
00828         return array($holders, $values);
00829     }
00830 
00839     function getPlaceholderPair(& $inputarr, $fields = null)
00840     {
00841         $pairs = array();
00842         $values = array();
00843         if (is_array($fields)) {
00844             $fields = array_change_key_case(array_flip($fields), CASE_LOWER);
00845             foreach (array_keys($inputarr) as $key) {
00846                 if (!isset($fields[strtolower($key)])) { continue; }
00847                 $qkey = $this->qfield($key);
00848                 if ($this->PARAM_STYLE == DBO_PARAM_QM) {
00849                     $pairs[] = "{$qkey}={$this->PARAM_STYLE}";
00850                 } else {
00851                     $pairs[] = "{$qkey}={$this->PARAM_STYLE}{$key}";
00852                 }
00853                 $values[$key] =& $inputarr[$key];
00854             }
00855         } else {
00856             foreach (array_keys($inputarr) as $key) {
00857                 $qkey = $this->qfield($key);
00858                 if ($this->PARAM_STYLE == DBO_PARAM_QM) {
00859                     $pairs[] = "{$qkey}={$this->PARAM_STYLE}";
00860                 } else {
00861                     $pairs[] = "{$qkey}={$this->PARAM_STYLE}{$key}";
00862                 }
00863                 $values[$key] =& $inputarr[$key];
00864             }
00865         }
00866         return array($pairs, $values);
00867     }
00868 }

Generated at Sat Feb 2 15:18:50 2008 for FleaPHP by  doxygen 1.5.3