Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
Yeray Santana Hualde
syncEnrollments
Commits
1d93cab0
Commit
1d93cab0
authored
Mar 02, 2012
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleaning up foreign key support. Added drop_foreign command.
Signed-off-by:
Taylor Otwell
<
taylorotwell@gmail.com
>
parent
2f2437a0
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
101 additions
and
111 deletions
+101
-111
grammar.php
laravel/database/schema/grammars/grammar.php
+52
-10
mysql.php
laravel/database/schema/grammars/mysql.php
+12
-33
postgres.php
laravel/database/schema/grammars/postgres.php
+13
-34
sqlserver.php
laravel/database/schema/grammars/sqlserver.php
+12
-33
table.php
laravel/database/schema/table.php
+12
-1
No files found.
laravel/database/schema/grammars/grammar.php
View file @
1d93cab0
...
...
@@ -6,14 +6,60 @@ use Laravel\Database\Schema\Table;
abstract
class
Grammar
extends
\Laravel\Database\Grammar
{
/**
* Ge
t the appropriate data type definition for the column
.
* Ge
nerate the SQL statement for creating a foreign key
.
*
* @param Table $table
* @param Command $command
* @return string
*/
public
function
foreign
(
Table
$table
,
Fluent
$command
)
{
$name
=
$command
->
name
;
// We need to wrap both of the table names in quoted identifiers to protect
// against any possible keyword collisions, both the table on which the
// command is being executed and the referenced table are wrapped.
$table
=
$this
->
wrap
(
$table
);
$on
=
$this
->
wrap
(
$command
->
on
);
// Next we need to columnize both the command table's columns as well as
// the columns referenced by the foreign key. We'll cast the referenced
// columns to an array since they aren't by the fluent command.
$foreign
=
$this
->
columnize
(
$command
->
columns
);
$referenced
=
$this
->
columnize
((
array
)
$command
->
references
);
$sql
=
"ALTER TABLE
$table
ADD CONSTRAINT
$name
"
;
return
$sql
.=
"FOREIGN KEY (
$foreign
) REFERENCES
$on
(
$referenced
)"
;
}
/**
* Drop a constraint from the table.
*
* @param Table $table
* @param Fluent $fluent
* @return string
*/
protected
function
drop_constraint
(
Table
$table
,
Fluent
$command
)
{
return
"ALTER TABLE "
.
$this
->
wrap
(
$table
)
.
" DROP CONSTRAINT "
.
$command
->
name
;
}
/**
* Get the SQL syntax for indicating if a column is unsigned.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected
function
type
(
Fluent
$column
)
protected
function
unsigned
(
Table
$table
,
Fluent
$column
)
{
return
$this
->
{
'type_'
.
$column
->
type
}(
$column
);
if
(
$column
->
type
==
'integer'
&&
$column
->
unsigned
)
{
return
' UNSIGNED'
;
}
}
/**
...
...
@@ -40,18 +86,14 @@ abstract class Grammar extends \Laravel\Database\Grammar {
}
/**
* Get the
SQL syntax for indicating if a column is unsigned
.
* Get the
appropriate data type definition for the column
.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected
function
unsigned
(
Table
$table
,
Fluent
$column
)
protected
function
type
(
Fluent
$column
)
{
if
(
$column
->
type
==
'integer'
&&
$column
->
unsigned
)
{
return
' UNSIGNED'
;
}
return
$this
->
{
'type_'
.
$column
->
type
}(
$column
);
}
}
\ No newline at end of file
laravel/database/schema/grammars/mysql.php
View file @
1d93cab0
...
...
@@ -197,39 +197,6 @@ class MySQL extends Grammar {
return
'ALTER TABLE '
.
$this
->
wrap
(
$table
)
.
" ADD
{
$type
}
{
$name
}
(
{
$keys
}
)"
;
}
/**
* Generate the SQL statement for creating a foreign key.
*
* @param Table $table
* @param Command $command
* @return string
*/
public
function
foreign
(
Table
$table
,
Fluent
$command
)
{
$name
=
$command
->
name
;
// We need to wrap both of the table names in quoted identifiers to protect
// against any possible keyword collisions, both the table on which the
// command is being executed and the referenced table are wrapped.
$table
=
$this
->
wrap
(
$table
);
$on
=
$this
->
wrap
(
$command
->
on
);
// Next we need to columnize both the command table's columns as well as
// the columns referenced by the foreign key. We'll cast the referenced
// columns to an array since they aren't by the fluent command.
$foreign
=
$this
->
columnize
(
$command
->
columns
);
$referenced
=
$this
->
columnize
((
array
)
$command
->
references
);
// Finally we can built the SQL. This should be the same for all database
// platforms we support, but we'll just keep it in the grammars since
// adding foreign keys using ALTER isn't supported by SQLite.
$sql
=
"ALTER TABLE
$table
ADD CONSTRAINT
$name
"
;
die
(
$sql
.=
"FOREIGN KEY (
$foreign
) REFERENCES
$on
(
$referenced
)"
);
}
/**
* Generate the SQL statement for a drop table command.
*
...
...
@@ -325,6 +292,18 @@ class MySQL extends Grammar {
return
'ALTER TABLE '
.
$this
->
wrap
(
$table
)
.
" DROP INDEX
{
$command
->
name
}
"
;
}
/**
* Drop a foreign key constraint from the table.
*
* @param Table $table
* @param Fluent $fluent
* @return string
*/
public
function
drop_foreign
(
Table
$table
,
Fluent
$command
)
{
return
$this
->
drop_constraint
(
$table
,
$command
);
}
/**
* Generate the data-type definition for a string.
*
...
...
laravel/database/schema/grammars/postgres.php
View file @
1d93cab0
...
...
@@ -198,39 +198,6 @@ class Postgres extends Grammar {
return
$create
.
" INDEX
{
$command
->
name
}
ON "
.
$this
->
wrap
(
$table
)
.
" (
{
$columns
}
)"
;
}
/**
* Generate the SQL statement for creating a foreign key.
*
* @param Table $table
* @param Command $command
* @return string
*/
public
function
foreign
(
Table
$table
,
Fluent
$command
)
{
$name
=
$command
->
name
;
// We need to wrap both of the table names in quoted identifiers to protect
// against any possible keyword collisions, both the table on which the
// command is being executed and the referenced table are wrapped.
$table
=
$this
->
wrap
(
$table
);
$on
=
$this
->
wrap
(
$command
->
on
);
// Next we need to columnize both the command table's columns as well as
// the columns referenced by the foreign key. We'll cast the referenced
// columns to an array since they aren't by the fluent command.
$foreign
=
$this
->
columnize
(
$command
->
columns
);
$referenced
=
$this
->
columnize
((
array
)
$command
->
references
);
// Finally we can built the SQL. This should be the same for all database
// platforms we support, but we'll just keep it in the grammars since
// adding foreign keys using ALTER isn't supported by SQLite.
$sql
=
"ALTER TABLE
$table
ADD CONSTRAINT
$name
"
;
die
(
$sql
.=
"FOREIGN KEY (
$foreign
) REFERENCES
$on
(
$referenced
)"
);
}
/**
* Generate the SQL statement for a drop table command.
*
...
...
@@ -287,7 +254,7 @@ class Postgres extends Grammar {
*/
public
function
drop_unique
(
Table
$table
,
Fluent
$command
)
{
return
"ALTER TABLE "
.
$this
->
wrap
(
$table
)
.
" DROP CONSTRAINT "
.
$command
->
name
;
return
$this
->
drop_constraint
(
$table
,
$command
)
;
}
/**
...
...
@@ -326,6 +293,18 @@ class Postgres extends Grammar {
return
'DROP INDEX '
.
$command
->
name
;
}
/**
* Drop a foreign key constraint from the table.
*
* @param Table $table
* @param Fluent $fluent
* @return string
*/
public
function
drop_foreign
(
Table
$table
,
Fluent
$command
)
{
return
$this
->
drop_constraint
(
$table
,
$command
);
}
/**
* Generate the data-type definition for a string.
*
...
...
laravel/database/schema/grammars/sqlserver.php
View file @
1d93cab0
...
...
@@ -212,39 +212,6 @@ class SQLServer extends Grammar {
return
$create
.
" INDEX
{
$command
->
name
}
ON "
.
$this
->
wrap
(
$table
)
.
" (
{
$columns
}
)"
;
}
/**
* Generate the SQL statement for creating a foreign key.
*
* @param Table $table
* @param Command $command
* @return string
*/
public
function
foreign
(
Table
$table
,
Fluent
$command
)
{
$name
=
$command
->
name
;
// We need to wrap both of the table names in quoted identifiers to protect
// against any possible keyword collisions, both the table on which the
// command is being executed and the referenced table are wrapped.
$table
=
$this
->
wrap
(
$table
);
$on
=
$this
->
wrap
(
$command
->
on
);
// Next we need to columnize both the command table's columns as well as
// the columns referenced by the foreign key. We'll cast the referenced
// columns to an array since they aren't by the fluent command.
$foreign
=
$this
->
columnize
(
$command
->
columns
);
$referenced
=
$this
->
columnize
((
array
)
$command
->
references
);
// Finally we can built the SQL. This should be the same for all database
// platforms we support, but we'll just keep it in the grammars since
// adding foreign keys using ALTER isn't supported by SQLite.
$sql
=
"ALTER TABLE
$table
ADD CONSTRAINT
$name
"
;
die
(
$sql
.=
"FOREIGN KEY (
$foreign
) REFERENCES
$on
(
$referenced
)"
);
}
/**
* Generate the SQL statement for a drop table command.
*
...
...
@@ -344,6 +311,18 @@ class SQLServer extends Grammar {
return
"DROP INDEX
{
$command
->
name
}
ON "
.
$this
->
wrap
(
$table
);
}
/**
* Drop a foreign key constraint from the table.
*
* @param Table $table
* @param Fluent $fluent
* @return string
*/
public
function
drop_foreign
(
Table
$table
,
Fluent
$command
)
{
return
$this
->
drop_constraint
(
$table
,
$command
);
}
/**
* Generate the data-type definition for a string.
*
...
...
laravel/database/schema/table.php
View file @
1d93cab0
...
...
@@ -207,6 +207,17 @@ class Table {
return
$this
->
drop_key
(
__FUNCTION__
,
$name
);
}
/**
* Drop a foreign key constraint from the table.
*
* @param string $name
* @return void
*/
public
function
drop_foreign
(
$name
)
{
return
$this
->
drop_key
(
__FUNCTION__
,
$name
);
}
/**
* Create a command to drop any type of index.
*
...
...
@@ -216,7 +227,7 @@ class Table {
*/
protected
function
drop_key
(
$type
,
$name
)
{
return
$this
->
command
(
$type
,
array
(
'name'
=>
$name
));
return
$this
->
command
(
$type
,
compact
(
'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