Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • 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
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
  1: <?php
  2: /**
  3:  * This file contains the MySQLi 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: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 16: 
 17: /**
 18:  * This class contains functions for database interaction based on MySQLi in
 19:  * CONTENIDO.
 20:  *
 21:  * Configurable via global $cfg['db']['connection'] configuration as follows:
 22:  * <pre>
 23:  * - host (string) Hostname or ip
 24:  * - database (string) Database name
 25:  * - user (string) User name
 26:  * - password (string) User password
 27:  * - options (array) Optional, MySQLi options array
 28:  * - socket (int) Optional, socket
 29:  * - port (int) Optional, port
 30:  * - flags (int) Optional, flags
 31:  * - charset (string) Optional, connection charset
 32:  * see http://www.php.net/manual/en/mysqli.real-connect.php
 33:  * </pre>
 34:  *
 35:  * @package Core
 36:  * @subpackage Database
 37:  */
 38: class cDbDriverMysqli extends cDbDriverAbstract {
 39: 
 40:     /**
 41:      * List of data types.
 42:      * @var array
 43:      */
 44:     protected $_dataTypes = array(
 45:         0 => 'decimal',
 46:         1 => 'tinyint',
 47:         2 => 'smallint',
 48:         3 => 'int',
 49:         4 => 'float',
 50:         5 => 'double',
 51:         7 => 'timestamp',
 52:         8 => 'bigint',
 53:         9 => 'mediumint',
 54:         10 => 'date',
 55:         11 => 'time',
 56:         12 => 'datetime',
 57:         13 => 'year',
 58:         252 => 'blob', // text, blob, tinyblob,mediumblob, etc...
 59:         253 => 'string', // varchar and char
 60:         254 => 'enum'
 61:     );
 62: 
 63:     /**
 64:      *
 65:      * @see cDbDriverAbstract::check()
 66:      */
 67:     public function check() {
 68:         return extension_loaded('mysqli');
 69:     }
 70: 
 71:     /**
 72:      *
 73:      * @see cDbDriverAbstract::connect()
 74:      */
 75:     public function connect() {
 76:         $dbHandler = @mysqli_init();
 77:         if (!$dbHandler || $dbHandler->connect_error != "" || $dbHandler->error != "") {
 78:             $this->_handler->halt('Can not initialize database connection.');
 79:             return NULL;
 80:         }
 81: 
 82:         if (isset($this->_dbCfg['connection'])) {
 83:             $connectConfig = $this->_dbCfg['connection'];
 84:         }
 85:         if (empty($connectConfig) || !isset($connectConfig['host']) || !isset($connectConfig['user']) || !isset($connectConfig['password'])) {
 86:             $this->_handler->halt('Database connection settings incomplete');
 87:             return NULL;
 88:         }
 89:         // set existing option flags
 90:         if (isset($connectConfig['options']) && is_array($connectConfig['options'])) {
 91:             foreach ($connectConfig['options'] as $optKey => $optVal) {
 92:                 mysqli_options($dbHandler, $optKey, $optVal);
 93:             }
 94:         }
 95: 
 96:         if (($iPos = strpos($connectConfig['host'], ':')) !== false) {
 97:             $hostData = explode(':', $connectConfig['host']);
 98:             $connectConfig['host'] = $hostData[0];
 99:             if (is_numeric($hostData[1])) {
100:                 $connectConfig['port'] = $hostData[1];
101:             } else {
102:                 $connectConfig['socket'] = $hostData[1];
103:             }
104:         }
105: 
106:         if (!isset($connectConfig['port'])) {
107:             $connectConfig['port'] = NULL;
108:         }
109:         if (!isset($connectConfig['socket'])) {
110:             $connectConfig['socket'] = NULL;
111:         }
112: 
113:         if (!isset($connectConfig['flags'])) {
114:             $connectConfig['flags'] = NULL;
115:         }
116:         if (!isset($connectConfig['database'])) {
117:             $connectConfig['database'] = NULL;
118:         }
119: 
120:         $res = mysqli_real_connect($dbHandler, $connectConfig['host'], $connectConfig['user'], $connectConfig['password'], $connectConfig['database'], $connectConfig['port'], $connectConfig['socket'], $connectConfig['flags']);
121: 
122:         // check if connection could be established
123:         if (false === $res) {
124:             $this->_handler->halt('MySQLi _connect() Error connecting to database ' . $connectConfig['database']);
125:             return NULL;
126:         }
127: 
128:         if ($res && $dbHandler && $connectConfig['database']) {
129:             if (!@mysqli_select_db($dbHandler, $connectConfig['database'])) {
130:                 $this->_handler->halt('MySQLi _connect() Cannot use database ' . $connectConfig['database']);
131:                 return NULL;
132:             } else {
133:                 // set connection charset
134:                 if (isset($connectConfig['charset']) && $connectConfig['charset'] != '') {
135:                     if (!@mysqli_set_charset($dbHandler, $connectConfig['charset'])) {
136:                         $this->_handler->halt('Could not set database charset to ' . $connectConfig['charset']);
137:                         return NULL;
138:                     }
139:                 }
140:             }
141:         }
142: 
143:         return $dbHandler;
144:     }
145: 
146:     /**
147:      *
148:      * @see cDbDriverAbstract::buildInsert()
149:      */
150:     public function buildInsert($tableName, array $fields) {
151:         $fieldList = '';
152:         $valueList = '';
153: 
154:         foreach ($fields as $field => $value) {
155:             $fieldList .= '`' . $field . '`, ';
156:             if (is_int($value)) {
157:                 $valueList .= $value . ', ';
158:             } else {
159:                 $valueList .= "'" . $this->escape($value) . "', ";
160:             }
161:         }
162: 
163:         $fieldList = substr($fieldList, 0, -2);
164:         $valueList = substr($valueList, 0, -2);
165:         return sprintf('INSERT INTO `%s` (%s) VALUES (%s)', $tableName, $fieldList, $valueList);
166:     }
167: 
168:     /**
169:      *
170:      * @see cDbDriverAbstract::buildUpdate()
171:      */
172:     public function buildUpdate($tableName, array $fields, array $whereClauses) {
173:         $updateList = '';
174:         $whereList = '';
175: 
176:         foreach ($fields as $field => $value) {
177:             $updateList .= '`' . $field . '`=';
178:             if (is_int($value)) {
179:                 $updateList .= $value . ', ';
180:             } else {
181:                 $updateList .= "'" . $this->escape($value) . "', ";
182:             }
183:         }
184: 
185:         foreach ($whereClauses as $field => $value) {
186:             $whereList .= '`' . $field . '`=';
187:             if (is_int($value)) {
188:                 $whereList .= $value . ' AND ';
189:             } else {
190:                 $whereList .= "'" . $this->escape($value) . "' AND ";
191:             }
192:         }
193: 
194:         $updateList = substr($updateList, 0, -2);
195:         $whereList = substr($whereList, 0, -5);
196: 
197:         return sprintf('UPDATE `%s` SET %s WHERE %s', $tableName, $updateList, $whereList);
198:     }
199: 
200:     /**
201:      *
202:      * @see cDbDriverAbstract::query()
203:      */
204:     public function query($query) {
205:         $linkId = $this->_handler->getLinkId();
206:         $queryId = mysqli_query($linkId, $query);
207: 
208:         $this->_handler->setQueryId($queryId);
209:         $this->_handler->setRow(0);
210:         $this->_handler->setErrorNumber($this->getErrorNumber());
211:         $this->_handler->setErrorMessage($this->getErrorMessage());
212:     }
213: 
214:     /**
215:      *
216:      * @see cDbDriverAbstract::nextRecord()
217:      */
218:     public function nextRecord() {
219:         $queryId = $this->_handler->getQueryId();
220:         $record = mysqli_fetch_array($queryId, MYSQLI_BOTH);
221: 
222:         $this->_handler->setRecord($record);
223:         $this->_handler->incrementRow();
224:         $this->_handler->setErrorNumber($this->getErrorNumber());
225:         $this->_handler->setErrorMessage($this->getErrorMessage());
226: 
227:         return is_array($record);
228:     }
229: 
230:     /**
231:      *
232:      * @see cDbDriverAbstract::getResultObject()
233:      */
234:     public function getResultObject($className = NULL) {
235:         $result = NULL;
236:         $queryId = $this->_handler->getQueryId();
237: 
238:         if ($queryId) {
239:             if ($className == NULL) {
240:                 $result = mysqli_fetch_object($queryId);
241:             } else {
242:                 $result = mysqli_fetch_object($queryId, $className);
243:             }
244:         }
245: 
246:         return $result;
247:     }
248: 
249:     /**
250:      *
251:      * @see cDbDriverAbstract::affectedRows()
252:      */
253:     public function affectedRows() {
254:         $linkId = $this->_handler->getLinkId();
255:         return ($linkId) ? mysqli_affected_rows($linkId) : 0;
256:     }
257: 
258:     /**
259:      *
260:      * @see cDbDriverAbstract::numRows()
261:      */
262:     public function numRows() {
263:         $queryId = $this->_handler->getQueryId();
264:         return ($queryId) ? mysqli_num_rows($queryId) : 0;
265:     }
266: 
267:     /**
268:      *
269:      * @see cDbDriverAbstract::numFields()
270:      */
271:     public function numFields() {
272:         $queryId = $this->_handler->getQueryId();
273:         return ($queryId) ? mysqli_num_fields($queryId) : 0;
274:     }
275: 
276:     /**
277:      *
278:      * @see cDbDriverAbstract::free()
279:      */
280:     public function free() {
281:         if (!is_object($this->_handler->getQueryId())) {
282:             return $this;
283:         }
284: 
285:         mysqli_free_result($this->_handler->getQueryId());
286:         $this->_handler->setQueryId(0);
287:     }
288: 
289:     /**
290:      *
291:      * @see cDbDriverAbstract::escape()
292:      */
293:     public function escape($string) {
294:         $linkId = $this->_handler->getLinkId();
295:         return mysqli_real_escape_string($linkId, $string);
296:     }
297: 
298:     /**
299:      *
300:      * @see cDbDriverAbstract::seek()
301:      */
302:     public function seek($pos = 0) {
303:         $queryId = $this->_handler->getQueryId();
304: 
305:         $status = mysqli_data_seek($queryId, $pos);
306:         if ($status) {
307:             $this->_handler->setRow($pos);
308:         } else {
309:             return 0;
310:         }
311: 
312:         return 1;
313:     }
314: 
315:     /**
316:      *
317:      * @see cDbDriverAbstract::getMetaData()
318:      */
319:     public function getMetaData($tableName, $full = false) {
320:         $res = array();
321: 
322:         $this->query(sprintf('SELECT * FROM `%s` LIMIT 1', $tableName));
323: 
324:         $id = $this->_handler->getQueryId();
325:         if (!$id) {
326:             $this->_handler->halt('Metadata query failed.');
327:             return false;
328:         }
329: 
330:         // made this IF due to performance (one if is faster than $count if's)
331:         $count = mysqli_num_fields($id);
332:         for ($i = 0; $i < $count; $i++) {
333:             $finfo = mysqli_fetch_field($id);
334:             $res[$i]['table'] = $finfo->table;
335:             $res[$i]['name'] = $finfo->name;
336:             $res[$i]['type'] = $this->_dataTypes[$finfo->type];
337:             $res[$i]['len'] = $finfo->max_length;
338:             $res[$i]['flags'] = $finfo->flags;
339:             if ($full) {
340:                 $res['meta'][$res[$i]['name']] = $i;
341:             }
342:         }
343:         if ($full) {
344:             $res['num_fields'] = $count;
345:         }
346: 
347:         $this->free();
348: 
349:         return $res;
350:     }
351: 
352:     /**
353:      *
354:      * @see cDbDriverAbstract::getTableNames()
355:      */
356:     public function getTableNames() {
357:         $return = array();
358:         if ($this->query('SHOW TABLES')) {
359:             while ($this->nextRecord()) {
360:                 $record = $this->getRecord();
361:                 $return[] = array(
362:                     'table_name' => $record[0],
363:                     'tablespace_name' => $this->_dbCfg['connection']['database'],
364:                     'database' => $this->_dbCfg['connection']['database']
365:                 );
366:             }
367: 
368:             $this->free();
369:         }
370:         return $return;
371:     }
372: 
373:     /**
374:      *
375:      * @see cDbDriverAbstract::getServerInfo()
376:      */
377:     public function getServerInfo() {
378:         $linkId = $this->_handler->getLinkId();
379: 
380:         if ($linkId) {
381:             $arr = array();
382:             $arr['description'] = mysqli_get_server_info($linkId);
383:             return $arr;
384:         }
385: 
386:         return NULL;
387:     }
388: 
389:     /**
390:      *
391:      * @see cDbDriverAbstract::getErrorNumber()
392:      */
393:     public function getErrorNumber() {
394:         $linkId = $this->_handler->getLinkId();
395: 
396:         if ($linkId) {
397:             return @mysqli_errno($linkId);
398:         } else {
399:             return @mysqli_connect_errno();
400:         }
401:     }
402: 
403:     /**
404:      *
405:      * @see cDbDriverAbstract::getErrorMessage()
406:      */
407:     public function getErrorMessage() {
408:         $linkId = $this->_handler->getLinkId();
409: 
410:         if ($linkId) {
411:             return @mysqli_error($linkId);
412:         } else {
413:             return @mysqli_connect_error();
414:         }
415:     }
416: 
417:     /**
418:      *
419:      * @see cDbDriverAbstract::disconnect()
420:      */
421:     public function disconnect() {
422:         mysqli_close($this->_handler->getLinkId());
423:     }
424: 
425: }
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen