1: <?php namespace Laravel\Database\Schema\Grammars;
2:
3: use Laravel\Fluent;
4: use Laravel\Database\Schema\Table;
5:
6: abstract class Grammar extends \Laravel\Database\Grammar {
7:
8: /**
9: * Generate the SQL statement for creating a foreign key.
10: *
11: * @param Table $table
12: * @param Fluent $command
13: * @return string
14: */
15: public function foreign(Table $table, Fluent $command)
16: {
17: $name = $command->name;
18:
19: // We need to wrap both of the table names in quoted identifiers to protect
20: // against any possible keyword collisions, both the table on which the
21: // command is being executed and the referenced table are wrapped.
22: $table = $this->wrap($table);
23:
24: $on = $this->wrap_table($command->on);
25:
26: // Next we need to columnize both the command table's columns as well as
27: // the columns referenced by the foreign key. We'll cast the referenced
28: // columns to an array since they aren't by the fluent command.
29: $foreign = $this->columnize($command->columns);
30:
31: $referenced = $this->columnize((array) $command->references);
32:
33: $sql = "ALTER TABLE $table ADD CONSTRAINT $name ";
34:
35: $sql .= "FOREIGN KEY ($foreign) REFERENCES $on ($referenced)";
36:
37: // Finally we will check for any "on delete" or "on update" options for
38: // the foreign key. These control the behavior of the constraint when
39: // an update or delete statement is run against the record.
40: if ( ! is_null($command->on_delete))
41: {
42: $sql .= " ON DELETE {$command->on_delete}";
43: }
44:
45: if ( ! is_null($command->on_update))
46: {
47: $sql .= " ON UPDATE {$command->on_update}";
48: }
49:
50: return $sql;
51: }
52:
53: /**
54: * Generate the SQL statement for a drop table command.
55: *
56: * @param Table $table
57: * @param Fluent $command
58: * @return string
59: */
60: public function drop(Table $table, Fluent $command)
61: {
62: return 'DROP TABLE '.$this->wrap($table);
63: }
64:
65: /**
66: * Drop a constraint from the table.
67: *
68: * @param Table $table
69: * @param Fluent $command
70: * @return string
71: */
72: protected function drop_constraint(Table $table, Fluent $command)
73: {
74: return "ALTER TABLE ".$this->wrap($table)." DROP CONSTRAINT ".$command->name;
75: }
76:
77: /**
78: * Wrap a value in keyword identifiers.
79: *
80: * @param Table|string $value
81: * @return string
82: */
83: public function wrap($value)
84: {
85: // This method is primarily for convenience so we can just pass a
86: // column or table instance into the wrap method without sending
87: // in the name each time we need to wrap one of these objects.
88: if ($value instanceof Table)
89: {
90: return $this->wrap_table($value->name);
91: }
92: elseif ($value instanceof Fluent)
93: {
94: $value = $value->name;
95: }
96:
97: return parent::wrap($value);
98: }
99:
100: /**
101: * Get the appropriate data type definition for the column.
102: *
103: * @param Fluent $column
104: * @return string
105: */
106: protected function type(Fluent $column)
107: {
108: return $this->{'type_'.$column->type}($column);
109: }
110:
111: /**
112: * Format a value so that it can be used in SQL DEFAULT clauses.
113: * @param mixed $value
114: * @return string
115: */
116: protected function default_value($value)
117: {
118: if (is_bool($value))
119: {
120: return intval($value);
121: }
122:
123: return strval($value);
124: }
125:
126: }