|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
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. |
|
14
|
|
|
* |
|
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>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\ORM\Mapping; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* A set of rules for determining the physical column, alias and table quotes and automatically escape database reserved |
|
26
|
|
|
* keyword |
|
27
|
|
|
* |
|
28
|
|
|
*@author Xavier HUBERTY <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class EscapingQuoteStrategy implements QuoteStrategy |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getColumnName($fieldName, ClassMetadata $class, AbstractPlatform $platform) |
|
36
|
|
|
{ |
|
37
|
|
|
if(isset($class->fieldMappings[$fieldName]['quoted'])){ |
|
38
|
|
|
return $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName']); |
|
39
|
|
|
} |
|
40
|
|
|
$reservedKeyList = $platform->getReservedKeywordsList(); |
|
41
|
|
|
if($reservedKeyList->isKeyword($fieldName)){ |
|
42
|
|
|
return $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName']); |
|
43
|
|
|
} |
|
44
|
|
|
return $class->fieldMappings[$fieldName]['columnName']; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getTableName(ClassMetadata $class, AbstractPlatform $platform) |
|
51
|
|
|
{ |
|
52
|
|
|
if(isset($class->table['quoted'])){ |
|
53
|
|
|
return $platform->quoteIdentifier($class->table['name']); |
|
54
|
|
|
} |
|
55
|
|
|
$reservedKeyList = $platform->getReservedKeywordsList(); |
|
56
|
|
|
if($reservedKeyList->isKeyword($class->table['name'])){ |
|
57
|
|
|
return $platform->quoteIdentifier($class->table['name']); |
|
58
|
|
|
} |
|
59
|
|
|
return $class->table['name']; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getSequenceName(array $definition, ClassMetadata $class, AbstractPlatform $platform) |
|
66
|
|
|
{ |
|
67
|
|
|
if(isset($definition['quoted'])){ |
|
68
|
|
|
return $platform->quoteIdentifier($class->table['name']); |
|
69
|
|
|
} |
|
70
|
|
|
$reservedKeyList = $platform->getReservedKeywordsList(); |
|
71
|
|
|
if($reservedKeyList->isKeyword($definition['sequenceName'])){ |
|
72
|
|
|
return $platform->quoteIdentifier($definition['sequenceName']); |
|
73
|
|
|
} |
|
74
|
|
|
return $definition['sequenceName']; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform) |
|
81
|
|
|
{ |
|
82
|
|
|
if(isset($joinColumn['quoted'])){ |
|
83
|
|
|
return $platform->quoteIdentifier($joinColumn['name']); |
|
84
|
|
|
} |
|
85
|
|
|
$reservedKeyList = $platform->getReservedKeywordsList(); |
|
86
|
|
|
if($reservedKeyList->isKeyword($joinColumn['name'])){ |
|
87
|
|
|
return $platform->quoteIdentifier($joinColumn['name']); |
|
88
|
|
|
} |
|
89
|
|
|
return $joinColumn['name']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getReferencedJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform) |
|
96
|
|
|
{ |
|
97
|
|
|
if(isset($joinColumn['quoted'])){ |
|
98
|
|
|
return $platform->quoteIdentifier($joinColumn['referencedColumnName']); |
|
99
|
|
|
} |
|
100
|
|
|
$reservedKeyList = $platform->getReservedKeywordsList(); |
|
101
|
|
|
if($reservedKeyList->isKeyword($joinColumn['referencedColumnName'])){ |
|
102
|
|
|
return $platform->quoteIdentifier($joinColumn['referencedColumnName']); |
|
103
|
|
|
} |
|
104
|
|
|
return $joinColumn['referencedColumnName']; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getJoinTableName(array $association, ClassMetadata $class, AbstractPlatform $platform) |
|
111
|
|
|
{ |
|
112
|
|
|
if(isset($association['joinTable']['quoted'])){ |
|
113
|
|
|
return $platform->quoteIdentifier($association['joinTable']['name']); |
|
114
|
|
|
} |
|
115
|
|
|
$reservedKeyList = $platform->getReservedKeywordsList(); |
|
116
|
|
|
if($reservedKeyList->isKeyword($association['joinTable']['name'])){ |
|
117
|
|
|
return $platform->quoteIdentifier($association['joinTable']['name']); |
|
118
|
|
|
} |
|
119
|
|
|
return $association['joinTable']['name']; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getIdentifierColumnNames(ClassMetadata $class, AbstractPlatform $platform) |
|
126
|
|
|
{ |
|
127
|
|
|
$quotedColumnNames = array(); |
|
128
|
|
|
|
|
129
|
|
|
foreach ($class->identifier as $fieldName) { |
|
130
|
|
|
if (isset($class->fieldMappings[$fieldName])) { |
|
131
|
|
|
$quotedColumnNames[] = $this->getColumnName($fieldName, $class, $platform); |
|
132
|
|
|
|
|
133
|
|
|
continue; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// Association defined as Id field |
|
137
|
|
|
$joinColumns = $class->associationMappings[$fieldName]['joinColumns']; |
|
138
|
|
|
$assocQuotedColumnNames = array_map( |
|
139
|
|
|
function ($joinColumn) use ($platform) |
|
140
|
|
|
{ |
|
141
|
|
|
if(isset($joinColumn['quoted'])){ |
|
142
|
|
|
return $platform->quoteIdentifier($joinColumn['name']); |
|
143
|
|
|
} |
|
144
|
|
|
$reservedKeyList = $platform->getReservedKeywordsList(); |
|
145
|
|
|
if($reservedKeyList->isKeyword($joinColumn['name'])){ |
|
146
|
|
|
return $platform->quoteIdentifier($joinColumn['name']); |
|
147
|
|
|
} |
|
148
|
|
|
return $joinColumn['name']; |
|
149
|
|
|
}, |
|
150
|
|
|
$joinColumns |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
$quotedColumnNames = array_merge($quotedColumnNames, $assocQuotedColumnNames); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $quotedColumnNames; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* {@inheritdoc} |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ClassMetadata $class = null) |
|
163
|
|
|
{ |
|
164
|
|
|
// 1 ) Concatenate column name and counter |
|
165
|
|
|
// 2 ) Trim the column alias to the maximum identifier length of the platform. |
|
166
|
|
|
// If the alias is to long, characters are cut off from the beginning. |
|
167
|
|
|
// 3 ) Strip non alphanumeric characters |
|
168
|
|
|
// 4 ) Prefix with "_" if the result its numeric |
|
169
|
|
|
$columnName = $columnName . '_' . $counter; |
|
170
|
|
|
$columnName = substr($columnName, -$platform->getMaxIdentifierLength()); |
|
171
|
|
|
$columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName); |
|
172
|
|
|
$columnName = is_numeric($columnName) ? '_' . $columnName : $columnName; |
|
173
|
|
|
|
|
174
|
|
|
return $platform->getSQLResultCasing($columnName); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|