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
    • NavigationMain
    • 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:      * @see cDbDriverAbstract::getResultObject
167:      */
168:     public function getResultObject($className = NULL) {
169:         $result = NULL;
170:         $queryId = $this->_handler->getQueryId();
171: 
172:         if (is_resource($queryId)) {
173:             if ($className == NULL) {
174:                 $result = mysql_fetch_object($queryId);
175:             } else {
176:                 $result = mysql_fetch_object($queryId, $className);
177:             }
178:         }
179: 
180:         return $result;
181:     }
182: 
183:     /**
184:      * @see cDbDriverAbstract::affectedRows
185:      */
186:     public function affectedRows() {
187:         $linkId = $this->_handler->getLinkId();
188: 
189:         return ($linkId) ? mysql_affected_rows($linkId) : 0;
190:     }
191: 
192:     /**
193:      * @see cDbDriverAbstract::numRows
194:      */
195:     public function numRows() {
196:         $queryId = $this->_handler->getQueryId();
197: 
198:         return ($queryId) ? mysql_num_rows($queryId) : 0;
199:     }
200: 
201:     /**
202:      * @see cDbDriverAbstract::numFields
203:      */
204:     public function numFields() {
205:         $queryId = $this->_handler->getQueryId();
206: 
207:         return ($queryId) ? mysql_num_fields($queryId) : 0;
208:     }
209: 
210:     /**
211:      * @see cDbDriverAbstract::free
212:      */
213:     public function free() {
214:         @mysql_free_result($this->_handler->getQueryId());
215:         $this->_handler->setQueryId(0);
216:     }
217: 
218:     /**
219:      * @see cDbDriverAbstract::escape
220:      */
221:     public function escape($string) {
222:         $linkId = $this->_handler->getLinkId();
223: 
224:         return mysql_real_escape_string($string, $linkId);
225:     }
226: 
227:     /**
228:      * @see cDbDriverAbstract::seek
229:      */
230:     public function seek($pos = 0) {
231:         $queryId = $this->_handler->getQueryId();
232: 
233:         $status = @mysql_data_seek($queryId, $pos);
234:         if ($status) {
235:             $this->_handler->setRow($pos);
236:         } else {
237:             return 0;
238:         }
239: 
240:         return 1;
241:     }
242: 
243:     /**
244:      * @see cDbDriverAbstract::getMetaData
245:      */
246:     public function getMetaData($tableName, $full = false) {
247:         $res = array();
248: 
249:         $this->query('SELECT * FROM `%s` LIMIT 1', $tableName);
250:         $id = $this->getQueryId();
251:         if (!$id) {
252:             $this->_handler->halt('Metadata query failed.');
253: 
254:             return false;
255:         }
256: 
257:         $count = @mysql_num_fields($id);
258: 
259:         // made this IF due to performance (one if is faster than $count if's)
260:         for ($i = 0; $i < $count; $i++) {
261:             $res[$i]['table'] = @mysql_field_table($id, $i);
262:             $res[$i]['name'] = @mysql_field_name($id, $i);
263:             $res[$i]['type'] = @mysql_field_type($id, $i);
264:             $res[$i]['len'] = @mysql_field_len($id, $i);
265:             $res[$i]['flags'] = @mysql_field_flags($id, $i);
266:             if ($full) {
267:                 $res['meta'][$res[$i]['name']] = $i;
268:             }
269:         }
270:         if ($full) {
271:             $res['num_fields'] = $count;
272:         }
273: 
274:         $this->free();
275: 
276:         return $res;
277:     }
278: 
279:     /**
280:      * @see cDbDriverAbstract::getTableNames
281:      */
282:     public function getTableNames() {
283:         $return = array();
284: 
285:         if ($this->query('SHOW TABLES')) {
286:             while ($this->nextRecord()) {
287:                 $record = $this->getRecord();
288:                 $return[] = array(
289:                     'table_name' => $record[0], 'tablespace_name' => $this->_dbCfg['connection']['database'], 'database' => $this->_dbCfg['connection']['database'],
290:                 );
291:             }
292: 
293:             $this->free();
294:         }
295: 
296:         return $return;
297:     }
298: 
299:     /**
300:      * @see cDbDriverAbstract::getServerInfo
301:      */
302:     public function getServerInfo() {
303:         $linkId = $this->_handler->getLinkId();
304: 
305:         if (is_resource($linkId)) {
306:             $arr = array();
307:             $arr['description'] = mysql_get_server_info($linkId);
308: 
309:             return $arr;
310:         }
311: 
312:         return NULL;
313:     }
314: 
315:     /**
316:      * @see cDbDriverAbstract::getErrorNumber
317:      */
318:     public function getErrorNumber() {
319:         $linkId = $this->_handler->getLinkId();
320: 
321:         if (is_resource($linkId)) {
322:             return mysql_errno($linkId);
323:         } else {
324:             return mysql_errno();
325:         }
326:     }
327: 
328:     /**
329:      * @see cDbDriverAbstract::getErrorMessage
330:      */
331:     public function getErrorMessage() {
332:         $linkId = $this->_handler->getLinkId();
333: 
334:         if (is_resource($linkId)) {
335:             return mysql_error($linkId);
336:         } else {
337:             return mysql_error();
338:         }
339:     }
340: 
341:     /**
342:      * @see cDbDriverAbstract::disconnect
343:      */
344:     public function disconnect() {
345:         mysql_close($this->_handler->getLinkId());
346:     }
347: 
348: }
CMS CONTENIDO 4.9.0 API documentation generated by ApiGen 2.8.0