From a1122a716cb780311d70d81a2379aaf1523a7981 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 11 Jun 2014 14:50:16 +0000 Subject: [PATCH] Add creationTable function for MySQL, SQLite and PostgreSQL git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1437 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayer.php | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/dblayer.php b/dblayer.php index 31ad9d6..3a4277c 100644 --- a/dblayer.php +++ b/dblayer.php @@ -445,6 +445,194 @@ class dblayer extends PDO return $arr; } + /** Create the table defined by the differents fields. + Define the SQL syntax based on SQL engines */ + public function createTable ($table, $fields, $primary, $unique) + { + if ($this->db === null) + throw new Exception ("Database not connected"); + switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME)) + { + case "sqlite": + $sql = "CREATE TABLE `table` (\n"; + $i = 0; + foreach ($fields as $field=>$params) + { + if ($i > 0) + $sql .= ",\n"; + // Name of field + $sql .= "`$field` "; + // Type of field : in $params[0] + if (!isset ($params[0])) + throw new Exception (_("No database type defined for field")); + switch ($params[0]) + { + case "integer": + $sql .= "INTEGER"; + $params = array_slice ($params, 1); + break; + case "varchar": + if (!isset ($params[1])) + throw new Exception (_("No Size provided for varchar field")); + $sql .= "VARCHAR(".$params[1].")"; + $params = array_slice ($params, 2); + break; + case "datetime": + $sql .= "DATETIME"; + $params = array_slice ($params, 1); + break; + default: + throw new Exception (_("Unknown type provided for field")); + } + // Primary key + if ($primary === $field) + $sql .= " PRIMARY KEY"; + // Others parameters for field + // Sort to put the autoincrement field in front of params, if it is + // present + sort ($params); + foreach ($params as $p) + { + switch ($p) + { + case "not null": $sql .= " NOT NULL"; break; + case "autoincrement": $sql .= " AUTOINCREMENT";break; + default: + throw new Exception (_("Unknown additionnal parameter for field"), + 500); + } + } + $i ++; + } + break; + case "mysql": + $sql = "CREATE TABLE `table` (\n"; + $i = 0; + foreach ($fields as $field=>$params) + { + if ($i > 0) + $sql .= ",\n"; + // Name of field + $sql .= "`$field` "; + // Type of field : in $params[0] + if (!isset ($params[0])) + throw new Exception (_("No database type defined for field")); + switch ($params[0]) + { + case "integer": + $sql .= "INTEGER"; + $params = array_slice ($params, 1); + break; + case "varchar": + if (!isset ($params[1])) + throw new Exception (_("No Size provided for varchar field")); + $sql .= "VARCHAR(".$params[1].")"; + $params = array_slice ($params, 2); + break; + case "datetime": + $sql .= "DATETIME"; + $params = array_slice ($params, 1); + break; + default: + throw new Exception (_("Unknown type provided for field")); + } + // Primary key + if ($primary === $field) + $sql .= " PRIMARY KEY"; + // Others parameters for field + // Sort to put the autoincrement field in front of params, if it is + // present + sort ($params); + foreach ($params as $p) + { + switch ($p) + { + case "not null": $sql .= " NOT NULL"; break; + case "autoincrement": $sql .= " AUTO_INCREMENT";break; + default: + throw new Exception (_("Unknown additionnal parameter for field"), + 500); + } + } + $i ++; + } + break; + case "pgsql": + $sql = "CREATE TABLE \"table\" (\n"; + $i = 0; + foreach ($fields as $field=>$params) + { + if ($i > 0) + $sql .= ",\n"; + // Name of field + $sql .= "\"$field\" "; + if (in_array ("autoincrement", $params)) + $sql .= "SERIAL"; + else + { + // Type of field : in $params[0] + if (!isset ($params[0])) + throw new Exception (_("No database type defined for field")); + switch ($params[0]) + { + case "integer": + $sql .= "INTEGER"; + $params = array_slice ($params, 1); + break; + case "varchar": + if (!isset ($params[1])) + throw new Exception (_("No Size provided for varchar field")); + $sql .= "VARCHAR(".$params[1].")"; + $params = array_slice ($params, 2); + break; + case "datetime": + $sql .= "timestamp with time zone"; + $params = array_slice ($params, 1); + break; + default: + throw new Exception (_("Unknown type provided for field")); + } + // Primary key + if ($primary === $field) + $sql .= " PRIMARY KEY"; + // Others parameters for field + // Sort to put the autoincrement field in front of params, if it is + // present + sort ($params); + foreach ($params as $p) + { + switch ($p) + { + case "not null": $sql .= " NOT NULL"; break; + default: + throw new Exception (_("Unknown additionnal parameter for field"), + 500); + } + } + } + $i ++; + } + break; + default: + throw new Exception (_("PDO Engine not supported in dbLayer", 500)); + } + + // Unique fields + if ($unique !== null) + { + foreach ($unique as $u) + { + $sql .= ",\n UNIQUE ("; + if (is_array ($u)) + $sql .=implode (",", $u); + else + $sql .= $u; + $sql .=")"; + } + } + $sql .=")"; + echo "$sql\n"; + } }