2006-08-22 03:16:55 +04:00
|
|
|
<?php
|
2006-12-29 17:01:31 +03:00
|
|
|
/*
|
2006-10-23 00:50:27 +04:00
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
Doctrine::autoload('Doctrine_Connection_Common');
|
2006-08-22 03:16:55 +04:00
|
|
|
/**
|
2006-10-23 00:50:27 +04:00
|
|
|
* Doctrine_Connection_Mysql
|
|
|
|
*
|
2006-11-14 01:08:41 +03:00
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Connection
|
2006-11-14 01:08:41 +03:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
2006-11-01 13:59:23 +03:00
|
|
|
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
2006-11-14 01:08:41 +03:00
|
|
|
* @version $Revision$
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
class Doctrine_Connection_Mysql extends Doctrine_Connection_Common
|
|
|
|
{
|
2006-10-31 02:27:26 +03:00
|
|
|
/**
|
|
|
|
* @var string $driverName the name of this connection driver
|
|
|
|
*/
|
|
|
|
protected $driverName = 'Mysql';
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-08-22 03:16:55 +04:00
|
|
|
/**
|
|
|
|
* the constructor
|
2006-10-31 15:54:58 +03:00
|
|
|
*
|
|
|
|
* @param Doctrine_Manager $manager
|
2006-11-20 01:15:23 +03:00
|
|
|
* @param PDO|Doctrine_Adapter $adapter database handler
|
2006-08-22 03:16:55 +04:00
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function __construct(Doctrine_Manager $manager, $adapter)
|
|
|
|
{
|
2007-06-20 03:33:04 +04:00
|
|
|
$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
|
2006-11-29 02:26:44 +03:00
|
|
|
$this->setAttribute(Doctrine::ATTR_DEFAULT_TABLE_TYPE, 'INNODB');
|
2006-11-08 13:18:15 +03:00
|
|
|
|
2006-10-31 02:00:09 +03:00
|
|
|
$this->supported = array(
|
|
|
|
'sequences' => 'emulated',
|
|
|
|
'indexes' => true,
|
|
|
|
'affected_rows' => true,
|
|
|
|
'transactions' => true,
|
|
|
|
'savepoints' => false,
|
|
|
|
'summary_functions' => true,
|
|
|
|
'order_by_text' => true,
|
|
|
|
'current_id' => 'emulated',
|
|
|
|
'limit_queries' => true,
|
|
|
|
'LOBs' => true,
|
|
|
|
'replace' => true,
|
|
|
|
'sub_selects' => true,
|
|
|
|
'auto_increment' => true,
|
|
|
|
'primary_key' => true,
|
|
|
|
'result_introspection' => true,
|
|
|
|
'prepared_statements' => 'emulated',
|
|
|
|
'identifier_quoting' => true,
|
|
|
|
'pattern_escaping' => true
|
|
|
|
);
|
2006-12-29 17:01:31 +03:00
|
|
|
|
|
|
|
$this->properties['string_quoting'] = array('start' => "'",
|
|
|
|
'end' => "'",
|
2006-11-30 17:40:50 +03:00
|
|
|
'escape' => '\\',
|
2006-11-28 01:22:31 +03:00
|
|
|
'escape_pattern' => '\\');
|
|
|
|
|
|
|
|
$this->properties['identifier_quoting'] = array('start' => '`',
|
2006-12-29 17:01:31 +03:00
|
|
|
'end' => '`',
|
2006-11-28 01:22:31 +03:00
|
|
|
'escape' => '`');
|
|
|
|
|
|
|
|
$this->properties['sql_comments'] = array(
|
|
|
|
array('start' => '-- ', 'end' => "\n", 'escape' => false),
|
|
|
|
array('start' => '#', 'end' => "\n", 'escape' => false),
|
|
|
|
array('start' => '/*', 'end' => '*/', 'escape' => false),
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->properties['varchar_max_length'] = 255;
|
2006-10-31 02:00:09 +03:00
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
parent::__construct($manager, $adapter);
|
2006-11-28 01:22:31 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-12-06 09:58:24 +03:00
|
|
|
/**
|
|
|
|
* Set the charset on the current connection
|
|
|
|
*
|
|
|
|
* @param string charset
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function setCharset($charset)
|
|
|
|
{
|
2007-11-03 18:47:24 +03:00
|
|
|
$query = 'SET NAMES ' . $this->quote($charset);
|
|
|
|
|
2007-01-06 01:04:11 +03:00
|
|
|
$this->exec($query);
|
2006-12-06 09:58:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-11-20 01:15:23 +03:00
|
|
|
/**
|
|
|
|
* Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
|
|
|
|
* query, except that if there is already a row in the table with the same
|
|
|
|
* key field values, the REPLACE query just updates its values instead of
|
|
|
|
* inserting a new row.
|
|
|
|
*
|
|
|
|
* The REPLACE type of query does not make part of the SQL standards. Since
|
|
|
|
* practically only MySQL implements it natively, this type of query is
|
|
|
|
* emulated through this method for other DBMS using standard types of
|
|
|
|
* queries inside a transaction to assure the atomicity of the operation.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*
|
|
|
|
* @param string $table name of the table on which the REPLACE query will
|
|
|
|
* be executed.
|
|
|
|
* @param array $fields associative array that describes the fields and the
|
|
|
|
* values that will be inserted or updated in the specified table. The
|
|
|
|
* indexes of the array are the names of all the fields of the table. The
|
|
|
|
* values of the array are also associative arrays that describe the
|
|
|
|
* values and other properties of the table fields.
|
|
|
|
*
|
|
|
|
* Here follows a list of field properties that need to be specified:
|
|
|
|
*
|
|
|
|
* value:
|
|
|
|
* Value to be assigned to the specified field. This value may be
|
|
|
|
* of specified in database independent type format as this
|
|
|
|
* function can perform the necessary datatype conversions.
|
|
|
|
*
|
|
|
|
* Default:
|
|
|
|
* this property is required unless the Null property
|
|
|
|
* is set to 1.
|
|
|
|
*
|
|
|
|
* type
|
|
|
|
* Name of the type of the field. Currently, all types Metabase
|
|
|
|
* are supported except for clob and blob.
|
|
|
|
*
|
|
|
|
* Default: no type conversion
|
|
|
|
*
|
|
|
|
* null
|
|
|
|
* Boolean property that indicates that the value for this field
|
|
|
|
* should be set to null.
|
|
|
|
*
|
|
|
|
* The default value for fields missing in INSERT queries may be
|
|
|
|
* specified the definition of a table. Often, the default value
|
|
|
|
* is already null, but since the REPLACE may be emulated using
|
|
|
|
* an UPDATE query, make sure that all fields of the table are
|
|
|
|
* listed in this function argument array.
|
|
|
|
*
|
|
|
|
* Default: 0
|
|
|
|
*
|
|
|
|
* key
|
|
|
|
* Boolean property that indicates that this field should be
|
|
|
|
* handled as a primary key or at least as part of the compound
|
|
|
|
* unique index of the table that will determine the row that will
|
|
|
|
* updated if it exists or inserted a new row otherwise.
|
|
|
|
*
|
|
|
|
* This function will fail if no key field is specified or if the
|
|
|
|
* value of a key field is set to null because fields that are
|
|
|
|
* part of unique index they may not be null.
|
|
|
|
*
|
|
|
|
* Default: 0
|
|
|
|
*
|
|
|
|
* @return integer the number of affected rows
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function replace($table, array $fields, array $keys)
|
|
|
|
{
|
2006-11-20 01:15:23 +03:00
|
|
|
$count = count($fields);
|
|
|
|
$query = $values = '';
|
|
|
|
$keys = $colnum = 0;
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
for (reset($fields); $colnum < $count; next($fields), $colnum++) {
|
2006-11-20 01:15:23 +03:00
|
|
|
$name = key($fields);
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($colnum > 0) {
|
2006-11-20 01:15:23 +03:00
|
|
|
$query .= ',';
|
|
|
|
$values.= ',';
|
|
|
|
}
|
|
|
|
|
|
|
|
$query .= $name;
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($fields[$name]['null']) && $fields[$name]['null']) {
|
2006-11-20 01:15:23 +03:00
|
|
|
$value = 'NULL';
|
|
|
|
} else {
|
|
|
|
$type = isset($fields[$name]['type']) ? $fields[$name]['type'] : null;
|
|
|
|
$value = $this->quote($fields[$name]['value'], $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$values .= $value;
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($fields[$name]['key']) && $fields[$name]['key']) {
|
|
|
|
if ($value === 'NULL') {
|
2006-11-20 01:15:23 +03:00
|
|
|
throw new Doctrine_Connection_Mysql_Exception('key value '.$name.' may not be NULL');
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-11-20 01:15:23 +03:00
|
|
|
$keys++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($keys == 0) {
|
2006-11-20 01:15:23 +03:00
|
|
|
throw new Doctrine_Connection_Mysql_Exception('not specified which fields are keys');
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-11-20 01:15:23 +03:00
|
|
|
$query = 'REPLACE INTO ' . $table . ' (' . $query . ') VALUES (' . $values . ')';
|
2006-10-23 00:50:27 +04:00
|
|
|
|
2007-01-06 01:04:11 +03:00
|
|
|
return $this->exec($query);
|
2006-11-20 01:15:23 +03:00
|
|
|
}
|
2007-11-03 18:47:24 +03:00
|
|
|
}
|