Doctrine 1.2.4
Doctrine_DataDict_Mysql Class Reference

Inherits Doctrine_DataDict.

Public Member Functions

 getCharsetFieldDeclaration ($charset)
 
 getCollationFieldDeclaration ($collation)
 
 getConnection ()
 
 getIntegerDeclaration ($name, $field)
 
 getModuleName ()
 
 getNativeDeclaration ($field)
 
 getPortableDeclaration (array $field)
 
 parseBoolean ($value)
 

Detailed Description

Definition at line 32 of file Mysql.php.

Member Function Documentation

Doctrine_DataDict_Mysql::getCharsetFieldDeclaration (   $charset)

Obtain DBMS specific SQL code portion needed to set the CHARACTER SET of a field declaration to be used in statements like CREATE TABLE.

Parameters
string$charsetname of the charset
Returns
string DBMS specific SQL code portion needed to set the CHARACTER SET of a field declaration.

Definition at line 445 of file Mysql.php.

{
return 'CHARACTER SET ' . $charset;
}
Doctrine_DataDict_Mysql::getCollationFieldDeclaration (   $collation)

Obtain DBMS specific SQL code portion needed to set the COLLATION of a field declaration to be used in statements like CREATE TABLE.

Parameters
string$collationname of the collation
Returns
string DBMS specific SQL code portion needed to set the COLLATION of a field declaration.

Definition at line 458 of file Mysql.php.

{
return 'COLLATE ' . $collation;
}
Doctrine_Connection_Module::getConnection ( )
inherited

getConnection returns the connection object this module uses

Returns
Doctrine_Connection

Definition at line 68 of file Module.php.

{
return $this->conn;
}
Doctrine_DataDict_Mysql::getIntegerDeclaration (   $name,
  $field 
)

Obtain DBMS specific SQL code portion needed to declare an integer type field to be used in statements like CREATE TABLE.

Parameters
string$namename the field to be declared.
string$fieldassociative array with the name of the properties of the field being declared as array indexes. Currently, the types of supported field properties are as follows:

unsigned Boolean flag that indicates whether the field should be declared as unsigned integer if possible.

default Integer value to be used as default for this field.

notnull Boolean flag that indicates whether this field is constrained to not be set to null.

Returns
string DBMS specific SQL code portion that should be used to declare the specified field.

Definition at line 488 of file Mysql.php.

{
$unique = (isset($field['unique']) && $field['unique']) ? ' UNIQUE' : '';
$default = $autoinc = '';
if ( ! empty($field['autoincrement'])) {
$autoinc = ' AUTO_INCREMENT';
} elseif (array_key_exists('default', $field)) {
if ($field['default'] === '') {
$field['default'] = empty($field['notnull']) ? null : 0;
}
$default = ' DEFAULT ' . (is_null($field['default'])
? 'NULL'
: $this->conn->quote($field['default']));
}
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
$unsigned = (isset($field['unsigned']) && $field['unsigned']) ? ' UNSIGNED' : '';
$comment = (isset($field['comment']) && $field['comment'])
? " COMMENT " . $this->conn->quote($field['comment'], 'text') : '';
$name = $this->conn->quoteIdentifier($name, true);
return $name . ' ' . $this->getNativeDeclaration($field) . $unsigned
. $default . $unique . $notnull . $autoinc . $comment;
}
Doctrine_Connection_Module::getModuleName ( )
inherited

getModuleName returns the name of this module

Returns
string the name of this module

Definition at line 79 of file Module.php.

{
return $this->moduleName;
}
Doctrine_DataDict_Mysql::getNativeDeclaration (   $field)

Obtain DBMS specific SQL code portion needed to declare an text type field to be used in statements like CREATE TABLE.

Parameters
array$fieldassociative array with the name of the properties of the field being declared as array indexes. Currently, the types of supported field properties are as follows:

length Integer value that determines the maximum length of the text field. If this argument is missing the field should be declared to have the longest length allowed by the DBMS.

default Text value to be used as default for this field.

notnull Boolean flag that indicates whether this field is constrained to not be set to null.

Returns
string DBMS specific SQL code portion that should be used to declare the specified field.

Definition at line 134 of file Mysql.php.

{
if ( ! isset($field['type'])) {
throw new Doctrine_DataDict_Exception('Missing column type.');
}
switch ($field['type']) {
case 'char':
$length = ( ! empty($field['length'])) ? $field['length'] : false;
return $length ? 'CHAR('.$length.')' : 'CHAR(255)';
case 'enum':
if ($this->conn->getAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM)) {
$values = array();
foreach ($field['values'] as $value) {
$values[] = $this->conn->quote($value, 'varchar');
}
return 'ENUM('.implode(', ', $values).')';
} else {
$field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255;
}
case 'set':
if ($this->conn->getAttribute(Doctrine_Core::ATTR_USE_NATIVE_SET)) {
$values = array();
foreach ($field['values'] as $value) {
$values[] = $this->conn->quote($value, 'varchar');
}
return 'SET('.implode(', ', $values).')';
} else {
$field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255;
}
case 'varchar':
case 'string':
case 'gzip':
if ( ! isset($field['length'])) {
if (array_key_exists('default', $field)) {
$field['length'] = $this->conn->varchar_max_length;
} else {
$field['length'] = false;
}
}
$length = ($field['length'] <= $this->conn->varchar_max_length) ? $field['length'] : false;
$fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
: ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
case 'array':
case 'object':
case 'clob':
if ( ! empty($field['length'])) {
$length = $field['length'];
if ($length <= 255) {
return 'TINYTEXT';
} elseif ($length <= 65532) {
return 'TEXT';
} elseif ($length <= 16777215) {
return 'MEDIUMTEXT';
}
}
return 'LONGTEXT';
case 'blob':
if ( ! empty($field['length'])) {
$length = $field['length'];
if ($length <= 255) {
return 'TINYBLOB';
} elseif ($length <= 65532) {
return 'BLOB';
} elseif ($length <= 16777215) {
return 'MEDIUMBLOB';
}
}
return 'LONGBLOB';
case 'integer':
case 'int':
if ( ! empty($field['length'])) {
$length = $field['length'];
if ($length <= 1) {
return 'TINYINT';
} elseif ($length == 2) {
return 'SMALLINT';
} elseif ($length == 3) {
return 'MEDIUMINT';
} elseif ($length == 4) {
return 'INT';
} elseif ($length > 4) {
return 'BIGINT';
}
}
return 'INT';
case 'boolean':
return 'TINYINT(1)';
case 'date':
return 'DATE';
case 'time':
return 'TIME';
case 'timestamp':
return 'DATETIME';
case 'float':
$length = !empty($field['length']) ? $field['length'] : 18;
$scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine_Core::ATTR_DECIMAL_PLACES);
return 'FLOAT('.$length.', '.$scale.')';
case 'double':
$length = !empty($field['length']) ? $field['length'] : 18;
$scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine_Core::ATTR_DECIMAL_PLACES);
return 'DOUBLE('.$length.', '.$scale.')';
case 'decimal':
$length = !empty($field['length']) ? $field['length'] : 18;
$scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine_Core::ATTR_DECIMAL_PLACES);
return 'DECIMAL('.$length.', '.$scale.')';
case 'bit':
return 'BIT';
}
return $field['type'] . (isset($field['length']) ? '('.$field['length'].')':null);
}
Doctrine_DataDict_Mysql::getPortableDeclaration ( array  $field)

Maps a native array description of a field to a MDB2 datatype and length

Parameters
array$fieldnative field description
Returns
array containing the various possible types, length, sign, fixed

Definition at line 256 of file Mysql.php.

{
$dbType = strtolower($field['type']);
$dbType = strtok($dbType, '(), ');
if ($dbType == 'national') {
$dbType = strtok('(), ');
}
if (isset($field['length'])) {
$length = $field['length'];
$decimal = '';
} else {
$length = strtok('(), ');
$decimal = strtok('(), ');
if ( ! $decimal ) {
$decimal = null;
}
}
$type = array();
$unsigned = $fixed = null;
if ( ! isset($field['name'])) {
$field['name'] = '';
}
$values = null;
$scale = null;
switch ($dbType) {
case 'tinyint':
$type[] = 'integer';
$type[] = 'boolean';
if (preg_match('/^(is|has)/', $field['name'])) {
$type = array_reverse($type);
}
$unsigned = preg_match('/ unsigned/i', $field['type']);
$length = 1;
break;
case 'smallint':
$type[] = 'integer';
$unsigned = preg_match('/ unsigned/i', $field['type']);
$length = 2;
break;
case 'mediumint':
$type[] = 'integer';
$unsigned = preg_match('/ unsigned/i', $field['type']);
$length = 3;
break;
case 'int':
case 'integer':
$type[] = 'integer';
$unsigned = preg_match('/ unsigned/i', $field['type']);
$length = 4;
break;
case 'bigint':
$type[] = 'integer';
$unsigned = preg_match('/ unsigned/i', $field['type']);
$length = 8;
break;
case 'tinytext':
case 'mediumtext':
case 'longtext':
case 'text':
case 'text':
case 'varchar':
$fixed = false;
case 'string':
case 'char':
$type[] = 'string';
if ($length == '1') {
$type[] = 'boolean';
if (preg_match('/^(is|has)/', $field['name'])) {
$type = array_reverse($type);
}
} elseif (strstr($dbType, 'text')) {
$type[] = 'clob';
if ($decimal == 'binary') {
$type[] = 'blob';
}
}
if ($fixed !== false) {
$fixed = true;
}
break;
case 'enum':
$type[] = 'enum';
preg_match_all('/\'((?:\'\'|[^\'])*)\'/', $field['type'], $matches);
$length = 0;
$fixed = false;
if (is_array($matches)) {
foreach ($matches[1] as &$value) {
$value = str_replace('\'\'', '\'', $value);
$length = max($length, strlen($value));
}
if ($length == '1' && count($matches[1]) == 2) {
$type[] = 'boolean';
if (preg_match('/^(is|has)/', $field['name'])) {
$type = array_reverse($type);
}
}
$values = $matches[1];
}
$type[] = 'integer';
break;
case 'set':
$fixed = false;
$type[] = 'text';
$type[] = 'integer';
break;
case 'date':
$type[] = 'date';
$length = null;
break;
case 'datetime':
case 'timestamp':
$type[] = 'timestamp';
$length = null;
break;
case 'time':
$type[] = 'time';
$length = null;
break;
case 'float':
case 'double':
case 'real':
$type[] = 'float';
$unsigned = preg_match('/ unsigned/i', $field['type']);
break;
case 'unknown':
case 'decimal':
if ($decimal !== null) {
$scale = $decimal;
}
case 'numeric':
$type[] = 'decimal';
$unsigned = preg_match('/ unsigned/i', $field['type']);
break;
case 'tinyblob':
case 'mediumblob':
case 'longblob':
case 'blob':
case 'binary':
case 'varbinary':
$type[] = 'blob';
$length = null;
break;
case 'year':
$type[] = 'integer';
$type[] = 'date';
$length = null;
break;
case 'bit':
$type[] = 'bit';
break;
case 'geometry':
case 'geometrycollection':
case 'point':
case 'multipoint':
case 'linestring':
case 'multilinestring':
case 'polygon':
case 'multipolygon':
$type[] = 'blob';
$length = null;
break;
default:
$type[] = $field['type'];
$length = isset($field['length']) ? $field['length']:null;
}
$length = ((int) $length == 0) ? null : (int) $length;
$def = array('type' => $type, 'length' => $length, 'unsigned' => $unsigned, 'fixed' => $fixed);
if ($values !== null) {
$def['values'] = $values;
}
if ($scale !== null) {
$def['scale'] = $scale;
}
return $def;
}
Doctrine_DataDict::parseBoolean (   $value)
inherited

parseBoolean parses a literal boolean value and returns proper sql equivalent

Parameters
string$valueboolean value to be parsed
Returns
string parsed boolean value

Definition at line 44 of file DataDict.php.

{
// parse booleans
if ($value == 'true') {
$value = 1;
} elseif ($value == 'false') {
$value = 0;
}
return $value;
}

The documentation for this class was generated from the following file: