3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\DBAL\Types;
22 use Doctrine\DBAL\Platforms\AbstractPlatform,
23 Doctrine\DBAL\DBALException;
26 * The base class for so-called Doctrine mapping types.
28 * A Type object is obtained by calling the static {@link getType()} method.
30 * @author Roman Borschel <roman@code-factory.org>
31 * @author Benjamin Eberlei <kontakt@beberlei.de>
36 const TARRAY = 'array';
37 const SIMPLE_ARRAY = 'simple_array';
38 const JSON_ARRAY = 'json_array';
39 const BIGINT = 'bigint';
40 const BOOLEAN = 'boolean';
41 const DATETIME = 'datetime';
42 const DATETIMETZ = 'datetimetz';
45 const DECIMAL = 'decimal';
46 const INTEGER = 'integer';
47 const OBJECT = 'object';
48 const SMALLINT = 'smallint';
49 const STRING = 'string';
52 const FLOAT = 'float';
55 /** Map of already instantiated type objects. One instance per type (flyweight). */
56 private static $_typeObjects = array();
58 /** The map of supported doctrine mapping types. */
59 private static $_typesMap = array(
60 self::TARRAY => 'Doctrine\DBAL\Types\ArrayType',
61 self::SIMPLE_ARRAY => 'Doctrine\DBAL\Types\SimpleArrayType',
62 self::JSON_ARRAY => 'Doctrine\DBAL\Types\JsonArrayType',
63 self::OBJECT => 'Doctrine\DBAL\Types\ObjectType',
64 self::BOOLEAN => 'Doctrine\DBAL\Types\BooleanType',
65 self::INTEGER => 'Doctrine\DBAL\Types\IntegerType',
66 self::SMALLINT => 'Doctrine\DBAL\Types\SmallIntType',
67 self::BIGINT => 'Doctrine\DBAL\Types\BigIntType',
68 self::STRING => 'Doctrine\DBAL\Types\StringType',
69 self::TEXT => 'Doctrine\DBAL\Types\TextType',
70 self::DATETIME => 'Doctrine\DBAL\Types\DateTimeType',
71 self::DATETIMETZ => 'Doctrine\DBAL\Types\DateTimeTzType',
72 self::DATE => 'Doctrine\DBAL\Types\DateType',
73 self::TIME => 'Doctrine\DBAL\Types\TimeType',
74 self::DECIMAL => 'Doctrine\DBAL\Types\DecimalType',
75 self::FLOAT => 'Doctrine\DBAL\Types\FloatType',
76 self::BLOB => 'Doctrine\DBAL\Types\BlobType',
77 self::GUID => 'Doctrine\DBAL\Types\GuidType',
80 /* Prevent instantiation and force use of the factory method. */
81 final private function __construct() {}
84 * Converts a value from its PHP representation to its database representation
87 * @param mixed $value The value to convert.
88 * @param AbstractPlatform $platform The currently used database platform.
89 * @return mixed The database representation of the value.
91 public function convertToDatabaseValue($value, AbstractPlatform $platform)
97 * Converts a value from its database representation to its PHP representation
100 * @param mixed $value The value to convert.
101 * @param AbstractPlatform $platform The currently used database platform.
102 * @return mixed The PHP representation of the value.
104 public function convertToPHPValue($value, AbstractPlatform $platform)
110 * Gets the default length of this type.
114 public function getDefaultLength(AbstractPlatform $platform)
120 * Gets the SQL declaration snippet for a field of this type.
122 * @param array $fieldDeclaration The field declaration.
123 * @param AbstractPlatform $platform The currently used database platform.
125 abstract public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform);
128 * Gets the name of this type.
133 abstract public function getName();
136 * Factory method to create type instances.
137 * Type instances are implemented as flyweights.
140 * @throws DBALException
141 * @param string $name The name of the type (as returned by getName()).
142 * @return \Doctrine\DBAL\Types\Type
144 public static function getType($name)
146 if ( ! isset(self::$_typeObjects[$name])) {
147 if ( ! isset(self::$_typesMap[$name])) {
148 throw DBALException::unknownColumnType($name);
150 self::$_typeObjects[$name] = new self::$_typesMap[$name]();
153 return self::$_typeObjects[$name];
157 * Adds a custom type to the type map.
160 * @param string $name Name of the type. This should correspond to what getName() returns.
161 * @param string $className The class name of the custom type.
162 * @throws DBALException
164 public static function addType($name, $className)
166 if (isset(self::$_typesMap[$name])) {
167 throw DBALException::typeExists($name);
170 self::$_typesMap[$name] = $className;
174 * Checks if exists support for a type.
177 * @param string $name Name of the type
178 * @return boolean TRUE if type is supported; FALSE otherwise
180 public static function hasType($name)
182 return isset(self::$_typesMap[$name]);
186 * Overrides an already defined type to use a different implementation.
189 * @param string $name
190 * @param string $className
191 * @throws DBALException
193 public static function overrideType($name, $className)
195 if ( ! isset(self::$_typesMap[$name])) {
196 throw DBALException::typeNotFound($name);
199 if (isset(self::$_typeObjects[$name])) {
200 unset(self::$_typeObjects[$name]);
203 self::$_typesMap[$name] = $className;
207 * Gets the (preferred) binding type for values of this type that
208 * can be used when binding parameters to prepared statements.
210 * This method should return one of the PDO::PARAM_* constants, that is, one of:
220 public function getBindingType()
222 return \PDO::PARAM_STR;
226 * Get the types array map which holds all registered types and the corresponding
229 * @return array $typesMap
231 public static function getTypesMap()
233 return self::$_typesMap;
236 public function __toString()
238 $e = explode('\\', get_class($this));
239 return str_replace('Type', '', end($e));
243 * Does working with this column require SQL conversion functions?
245 * This is a metadata function that is required for example in the ORM.
246 * Usage of {@link convertToDatabaseValueSQL} and
247 * {@link convertToPHPValueSQL} works for any type and mostly
248 * does nothing. This method can additionally be used for optimization purposes.
252 public function canRequireSQLConversion()
258 * Modifies the SQL expression (identifier, parameter) to convert to a database value.
260 * @param string $sqlExpr
261 * @param AbstractPlatform $platform
264 public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
270 * Modifies the SQL expression (identifier, parameter) to convert to a PHP value.
272 * @param string $sqlExpr
273 * @param AbstractPlatform $platform
276 public function convertToPHPValueSQL($sqlExpr, $platform)
282 * Get an array of database types that map to this Doctrine type.
284 * @param AbstractPlatform $platform
287 public function getMappedDatabaseTypes(AbstractPlatform $platform)
293 * If this Doctrine Type maps to an already mapped database type,
294 * reverse schema engineering can't take them apart. You need to mark
295 * one of those types as commented, which will have Doctrine use an SQL
296 * comment to typehint the actual Doctrine Type.
298 * @param AbstractPlatform $platform
301 public function requiresSQLCommentHint(AbstractPlatform $platform)