Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
e9395946
Commit
e9395946
authored
Sep 11, 2014
by
Steve Müller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
normalize asset names consistently
parent
d4005861
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
15 deletions
+101
-15
Table.php
lib/Doctrine/DBAL/Schema/Table.php
+29
-15
TableTest.php
tests/Doctrine/Tests/DBAL/Schema/TableTest.php
+72
-0
No files found.
lib/Doctrine/DBAL/Schema/Table.php
View file @
e9395946
...
...
@@ -183,7 +183,7 @@ class Table extends AbstractAsset
*/
public
function
dropIndex
(
$indexName
)
{
$indexName
=
strtolow
er
(
$indexName
);
$indexName
=
$this
->
normalizeIdentifi
er
(
$indexName
);
if
(
!
$this
->
hasIndex
(
$indexName
))
{
throw
SchemaException
::
indexDoesNotExist
(
$indexName
,
$this
->
_name
);
}
...
...
@@ -222,8 +222,8 @@ class Table extends AbstractAsset
*/
public
function
renameIndex
(
$oldIndexName
,
$newIndexName
=
null
)
{
$oldIndexName
=
strtolow
er
(
$oldIndexName
);
$normalizedNewIndexName
=
strtolow
er
(
$newIndexName
);
$oldIndexName
=
$this
->
normalizeIdentifi
er
(
$oldIndexName
);
$normalizedNewIndexName
=
$this
->
normalizeIdentifi
er
(
$newIndexName
);
if
(
$oldIndexName
===
$normalizedNewIndexName
)
{
return
$this
;
...
...
@@ -287,7 +287,7 @@ class Table extends AbstractAsset
*/
private
function
_createIndex
(
array
$columnNames
,
$indexName
,
$isUnique
,
$isPrimary
,
array
$flags
=
array
(),
array
$options
=
array
())
{
if
(
preg_match
(
'(([^a-zA-Z0-9_]+))'
,
$
indexName
))
{
if
(
preg_match
(
'(([^a-zA-Z0-9_]+))'
,
$
this
->
normalizeIdentifier
(
$indexName
)
))
{
throw
SchemaException
::
indexNameInvalid
(
$indexName
);
}
...
...
@@ -362,7 +362,7 @@ class Table extends AbstractAsset
*/
public
function
dropColumn
(
$columnName
)
{
$columnName
=
strtolow
er
(
$columnName
);
$columnName
=
$this
->
normalizeIdentifi
er
(
$columnName
);
unset
(
$this
->
_columns
[
$columnName
]);
return
$this
;
...
...
@@ -469,7 +469,7 @@ class Table extends AbstractAsset
protected
function
_addColumn
(
Column
$column
)
{
$columnName
=
$column
->
getName
();
$columnName
=
strtolow
er
(
$columnName
);
$columnName
=
$this
->
normalizeIdentifi
er
(
$columnName
);
if
(
isset
(
$this
->
_columns
[
$columnName
]))
{
throw
SchemaException
::
columnAlreadyExists
(
$this
->
getName
(),
$columnName
);
...
...
@@ -497,7 +497,7 @@ class Table extends AbstractAsset
}
$indexName
=
$indexCandidate
->
getName
();
$indexName
=
strtolow
er
(
$indexName
);
$indexName
=
$this
->
normalizeIdentifi
er
(
$indexName
);
if
(
isset
(
$this
->
_indexes
[
$indexName
])
||
(
$this
->
_primaryKeyName
!=
false
&&
$indexCandidate
->
isPrimary
()))
{
throw
SchemaException
::
indexAlreadyExists
(
$indexName
,
$this
->
_name
);
...
...
@@ -535,7 +535,7 @@ class Table extends AbstractAsset
array_merge
((
array
)
$this
->
getName
(),
$constraint
->
getLocalColumns
()),
"fk"
,
$this
->
_getMaxIdentifierLength
()
);
}
$name
=
strtolow
er
(
$name
);
$name
=
$this
->
normalizeIdentifi
er
(
$name
);
$this
->
_fkConstraints
[
$name
]
=
$constraint
;
// add an explicit index on the foreign key columns. If there is already an index that fulfils this requirements drop the request.
...
...
@@ -553,7 +553,7 @@ class Table extends AbstractAsset
*/
public
function
hasForeignKey
(
$constraintName
)
{
$constraintName
=
strtolow
er
(
$constraintName
);
$constraintName
=
$this
->
normalizeIdentifi
er
(
$constraintName
);
return
isset
(
$this
->
_fkConstraints
[
$constraintName
]);
}
...
...
@@ -569,7 +569,7 @@ class Table extends AbstractAsset
*/
public
function
getForeignKey
(
$constraintName
)
{
$constraintName
=
strtolow
er
(
$constraintName
);
$constraintName
=
$this
->
normalizeIdentifi
er
(
$constraintName
);
if
(
!
$this
->
hasForeignKey
(
$constraintName
))
{
throw
SchemaException
::
foreignKeyDoesNotExist
(
$constraintName
,
$this
->
_name
);
}
...
...
@@ -588,7 +588,7 @@ class Table extends AbstractAsset
*/
public
function
removeForeignKey
(
$constraintName
)
{
$constraintName
=
strtolow
er
(
$constraintName
);
$constraintName
=
$this
->
normalizeIdentifi
er
(
$constraintName
);
if
(
!
$this
->
hasForeignKey
(
$constraintName
))
{
throw
SchemaException
::
foreignKeyDoesNotExist
(
$constraintName
,
$this
->
_name
);
}
...
...
@@ -631,7 +631,7 @@ class Table extends AbstractAsset
*/
public
function
hasColumn
(
$columnName
)
{
$columnName
=
$this
->
trimQuotes
(
strtolower
(
$columnName
)
);
$columnName
=
$this
->
normalizeIdentifier
(
$columnName
);
return
isset
(
$this
->
_columns
[
$columnName
]);
}
...
...
@@ -647,7 +647,7 @@ class Table extends AbstractAsset
*/
public
function
getColumn
(
$columnName
)
{
$columnName
=
strtolower
(
$this
->
trimQuotes
(
$columnName
)
);
$columnName
=
$this
->
normalizeIdentifier
(
$columnName
);
if
(
!
$this
->
hasColumn
(
$columnName
))
{
throw
SchemaException
::
columnDoesNotExist
(
$columnName
,
$this
->
_name
);
}
...
...
@@ -704,7 +704,7 @@ class Table extends AbstractAsset
*/
public
function
hasIndex
(
$indexName
)
{
$indexName
=
strtolow
er
(
$indexName
);
$indexName
=
$this
->
normalizeIdentifi
er
(
$indexName
);
return
(
isset
(
$this
->
_indexes
[
$indexName
]));
}
...
...
@@ -720,7 +720,7 @@ class Table extends AbstractAsset
*/
public
function
getIndex
(
$indexName
)
{
$indexName
=
strtolow
er
(
$indexName
);
$indexName
=
$this
->
normalizeIdentifi
er
(
$indexName
);
if
(
!
$this
->
hasIndex
(
$indexName
))
{
throw
SchemaException
::
indexDoesNotExist
(
$indexName
,
$this
->
_name
);
}
...
...
@@ -814,4 +814,18 @@ class Table extends AbstractAsset
$this
->
_fkConstraints
[
$k
]
->
setLocalTable
(
$this
);
}
}
/**
* Normalizes a given identifier.
*
* Trims quotes and lowercases the given identifier.
*
* @param string $identifier The identifier to normalize.
*
* @return string The normalized identifier.
*/
private
function
normalizeIdentifier
(
$identifier
)
{
return
$this
->
trimQuotes
(
strtolower
(
$identifier
));
}
}
tests/Doctrine/Tests/DBAL/Schema/TableTest.php
View file @
e9395946
...
...
@@ -623,4 +623,76 @@ class TableTest extends \Doctrine\Tests\DbalTestCase
$table
->
renameIndex
(
'idx_id'
,
'idx_foo'
);
}
/**
* @dataProvider getNormalizesAssetNames
* @group DBAL-831
*/
public
function
testNormalizesColumnNames
(
$assetName
)
{
$table
=
new
Table
(
'test'
);
$table
->
addColumn
(
$assetName
,
'integer'
);
$table
->
addIndex
(
array
(
$assetName
),
$assetName
);
$table
->
addForeignKeyConstraint
(
'test'
,
array
(
$assetName
),
array
(
$assetName
),
array
(),
$assetName
);
$this
->
assertTrue
(
$table
->
hasColumn
(
$assetName
));
$this
->
assertTrue
(
$table
->
hasColumn
(
'foo'
));
$this
->
assertInstanceOf
(
'Doctrine\DBAL\Schema\Column'
,
$table
->
getColumn
(
$assetName
));
$this
->
assertInstanceOf
(
'Doctrine\DBAL\Schema\Column'
,
$table
->
getColumn
(
'foo'
));
$this
->
assertTrue
(
$table
->
hasIndex
(
$assetName
));
$this
->
assertTrue
(
$table
->
hasIndex
(
'foo'
));
$this
->
assertInstanceOf
(
'Doctrine\DBAL\Schema\Index'
,
$table
->
getIndex
(
$assetName
));
$this
->
assertInstanceOf
(
'Doctrine\DBAL\Schema\Index'
,
$table
->
getIndex
(
'foo'
));
$this
->
assertTrue
(
$table
->
hasForeignKey
(
$assetName
));
$this
->
assertTrue
(
$table
->
hasForeignKey
(
'foo'
));
$this
->
assertInstanceOf
(
'Doctrine\DBAL\Schema\ForeignKeyConstraint'
,
$table
->
getForeignKey
(
$assetName
));
$this
->
assertInstanceOf
(
'Doctrine\DBAL\Schema\ForeignKeyConstraint'
,
$table
->
getForeignKey
(
'foo'
));
$table
->
renameIndex
(
$assetName
,
$assetName
);
$this
->
assertTrue
(
$table
->
hasIndex
(
$assetName
));
$this
->
assertTrue
(
$table
->
hasIndex
(
'foo'
));
$table
->
renameIndex
(
$assetName
,
'foo'
);
$this
->
assertTrue
(
$table
->
hasIndex
(
$assetName
));
$this
->
assertTrue
(
$table
->
hasIndex
(
'foo'
));
$table
->
renameIndex
(
'foo'
,
$assetName
);
$this
->
assertTrue
(
$table
->
hasIndex
(
$assetName
));
$this
->
assertTrue
(
$table
->
hasIndex
(
'foo'
));
$table
->
renameIndex
(
$assetName
,
'bar'
);
$this
->
assertFalse
(
$table
->
hasIndex
(
$assetName
));
$this
->
assertFalse
(
$table
->
hasIndex
(
'foo'
));
$this
->
assertTrue
(
$table
->
hasIndex
(
'bar'
));
$table
->
renameIndex
(
'bar'
,
$assetName
);
$table
->
dropColumn
(
$assetName
);
$table
->
dropIndex
(
$assetName
);
$table
->
removeForeignKey
(
$assetName
);
$this
->
assertFalse
(
$table
->
hasColumn
(
$assetName
));
$this
->
assertFalse
(
$table
->
hasColumn
(
'foo'
));
$this
->
assertFalse
(
$table
->
hasIndex
(
$assetName
));
$this
->
assertFalse
(
$table
->
hasIndex
(
'foo'
));
$this
->
assertFalse
(
$table
->
hasForeignKey
(
$assetName
));
$this
->
assertFalse
(
$table
->
hasForeignKey
(
'foo'
));
}
public
function
getNormalizesAssetNames
()
{
return
array
(
array
(
'foo'
),
array
(
'FOO'
),
array
(
'`foo`'
),
array
(
'`FOO`'
),
array
(
'"foo"'
),
array
(
'"FOO"'
),
array
(
'"foo"'
),
array
(
'"FOO"'
),
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment