Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cDb
  • cDbDriverAbstract
  • cDbDriverHandler
  • cDbDriverMysql
  • cDbDriverMysqli

Exceptions

  • cDbException
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the MySQL database driver class.
  4:  *
  5:  * @package    Core
  6:  * @subpackage Database
  7:  * @version    SVN Revision $Rev:$
  8:  *
  9:  * @author     Dominik Ziegler
 10:  * @copyright  four for business AG <www.4fb.de>
 11:  * @license    http://www.contenido.org/license/LIZENZ.txt
 12:  * @link       http://www.4fb.de
 13:  * @link       http://www.contenido.org
 14:  */
 15: 
 16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 17: 
 18: /**
 19:  * This class contains functions for database interaction based on MySQL in CONTENIDO.
 20:  *
 21:  * Configurable via global $cfg['db']['connection'] configuration as follows:
 22:  * <pre>
 23:  * - host      (string)  Hostname or ip (with port, e. g. "example.com:3307")
 24:  * - database  (string)  Database name
 25:  * - user      (string)  User name
 26:  * - password  (string)  User password
 27:  * - charset   (string)  Optional, connection charset
 28:  * see http://php.net/manual/en/function.mysql-connect.php
 29:  * </pre>
 30:  *
 31:  * @package    Core
 32:  * @subpackage Database
 33:  */
 34: class cDbDriverMysql extends cDbDriverAbstract {
 35: 
 36:     /**
 37:      * @see cDbDriverAbstract::check()
 38:      */
 39:     public function check() {
 40:         return function_exists("mysql_connect");
 41:     }
 42: 
 43:     /**
 44:      * @see cDbDriverAbstract::connect()
 45:      */
 46:     public function connect() {
 47:         if (isset($this->_dbCfg['connection'])) {
 48:             $connectConfig = $this->_dbCfg['connection'];
 49:         }
 50:         if (empty($connectConfig) || !isset($connectConfig['host']) || !isset($connectConfig['user']) || !isset($connectConfig['password'])) {
 51:             $this->_handler->halt('Database connection settings incomplete');
 52: 
 53:             return NULL;
 54:         }
 55: 
 56:         // establish connection, select database
 57:         $dbHandler = mysql_connect($connectConfig['host'], $connectConfig['user'], $connectConfig['password']);
 58:         if (!$dbHandler || !is_resource($dbHandler)) {
 59:             $this->_handler->halt('Error during establishing a connection with database.');
 60: 
 61:             return NULL;
 62:         }
 63: 
 64:         if (isset($connectConfig['database'])) {
 65:             if (!mysql_select_db($connectConfig['database'], $dbHandler)) {
 66:                 $this->_handler->halt('Can not use database ' . $connectConfig['database']);
 67: 
 68:                 return NULL;
 69:             } else {
 70:                 //set connection charset
 71:                 if (isset($connectConfig['charset']) && $connectConfig['charset'] != '') {
 72:                     if (!mysql_set_charset($connectConfig['charset'], $dbHandler)) {
 73:                         $this->_handler->halt('Could not set database charset to ' . $connectConfig['charset']);
 74: 
 75:                         return NULL;
 76:                     }
 77:                 }
 78:             }
 79:         }
 80: 
 81:         return $dbHandler;
 82:     }
 83: 
 84:     /**
 85:      * @see cDbDriverAbstract::buildInsert()
 86:      */
 87:     public function buildInsert($tableName, array $fields) {
 88:         $fieldList = '';
 89:         $valueList = '';
 90: 
 91:         foreach ($fields as $field => $value) {
 92:             $fieldList .= '`' . $field . '`, ';
 93:             if (is_int($value)) {
 94:                 $valueList .= $value . ', ';
 95:             } else {
 96:                 $valueList .= "'" . $this->escape($value) . "', ";
 97:             }
 98:         }
 99: 
100:         $fieldList = substr($fieldList, 0, -2);
101:         $valueList = substr($valueList, 0, -2);
102: 
103:         return sprintf('INSERT INTO `%s` (%s) VALUES (%s)', $tableName, $fieldList, $valueList);
104:     }
105: 
106:     /**
107:      * @see cDbDriverAbstract::buildUpdate()
108:      */
109:     public function buildUpdate($tableName, array $fields, array $whereClauses) {
110:         $updateList = '';
111:         $whereList = '';
112: 
113:         foreach ($fields as $field => $value) {
114:             $updateList .= '`' . $field . '`=';
115:             if (is_int($value)) {
116:                 $updateList .= $value . ', ';
117:             } else {
118:                 $updateList .= "'" . $this->escape($value) . "', ";
119:             }
120:         }
121: 
122:         foreach ($whereClauses as $field => $value) {
123:             $whereList .= '`' . $field . '`=';
124:             if (is_int($value)) {
125:                 $whereList .= $value . ' AND ';
126:             } else {
127:                 $whereList .= "'" . $this->escape($value) . "' AND ";
128:             }
129:         }
130: 
131:         $updateList = substr($updateList, 0, -2);
132:         $whereList = substr($whereList, 0, -5);
133: 
134:         return sprintf('UPDATE `%s` SET %s WHERE %s', $tableName, $updateList, $whereList);
135:     }
136: 
137:     /**
138:      * @see cDbDriverAbstract::query()
139:      */
140:     public function query($query) {
141:         $linkId = $this->_handler->getLinkId();
142:         $queryId = @mysql_query($query, $linkId);
143: 
144:         $this->_handler->setQueryId($queryId);
145:         $this->_handler->setRow(0);
146:         $this->_handler->setErrorNumber($this->getErrorNumber());
147:         $this->_handler->setErrorMessage($this->getErrorMessage());
148:     }
149: 
150:     /**
151:      * @see cDbDriverAbstract::nextRecord()
152:      */
153:     public function nextRecord() {
154:         $queryId = $this->_handler->getQueryId();
155:         $record = @mysql_fetch_array($queryId);
156: 
157:         $this->_handler->setRecord($record);
158:         $this->_handler->incrementRow();
159:         $this->_handler->setErrorNumber($this->getErrorNumber());
160:         $this->_handler->setErrorMessage($this->getErrorMessage());
161: 
162:         return is_array($record);
163:     }
164: 
165:     /**
166:      *
167:      * @param string $className
168:      * @return Ambigous <NULL, object, false>
169:      * @see cDbDriverAbstract::getResultObject()
170:      */
171:     public function getResultObject($className = NULL) {
172:         $result = NULL;
173:         $queryId = $this->_handler->getQueryId();
174: 
175:         if (is_resource($queryId)) {
176:             if ($className == NULL) {
177:                 $result = mysql_fetch_object($queryId);
178:             } else {
179:                 $result = mysql_fetch_object($queryId, $className);
180:             }
181:         }
182: 
183:         return $result;
184:     }
185: 
186:     /**
187:      * @see cDbDriverAbstract::affectedRows()
188:      */
189:     public function affectedRows() {
190:         $linkId = $this->_handler->getLinkId();
191: 
192:         return ($linkId) ? mysql_affected_rows($linkId) : 0;
193:     }
194: 
195:     /**
196:      * @see cDbDriverAbstract::numRows()
197:      */
198:     public function numRows() {
199:         $queryId = $this->_handler->getQueryId();
200: 
201:         return ($queryId) ? mysql_num_rows($queryId) : 0;
202:     }
203: 
204:     /**
205:      * @see cDbDriverAbstract::numFields()
206:      */
207:     public function numFields() {
208:         $queryId = $this->_handler->getQueryId();
209: 
210:         return ($queryId) ? mysql_num_fields($queryId) : 0;
211:     }
212: 
213:     /**
214:      * @see cDbDriverAbstract::free()
215:      */
216:     public function free() {
217:         @mysql_free_result($this->_handler->getQueryId());
218:         $this->_handler->setQueryId(0);
219:     }
220: 
221:     /**
222:      * @see cDbDriverAbstract::escape()
223:      */
224:     public function escape($string) {
225:         $linkId = $this->_handler->getLinkId();
226: 
227:         return mysql_real_escape_string($string, $linkId);
228:     }
229: 
230:     /**
231:      * @param int $pos
232:      * @return int
233:      * @see cDbDriverAbstract::seek()
234:      */
235:     public function seek($pos = 0) {
236:         $queryId = $this->_handler->getQueryId();
237: 
238:         $status = @mysql_data_seek($queryId, $pos);
239:         if ($status) {
240:             $this->_handler->setRow($pos);
241:         } else {
242:             return 0;
243:         }
244: 
245:         return 1;
246:     }
247: 
248:     /**
249:      * @see cDbDriverAbstract::getMetaData()
250:      */
251:     public function getMetaData($tableName, $full = false) {
252:         $res = array();
253: 
254:         $this->query(sprintf('SELECT * FROM `%s` LIMIT 1', $tableName));
255:         $id = $this->_handler->getQueryId();
256:         if (!$id) {
257:             $this->_handler->halt('Metadata query failed.');
258: 
259:             return false;
260:         }
261: 
262:         // made this IF due to performance (one if is faster than $count if's)
263:         $count = @mysql_num_fields($id);
264:         for ($i = 0; $i < $count; $i++) {
265:             $res[$i]['table'] = @mysql_field_table($id, $i);
266:             $res[$i]['name'] = @mysql_field_name($id, $i);
267:             $res[$i]['type'] = @mysql_field_type($id, $i);
268:             $res[$i]['len'] = @mysql_field_len($id, $i);
269:             $res[$i]['flags'] = @mysql_field_flags($id, $i);
270:             if ($full) {
271:                 $res['meta'][$res[$i]['name']] = $i;
272:             }
273:         }
274:         if ($full) {
275:             $res['num_fields'] = $count;
276:         }
277: 
278:         $this->free();
279: 
280:         return $res;
281:     }
282: 
283:     /**
284:      * @see cDbDriverAbstract::getTableNames()
285:      */
286:     public function getTableNames() {
287:         $return = array();
288: 
289:         if ($this->query('SHOW TABLES')) {
290:             while ($this->nextRecord()) {
291:                 $record = $this->getRecord();
292:                 $return[] = array(
293:                     'table_name' => $record[0], 'tablespace_name' => $this->_dbCfg['connection']['database'], 'database' => $this->_dbCfg['connection']['database'],
294:                 );
295:             }
296: 
297:             $this->free();
298:         }
299: 
300:         return $return;
301:     }
302: 
303:     /**
304:      * @see cDbDriverAbstract::getServerInfo()
305:      */
306:     public function getServerInfo() {
307:         $linkId = $this->_handler->getLinkId();
308: 
309:         if (is_resource($linkId)) {
310:             $arr = array();
311:             $arr['description'] = mysql_get_server_info($linkId);
312: 
313:             return $arr;
314:         }
315: 
316:         return NULL;
317:     }
318: 
319:     /**
320:      * @see cDbDriverAbstract::getErrorNumber()
321:      */
322:     public function getErrorNumber() {
323:         $linkId = $this->_handler->getLinkId();
324: 
325:         if (is_resource($linkId)) {
326:             return mysql_errno($linkId);
327:         } else {
328:             return mysql_errno();
329:         }
330:     }
331: 
332:     /**
333:      * @see cDbDriverAbstract::getErrorMessage()
334:      */
335:     public function getErrorMessage() {
336:         $linkId = $this->_handler->getLinkId();
337: 
338:         if (is_resource($linkId)) {
339:             return mysql_error($linkId);
340:         } else {
341:             return mysql_error();
342:         }
343:     }
344: 
345:     /**
346:      * @see cDbDriverAbstract::disconnect()
347:      */
348:     public function disconnect() {
349:         mysql_close($this->_handler->getLinkId());
350:     }
351: 
352: }
CMS CONTENIDO 4.9.3 API documentation generated by ApiGen 2.8.0