# Coverage for Doctrine_Connection_Mysql

Back to coverage report

1 | <?php
2 | /*
3 | * $Id: Mysql.php 3183 2007-11-18 16:06:37Z romanb $
4 | *
5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 | *
17 | * This software consists of voluntary contributions made by many individuals
18 | * and is licensed under the LGPL. For more information, see
19 | * <http://www.phpdoctrine.org>.
20 | */
21 | Doctrine::autoload('Doctrine_Connection_Common');
22 | /**
23 | * Doctrine_Connection_Mysql
24 | *
25 | * @package Doctrine
26 | * @subpackage Connection
27 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
29 | * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
30 | * @version $Revision: 3183 $
31 | * @link www.phpdoctrine.org
32 | * @since 1.0
33 | */
34 | class Doctrine_Connection_Mysql extends Doctrine_Connection_Common
35 | {
36 | /**
37 | * @var string $driverName the name of this connection driver
38 | */
39 | protected $driverName = 'Mysql';
40 |
41 | /**
42 | * the constructor
43 | *
44 | * @param Doctrine_Manager $manager
45 | * @param PDO|Doctrine_Adapter $adapter database handler
46 | */
47 | public function __construct(Doctrine_Manager $manager, $adapter)
48 | {
49 | $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
50 | $this->setAttribute(Doctrine::ATTR_DEFAULT_TABLE_TYPE, 'INNODB');
51 |
52 | $this->supported = array(
53 | 'sequences' => 'emulated',
54 | 'indexes' => true,
55 | 'affected_rows' => true,
56 | 'transactions' => true,
57 | 'savepoints' => false,
58 | 'summary_functions' => true,
59 | 'order_by_text' => true,
60 | 'current_id' => 'emulated',
61 | 'limit_queries' => true,
62 | 'LOBs' => true,
63 | 'replace' => true,
64 | 'sub_selects' => true,
65 | 'auto_increment' => true,
66 | 'primary_key' => true,
67 | 'result_introspection' => true,
68 | 'prepared_statements' => 'emulated',
69 | 'identifier_quoting' => true,
70 | 'pattern_escaping' => true
71 | );
72 |
73 | $this->properties['string_quoting'] = array('start' => "'",
74 | 'end' => "'",
75 | 'escape' => '\\',
76 | 'escape_pattern' => '\\');
77 |
78 | $this->properties['identifier_quoting'] = array('start' => '`',
79 | 'end' => '`',
80 | 'escape' => '`');
81 |
82 | $this->properties['sql_comments'] = array(
83 | array('start' => '-- ', 'end' => "\n", 'escape' => false),
84 | array('start' => '#', 'end' => "\n", 'escape' => false),
85 | array('start' => '/*', 'end' => '*/', 'escape' => false),
86 | );
87 |
88 | $this->properties['varchar_max_length'] = 255;
89 |
90 | parent::__construct($manager, $adapter);
91 | }
92 |
93 | /**
94 | * Set the charset on the current connection
95 | *
96 | * @param string charset
97 | */
98 | public function setCharset($charset)
99 | {
100 | $query = 'SET NAMES ' . $this->quote($charset);
101 |
102 | $this->exec($query);
103 | }
104 |
105 | /**
106 | * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
107 | * query, except that if there is already a row in the table with the same
108 | * key field values, the REPLACE query just updates its values instead of
109 | * inserting a new row.
110 | *
111 | * The REPLACE type of query does not make part of the SQL standards. Since
112 | * practically only MySQL implements it natively, this type of query is
113 | * emulated through this method for other DBMS using standard types of
114 | * queries inside a transaction to assure the atomicity of the operation.
115 | *
116 | * @access public
117 | *
118 | * @param string $table name of the table on which the REPLACE query will
119 | * be executed.
120 | * @param array $fields associative array that describes the fields and the
121 | * values that will be inserted or updated in the specified table. The
122 | * indexes of the array are the names of all the fields of the table. The
123 | * values of the array are also associative arrays that describe the
124 | * values and other properties of the table fields.
125 | *
126 | * Here follows a list of field properties that need to be specified:
127 | *
128 | * value:
129 | * Value to be assigned to the specified field. This value may be
130 | * of specified in database independent type format as this
131 | * function can perform the necessary datatype conversions.
132 | *
133 | * Default:
134 | * this property is required unless the Null property
135 | * is set to 1.
136 | *
137 | * type
138 | * Name of the type of the field. Currently, all types Metabase
139 | * are supported except for clob and blob.
140 | *
141 | * Default: no type conversion
142 | *
143 | * null
144 | * Boolean property that indicates that the value for this field
145 | * should be set to null.
146 | *
147 | * The default value for fields missing in INSERT queries may be
148 | * specified the definition of a table. Often, the default value
149 | * is already null, but since the REPLACE may be emulated using
150 | * an UPDATE query, make sure that all fields of the table are
151 | * listed in this function argument array.
152 | *
153 | * Default: 0
154 | *
155 | * key
156 | * Boolean property that indicates that this field should be
157 | * handled as a primary key or at least as part of the compound
158 | * unique index of the table that will determine the row that will
159 | * updated if it exists or inserted a new row otherwise.
160 | *
161 | * This function will fail if no key field is specified or if the
162 | * value of a key field is set to null because fields that are
163 | * part of unique index they may not be null.
164 | *
165 | * Default: 0
166 | *
167 | * @return integer the number of affected rows
168 | */
169 | public function replace(Doctrine_Table $table, array $fields, array $keys)
170 | {
171 | $count = count($fields);
172 | $query = $values = '';
173 | $keys = $colnum = 0;
174 |
175 | for (reset($fields); $colnum < $count; next($fields), $colnum++) {
176 | $name = key($fields);
177 |
178 | if ($colnum > 0) {
179 | $query .= ',';
180 | $values.= ',';
181 | }
182 |
183 | $query .= $table->getColumnName($name);
184 |
185 | if (isset($fields[$name]['null']) && $fields[$name]['null']) {
186 | $value = 'NULL';
187 | } else {
188 | $type = isset($fields[$name]['type']) ? $fields[$name]['type'] : null;
189 | $value = $this->quote($fields[$name]['value'], $type);
190 | }
191 |
192 | $values .= $value;
193 |
194 | if (isset($fields[$name]['key']) && $fields[$name]['key']) {
195 | if ($value === 'NULL') {
196 | throw new Doctrine_Connection_Mysql_Exception('key value '.$name.' may not be NULL');
197 | }
198 | $keys++;
199 | }
200 | }
201 |
202 | if ($keys == 0) {
203 | throw new Doctrine_Connection_Mysql_Exception('not specified which fields are keys');
204 | }
205 | $query = 'REPLACE INTO ' . $table->getTableName() . ' (' . $query . ') VALUES (' . $values . ')';
206 |
207 | return $this->exec($query);
208 | }
209 | }