Coverage for Doctrine_Connection_Mysql

Back to coverage report

1 <?php
2 /*
3  *  $Id: Mysql.php 3884 2008-02-22 18:26:35Z jwage $
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: 3884 $
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      * returns the name of the connected database
95      *
96      * @return string
97      */
98     public function getDatabaseName()
99     {
100         return $this->fetchOne('SELECT DATABASE()');
101     }
102
103     /**
104      * Set the charset on the current connection
105      *
106      * @param string    charset
107      */
108     public function setCharset($charset)
109     {
110         $query = 'SET NAMES ' . $this->quote($charset);
111
112         $this->exec($query);
113     }
114
115     /**
116      * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
117      * query, except that if there is already a row in the table with the same
118      * key field values, the REPLACE query just updates its values instead of
119      * inserting a new row.
120      *
121      * The REPLACE type of query does not make part of the SQL standards. Since
122      * practically only MySQL implements it natively, this type of query is
123      * emulated through this method for other DBMS using standard types of
124      * queries inside a transaction to assure the atomicity of the operation.
125      *
126      * @access public
127      *
128      * @param string $table name of the table on which the REPLACE query will
129      *  be executed.
130      * @param array $fields associative array that describes the fields and the
131      *  values that will be inserted or updated in the specified table. The
132      *  indexes of the array are the names of all the fields of the table. The
133      *  values of the array are also associative arrays that describe the
134      *  values and other properties of the table fields.
135      *
136      *  Here follows a list of field properties that need to be specified:
137      *
138      *    value:
139      *          Value to be assigned to the specified field. This value may be
140      *          of specified in database independent type format as this
141      *          function can perform the necessary datatype conversions.
142      *
143      *    Default:
144      *          this property is required unless the Null property
145      *          is set to 1.
146      *
147      *    type
148      *          Name of the type of the field. Currently, all types Metabase
149      *          are supported except for clob and blob.
150      *
151      *    Default: no type conversion
152      *
153      *    null
154      *          Boolean property that indicates that the value for this field
155      *          should be set to null.
156      *
157      *          The default value for fields missing in INSERT queries may be
158      *          specified the definition of a table. Often, the default value
159      *          is already null, but since the REPLACE may be emulated using
160      *          an UPDATE query, make sure that all fields of the table are
161      *          listed in this function argument array.
162      *
163      *    Default: 0
164      *
165      *    key
166      *          Boolean property that indicates that this field should be
167      *          handled as a primary key or at least as part of the compound
168      *          unique index of the table that will determine the row that will
169      *          updated if it exists or inserted a new row otherwise.
170      *
171      *          This function will fail if no key field is specified or if the
172      *          value of a key field is set to null because fields that are
173      *          part of unique index they may not be null.
174      *
175      *    Default: 0
176      *
177      * @return integer      the number of affected rows
178      */
179     public function replace(Doctrine_Table $table, array $fields, array $keys)
180     {
181         $count = count($fields);
182         $query = $values = '';
183         $keys = $colnum = 0;
184
185         for (reset($fields); $colnum < $count; next($fields), $colnum++) {
186             $name = key($fields);
187
188             if ($colnum > 0) {
189                 $query .= ',';
190                 $values.= ',';
191             }
192
193             $query .= $table->getColumnName($name);
194
195             if (isset($fields[$name]['null']) && $fields[$name]['null']) {
196                 $value = 'NULL';
197             } else {
198                 $type = isset($fields[$name]['type']) ? $fields[$name]['type'] : null;
199                 $value = $this->quote($fields[$name]['value'], $type);
200             }
201
202             $values .= $value;
203
204             if (isset($fields[$name]['key']) && $fields[$name]['key']) {
205                 if ($value === 'NULL') {
206                     throw new Doctrine_Connection_Mysql_Exception('key value '.$name.' may not be NULL');
207                 }
208                 $keys++;
209             }
210         }
211
212         if ($keys == 0) {
213             throw new Doctrine_Connection_Mysql_Exception('not specified which fields are keys');
214         }
215         $query = 'REPLACE INTO ' . $table->getTableName() . ' (' . $query . ') VALUES (' . $values . ')';
216
217         return $this->exec($query);
218     }
219 }