1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
<?php
/**
* Database column class, to describe tables
*
* This file is part of Zoph.
*
* Zoph is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package Zoph
* @author Jeroen Roos
*/
namespace db;
use exception\unknownColumnTypeException;
/**
* Database column class, to describe tables
*
* Used in CREATE TABLE queries, possibly later also in others (ALTER?)
*
* @package Zoph
* @author Jeroen Roos
*/
class column {
/** @var string name of the column */
private $name;
/** @var string type of the column */
private $type;
/** @var int size of the column */
private $size;
/** @var array possible values for ENUM type */
private $values;
/** @var int decimals in the column */
private $decimals;
/** @var bool column value may be NULL */
private $notNull = false;
/** @var bool column value autoincrements */
private $autoIncrement = false;
/** @var bool column is key */
private $isKey = false;
/** @var string if column is key, this stores the name of the key */
private $keyName = "";
/** @var in if column is key, this stores the length of the key (or 0 for whole field) */
private $keyLength = 0;
/** @var bool column is primary key */
private $primaryKey = false;
/** @var bool column is unsigned */
private $unsigned = false;
/** @var string on update */
private $onUpdate = "";
/** @var string default value */
private $default = "";
private static $noQuotes = array(
"CURRENT_TIMESTAMP",
"NULL"
);
/**
* Create a new column
* @param string name
*/
public function __construct($name) {
$this->name=$name;
}
/**
* Set column to UNSIGNED
*/
public function unsigned() {
$this->unsigned=true;
return $this;
}
/**
* Set column to AUTO_INCREMENT
*/
public function autoIncrement() {
$this->autoIncrement=true;
$this->setKey(); // Auto_increment fields must be key
return $this;
}
/**
* Set column to NOT NULL
*/
public function notNull() {
$this->notNull=true;
return $this;
}
/**
* Is column to NOT NULL?
* @return bool is NOT NULL
*/
public function isNotNull() {
return($this->notNull);
}
/**
* Set column as KEY
*/
public function setKey(string $keyname = null, int $length = 0) {
$this->isKey=true;
if ($keyname) {
$this->keyName=$keyname;
} else {
$this->keyName=$this->name;
}
if ($length > 0) {
$this->keyLength=$length;
}
return $this;
}
/**
* Set column as PRIMARY KEY
*/
public function setPrimaryKey() {
$this->primaryKey=true;
return $this;
}
/**
* Check if column is KEY
* @return bool is KEY
*/
public function isKey() {
return !$this->isPrimaryKey() && $this->isKey;
}
/**
* Get column name
* @return string name
*/
public function getName() {
return $this->name;
}
/**
* Get key name
* @return string keyName
*/
public function getKeyName() {
if (!$this->isKey) {
throw new keyException($this->name . " is not a key");
}
return $this->keyName;
}
/**
* Get key length
* @return int part of the field to use as key
*/
public function getKeyLength() {
if (!$this->isKey) {
throw new keyException($this->name . " is not a key");
}
return $this->keyLength;
}
/**
* Check if column is PRIMARY KEY
* @return bool is PRIMARY KEY
*/
public function isPrimaryKey() {
return $this->primaryKey;
}
/**
* Set column DEFAULT value
*/
public function default(string $default) {
$this->default = $this->addQuotes($default);
return $this;
}
/**
* Set column ON UPDATE value
*/
public function onUpdate(string $onUpdate) {
$this->onUpdate = $this->addQuotes($onUpdate);
return $this;
}
/**
* VARCHAR type
* @param size int length of field
*/
public function varchar(int $size) {
$this->type=type::VARCHAR;
$this->size=$size;
return $this;
}
/**
* CHAR type
* @param size int length of field
*/
public function char(int $size) {
$this->type=type::CHAR;
$this->size=$size;
return $this;
}
/**
* TINYINT type
* note that specifying the 'length' is not supported
* this only works with 'ZEROFILL', which is not supported
* (and it's not really the length)
*/
public function tinyint() {
$this->type=type::TINYINT;
return $this;
}
/**
* SMALLINT type
* note that specifying the 'length' is not supported
* this only works with 'ZEROFILL', which is not supported
* (and it's not really the length)
*/
public function smallint() {
$this->type=type::SMALLINT;
return $this;
}
/**
* INT type
* note that specifying the 'length' is not supported
* this only works with 'ZEROFILL', which is not supported
* (and it's not really the length)
*/
public function int() {
$this->type=type::INT;
return $this;
}
/**
* FLOAT type
* single precission float
* @param int m total number of digits
* @param int d number of digits after the comma
* (and it's not really the length)
*/
public function float(int $m, int $d) {
$this->type=type::FLOAT;
$this->size = $m;
$this->decimals = $d;
return $this;
}
/**
* TEXT type
* note that specifying the length is not supported
*/
public function text() {
$this->type=type::TEXT;
return $this;
}
/**
* BLOB type
* note that specifying the length is not supported
*/
public function blob() {
$this->type=type::BLOB;
return $this;
}
/**
* DATE type
*/
public function date() {
$this->type=type::DATE;
return $this;
}
/**
* DATETIME type
*/
public function datetime() {
$this->type=type::DATETIME;
return $this;
}
/**
* TIMESTAMP type
*/
public function timestamp() {
$this->type=type::TIMESTAMP;
return $this;
}
/**
* ENUM type
*/
public function enum(...$values) {
$this->type=type::ENUM;
$this->values=$values;
return $this;
}
/**
* Build the clause
* @return string clause
*/
public function __toString() {
$sql = "`" . $this->name . "` " . strtoupper($this->type);
switch ($this->type) {
case type::TINYINT:
case type::SMALLINT:
case type::INT:
$sql .= $this->unsigned ? " UNSIGNED" : "";
$sql .= $this->autoIncrement ? " AUTO_INCREMENT" : "";
break;
case type::FLOAT:
$sql .= "(" . $this->size . "," . $this->decimals . ")";
$sql .= $this->unsigned ? " UNSIGNED" : "";
$sql .= $this->autoIncrement ? " AUTO_INCREMENT" : "";
break;
case type::CHAR:
case type::VARCHAR:
$sql .= "(" . $this->size . ")";
break;
case type::TEXT:
case type::BLOB:
case type::DATE:
case type::DATETIME:
case type::TIMESTAMP:
break;
case type::ENUM:
$sql .= "(\"" . implode("\", \"", $this->values) . "\")";
break;
default:
throw new unknownColumnTypeException("Unknown column type: " . e($this->type));
break;
}
$sql .= $this->notNull ? " NOT NULL" : "";
$sql .= $this->default ? " DEFAULT " . $this->default : "";
$sql .= $this->onUpdate ? " ON UPDATE " . $this->onUpdate : "";
return $sql;
}
/**
* Add Quotes for ON UPDATE and DEFAULT
*
* This adds quotes to a string, except if they are in the 'noQuotes' array
* this is used for SQL keywords as ON UPDATE or DEFAULT value
* @param string to be quoted
* @return string quoted string
*/
private function addQuotes(string $string) {
if (!in_array($string, static::$noQuotes)) {
$string = "\"" . $string . "\"";
}
return $string;
}
}
|