openrat-cms

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

Column.class.php (3792B)


      1 <?php
      2 
      3 
      4 namespace database;
      5 
      6 
      7 class Column
      8 {
      9 	const TYPE_INT     = 1;
     10 	const TYPE_VARCHAR = 2;
     11 	const TYPE_TEXT    = 3;
     12 	const TYPE_BLOB    = 4;
     13 
     14 	private $db;
     15 	private $dbmsType;
     16 	private $name;
     17 	private $table;
     18 
     19 	private $type     = self::TYPE_INT;
     20 	private $charset  = null;
     21 	private $size     = null;
     22 	private $default  = null;
     23 	private $nullable = false;
     24 
     25 	public function type( $type ) {
     26 		$this->type = $type;
     27 		return $this;
     28 	}
     29 
     30 
     31 	public function charset( $charset ) {
     32 		$this->charset = $charset;
     33 		return $this;
     34 	}
     35 
     36 
     37 	public function size( $size ) {
     38 		$this->size = $size;
     39 		return $this;
     40 	}
     41 
     42 	public function defaultValue( $default ) {
     43 		$this->default = $default;
     44 		return $this;
     45 	}
     46 
     47 
     48 	public function nullable() {
     49 		$this->nullable = true;;
     50 		return $this;
     51 	}
     52 
     53 	/**
     54 	 * Column constructor.
     55 	 *
     56 	 * @param $db Database
     57 	 * @param $type
     58 	 * @param $table Table
     59 	 * @param $name
     60 	 */
     61 	public function __construct($db, $type, $table, $name)
     62 	{
     63 		$this->db     = $db;
     64 		$this->dbmsType = $type;
     65 		$this->table  = $table;
     66 		$this->name   = $name;
     67 	}
     68 
     69 
     70 	/**
     71 	 * Creating the column definition.
     72 	 */
     73 	protected function getColumnDefinition()
     74 	{
     75 		$table = $this->table->getSqlName();
     76 
     77 		switch ($this->type) {
     78 			case self::TYPE_INT:
     79 				switch ($this->dbmsType) {
     80 					case DbVersion::TYPE_MYSQL:
     81 						if ($this->size == 1)
     82 							$dbmsInternalType = 'TINYINT';
     83 						else
     84 							$dbmsInternalType = 'INT';
     85 						break;
     86 
     87 					case DbVersion::TYPE_ORACLE:
     88 						$dbmsInternalType = 'NUMBER';
     89 						break;
     90 
     91 					default:
     92 						$dbmsInternalType = 'INTEGER';
     93 
     94 				}
     95 				break;
     96 
     97 			case self::TYPE_VARCHAR:
     98 				switch ($this->dbmsType) {
     99 					default:
    100 						$dbmsInternalType = 'VARCHAR';
    101 
    102 				}
    103 				break;
    104 
    105 			case self::TYPE_TEXT:
    106 				switch ($this->dbmsType) {
    107 					case DbVersion::TYPE_MYSQL:
    108 						$dbmsInternalType = 'MEDIUMTEXT';
    109 						break;
    110 
    111 					case DbVersion::TYPE_ORACLE:
    112 						$dbmsInternalType = 'CLOB';
    113 						break;
    114 
    115 					default:
    116 						$dbmsInternalType = 'TEXT';
    117 
    118 				}
    119 				break;
    120 
    121 			case self::TYPE_BLOB:
    122 				switch ($this->dbmsType) {
    123 					case DbVersion::TYPE_MYSQL:
    124 						$dbmsInternalType = 'MEDIUMBLOB';
    125 						break;
    126 
    127 					case DbVersion::TYPE_ORACLE:
    128 						$dbmsInternalType = 'CLOB';
    129 						break;
    130 
    131 					case DbVersion::TYPE_POSTGRES:
    132 						$dbmsInternalType = 'TEXT';
    133 						break;
    134 
    135 					case DbVersion::TYPE_SQLITE:
    136 						$dbmsInternalType = 'TEXT';
    137 						break;
    138 
    139 					default:
    140 						$dbmsInternalType = 'BLOB';
    141 
    142 				}
    143 				break;
    144 			default:
    145 				throw new \LogicException( 'Unknown Column type: ' . $this->type);
    146 		}
    147 
    148 		if ($this->dbmsType == DbVersion::TYPE_ORACLE) {
    149 			// TEXT-columns must be nullable in Oracle, because empty strings are treated as NULL. BAD BAD BAD, Oracle!
    150 			if ($this->type == self::TYPE_VARCHAR || $this->type == self::TYPE_TEXT)
    151 				$nullable = true;
    152 
    153 		}
    154 
    155 		return $dbmsInternalType .
    156 			($this->size    !== null ? '(' . $this->size . ')' : '') .
    157 			($this->charset !== null ? ' CHARACTER SET ' . $this->charset : '') .
    158 			($this->default !== null ? ' DEFAULT ' . (is_string($this->default) ? "'" : '') . $this->default . (is_string($this->default) ? "'" : '') : '') .
    159 			' ' . ($this->nullable ? 'NULL' : 'NOT NULL');
    160 	}
    161 
    162 
    163 	public function add() {
    164 		$table = $this->table->getSqlName();
    165 		$ddl = $this->db->sql('ALTER TABLE ' . $table .
    166 			' ADD COLUMN ' . $this->name . ' ' . $this->getColumnDefinition(). ';'
    167 		);
    168 		$ddl->execute();
    169 	}
    170 
    171 	public function modify() {
    172 		$table = $this->table->getSqlName();
    173 		$ddl = $this->db->sql('ALTER TABLE ' . $table .
    174 			' MODIFY COLUMN ' . $this->name . ' ' . $this->getColumnDefinition() . ';'
    175 		);
    176 		$ddl->execute();
    177 	}
    178 
    179 	function drop()
    180 	{
    181 		$table = $this->table->getSqlName();
    182 
    183 		$ddl = $this->db->sql('ALTER TABLE ' . $table . ' DROP COLUMN ' . $this->name . ';');
    184 		$ddl->execute();
    185 	}
    186 }