[commits] [Wiki] changed: Doc/Dev/HordeDb

Wiki Guest wikiguest at horde.org
Thu May 7 09:10:17 UTC 2020


guest [89.12.183.15]  Thu, 07 May 2020 09:10:17 +0000

Modified page: https://wiki.horde.org/Doc/Dev/HordeDb
New Revision:  2
Change log:  Added explanation on how to set indeces

@@ -35,8 +35,47 @@
  +++ Dropping a column from an existing table


  +++ Adding an index
+
+It's possible to use a helper function to check if an index is already set:
+
+<code>
+/**
+ * Helper function to test if an index already exists
+ */
+private function hasIndex($tableName, $column)
+{
+    $indexedColumns = array();
+    foreach ($this->indexes($tableName) as $index) {
+        $indexedColumns = array_merge($indexedColumns, $index->columns);
+    }
+
+    if (is_array($column)) {
+        foreach ($column as $entry) {
+            return in_array($entry, $indexedColumns);
+        }
+    } else {
+        return in_array($column, $indexedColumns);
+    }
+}
+</code>
+
+Add the index
+
+<code>
+if (!$this->hasIndex('my_table', array('my_column', 'my_column2'))) {
+    $this->addIndex('my_table', array('my_column', 'my_column2'));
+}
+</code>
+
+It's also possible to add unique keys or a self defined names via the  
options:
+
+<code>
+if (!$this->hasIndex('my_table', 'my_column')) {
+    $this->addIndex('my_table', 'my_column', array('unique' => true));
+}
+</code>


  +++ Adding a unique key / unique index




More information about the commits mailing list