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
fe067f20
Unverified
Commit
fe067f20
authored
Sep 22, 2016
by
Guilherme Blanco
Committed by
Sergei Morozov
Nov 02, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added flag support to unique constraint
parent
36522aa4
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
102 additions
and
62 deletions
+102
-62
AbstractPlatform.php
lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
+12
-6
DrizzlePlatform.php
lib/Doctrine/DBAL/Platforms/DrizzlePlatform.php
+2
-2
MySqlPlatform.php
lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
+2
-2
OraclePlatform.php
lib/Doctrine/DBAL/Platforms/OraclePlatform.php
+4
-4
SQLAnywherePlatform.php
lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php
+0
-21
SQLServerPlatform.php
lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
+0
-12
SqlitePlatform.php
lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
+9
-7
Table.php
lib/Doctrine/DBAL/Schema/Table.php
+9
-7
UniqueConstraint.php
lib/Doctrine/DBAL/Schema/UniqueConstraint.php
+64
-1
No files found.
lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
View file @
fe067f20
...
...
@@ -2372,24 +2372,30 @@ abstract class AbstractPlatform
* constraint declaration to be used in statements like CREATE TABLE.
*
* @param string $name The name of the unique constraint.
* @param
Index $index The index
definition.
* @param
UniqueConstraint $constraint The unique constraint
definition.
*
* @return string DBMS specific SQL code portion needed to set a constraint.
*
* @throws InvalidArgumentException
*/
public
function
getUniqueConstraintDeclarationSQL
(
$name
,
Index
$index
)
public
function
getUniqueConstraintDeclarationSQL
(
$name
,
UniqueConstraint
$constraint
)
{
$columns
=
$
index
->
getColumns
();
$columns
=
$
constraint
->
getColumns
();
$name
=
new
Identifier
(
$name
);
if
(
count
(
$columns
)
===
0
)
{
throw
new
InvalidArgumentException
(
"Incomplete definition. 'columns' required."
);
}
return
'CONSTRAINT '
.
$name
->
getQuotedName
(
$this
)
.
' UNIQUE ('
$flags
=
''
;
if
(
$constraint
->
hasFlag
(
'clustered'
))
{
$flags
=
'CLUSTERED '
;
}
return
'CONSTRAINT '
.
$name
->
getQuotedName
(
$this
)
.
' UNIQUE '
.
$flags
.
' ('
.
$this
->
getIndexFieldDeclarationListSQL
(
$index
)
.
')'
.
$this
->
getPartialIndexSQL
(
$index
)
;
.
')'
;
}
/**
...
...
lib/Doctrine/DBAL/Platforms/DrizzlePlatform.php
View file @
fe067f20
...
...
@@ -191,8 +191,8 @@ class DrizzlePlatform extends AbstractPlatform
$queryFields
=
$this
->
getColumnDeclarationListSQL
(
$columns
);
if
(
isset
(
$options
[
'uniqueConstraints'
])
&&
!
empty
(
$options
[
'uniqueConstraints'
]))
{
foreach
(
$options
[
'uniqueConstraints'
]
as
$
index
=>
$definition
)
{
$queryFields
.=
', '
.
$this
->
getUniqueConstraintDeclarationSQL
(
$
index
,
$definition
);
foreach
(
$options
[
'uniqueConstraints'
]
as
$
name
=>
$definition
)
{
$queryFields
.=
', '
.
$this
->
getUniqueConstraintDeclarationSQL
(
$
name
,
$definition
);
}
}
...
...
lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
View file @
fe067f20
...
...
@@ -418,8 +418,8 @@ SQL
$queryFields
=
$this
->
getColumnDeclarationListSQL
(
$columns
);
if
(
isset
(
$options
[
'uniqueConstraints'
])
&&
!
empty
(
$options
[
'uniqueConstraints'
]))
{
foreach
(
$options
[
'uniqueConstraints'
]
as
$
index
=>
$definition
)
{
$queryFields
.=
', '
.
$this
->
getUniqueConstraintDeclarationSQL
(
$
index
,
$definition
);
foreach
(
$options
[
'uniqueConstraints'
]
as
$
name
=>
$definition
)
{
$queryFields
.=
', '
.
$this
->
getUniqueConstraintDeclarationSQL
(
$
name
,
$definition
);
}
}
...
...
lib/Doctrine/DBAL/Platforms/OraclePlatform.php
View file @
fe067f20
...
...
@@ -376,11 +376,11 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
protected
function
_getCreateTableSQL
(
$table
,
array
$columns
,
array
$options
=
[])
protected
function
_getCreateTableSQL
(
$table
Name
,
array
$columns
,
array
$options
=
[])
{
$indexes
=
$options
[
'indexes'
]
??
[];
$options
[
'indexes'
]
=
[];
$sql
=
parent
::
_getCreateTableSQL
(
$table
,
$columns
,
$options
);
$sql
=
parent
::
_getCreateTableSQL
(
$table
Name
,
$columns
,
$options
);
foreach
(
$columns
as
$name
=>
$column
)
{
if
(
isset
(
$column
[
'sequence'
]))
{
...
...
@@ -392,12 +392,12 @@ class OraclePlatform extends AbstractPlatform
continue
;
}
$sql
=
array_merge
(
$sql
,
$this
->
getCreateAutoincrementSql
(
$name
,
$table
));
$sql
=
array_merge
(
$sql
,
$this
->
getCreateAutoincrementSql
(
$name
,
$table
Name
));
}
if
(
isset
(
$indexes
)
&&
!
empty
(
$indexes
))
{
foreach
(
$indexes
as
$index
)
{
$sql
[]
=
$this
->
getCreateIndexSQL
(
$index
,
$table
);
$sql
[]
=
$this
->
getCreateIndexSQL
(
$index
,
$table
Name
);
}
}
...
...
lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php
View file @
fe067f20
...
...
@@ -1160,27 +1160,6 @@ SQL
return
'TRUNCATE TABLE '
.
$tableIdentifier
->
getQuotedName
(
$this
);
}
/**
* {@inheritdoc}
*/
public
function
getUniqueConstraintDeclarationSQL
(
$name
,
Index
$index
)
{
if
(
$index
->
isPrimary
())
{
throw
new
InvalidArgumentException
(
'Cannot create primary key constraint declarations with getUniqueConstraintDeclarationSQL().'
);
}
if
(
!
$index
->
isUnique
())
{
throw
new
InvalidArgumentException
(
'Can only create unique constraint declarations, no common index declarations with '
.
'getUniqueConstraintDeclarationSQL().'
);
}
return
$this
->
getTableConstraintDeclarationSQL
(
$index
,
$name
);
}
/**
* {@inheritdoc}
*/
...
...
lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
View file @
fe067f20
...
...
@@ -397,18 +397,6 @@ SQL
' FOR '
.
$columnName
->
getQuotedName
(
$this
);
}
/**
* {@inheritDoc}
*/
public
function
getUniqueConstraintDeclarationSQL
(
$name
,
Index
$index
)
{
$constraint
=
parent
::
getUniqueConstraintDeclarationSQL
(
$name
,
$index
);
$constraint
=
$this
->
_appendUniqueConstraintDefinition
(
$constraint
,
$index
);
return
$constraint
;
}
/**
* {@inheritDoc}
*/
...
...
lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
View file @
fe067f20
...
...
@@ -314,9 +314,9 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
protected
function
_getCreateTableSQL
(
$
n
ame
,
array
$columns
,
array
$options
=
[])
protected
function
_getCreateTableSQL
(
$
tableN
ame
,
array
$columns
,
array
$options
=
[])
{
$
name
=
str_replace
(
'.'
,
'__'
,
$n
ame
);
$
tableName
=
str_replace
(
'.'
,
'__'
,
$tableN
ame
);
$queryFields
=
$this
->
getColumnDeclarationListSQL
(
$columns
);
if
(
isset
(
$options
[
'uniqueConstraints'
])
&&
!
empty
(
$options
[
'uniqueConstraints'
]))
{
...
...
@@ -340,7 +340,7 @@ class SqlitePlatform extends AbstractPlatform
$tableComment
=
$this
->
getInlineTableCommentSQL
(
$comment
);
}
$query
=
[
'CREATE TABLE '
.
$
n
ame
.
' '
.
$tableComment
.
'('
.
$queryFields
.
')'
];
$query
=
[
'CREATE TABLE '
.
$
tableN
ame
.
' '
.
$tableComment
.
'('
.
$queryFields
.
')'
];
if
(
isset
(
$options
[
'alter'
])
&&
$options
[
'alter'
]
===
true
)
{
return
$query
;
...
...
@@ -348,13 +348,13 @@ class SqlitePlatform extends AbstractPlatform
if
(
isset
(
$options
[
'indexes'
])
&&
!
empty
(
$options
[
'indexes'
]))
{
foreach
(
$options
[
'indexes'
]
as
$indexDef
)
{
$query
[]
=
$this
->
getCreateIndexSQL
(
$indexDef
,
$
n
ame
);
$query
[]
=
$this
->
getCreateIndexSQL
(
$indexDef
,
$
tableN
ame
);
}
}
if
(
isset
(
$options
[
'unique'
])
&&
!
empty
(
$options
[
'unique'
]))
{
foreach
(
$options
[
'unique'
]
as
$indexDef
)
{
$query
[]
=
$this
->
getCreateIndexSQL
(
$indexDef
,
$
n
ame
);
$query
[]
=
$this
->
getCreateIndexSQL
(
$indexDef
,
$
tableN
ame
);
}
}
...
...
@@ -389,8 +389,10 @@ class SqlitePlatform extends AbstractPlatform
*/
protected
function
getVarcharTypeDeclarationSQLSnippet
(
$length
,
$fixed
)
{
return
$fixed
?
(
$length
?
'CHAR('
.
$length
.
')'
:
'CHAR(255)'
)
:
(
$length
?
'VARCHAR('
.
$length
.
')'
:
'TEXT'
);
return
$fixed
?
(
$length
?
'CHAR('
.
$length
.
')'
:
'CHAR(255)'
)
:
(
$length
?
'VARCHAR('
.
$length
.
')'
:
'TEXT'
)
;
}
/**
...
...
lib/Doctrine/DBAL/Schema/Table.php
View file @
fe067f20
...
...
@@ -123,11 +123,12 @@ class Table extends AbstractAsset
/**
* @param mixed[] $columnNames
* @param string|null $indexName
* @param array $flags
* @param mixed[] $options
*
* @return self
*/
public
function
addUniqueConstraint
(
array
$columnNames
,
$indexName
=
null
,
array
$options
=
[])
public
function
addUniqueConstraint
(
array
$columnNames
,
$indexName
=
null
,
array
$
flags
=
array
(),
array
$
options
=
[])
{
if
(
$indexName
===
null
)
{
$indexName
=
$this
->
_generateIdentifierName
(
...
...
@@ -137,7 +138,7 @@ class Table extends AbstractAsset
);
}
return
$this
->
_addUniqueConstraint
(
$this
->
_createUniqueConstraint
(
$columnNames
,
$indexName
,
$options
));
return
$this
->
_addUniqueConstraint
(
$this
->
_createUniqueConstraint
(
$columnNames
,
$indexName
,
$
flags
,
$
options
));
}
/**
...
...
@@ -866,7 +867,7 @@ class Table extends AbstractAsset
/**
* @return self
*/
protected
function
_addUniqueConstraint
(
UniqueConstraint
$
uniqueC
onstraint
)
protected
function
_addUniqueConstraint
(
UniqueConstraint
$
c
onstraint
)
{
$name
=
strlen
(
$uniqueConstraint
->
getName
())
?
$uniqueConstraint
->
getName
()
...
...
@@ -878,7 +879,7 @@ class Table extends AbstractAsset
$name
=
$this
->
normalizeIdentifier
(
$name
);
$this
->
_uniqueConstraints
[
$name
]
=
$
uniqueC
onstraint
;
$this
->
_uniqueConstraints
[
$name
]
=
$
c
onstraint
;
// If there is already an index that fulfills this requirements drop the request. In the case of __construct
// calling this method during hydration from schema-details all the explicitly added indexes lead to duplicates.
...
...
@@ -979,13 +980,14 @@ class Table extends AbstractAsset
/**
* @param mixed[] $columns
* @param string $indexName
* @param mixed[] $options
* @param mixed[] $flags
* @param array $options
*
* @return UniqueConstraint
*
* @throws SchemaException
*/
private
function
_createUniqueConstraint
(
array
$columns
,
$indexName
,
array
$options
=
[])
private
function
_createUniqueConstraint
(
array
$columns
,
$indexName
,
array
$
flags
=
array
(),
array
$
options
=
[])
{
if
(
preg_match
(
'(([^a-zA-Z0-9_]+))'
,
$this
->
normalizeIdentifier
(
$indexName
)))
{
throw
SchemaException
::
indexNameInvalid
(
$indexName
);
...
...
@@ -1003,7 +1005,7 @@ class Table extends AbstractAsset
}
}
return
new
UniqueConstraint
(
$indexName
,
$columns
,
$options
);
return
new
UniqueConstraint
(
$indexName
,
$columns
,
$
flags
,
$
options
);
}
/**
...
...
lib/Doctrine/DBAL/Schema/UniqueConstraint.php
View file @
fe067f20
...
...
@@ -22,6 +22,14 @@ class UniqueConstraint extends AbstractAsset implements Constraint
*/
protected
$columns
=
[];
/**
* Platform specific flags
* array($flagName => true)
*
* @var array
*/
protected
$flags
=
array
();
/**
* Platform specific options
*
...
...
@@ -32,9 +40,10 @@ class UniqueConstraint extends AbstractAsset implements Constraint
/**
* @param string $indexName
* @param string[] $columns
* @param array $flags
* @param mixed[] $options
*/
public
function
__construct
(
$indexName
,
array
$columns
,
array
$options
=
[])
public
function
__construct
(
$indexName
,
array
$columns
,
array
$
flags
=
array
(),
array
$
options
=
[])
{
$this
->
_setName
(
$indexName
);
...
...
@@ -43,6 +52,10 @@ class UniqueConstraint extends AbstractAsset implements Constraint
foreach
(
$columns
as
$column
)
{
$this
->
_addColumn
(
$column
);
}
foreach
(
$flags
as
$flag
)
{
$this
->
addFlag
(
$flag
);
}
}
/**
...
...
@@ -91,6 +104,56 @@ class UniqueConstraint extends AbstractAsset implements Constraint
return
array_map
([
$this
,
'trimQuotes'
],
$this
->
getColumns
());
}
/**
* Returns platform specific flags for unique constraint.
*
* @return string[]
*/
public
function
getFlags
()
{
return
array_keys
(
$this
->
flags
);
}
/**
* Adds flag for a unique constraint that translates to platform specific handling.
*
* @example $uniqueConstraint->addFlag('CLUSTERED')
*
* @param string $flag
*
* @return self
*/
public
function
addFlag
(
$flag
)
{
$this
->
flags
[
strtolower
(
$flag
)]
=
true
;
return
$this
;
}
/**
* Does this unique constraint have a specific flag?
*
* @param string $flag
*
* @return boolean
*/
public
function
hasFlag
(
$flag
)
{
return
isset
(
$this
->
flags
[
strtolower
(
$flag
)]);
}
/**
* Removes a flag.
*
* @param string $flag
*
* @return void
*/
public
function
removeFlag
(
$flag
)
{
unset
(
$this
->
flags
[
strtolower
(
$flag
)]);
}
/**
* @param string $name
*
...
...
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