Source for file DibiConnection.php

Documentation is available at DibiConnection.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * dibi - tiny'n'smart database abstraction layer
  5. 5:  * ----------------------------------------------
  6. 6:  *
  7. 7:  * @copyright  Copyright (c) 2005, 2010 David Grudl
  8. 8:  * @license    http://dibiphp.com/license  dibi license
  9. 9:  * @link       http://dibiphp.com
  10. 10:  * @package    dibi
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * dibi connection.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2005, 2010 David Grudl
  19. 19:  * @package    dibi
  20. 20:  *
  21. 21:  * @property-read bool $connected 
  22. 22:  * @property-read mixed $config 
  23. 23:  * @property-read IDibiDriver $driver 
  24. 24:  * @property-read int $affectedRows 
  25. 25:  * @property-read int $insertId 
  26. 26:  * @property IDibiProfiler $profiler 
  27. 27:  * @property-read DibiDatabaseInfo $databaseInfo 
  28. 28:  */
  29. 29: class DibiConnection extends DibiObject
  30. 30: {
  31. 31:     /** @var array  Current connection configuration */
  32. 32:     private $config;
  33. 33:  
  34. 34:     /** @var IDibiDriver */
  35. 35:     private $driver;
  36. 36:  
  37. 37:     /** @var DibiTranslator */
  38. 38:     private $translator;
  39. 39:  
  40. 40:     /** @var IDibiProfiler */
  41. 41:     private $profiler;
  42. 42:  
  43. 43:     /** @var bool  Is connected? */
  44. 44:     private $connected FALSE;
  45. 45:  
  46. 46:  
  47. 47:  
  48. 48:     /**
  49. 49:      * Connection options: (see driver-specific options too)
  50. 50:      *   - lazy (bool) => if TRUE, connection will be established only when required
  51. 51:      *   - result (array) => result set options
  52. 52:      *       - detectTypes (bool) => detect the types of result set fields?
  53. 53:      *       - formatDateTime => date-time format (if empty, DateTime objects will be returned)
  54. 54:      *   - profiler (array or bool)
  55. 55:      *       - run (bool) => enable profiler?
  56. 56:      *       - class => profiler class name (default is DibiProfiler)
  57. 57:      *   - substitutes (array) => map of driver specific substitutes (under development)
  58. 58:  
  59. 59:      * @param  mixed   connection parameters
  60. 60:      * @param  string  connection name
  61. 61:      * @throws DibiException
  62. 62:      */
  63. 63:     public function __construct($config$name NULL)
  64. 64:     {
  65. 65:         // DSN string
  66. 66:         if (is_string($config)) {
  67. 67:             parse_str($config$config);
  68. 68:  
  69. 69:         elseif ($config instanceof Traversable{
  70. 70:             $tmp array();
  71. 71:             foreach ($config as $key => $val{
  72. 72:                 $tmp[$key$val instanceof Traversable iterator_to_array($val$val;
  73. 73:             }
  74. 74:             $config $tmp;
  75. 75:  
  76. 76:         elseif (!is_array($config)) {
  77. 77:             throw new InvalidArgumentException('Configuration must be array, string or object.');
  78. 78:         }
  79. 79:  
  80. 80:         self::alias($config'username''user');
  81. 81:         self::alias($config'password''pass');
  82. 82:         self::alias($config'host''hostname');
  83. 83:         self::alias($config'result|detectTypes''resultDetectTypes')// back compatibility
  84. 84:         self::alias($config'result|formatDateTime''resultDateTime');
  85. 85:  
  86. 86:         if (!isset($config['driver'])) {
  87. 87:             $config['driver'dibi::$defaultDriver;
  88. 88:         }
  89. 89:  
  90. 90:         $driver preg_replace('#[^a-z0-9_]#''_'strtolower($config['driver']));
  91. 91:         $class "Dibi" $driver "Driver";
  92. 92:         if (!class_exists($classFALSE)) {
  93. 93:             include_once dirname(__FILE__"/../drivers/$driver.php";
  94. 94:  
  95. 95:             if (!class_exists($classFALSE)) {
  96. 96:                 throw new DibiException("Unable to create instance of dibi driver '$class'.");
  97. 97:             }
  98. 98:         }
  99. 99:  
  100. 100:         $config['name'$name;
  101. 101:         $this->config $config;
  102. 102:         $this->driver new $class;
  103. 103:         $this->translator new DibiTranslator($this->driver);
  104. 104:  
  105. 105:         // profiler
  106. 106:         $profilerCfg $config['profiler'];
  107. 107:         if (is_scalar($profilerCfg)) // back compatibility
  108. 108:             $profilerCfg array(
  109. 109:                 'run' => (bool) $profilerCfg,
  110. 110:                 'class' => strlen($profilerCfg$profilerCfg NULL,
  111. 111:             );
  112. 112:         }
  113. 113:  
  114. 114:         if (!empty($profilerCfg['run'])) {
  115. 115:             $class isset($profilerCfg['class']$profilerCfg['class''DibiProfiler';
  116. 116:             if (!class_exists($class)) {
  117. 117:                 throw new DibiException("Unable to create instance of dibi profiler '$class'.");
  118. 118:             }
  119. 119:             $this->setProfiler(new $class($profilerCfg));
  120. 120:         }
  121. 121:  
  122. 122:         if (!empty($config['substitutes'])) {
  123. 123:             foreach ($config['substitutes'as $key => $value{
  124. 124:                 dibi::addSubst($key$value);
  125. 125:             }
  126. 126:         }
  127. 127:  
  128. 128:         if (empty($config['lazy'])) {
  129. 129:             $this->connect();
  130. 130:         }
  131. 131:     }
  132. 132:  
  133. 133:  
  134. 134:  
  135. 135:     /**
  136. 136:      * Automatically frees the resources allocated for this result set.
  137. 137:      * @return void 
  138. 138:      */
  139. 139:     public function __destruct()
  140. 140:     {
  141. 141:         // disconnects and rolls back transaction - do not rely on auto-disconnect and rollback!
  142. 142:         $this->connected && $this->disconnect();
  143. 143:     }
  144. 144:  
  145. 145:  
  146. 146:  
  147. 147:     /**
  148. 148:      * Connects to a database.
  149. 149:      * @return void 
  150. 150:      */
  151. 151:     final public function connect()
  152. 152:     {
  153. 153:         if ($this->profiler !== NULL{
  154. 154:             $ticket $this->profiler->before($thisIDibiProfiler::CONNECT);
  155. 155:         }
  156. 156:         $this->driver->connect($this->config);
  157. 157:         $this->connected TRUE;
  158. 158:         if (isset($ticket)) {
  159. 159:             $this->profiler->after($ticket);
  160. 160:         }
  161. 161:     }
  162. 162:  
  163. 163:  
  164. 164:  
  165. 165:     /**
  166. 166:      * Disconnects from a database.
  167. 167:      * @return void 
  168. 168:      */
  169. 169:     final public function disconnect()
  170. 170:     {
  171. 171:         $this->driver->disconnect();
  172. 172:         $this->connected FALSE;
  173. 173:     }
  174. 174:  
  175. 175:  
  176. 176:  
  177. 177:     /**
  178. 178:      * Returns TRUE when connection was established.
  179. 179:      * @return bool 
  180. 180:      */
  181. 181:     final public function isConnected()
  182. 182:     {
  183. 183:         return $this->connected;
  184. 184:     }
  185. 185:  
  186. 186:  
  187. 187:  
  188. 188:     /**
  189. 189:      * Returns configuration variable. If no $key is passed, returns the entire array.
  190. 190:      * @see self::__construct
  191. 191:      * @param  string 
  192. 192:      * @param  mixed  default value to use if key not found
  193. 193:      * @return mixed 
  194. 194:      */
  195. 195:     final public function getConfig($key NULL$default NULL)
  196. 196:     {
  197. 197:         if ($key === NULL{
  198. 198:             return $this->config;
  199. 199:  
  200. 200:         elseif (isset($this->config[$key])) {
  201. 201:             return $this->config[$key];
  202. 202:  
  203. 203:         else {
  204. 204:             return $default;
  205. 205:         }
  206. 206:     }
  207. 207:  
  208. 208:  
  209. 209:  
  210. 210:     /**
  211. 211:      * Apply configuration alias or default values.
  212. 212:      * @param  array  connect configuration
  213. 213:      * @param  string key
  214. 214:      * @param  string alias key
  215. 215:      * @return void 
  216. 216:      */
  217. 217:     public static function alias(&$config$key$alias)
  218. 218:     {
  219. 219:         $foo $config;
  220. 220:         foreach (explode('|'$keyas $key$foo $foo[$key];
  221. 221:  
  222. 222:         if (!isset($foo&& isset($config[$alias])) {
  223. 223:             $foo $config[$alias];
  224. 224:             unset($config[$alias]);
  225. 225:         }
  226. 226:     }
  227. 227:  
  228. 228:  
  229. 229:  
  230. 230:     /**
  231. 231:      * Returns the driver and connects to a database in lazy mode.
  232. 232:      * @return IDibiDriver 
  233. 233:      */
  234. 234:     final public function getDriver()
  235. 235:     {
  236. 236:         $this->connected || $this->connect();
  237. 237:         return $this->driver;
  238. 238:     }
  239. 239:  
  240. 240:  
  241. 241:  
  242. 242:     /**
  243. 243:      * Generates (translates) and executes SQL query.
  244. 244:      * @param  array|mixed     one or more arguments
  245. 245:      * @return DibiResult|int  result set object (if any)
  246. 246:      * @throws DibiException
  247. 247:      */
  248. 248:     final public function query($args)
  249. 249:     {
  250. 250:         $this->connected || $this->connect();
  251. 251:         $args func_get_args();
  252. 252:         return $this->nativeQuery($this->translator->translate($args));
  253. 253:     }
  254. 254:  
  255. 255:  
  256. 256:  
  257. 257:     /**
  258. 258:      * Generates and returns SQL query.
  259. 259:      * @param  array|mixed     one or more arguments
  260. 260:      * @return string 
  261. 261:      * @throws DibiException
  262. 262:      */
  263. 263:     final public function translate($args)
  264. 264:     {
  265. 265:         $this->connected || $this->connect();
  266. 266:         $args func_get_args();
  267. 267:         return $this->translator->translate($args);
  268. 268:     }
  269. 269:  
  270. 270:  
  271. 271:  
  272. 272:     /** @deprecated */
  273. 273:     function sql($args)
  274. 274:     {
  275. 275:         trigger_error(__METHOD__ . '() is deprecated; use translate() instead.'E_USER_NOTICE);
  276. 276:         $this->connected || $this->connect();
  277. 277:         $args func_get_args();
  278. 278:         return $this->translator->translate($args);
  279. 279:     }
  280. 280:  
  281. 281:  
  282. 282:  
  283. 283:     /**
  284. 284:      * Generates and prints SQL query.
  285. 285:      * @param  array|mixed one or more arguments
  286. 286:      * @return bool 
  287. 287:      */
  288. 288:     final public function test($args)
  289. 289:     {
  290. 290:         $this->connected || $this->connect();
  291. 291:         $args func_get_args();
  292. 292:         try {
  293. 293:             dibi::dump($this->translator->translate($args));
  294. 294:             return TRUE;
  295. 295:  
  296. 296:         catch (DibiException $e{
  297. 297:             dibi::dump($e->getSql());
  298. 298:             return FALSE;
  299. 299:         }
  300. 300:     }
  301. 301:  
  302. 302:  
  303. 303:  
  304. 304:     /**
  305. 305:      * Generates (translates) and returns SQL query as DibiDataSource.
  306. 306:      * @param  array|mixed     one or more arguments
  307. 307:      * @return DibiDataSource 
  308. 308:      * @throws DibiException
  309. 309:      */
  310. 310:     final public function dataSource($args)
  311. 311:     {
  312. 312:         $this->connected || $this->connect();
  313. 313:         $args func_get_args();
  314. 314:         return new DibiDataSource($this->translator->translate($args)$this);
  315. 315:     }
  316. 316:  
  317. 317:  
  318. 318:  
  319. 319:     /**
  320. 320:      * Executes the SQL query.
  321. 321:      * @param  string           SQL statement.
  322. 322:      * @return DibiResult|int  result set object (if any)
  323. 323:      * @throws DibiException
  324. 324:      */
  325. 325:     final public function nativeQuery($sql)
  326. 326:     {
  327. 327:         $this->connected || $this->connect();
  328. 328:  
  329. 329:         if ($this->profiler !== NULL{
  330. 330:             $event IDibiProfiler::QUERY;
  331. 331:             if (preg_match('#\s*(SELECT|UPDATE|INSERT|DELETE)#i'$sql$matches)) {
  332. 332:                 static $events array(
  333. 333:                     'SELECT' => IDibiProfiler::SELECT'UPDATE' => IDibiProfiler::UPDATE,
  334. 334:                     'INSERT' => IDibiProfiler::INSERT'DELETE' => IDibiProfiler::DELETE,
  335. 335:                 );
  336. 336:                 $event $events[strtoupper($matches[1])];
  337. 337:             }
  338. 338:             $ticket $this->profiler->before($this$event$sql);
  339. 339:         }
  340. 340:  
  341. 341:         dibi::$sql $sql;
  342. 342:         if ($res $this->driver->query($sql)) // intentionally =
  343. 343:             $res new DibiResult($res$this->config['result']);
  344. 344:         else {
  345. 345:             $res $this->driver->getAffectedRows();
  346. 346:         }
  347. 347:  
  348. 348:         if (isset($ticket)) {
  349. 349:             $this->profiler->after($ticket$res);
  350. 350:         }
  351. 351:         return $res;
  352. 352:     }
  353. 353:  
  354. 354:  
  355. 355:  
  356. 356:     /**
  357. 357:      * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
  358. 358:      * @return int  number of rows
  359. 359:      * @throws DibiException
  360. 360:      */
  361. 361:     public function getAffectedRows()
  362. 362:     {
  363. 363:         $this->connected || $this->connect();
  364. 364:         $rows $this->driver->getAffectedRows();
  365. 365:         if (!is_int($rows|| $rows 0throw new DibiException('Cannot retrieve number of affected rows.');
  366. 366:         return $rows;
  367. 367:     }
  368. 368:  
  369. 369:  
  370. 370:  
  371. 371:     /**
  372. 372:      * Gets the number of affected rows. Alias for getAffectedRows().
  373. 373:      * @return int  number of rows
  374. 374:      * @throws DibiException
  375. 375:      */
  376. 376:     public function affectedRows()
  377. 377:     {
  378. 378:         return $this->getAffectedRows();
  379. 379:     }
  380. 380:  
  381. 381:  
  382. 382:  
  383. 383:     /**
  384. 384:      * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
  385. 385:      * @param  string     optional sequence name
  386. 386:      * @return int
  387. 387:      * @throws DibiException
  388. 388:      */
  389. 389:     public function getInsertId($sequence NULL)
  390. 390:     {
  391. 391:         $this->connected || $this->connect();
  392. 392:         $id $this->driver->getInsertId($sequence);
  393. 393:         if ($id 1throw new DibiException('Cannot retrieve last generated ID.');
  394. 394:         return (int) $id;
  395. 395:     }
  396. 396:  
  397. 397:  
  398. 398:  
  399. 399:     /**
  400. 400:      * Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId().
  401. 401:      * @param  string     optional sequence name
  402. 402:      * @return int
  403. 403:      * @throws DibiException
  404. 404:      */
  405. 405:     public function insertId($sequence NULL)
  406. 406:     {
  407. 407:         return $this->getInsertId($sequence);
  408. 408:     }
  409. 409:  
  410. 410:  
  411. 411:  
  412. 412:     /**
  413. 413:      * Begins a transaction (if supported).
  414. 414:      * @param  string  optional savepoint name
  415. 415:      * @return void
  416. 416:      */
  417. 417:     public function begin($savepoint NULL)
  418. 418:     {
  419. 419:         $this->connected || $this->connect();
  420. 420:         if ($this->profiler !== NULL{
  421. 421:             $ticket $this->profiler->before($thisIDibiProfiler::BEGIN$savepoint);
  422. 422:         }
  423. 423:         $this->driver->begin($savepoint);
  424. 424:         if (isset($ticket)) {
  425. 425:             $this->profiler->after($ticket);
  426. 426:         }
  427. 427:     }
  428. 428:  
  429. 429:  
  430. 430:  
  431. 431:     /**
  432. 432:      * Commits statements in a transaction.
  433. 433:      * @param  string  optional savepoint name
  434. 434:      * @return void
  435. 435:      */
  436. 436:     public function commit($savepoint NULL)
  437. 437:     {
  438. 438:         $this->connected || $this->connect();
  439. 439:         if ($this->profiler !== NULL{
  440. 440:             $ticket $this->profiler->before($thisIDibiProfiler::COMMIT$savepoint);
  441. 441:         }
  442. 442:         $this->driver->commit($savepoint);
  443. 443:         if (isset($ticket)) {
  444. 444:             $this->profiler->after($ticket);
  445. 445:         }
  446. 446:     }
  447. 447:  
  448. 448:  
  449. 449:  
  450. 450:     /**
  451. 451:      * Rollback changes in a transaction.
  452. 452:      * @param  string  optional savepoint name
  453. 453:      * @return void
  454. 454:      */
  455. 455:     public function rollback($savepoint NULL)
  456. 456:     {
  457. 457:         $this->connected || $this->connect();
  458. 458:         if ($this->profiler !== NULL{
  459. 459:             $ticket $this->profiler->before($thisIDibiProfiler::ROLLBACK$savepoint);
  460. 460:         }
  461. 461:         $this->driver->rollback($savepoint);
  462. 462:         if (isset($ticket)) {
  463. 463:             $this->profiler->after($ticket);
  464. 464:         }
  465. 465:     }
  466. 466:  
  467. 467:  
  468. 468:  
  469. 469:     /********************* fluent SQL builders ****************d*g**/
  470. 470:  
  471. 471:  
  472. 472:  
  473. 473:     /**
  474. 474:      * @return DibiFluent
  475. 475:      */
  476. 476:     public function command()
  477. 477:     {
  478. 478:         return new DibiFluent($this);
  479. 479:     }
  480. 480:  
  481. 481:  
  482. 482:  
  483. 483:     /**
  484. 484:      * @param  string    column name
  485. 485:      * @return DibiFluent
  486. 486:      */
  487. 487:     public function select($args)
  488. 488:     {
  489. 489:         $args func_get_args();
  490. 490:         return $this->command()->__call('select'$args);
  491. 491:     }
  492. 492:  
  493. 493:  
  494. 494:  
  495. 495:     /**
  496. 496:      * @param  string   table
  497. 497:      * @param  array
  498. 498:      * @return DibiFluent
  499. 499:      */
  500. 500:     public function update($table$args)
  501. 501:     {
  502. 502:         if (!(is_array($args|| $args instanceof Traversable)) {
  503. 503:             throw new InvalidArgumentException('Arguments must be array or Traversable.');
  504. 504:         }
  505. 505:         return $this->command()->update('%n'$table)->set($args);
  506. 506:     }
  507. 507:  
  508. 508:  
  509. 509:  
  510. 510:     /**
  511. 511:      * @param  string   table
  512. 512:      * @param  array
  513. 513:      * @return DibiFluent
  514. 514:      */
  515. 515:     public function insert($table$args)
  516. 516:     {
  517. 517:         if ($args instanceof Traversable{
  518. 518:             $args iterator_to_array($args);
  519. 519:         elseif (!is_array($args)) {
  520. 520:             throw new InvalidArgumentException('Arguments must be array or Traversable.');
  521. 521:         }
  522. 522:         return $this->command()->insert()
  523. 523:             ->into('%n'$table'(%n)'array_keys($args))->values('%l'$args);
  524. 524:     }
  525. 525:  
  526. 526:  
  527. 527:  
  528. 528:     /**
  529. 529:      * @param  string   table
  530. 530:      * @return DibiFluent
  531. 531:      */
  532. 532:     public function delete($table)
  533. 533:     {
  534. 534:         return $this->command()->delete()->from('%n'$table);
  535. 535:     }
  536. 536:  
  537. 537:  
  538. 538:  
  539. 539:     /********************* profiler ****************d*g**/
  540. 540:  
  541. 541:  
  542. 542:  
  543. 543:     /**
  544. 544:      * @param  IDibiProfiler
  545. 545:      * @return DibiConnection  provides a fluent interface
  546. 546:      */
  547. 547:     public function setProfiler(IDibiProfiler $profiler NULL)
  548. 548:     {
  549. 549:         $this->profiler $profiler;
  550. 550:         return $this;
  551. 551:     }
  552. 552:  
  553. 553:  
  554. 554:  
  555. 555:     /**
  556. 556:      * @return IDibiProfiler
  557. 557:      */
  558. 558:     public function getProfiler()
  559. 559:     {
  560. 560:         return $this->profiler;
  561. 561:     }
  562. 562:  
  563. 563:  
  564. 564:  
  565. 565:     /********************* shortcuts ****************d*g**/
  566. 566:  
  567. 567:  
  568. 568:  
  569. 569:     /**
  570. 570:      * Executes SQL query and fetch result - shortcut for query() & fetch().
  571. 571:      * @param  array|mixed    one or more arguments
  572. 572:      * @return DibiRow
  573. 573:      * @throws DibiException
  574. 574:      */
  575. 575:     public function fetch($args)
  576. 576:     {
  577. 577:         $args func_get_args();
  578. 578:         return $this->query($args)->fetch();
  579. 579:     }
  580. 580:  
  581. 581:  
  582. 582:  
  583. 583:     /**
  584. 584:      * Executes SQL query and fetch results - shortcut for query() & fetchAll().
  585. 585:      * @param  array|mixed    one or more arguments
  586. 586:      * @return array of DibiRow
  587. 587:      * @throws DibiException
  588. 588:      */
  589. 589:     public function fetchAll($args)
  590. 590:     {
  591. 591:         $args func_get_args();
  592. 592:         return $this->query($args)->fetchAll();
  593. 593:     }
  594. 594:  
  595. 595:  
  596. 596:  
  597. 597:     /**
  598. 598:      * Executes SQL query and fetch first column - shortcut for query() & fetchSingle().
  599. 599:      * @param  array|mixed    one or more arguments
  600. 600:      * @return string
  601. 601:      * @throws DibiException
  602. 602:      */
  603. 603:     public function fetchSingle($args)
  604. 604:     {
  605. 605:         $args func_get_args();
  606. 606:         return $this->query($args)->fetchSingle();
  607. 607:     }
  608. 608:  
  609. 609:  
  610. 610:  
  611. 611:     /**
  612. 612:      * Executes SQL query and fetch pairs - shortcut for query() & fetchPairs().
  613. 613:      * @param  array|mixed    one or more arguments
  614. 614:      * @return string
  615. 615:      * @throws DibiException
  616. 616:      */
  617. 617:     public function fetchPairs($args)
  618. 618:     {
  619. 619:         $args func_get_args();
  620. 620:         return $this->query($args)->fetchPairs();
  621. 621:     }
  622. 622:  
  623. 623:  
  624. 624:  
  625. 625:     /********************* misc ****************d*g**/
  626. 626:  
  627. 627:  
  628. 628:  
  629. 629:     /**
  630. 630:      * Import SQL dump from file - extreme fast!
  631. 631:      * @param  string  filename
  632. 632:      * @return int  count of sql commands
  633. 633:      */
  634. 634:     public function loadFile($file)
  635. 635:     {
  636. 636:         $this->connected || $this->connect();
  637. 637:         @set_time_limit(0)// intentionally @
  638. 638:  
  639. 639:         $handle @fopen($file'r')// intentionally @
  640. 640:         if (!$handle{
  641. 641:             throw new FileNotFoundException("Cannot open file '$file'.");
  642. 642:         }
  643. 643:  
  644. 644:         $count 0;
  645. 645:         $sql '';
  646. 646:         while (!feof($handle)) {
  647. 647:             $s fgets($handle);
  648. 648:             $sql .= $s;
  649. 649:             if (substr(rtrim($s)-1=== ';'{
  650. 650:                 $this->driver->query($sql);
  651. 651:                 $sql '';
  652. 652:                 $count++;
  653. 653:             }
  654. 654:         }
  655. 655:         fclose($handle);
  656. 656:         return $count;
  657. 657:     }
  658. 658:  
  659. 659:  
  660. 660:  
  661. 661:     /**
  662. 662:      * Gets a information about the current database.
  663. 663:      * @return DibiDatabaseInfo
  664. 664:      */
  665. 665:     public function getDatabaseInfo()
  666. 666:     {
  667. 667:         $this->connected || $this->connect();
  668. 668:         return new DibiDatabaseInfo($this->driver->getReflector()isset($this->config['database']$this->config['database'NULL);
  669. 669:     }
  670. 670:  
  671. 671:  
  672. 672:  
  673. 673:     /**
  674. 674:      * Prevents unserialization.
  675. 675:      */
  676. 676:     public function __wakeup()
  677. 677:     {
  678. 678:         throw new NotSupportedException('You cannot serialize or unserialize ' $this->getClass(' instances.');
  679. 679:     }
  680. 680:  
  681. 681:  
  682. 682:  
  683. 683:     /**
  684. 684:      * Prevents serialization.
  685. 685:      */
  686. 686:     public function __sleep()
  687. 687:     {
  688. 688:         throw new NotSupportedException('You cannot serialize or unserialize ' $this->getClass(' instances.');
  689. 689:     }
  690. 690: