| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * $Id$ |
|---|
| 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 | |
|---|
| 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$ |
|---|
| 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(Doctrine::ATTR_DEFAULT_TABLE_TYPE, 'INNODB'); |
|---|
| 50 | $this->supported = array( |
|---|
| 51 | 'sequences' => 'emulated', |
|---|
| 52 | 'indexes' => true, |
|---|
| 53 | 'affected_rows' => true, |
|---|
| 54 | 'transactions' => true, |
|---|
| 55 | 'savepoints' => false, |
|---|
| 56 | 'summary_functions' => true, |
|---|
| 57 | 'order_by_text' => true, |
|---|
| 58 | 'current_id' => 'emulated', |
|---|
| 59 | 'limit_queries' => true, |
|---|
| 60 | 'LOBs' => true, |
|---|
| 61 | 'replace' => true, |
|---|
| 62 | 'sub_selects' => true, |
|---|
| 63 | 'auto_increment' => true, |
|---|
| 64 | 'primary_key' => true, |
|---|
| 65 | 'result_introspection' => true, |
|---|
| 66 | 'prepared_statements' => 'emulated', |
|---|
| 67 | 'identifier_quoting' => true, |
|---|
| 68 | 'pattern_escaping' => true |
|---|
| 69 | ); |
|---|
| 70 | |
|---|
| 71 | $this->properties['string_quoting'] = array('start' => "'", |
|---|
| 72 | 'end' => "'", |
|---|
| 73 | 'escape' => '\\', |
|---|
| 74 | 'escape_pattern' => '\\'); |
|---|
| 75 | |
|---|
| 76 | $this->properties['identifier_quoting'] = array('start' => '`', |
|---|
| 77 | 'end' => '`', |
|---|
| 78 | 'escape' => '`'); |
|---|
| 79 | |
|---|
| 80 | $this->properties['sql_comments'] = array( |
|---|
| 81 | array('start' => '-- ', 'end' => "\n", 'escape' => false), |
|---|
| 82 | array('start' => '#', 'end' => "\n", 'escape' => false), |
|---|
| 83 | array('start' => '/*', 'end' => '*/', 'escape' => false), |
|---|
| 84 | ); |
|---|
| 85 | |
|---|
| 86 | $this->properties['varchar_max_length'] = 255; |
|---|
| 87 | |
|---|
| 88 | parent::__construct($manager, $adapter); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * Overrides connect Method, to add specific attributes |
|---|
| 93 | * PDO emulate prepares is required to avoid bugs on mysql < 5.1 |
|---|
| 94 | * when trying to prepare DROP DATABASE or CREATE DATABASE statements |
|---|
| 95 | * |
|---|
| 96 | * @see Doctrine_Connection :: connect(); |
|---|
| 97 | * @return boolean connected |
|---|
| 98 | */ |
|---|
| 99 | public function connect() |
|---|
| 100 | { |
|---|
| 101 | $connected = parent::connect(); |
|---|
| 102 | $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
|---|
| 103 | |
|---|
| 104 | return $connected; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * returns the name of the connected database |
|---|
| 110 | * |
|---|
| 111 | * @return string |
|---|
| 112 | */ |
|---|
| 113 | public function getDatabaseName() |
|---|
| 114 | { |
|---|
| 115 | return $this->fetchOne('SELECT DATABASE()'); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * Set the charset on the current connection |
|---|
| 120 | * |
|---|
| 121 | * @param string charset |
|---|
| 122 | */ |
|---|
| 123 | public function setCharset($charset) |
|---|
| 124 | { |
|---|
| 125 | $query = 'SET NAMES ' . $this->quote($charset); |
|---|
| 126 | |
|---|
| 127 | $this->exec($query); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT |
|---|
| 132 | * query, except that if there is already a row in the table with the same |
|---|
| 133 | * key field values, the REPLACE query just updates its values instead of |
|---|
| 134 | * inserting a new row. |
|---|
| 135 | * |
|---|
| 136 | * The REPLACE type of query does not make part of the SQL standards. Since |
|---|
| 137 | * practically only MySQL implements it natively, this type of query is |
|---|
| 138 | * emulated through this method for other DBMS using standard types of |
|---|
| 139 | * queries inside a transaction to assure the atomicity of the operation. |
|---|
| 140 | * |
|---|
| 141 | * @access public |
|---|
| 142 | * |
|---|
| 143 | * @param string $table name of the table on which the REPLACE query will |
|---|
| 144 | * be executed. |
|---|
| 145 | * @param array $fields associative array that describes the fields and the |
|---|
| 146 | * values that will be inserted or updated in the specified table. The |
|---|
| 147 | * indexes of the array are the names of all the fields of the table. The |
|---|
| 148 | * values of the array are also associative arrays that describe the |
|---|
| 149 | * values and other properties of the table fields. |
|---|
| 150 | * |
|---|
| 151 | * Here follows a list of field properties that need to be specified: |
|---|
| 152 | * |
|---|
| 153 | * value: |
|---|
| 154 | * Value to be assigned to the specified field. This value may be |
|---|
| 155 | * of specified in database independent type format as this |
|---|
| 156 | * function can perform the necessary datatype conversions. |
|---|
| 157 | * |
|---|
| 158 | * Default: |
|---|
| 159 | * this property is required unless the Null property |
|---|
| 160 | * is set to 1. |
|---|
| 161 | * |
|---|
| 162 | * type |
|---|
| 163 | * Name of the type of the field. Currently, all types Metabase |
|---|
| 164 | * are supported except for clob and blob. |
|---|
| 165 | * |
|---|
| 166 | * Default: no type conversion |
|---|
| 167 | * |
|---|
| 168 | * null |
|---|
| 169 | * Boolean property that indicates that the value for this field |
|---|
| 170 | * should be set to null. |
|---|
| 171 | * |
|---|
| 172 | * The default value for fields missing in INSERT queries may be |
|---|
| 173 | * specified the definition of a table. Often, the default value |
|---|
| 174 | * is already null, but since the REPLACE may be emulated using |
|---|
| 175 | * an UPDATE query, make sure that all fields of the table are |
|---|
| 176 | * listed in this function argument array. |
|---|
| 177 | * |
|---|
| 178 | * Default: 0 |
|---|
| 179 | * |
|---|
| 180 | * key |
|---|
| 181 | * Boolean property that indicates that this field should be |
|---|
| 182 | * handled as a primary key or at least as part of the compound |
|---|
| 183 | * unique index of the table that will determine the row that will |
|---|
| 184 | * updated if it exists or inserted a new row otherwise. |
|---|
| 185 | * |
|---|
| 186 | * This function will fail if no key field is specified or if the |
|---|
| 187 | * value of a key field is set to null because fields that are |
|---|
| 188 | * part of unique index they may not be null. |
|---|
| 189 | * |
|---|
| 190 | * Default: 0 |
|---|
| 191 | * |
|---|
| 192 | * @return integer the number of affected rows |
|---|
| 193 | */ |
|---|
| 194 | public function replace(Doctrine_Table $table, array $fields, array $keys) |
|---|
| 195 | { |
|---|
| 196 | if (empty($keys)) { |
|---|
| 197 | throw new Doctrine_Connection_Exception('Not specified which fields are keys'); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | $columns = array(); |
|---|
| 201 | $values = array(); |
|---|
| 202 | $params = array(); |
|---|
| 203 | foreach ($fields as $fieldName => $value) { |
|---|
| 204 | $columns[] = $table->getColumnName($fieldName); |
|---|
| 205 | $values[] = '?'; |
|---|
| 206 | $params[] = $value; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | $query = 'REPLACE INTO ' . $this->quoteIdentifier($table->getTableName()) . ' (' . implode(',', $columns) . ') VALUES (' . implode(',', $values) . ')'; |
|---|
| 210 | |
|---|
| 211 | return $this->exec($query, $params); |
|---|
| 212 | } |
|---|
| 213 | } |
|---|