1: <?php
2: /**
3: * Copyright 2007 Maintainable Software, LLC
4: * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
5: *
6: * @author Mike Naberezny <mike@maintainable.com>
7: * @author Derek DeVries <derek@maintainable.com>
8: * @author Chuck Hagenbuch <chuck@horde.org>
9: * @license http://www.horde.org/licenses/bsd
10: * @category Horde
11: * @package Db
12: * @subpackage Adapter
13: */
14:
15: /**
16: * PDO_MySQL Horde_Db_Adapter
17: *
18: * @author Mike Naberezny <mike@maintainable.com>
19: * @author Derek DeVries <derek@maintainable.com>
20: * @author Chuck Hagenbuch <chuck@horde.org>
21: * @license http://www.horde.org/licenses/bsd
22: * @category Horde
23: * @package Db
24: * @subpackage Adapter
25: */
26: class Horde_Db_Adapter_Pdo_Mysql extends Horde_Db_Adapter_Pdo_Base
27: {
28: /**
29: * @var string
30: */
31: protected $_schemaClass = 'Horde_Db_Adapter_Mysql_Schema';
32:
33: /**
34: * @return string
35: */
36: public function adapterName()
37: {
38: return 'PDO_MySQL';
39: }
40:
41: /**
42: * @return boolean
43: */
44: public function supportsMigrations()
45: {
46: return true;
47: }
48:
49:
50: /*##########################################################################
51: # Connection Management
52: ##########################################################################*/
53:
54: /**
55: * Connect to the db
56: */
57: public function connect()
58: {
59: if ($this->_active) {
60: return;
61: }
62:
63: parent::connect();
64:
65: // ? $this->_connection->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
66:
67: // Set the default charset. http://dev.mysql.com/doc/refman/5.1/en/charset-connection.html
68: if (!empty($this->_config['charset'])) {
69: $this->setCharset($this->_config['charset']);
70: }
71: }
72:
73:
74: /*##########################################################################
75: # Protected
76: ##########################################################################*/
77:
78: /**
79: * Parse configuration array into options for PDO constructor.
80: *
81: * http://pecl.php.net/bugs/7234
82: * Setting a bogus socket does not appear to work.
83: *
84: * @throws Horde_Db_Exception
85: * @return array [dsn, username, password]
86: */
87: protected function _parseConfig()
88: {
89: $this->_config['adapter'] = 'mysql';
90:
91: $this->_checkRequiredConfig(array('adapter', 'username'));
92:
93: if (!empty($this->_config['socket'])) {
94: $this->_config['unix_socket'] = $this->_config['socket'];
95: unset($this->_config['socket']);
96: }
97:
98: if (!empty($this->_config['host']) &&
99: $this->_config['host'] == 'localhost') {
100: $this->_config['host'] = '127.0.0.1';
101: }
102:
103: // Try an empty password if it's not set.
104: if (!isset($this->_config['password'])) {
105: $this->_config['password'] = '';
106: }
107:
108: // Collect options to build PDO Data Source Name (DSN) string.
109: $dsnOpts = $this->_config;
110: unset($dsnOpts['adapter'],
111: $dsnOpts['username'],
112: $dsnOpts['password'],
113: $dsnOpts['charset'],
114: $dsnOpts['phptype']);
115: $dsnOpts = $this->_normalizeConfig($dsnOpts);
116:
117: if (isset($dsnOpts['port'])) {
118: if (empty($dsnOpts['host'])) {
119: throw new Horde_Db_Exception('Host is required if port is specified');
120: }
121: }
122:
123: if (isset($dsnOpts['unix_socket'])) {
124: if (!empty($dsnOpts['host']) ||
125: !empty($dsnOpts['port'])) {
126: throw new Horde_Db_Exception('Host and port must not be set if using a UNIX socket');
127: }
128: }
129:
130: // Return DSN and user/pass for connection.
131: return array(
132: $this->_buildDsnString($dsnOpts),
133: $this->_config['username'],
134: $this->_config['password']);
135: }
136: }
137: