|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Licensed to CRATE Technology GmbH("Crate") under one or more contributor |
|
4
|
|
|
* license agreements. See the NOTICE file distributed with this work for |
|
5
|
|
|
* additional information regarding copyright ownership. Crate licenses |
|
6
|
|
|
* this file to you under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. You may |
|
8
|
|
|
* obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15
|
|
|
* License for the specific language governing permissions and limitations |
|
16
|
|
|
* under the License. |
|
17
|
|
|
* |
|
18
|
|
|
* However, if you have executed another commercial license agreement |
|
19
|
|
|
* with Crate these terms will supersede the license and you may use the |
|
20
|
|
|
* software solely pursuant to the terms of the relevant commercial agreement. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
namespace Crate\DBAL\Types; |
|
24
|
|
|
|
|
25
|
|
|
use Crate\DBAL\Platforms\CratePlatform; |
|
26
|
|
|
use Crate\PDO\PDO; |
|
27
|
|
|
use Doctrine\DBAL\Types\Type; |
|
28
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Type that maps a PHP associative array (map) to an object SQL type. |
|
32
|
|
|
* |
|
33
|
|
|
* TODO: Add support for strict|dynamic|ignored object types |
|
34
|
|
|
* |
|
35
|
|
|
*/ |
|
36
|
|
|
class MapType extends Type |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
const NAME = 'map'; |
|
40
|
|
|
const STRICT = 'strict'; |
|
41
|
|
|
const DYNAMIC = 'dynamic'; |
|
42
|
|
|
const IGNORED = 'ignored'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Gets the name of this type. |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
74 |
|
public function getName() |
|
50
|
|
|
{ |
|
51
|
74 |
|
return self::NAME; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Gets the (preferred) binding type for values of this type that |
|
56
|
|
|
* can be used when binding parameters to prepared statements. |
|
57
|
|
|
* |
|
58
|
|
|
* @return integer |
|
59
|
|
|
*/ |
|
60
|
54 |
|
public function getBindingType() |
|
61
|
|
|
{ |
|
62
|
54 |
|
return PDO::PARAM_OBJECT; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
60 |
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
|
66
|
|
|
{ |
|
67
|
60 |
|
if (!is_array($value) || (count($value) > 0 && !(array_keys($value) !== range(0, count($value) - 1)))) { |
|
68
|
4 |
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
56 |
|
return $value; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
|
75
|
|
|
{ |
|
76
|
2 |
|
return $value == null ?: (array) $value; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Gets the SQL declaration snippet for a field of this type. |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $fieldDeclaration The field declaration. |
|
83
|
|
|
* @param AbstractPlatform $platform The currently used database platform. |
|
84
|
|
|
* @return string |
|
85
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
86
|
|
|
*/ |
|
87
|
72 |
|
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
|
88
|
|
|
{ |
|
89
|
72 |
|
$options = !array_key_exists('platformOptions', $fieldDeclaration) ? |
|
90
|
72 |
|
array() : $fieldDeclaration['platformOptions']; |
|
91
|
|
|
|
|
92
|
72 |
|
return $this->getMapTypeDeclarationSQL($platform, $fieldDeclaration, $options); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Gets the SQL snippet used to declare an OBJECT column type. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $field |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
102
|
|
|
*/ |
|
103
|
72 |
|
public function getMapTypeDeclarationSQL(AbstractPlatform $platform, array $field, array $options) |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
72 |
|
$type = array_key_exists('type', $options) ? $options['type'] : MapType::DYNAMIC; |
|
106
|
|
|
|
|
107
|
72 |
|
$fields = array_key_exists('fields', $options) ? $options['fields'] : array(); |
|
108
|
72 |
|
$columns = array(); |
|
109
|
72 |
|
foreach ($fields as $field) { |
|
110
|
70 |
|
$columns[$field->getQuotedName($platform)] = CratePlatform::prepareColumnData($platform, $field); |
|
111
|
|
|
} |
|
112
|
72 |
|
$objectFields = $platform->getColumnDeclarationListSQL($columns); |
|
113
|
|
|
|
|
114
|
72 |
|
$declaration = count($columns) > 0 ? ' AS ( ' . $objectFields . ' )' : ''; |
|
115
|
72 |
|
return 'OBJECT ( ' . $type . ' )' . $declaration ; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.