dblayer : Add foreign keys in creation of table

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1439 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-06-12 12:39:51 +00:00
parent 1d1e0cb0b6
commit ab6a4715ac

View File

@@ -446,8 +446,23 @@ class dblayer extends PDO
} }
/** Create the table defined by the differents fields. /** Create the table defined by the differents fields.
Define the SQL syntax based on SQL engines */ Define the SQL syntax based on SQL engines
public function createTable ($table, $fields, $primary, $unique) $table = "dns zones";
$fields = array (
"id"=>array ("integer", "not null", "autoincrement"),
"zo ne"=>array ("varchar", "255", "not null"),
"vie wname"=>array ("varchar", "255"),
"view clients"=>array ("varchar", "255"),
"comme nt"=>array ("varchar", "1024"),
"opendate"=>array ("datetime", "not null"),
"closedate"=>array ("datetime"),
);
$primary = "id";
$unique = array ("id", array ("zo ne", "vie wname"));
$foreign = array ("zone"=>"table.field",...);
*/
public function createTable ($table, $fields, $primary, $unique,
$foreign = array ())
{ {
if ($this->db === null) if ($this->db === null)
throw new Exception ("Database not connected"); throw new Exception ("Database not connected");
@@ -517,7 +532,18 @@ class dblayer extends PDO
$sql .="`)"; $sql .="`)";
} }
} }
// TODO : Foreign keys // Foreign keys
$i = 0;
foreach ($foreign as $field=>$k)
{
$sql .= ",\n FOREIGN KEY(`$field`) REFERENCES `".$k[0]."`(`".
$k[1]."`)";
if (isset ($k[2]))
$sql .= " ".$k[2];
if ($i > 0)
$sql .= ",";
$i++;
}
$sql .=")"; $sql .=")";
break; break;
case "mysql": case "mysql":
@@ -584,8 +610,20 @@ class dblayer extends PDO
$sql .="`)"; $sql .="`)";
} }
} }
// TODO : Foreign keys // Foreign keys
$sql .=")"; $i = 0;
foreach ($foreign as $field=>$k)
{
$sql .= ",\n FOREIGN KEY(`$field`) REFERENCES `".$k[0]."`(`".
$k[1]."`)";
if (isset ($k[2]))
$sql .= " ".$k[2];
if ($i > 0)
$sql .= ",";
$i++;
}
$sql .=") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
break; break;
case "pgsql": case "pgsql":
$sql = "CREATE TABLE \"$table\" (\n"; $sql = "CREATE TABLE \"$table\" (\n";
@@ -655,7 +693,18 @@ class dblayer extends PDO
$sql .="\")"; $sql .="\")";
} }
} }
// TODO : Foreign keys // Foreign keys
$i = 0;
foreach ($foreign as $field=>$k)
{
$sql .= ",\n FOREIGN KEY(\"$field\") REFERENCES \"".$k[0]."\"(\"".
$k[1]."\")";
if (isset ($k[2]))
$sql .= " ".$k[2];
if ($i > 0)
$sql .= ",";
$i++;
}
$sql .=")"; $sql .=")";
break; break;
default: default: