summaryrefslogtreecommitdiffstats
path: root/install.php
diff options
context:
space:
mode:
authorSimon Rettberg2017-06-23 15:50:59 +0200
committerSimon Rettberg2017-06-23 15:50:59 +0200
commitcfd49c7f0c4f0d1301a068e470125caa526f14de (patch)
treecd55e2aa91be28809e8d0c6a7822d6eb94174604 /install.php
parent[inc/Util] Fix: Print trace in CLI mode again (diff)
downloadslx-admin-cfd49c7f0c4f0d1301a068e470125caa526f14de.tar.gz
slx-admin-cfd49c7f0c4f0d1301a068e470125caa526f14de.tar.xz
slx-admin-cfd49c7f0c4f0d1301a068e470125caa526f14de.zip
[install.php] Add functions for getting and deleting constraints
Diffstat (limited to 'install.php')
-rw-r--r--install.php41
1 files changed, 41 insertions, 0 deletions
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)) {