File modules/database/Table.class.php

Last commit: Thu Feb 16 01:04:38 2023 +0100	Jan Dankert	New: Tags for base objects.
1 <?php 2 3 4 namespace database; 5 6 7 class Table 8 { 9 const INDEX_PREFIX = 'IX'; 10 const CONSTRAINT_PREFIX = 'FK'; 11 12 13 /** 14 * @var string 15 */ 16 private $tablePrefix; 17 18 /** 19 * @var string 20 */ 21 private $tableSuffix; 22 23 public function getSqlName() 24 { 25 return $this->tablePrefix . $this->name . $this->tableSuffix; 26 } 27 28 29 /** 30 * @var Database 31 */ 32 private $db; 33 private $dbmsType; 34 35 /** 36 * Table name 37 * @var string 38 */ 39 private $name; 40 41 /** 42 * Table constructor. 43 * 44 * @param $db Database 45 * @param $type 46 * @param $name 47 */ 48 public function __construct($db, $type, $name) 49 { 50 $this->db = $db; 51 $this->dbmsType = $type; 52 $this->name = $name; 53 54 $this->tablePrefix = $db->conf['prefix']; 55 $this->tableSuffix = $db->conf['suffix']; 56 57 } 58 59 /** 60 * @param $columnName String 61 * @return Column Column 62 */ 63 public function column( $columnName ) { 64 return new Column( $this->db,$this->dbmsType, $this, $columnName ); 65 } 66 67 68 /** 69 * Erzeugt eine neue Tabelle. 70 * Die neue Tabelle enthält bereits eine Spalte "id" (da eine leere Tabelle i.d.R. nicht zulässig ist). 71 * @return Table 72 */ 73 public function add() 74 { 75 $tableName = $this->getSqlName(); 76 77 $table_opts = $this->dbmsType == DbVersion::TYPE_MYSQL ? ' ENGINE=InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci' : ''; 78 79 $ddl = $this->db->sql('CREATE TABLE ' . $tableName . '(id INTEGER)' . $table_opts . ';'); 80 // The syntax 'TYPE = InnoDB' was deprecated in MySQL 5.0 and was removed in MySQL 5.1 and later versions. 81 82 $ddl->execute(); 83 84 return $this; 85 } 86 87 88 public function addPrimaryKey($columnNames = 'id') 89 { 90 $table = $this->getSqlName(); 91 92 if (!is_array($columnNames)) 93 $columnNames = explode(',', $columnNames); 94 95 $ddl = $this->db->sql('ALTER TABLE ' . $table . ' ADD PRIMARY KEY (' . implode(',', $columnNames) . ');'); 96 $ddl->execute(); 97 98 } 99 100 101 102 # Creating a unique key 103 # param 1: name of index column. Seperate multiple columns with ',' 104 public function addIndex( $columnNames, $unique = false) 105 { 106 if (!is_array($columnNames)) 107 $columnNames = [$columnNames]; 108 109 $indexName = $this->tablePrefix . self::INDEX_PREFIX . '_' . $this->name . '_' . implode('_', $columnNames) . $this->tableSuffix; 110 111 // if [ "$type" == "oracle" ]; then 112 // cnt=$(($cnt+1)) 113 // echo "CREATE UNIQUE INDEX ${prefix}uidx_${cnt}" >> $outfile 114 // else 115 116 $ddl = $this->db->sql('CREATE ' . ($unique ? 'UNIQUE ' : '') . 'INDEX ' . $indexName . ' ON ' . $this->getSqlName() . ' (' . implode(',', $columnNames) . ');'); 117 $ddl->execute(); 118 119 } 120 121 122 /** 123 * Creating a unique key. 124 * param 1: name of index column. Seperate multiple columns with ',' 125 * 126 */ 127 public function addUniqueIndex( $columnNames) 128 { 129 $this->addIndex( $columnNames, true); 130 } 131 132 133 # Creating a foreign key 134 # param 1: column name 135 # param 2: target table name 136 # param 3: target column name 137 public function addConstraint($columnName, $targetTableName, $targetColumnName = 'id') 138 { 139 $targetTable = new Table($this->db,$this->dbmsType,$targetTableName); 140 $targetTablename = $targetTable->getSqlName(); 141 142 $constraintName = $this->tablePrefix . self::CONSTRAINT_PREFIX . '_' . $this->name . $this->tableSuffix . '_' . $columnName; 143 144 // Oracle doesn't support "ON DELETE RESTRICT"-Statements, but its the default. 145 146 $ddl = $this->db->sql('ALTER TABLE ' . $this->getSqlName() . ' ADD CONSTRAINT ' . $constraintName . ' FOREIGN KEY (' . $columnName . ') REFERENCES ' . $targetTablename . ' (' . $targetColumnName . ') ON DELETE RESTRICT ON UPDATE RESTRICT;'); 147 $ddl->execute(); 148 } 149 150 151 public function drop() 152 { 153 $table = $this->getSqlName(); 154 155 $ddl = $this->db->sql('DROP TABLE ' . $table . ';'); 156 $ddl->execute(); 157 } 158 159 function dropIndex($columnNames) 160 { 161 if (!is_array($columnNames)) 162 $columnNames = [$columnNames]; 163 164 $indexName = $this->tablePrefix . self::INDEX_PREFIX . '_' . $this->name . '_' . implode('_', $columnNames) . $this->tableSuffix; 165 166 $ddl = $this->db->sql('DROP INDEX ' . $indexName . ' ON ' . $this->getSqlName() . ';'); 167 $ddl->execute(); 168 } 169 170 public function dropUniqueIndex($indexName) 171 { 172 $this->dropIndex($indexName); 173 } 174 175 public function dropPrimaryKey( $columnNames) 176 { 177 $table = $this->getSqlName(); 178 179 if (!is_array($columnNames)) 180 $columnNames = explode(',', $columnNames); 181 182 $ddl = $this->db->sql('ALTER TABLE ' . $table . ' DROP PRIMARY KEY(' . implode(',', $columnNames) . ')'); 183 $ddl->execute(); 184 } 185 186 187 public function dropConstraint($columnName) 188 { 189 190 $constraintName = $this->tablePrefix . self::CONSTRAINT_PREFIX . '_' . $this->name . $this->tableSuffix . '_' . $columnName; 191 192 $table = $this->getSqlName(); 193 // In MySQL, there’s no DROP CONSTRAINT, you have to use DROP FOREIGN KEY instead 194 $ddl = $this->db->sql('ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $constraintName . ';'); 195 $ddl->execute(); 196 } 197 198 }
Download modules/database/Table.class.php
History Thu, 16 Feb 2023 01:04:38 +0100 Jan Dankert New: Tags for base objects. Sun, 7 Nov 2021 23:45:50 +0100 Jan Dankert Fix: First successful migration to the new "content" table. Wed, 7 Jul 2021 22:29:06 +0200 Jan Dankert Refactoring: Split values and content, new table "content". Sun, 7 Mar 2021 00:10:20 +0100 Jan Dankert Refactoring: Hopefully more performance while accessing the database resultsets. Sat, 31 Oct 2020 00:43:29 +0100 Jan Dankert New: Support for OpenId Connect; Removed: Support for LDAP. Tue, 29 Sep 2020 22:17:11 +0200 Jan Dankert Refactoring: Do not use global constants. Fri, 25 Sep 2020 01:00:58 +0200 Jan Dankert Refactoring: More OO in the database updater :)