Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support foreign key #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ rename_column($table_name, $column_name, $new_column_name)
change_column($table_name, $column_name, $params)
remove_column($table_name, $column_name)

add_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name)
remove_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name)

add_index($table_name, $index_name, $columns, $index_type = 'normal')
remove_index($table_name, $index_name)
```
Expand Down
26 changes: 26 additions & 0 deletions classes/Drivers/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,32 @@ abstract public function change_column($table_name, $column_name, $params);
*/
abstract public function remove_column($table_name, $column_name);

/**
* Add a foreign key to a table
*
* @example add_foreign_key ( "client", "city_id", "city", "id");
*
* @param string Name of the table
* @param string Name of the column
* @param string Name of referenced table
* @param string Name of referenced column
* @return boll Returns true if no errors
*/
abstract public function add_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name);

/**
* Remove a foreign key from a table
*
* @example remove_foreign_key ( "client", "city_id", "city", "id");
*
* @param string Name of the table
* @param string Name of the column
* @param string Name of referenced table
* @param string Name of referenced column
* @return boll Returns true if no errors
*/
abstract public function remove_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name);

/**
* Add an index
*
Expand Down
12 changes: 12 additions & 0 deletions classes/Drivers/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ public function remove_column($table_name, $column_name)
return $this->run_query("ALTER TABLE $table_name DROP COLUMN $column_name ;");
}

public function add_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name)
{
$fk = 'fk'.$table_name.$column_name.$referenced_table_name.$referenced_column_name;
return $this->run_query("ALTER TABLE $table_name ADD CONSTRAINT $fk "
."FOREIGN KEY ( `$column_name` ) REFERENCES `$referenced_table_name` ( `$referenced_column_name` ); ");
}

public function remove_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name){
$fk = 'fk'.$table_name.$column_name.$referenced_table_name.$referenced_column_name;
return $this->run_query("ALTER TABLE $table_name DROP FOREIGN KEY $fk;");
}

public function add_index($table_name, $index_name, $columns, $index_type = 'normal')
{
switch ($index_type)
Expand Down
34 changes: 34 additions & 0 deletions classes/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,40 @@ public function remove_column($table_name, $column_name)
return $ret;
}

/**
* Add a foreign key to a table
*
* @example add_foreign_key ( "client", "city_id", "city", "id");
*
* @param string Name of the table
* @param string Name of the column
* @param string Name of referenced table
* @param string Name of referenced column
* @return boll Returns true if no errors
*/
public function add_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name)
{
$ret = $this->driver->add_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name);
return $ret;
}

/**
* Add a foreign key to a table
*
* @example remove_foreign_key ( "client", "city_id", "city", "id");
*
* @param string Name of the table
* @param string Name of the column
* @param string Name of referenced table
* @param string Name of referenced column
* @return boll Returns true if no errors
*/
public function remove_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name)
{
$ret = $this->driver->remove_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name);
return $ret;
}

/**
* Add an index
*
Expand Down