|
1 <?php |
|
2 /* |
|
3 * $Id$ |
|
4 * |
|
5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
16 * |
|
17 * This software consists of voluntary contributions made by many individuals |
|
18 * and is licensed under the LGPL. For more information, see |
|
19 * <http://www.doctrine-project.org>. |
|
20 */ |
|
21 |
|
22 namespace Doctrine\DBAL\Schema; |
|
23 |
|
24 use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
25 |
|
26 /** |
|
27 * The abstract asset allows to reset the name of all assets without publishing this to the public userland. |
|
28 * |
|
29 * This encapsulation hack is necessary to keep a consistent state of the database schema. Say we have a list of tables |
|
30 * array($tableName => Table($tableName)); if you want to rename the table, you have to make sure |
|
31 * |
|
32 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
|
33 * @link www.doctrine-project.org |
|
34 * @since 2.0 |
|
35 * @version $Revision$ |
|
36 * @author Benjamin Eberlei <kontakt@beberlei.de> |
|
37 */ |
|
38 abstract class AbstractAsset |
|
39 { |
|
40 /** |
|
41 * @var string |
|
42 */ |
|
43 protected $_name; |
|
44 |
|
45 protected $_quoted = false; |
|
46 |
|
47 /** |
|
48 * Set name of this asset |
|
49 * |
|
50 * @param string $name |
|
51 */ |
|
52 protected function _setName($name) |
|
53 { |
|
54 if ($this->isQuoted($name)) { |
|
55 $this->_quoted = true; |
|
56 $name = $this->trimQuotes($name); |
|
57 } |
|
58 $this->_name = $name; |
|
59 } |
|
60 |
|
61 /** |
|
62 * Check if this identifier is quoted. |
|
63 * |
|
64 * @param string $identifier |
|
65 * @return bool |
|
66 */ |
|
67 protected function isQuoted($identifier) |
|
68 { |
|
69 return (isset($identifier[0]) && ($identifier[0] == '`' || $identifier[0] == '"')); |
|
70 } |
|
71 |
|
72 /** |
|
73 * Trim quotes from the identifier. |
|
74 * |
|
75 * @param string $identifier |
|
76 * @return string |
|
77 */ |
|
78 protected function trimQuotes($identifier) |
|
79 { |
|
80 return trim($identifier, '`"'); |
|
81 } |
|
82 |
|
83 /** |
|
84 * Return name of this schema asset. |
|
85 * |
|
86 * @return string |
|
87 */ |
|
88 public function getName() |
|
89 { |
|
90 return $this->_name; |
|
91 } |
|
92 |
|
93 /** |
|
94 * Get the quoted representation of this asset but only if it was defined with one. Otherwise |
|
95 * return the plain unquoted value as inserted. |
|
96 * |
|
97 * @param AbstractPlatform $platform |
|
98 * @return string |
|
99 */ |
|
100 public function getQuotedName(AbstractPlatform $platform) |
|
101 { |
|
102 return ($this->_quoted) ? $platform->quoteIdentifier($this->_name) : $this->_name; |
|
103 } |
|
104 |
|
105 /** |
|
106 * Generate an identifier from a list of column names obeying a certain string length. |
|
107 * |
|
108 * This is especially important for Oracle, since it does not allow identifiers larger than 30 chars, |
|
109 * however building idents automatically for foreign keys, composite keys or such can easily create |
|
110 * very long names. |
|
111 * |
|
112 * @param array $columnNames |
|
113 * @param string $prefix |
|
114 * @param int $maxSize |
|
115 * @return string |
|
116 */ |
|
117 protected function _generateIdentifierName($columnNames, $prefix='', $maxSize=30) |
|
118 { |
|
119 /*$columnCount = count($columnNames); |
|
120 $postfixLen = strlen($postfix); |
|
121 $parts = array_map(function($columnName) use($columnCount, $postfixLen, $maxSize) { |
|
122 return substr($columnName, -floor(($maxSize-$postfixLen)/$columnCount - 1)); |
|
123 }, $columnNames); |
|
124 $parts[] = $postfix; |
|
125 |
|
126 $identifier = trim(implode("_", $parts), '_'); |
|
127 // using implicit schema support of DB2 and Postgres there might be dots in the auto-generated |
|
128 // identifier names which can easily be replaced by underscores. |
|
129 $identifier = str_replace(".", "_", $identifier); |
|
130 |
|
131 if (is_numeric(substr($identifier, 0, 1))) { |
|
132 $identifier = "i" . substr($identifier, 0, strlen($identifier)-1); |
|
133 } |
|
134 |
|
135 return $identifier;*/ |
|
136 |
|
137 |
|
138 $hash = implode("", array_map(function($column) { |
|
139 return dechex(crc32($column)); |
|
140 }, $columnNames)); |
|
141 return substr(strtoupper($prefix . "_" . $hash), 0, $maxSize); |
|
142 } |
|
143 } |