From cfd49c7f0c4f0d1301a068e470125caa526f14de Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 23 Jun 2017 15:50:59 +0200 Subject: [install.php] Add functions for getting and deleting constraints --- install.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'install.php') diff --git a/install.php b/install.php index d09cbff2..da1d5d02 100644 --- a/install.php +++ b/install.php @@ -90,6 +90,47 @@ function tableRename($old, $new) { } +/** + * Get all constraints from given table+column to another table+column. + * + * @param string $table source table, being constrained + * @param string $column source column + * @param string $refTable referenced table, dictating the constraints + * @param string $refColumn referenced column + * @return false|string[] list of constraints matching the request, false on error + */ +function tableGetContraints($table, $column, $refTable, $refColumn) +{ + $db = 'openslx'; + if (defined('CONFIG_SQL_DB')) { + $db = CONFIG_SQL_DB; + } elseif (defined('CONFIG_SQL_DSN')) { + if (preg_match('/dbname\s*=\s*([^;\s]+)\s*(;|$)/i', CONFIG_SQL_DSN, $out)) { + $db = $out[1]; + define('CONFIG_SQL_DB', $db); + } + } + $res = Database::simpleQuery('SELECT `CONSTRAINT_NAME` FROM information_schema.KEY_COLUMN_USAGE' + . ' WHERE `TABLE_SCHEMA` = :db AND `TABLE_NAME` = :table AND `COLUMN_NAME` = :column' + . ' AND `REFERENCED_TABLE_NAME` = :refTable AND `REFERENCED_COLUMN_NAME` = :refColumn', + compact('db', 'table', 'column', 'refTable', 'refColumn')); + if ($res === false) + return false; + return $res->fetchAll(PDO::FETCH_COLUMN, 0); +} + +/** + * Drop constraint from a table. + * + * @param string $table table name + * @param string $constraint constraint name + * @return bool success indicator + */ +function tableDeleteConstraint($table, $constraint) +{ + return Database::exec("ALTER TABLE `$table` DROP FOREIGN KEY `$constraint`") !== false; +} + function tableCreate($table, $structure, $fatalOnError = true) { if (tableExists($table)) { -- cgit v1.2.3-55-g7522