openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Table.class.php (4702B)


      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 	public function column( $columnName ) {
     60 		return new Column( $this->db,$this->dbmsType, $this, $columnName );
     61 	}
     62 
     63 
     64 	/**
     65 	 * Erzeugt eine neue Tabelle.
     66 	 * Die neue Tabelle enthält bereits eine Spalte "id" (da eine leere Tabelle i.d.R. nicht zulässig ist).
     67 	 */
     68 	public function add()
     69 	{
     70 		$tableName = $this->getSqlName();
     71 
     72 		$table_opts = $this->dbmsType == DbVersion::TYPE_MYSQL ? ' ENGINE=InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci' : '';
     73 
     74 		$ddl = $this->db->sql('CREATE TABLE ' . $tableName . '(id INTEGER)' . $table_opts . ';');
     75 		// The syntax 'TYPE = InnoDB' was deprecated in MySQL 5.0 and was removed in MySQL 5.1 and later versions.
     76 
     77 		$ddl->execute();
     78 
     79 		return $this;
     80 	}
     81 
     82 
     83 	public function addPrimaryKey($columnNames = 'id')
     84 	{
     85 		$table = $this->getSqlName();
     86 
     87 		if (!is_array($columnNames))
     88 			$columnNames = explode(',', $columnNames);
     89 
     90 		$ddl = $this->db->sql('ALTER TABLE ' . $table . ' ADD PRIMARY KEY (' . implode(',', $columnNames) . ');');
     91 		$ddl->execute();
     92 
     93 	}
     94 
     95 
     96 
     97 	# Creating a unique key
     98 	# param 1: name of index column. Seperate multiple columns with ','
     99 	public function addIndex( $columnNames, $unique = false)
    100 	{
    101 		if (!is_array($columnNames))
    102 			$columnNames = [$columnNames];
    103 
    104 		$indexName = $this->tablePrefix . self::INDEX_PREFIX . '_' . $this->name . '_' . implode('_', $columnNames) . $this->tableSuffix;
    105 
    106 //	if	[ "$type" == "oracle" ]; then
    107 //	cnt=$(($cnt+1))
    108 //	echo "CREATE UNIQUE INDEX ${prefix}uidx_${cnt}" >> $outfile
    109 //	else
    110 
    111 		$ddl = $this->db->sql('CREATE ' . ($unique ? 'UNIQUE ' : '') . 'INDEX ' . $indexName . ' ON ' . $this->getSqlName() . ' (' . implode(',', $columnNames) . ');');
    112 		$ddl->execute();
    113 
    114 	}
    115 
    116 
    117 	/**
    118 	 * Creating a unique key.
    119 	 * param 1: name of index column. Seperate multiple columns with ','
    120 	 *
    121 	 */
    122 	public function addUniqueIndex( $columnNames)
    123 	{
    124 		$this->addIndex( $columnNames, true);
    125 	}
    126 
    127 
    128 	# Creating a foreign key
    129 	# param 1: column name
    130 	# param 2: target table name
    131 	# param 3: target column name
    132 	public function addConstraint($columnName, $targetTableName, $targetColumnName = 'id')
    133 	{
    134 		$targetTable = new Table($this->db,$this->dbmsType,$targetTableName);
    135 		$targetTablename = $targetTable->getSqlName();
    136 
    137 		$constraintName = $this->tablePrefix . self::CONSTRAINT_PREFIX . '_' . $this->name . $this->tableSuffix . '_' . $columnName;
    138 
    139 		// Oracle doesn't support "ON DELETE RESTRICT"-Statements, but its the default.
    140 
    141 		$ddl = $this->db->sql('ALTER TABLE ' . $this->getSqlName() . ' ADD CONSTRAINT ' . $constraintName . ' FOREIGN KEY (' . $columnName . ') REFERENCES ' . $targetTablename . ' (' . $targetColumnName . ') ON DELETE RESTRICT ON UPDATE RESTRICT;');
    142 		$ddl->execute();
    143 	}
    144 
    145 
    146 	public function drop()
    147 	{
    148 		$table = $this->getSqlName();
    149 
    150 		$ddl = $this->db->sql('DROP TABLE ' . $table . ';');
    151 		$ddl->execute();
    152 	}
    153 
    154 	function dropIndex($columnNames)
    155 	{
    156 		if (!is_array($columnNames))
    157 			$columnNames = [$columnNames];
    158 
    159 		$indexName = $this->tablePrefix . self::INDEX_PREFIX . '_' . $this->name . '_' . implode('_', $columnNames) . $this->tableSuffix;
    160 
    161 		$ddl = $this->db->sql('DROP INDEX ' . $indexName . ' ON ' . $this->getSqlName() . ';');
    162 		$ddl->execute();
    163 	}
    164 
    165 	public function dropUniqueIndex($indexName)
    166 	{
    167 		$this->dropIndex($indexName);
    168 	}
    169 
    170 	public function dropPrimaryKey( $columnNames)
    171 	{
    172 		$table = $this->getSqlName();
    173 
    174 		if (!is_array($columnNames))
    175 			$columnNames = explode(',', $columnNames);
    176 
    177 		$ddl = $this->db->sql('ALTER TABLE ' . $table . ' DROP PRIMARY KEY(' . implode(',', $columnNames) . ')');
    178 		$ddl->execute();
    179 	}
    180 
    181 
    182 	public function dropConstraint($columnName)
    183 	{
    184 
    185 		$constraintName = $this->tablePrefix . self::CONSTRAINT_PREFIX . '_' . $this->name . $this->tableSuffix . '_' . $columnName;
    186 
    187 		$table = $this->getSqlName();
    188 		// In MySQL, there’s no DROP CONSTRAINT, you have to use DROP FOREIGN KEY instead
    189 		$ddl = $this->db->sql('ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $constraintName . ';');
    190 		$ddl->execute();
    191 	}
    192 
    193 }