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
|
<?php
/**
* Database CREATE TABLE query
*
* 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;
/**
* Database CREATE TABLE query
*
* @package Zoph
* @author Jeroen Roos
*/
class create extends query {
/** @var string name of the table */
private $name;
/** @var string MariaDB engine */
private $engine="InnoDB";
/** @var array columns */
private $columns=array();
/** @var array primary keys */
private $pKeys=array();
/** @var array keys */
private $keys=array();
/** @var array index */
private $index=array();
/** @var array keylength */
private $keyLength=array();
/** @var array columns that can not be 'null' */
private $notNull=array();
/** @var bool create table with IF NOT EXISTS statement */
private $ifNotExists=false;
/**
* Create a new table
* @param string name
* @param array columns
*/
public function __construct(string $name) {
$this->name=$name;
}
public function addColumns(array $columns) : void {
foreach ($columns as $column) {
$this->addColumn($column);
}
}
public function addColumn(column $column) : void {
$this->columns[]=$column;
}
public function addIndex(string $name, array $fields) : void {
$this->index[$name]=$fields;
}
public function ifNotExists($if = true) : void {
$this->ifNotExists = $if;
}
/**
* Build the clause
* @return string clause
*/
public function __toString() : string {
$this->pKeys=array();
$this->keys=array();
$this->notNull=array();
$sql = "CREATE TABLE " . ($this->ifNotExists ? "IF NOT EXISTS " : "") .
db::getPrefix() . $this->name . " (\n";
foreach ($this->columns as $column) {
$sql .= " " . $column . ",\n";
if ($column->isKey()) {
$this->keys[$column->getKeyName()]=$column;
$this->keyLength[$column->getKeyName()]=$column->getKeyLength();
}
if ($column->isPrimaryKey()) {
$this->pKeys[]=$column->getName();
}
if ($column->isNotNull()) {
$this->notNull[]=$column;
}
}
$sql .= $this->getKeyString();
$sql .= $this->getIndexString();
// Remove last comma
$sql = substr($sql, 0, -2) . "\n";
$sql .= ") ENGINE=" . $this->engine;
return $sql;
}
/**
* Get KEY and PRIMARY KEY statements
* @return string SQL snippet.
*/
private function getKeyString() : string {
$sql = "";
if (!empty($this->pKeys)) {
$sql .= " PRIMARY KEY (" . implode(", ", $this->pKeys) . "),\n";
}
if (!empty($this->keys)) {
foreach ($this->keys as $name => $column) {
if ($this->keyLength[$name] > 0) {
$length = "(" . $this->keyLength[$name] . ")";
} else {
$length = "";
}
$sql .= " KEY " . $name . "(" . $column->getName() . $length . "),\n";
}
}
return $sql;
}
/**
* Get INDEX statements
* INDEX and KEY are synonyms in MySQL, we use KEY for single column indices and
* INDEX for multi-column indices except for PRIMARY KEYs.
* @return string SQL snippet
*/
private function getIndexString() : string {
$sql = "";
if (!empty($this->index)) {
foreach ($this->index as $name => $columns) {
$sql .= " INDEX " . $name . " (" . implode(", ", $columns) . "),\n";
}
}
return $sql;
}
}
|