The createTable must use the class fields and not external variables. Easier !
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1443 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
45
dblayer.php
45
dblayer.php
@@ -39,6 +39,8 @@ class dblayer extends PDO
|
|||||||
protected $primary = null;
|
protected $primary = null;
|
||||||
/** An array to define the unique fields (or array of unique fields) */
|
/** An array to define the unique fields (or array of unique fields) */
|
||||||
protected $unique = null;
|
protected $unique = null;
|
||||||
|
/** An array to define the foreign keys of the field */
|
||||||
|
protected $foreign = array ();
|
||||||
/** The db connection */
|
/** The db connection */
|
||||||
protected $db = null;
|
protected $db = null;
|
||||||
/** The verify stack */
|
/** The verify stack */
|
||||||
@@ -461,17 +463,16 @@ class dblayer extends PDO
|
|||||||
$unique = array ("id", array ("zo ne", "vie wname"));
|
$unique = array ("id", array ("zo ne", "vie wname"));
|
||||||
$foreign = array ("zone"=>"table.field",...);
|
$foreign = array ("zone"=>"table.field",...);
|
||||||
*/
|
*/
|
||||||
public function createTable ($table, $fields, $primary, $unique,
|
public function createTable ()
|
||||||
$foreign = array ())
|
|
||||||
{
|
{
|
||||||
if ($this->db === null)
|
if ($this->db === null)
|
||||||
throw new Exception ("Database not connected");
|
throw new Exception ("Database not connected");
|
||||||
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
|
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
|
||||||
{
|
{
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
$sql = "CREATE TABLE `$table` (\n";
|
$sql = "CREATE TABLE `$this->table` (\n";
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach ($fields as $field=>$params)
|
foreach ($this->fields as $field=>$params)
|
||||||
{
|
{
|
||||||
if ($i > 0)
|
if ($i > 0)
|
||||||
$sql .= ",\n";
|
$sql .= ",\n";
|
||||||
@@ -500,7 +501,7 @@ class dblayer extends PDO
|
|||||||
throw new Exception (_("Unknown type provided for field"));
|
throw new Exception (_("Unknown type provided for field"));
|
||||||
}
|
}
|
||||||
// Primary key
|
// Primary key
|
||||||
if ($primary === $field)
|
if ($this->primary === $field)
|
||||||
$sql .= " PRIMARY KEY";
|
$sql .= " PRIMARY KEY";
|
||||||
// Others parameters for field
|
// Others parameters for field
|
||||||
// Sort to put the autoincrement field in front of params, if it is
|
// Sort to put the autoincrement field in front of params, if it is
|
||||||
@@ -520,9 +521,9 @@ class dblayer extends PDO
|
|||||||
$i ++;
|
$i ++;
|
||||||
}
|
}
|
||||||
// Unique fields
|
// Unique fields
|
||||||
if ($unique !== null)
|
if ($this->unique !== null)
|
||||||
{
|
{
|
||||||
foreach ($unique as $u)
|
foreach ($this->unique as $u)
|
||||||
{
|
{
|
||||||
$sql .= ",\n UNIQUE (`";
|
$sql .= ",\n UNIQUE (`";
|
||||||
if (is_array ($u))
|
if (is_array ($u))
|
||||||
@@ -534,7 +535,7 @@ class dblayer extends PDO
|
|||||||
}
|
}
|
||||||
// Foreign keys
|
// Foreign keys
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach ($foreign as $field=>$k)
|
foreach ($this->foreign as $field=>$k)
|
||||||
{
|
{
|
||||||
$sql .= ",\n FOREIGN KEY(`$field`) REFERENCES `".$k[0]."`(`".
|
$sql .= ",\n FOREIGN KEY(`$field`) REFERENCES `".$k[0]."`(`".
|
||||||
$k[1]."`)";
|
$k[1]."`)";
|
||||||
@@ -547,9 +548,9 @@ class dblayer extends PDO
|
|||||||
$sql .=")";
|
$sql .=")";
|
||||||
break;
|
break;
|
||||||
case "mysql":
|
case "mysql":
|
||||||
$sql = "CREATE TABLE `$table` (\n";
|
$sql = "CREATE TABLE `$this->table` (\n";
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach ($fields as $field=>$params)
|
foreach ($this->fields as $field=>$params)
|
||||||
{
|
{
|
||||||
if ($i > 0)
|
if ($i > 0)
|
||||||
$sql .= ",\n";
|
$sql .= ",\n";
|
||||||
@@ -578,7 +579,7 @@ class dblayer extends PDO
|
|||||||
throw new Exception (_("Unknown type provided for field"));
|
throw new Exception (_("Unknown type provided for field"));
|
||||||
}
|
}
|
||||||
// Primary key
|
// Primary key
|
||||||
if ($primary === $field)
|
if ($this->primary === $field)
|
||||||
$sql .= " PRIMARY KEY";
|
$sql .= " PRIMARY KEY";
|
||||||
// Others parameters for field
|
// Others parameters for field
|
||||||
// Sort to put the autoincrement field in front of params, if it is
|
// Sort to put the autoincrement field in front of params, if it is
|
||||||
@@ -598,9 +599,9 @@ class dblayer extends PDO
|
|||||||
$i ++;
|
$i ++;
|
||||||
}
|
}
|
||||||
// Unique fields
|
// Unique fields
|
||||||
if ($unique !== null)
|
if ($this->unique !== null)
|
||||||
{
|
{
|
||||||
foreach ($unique as $u)
|
foreach ($this->unique as $u)
|
||||||
{
|
{
|
||||||
$sql .= ",\n UNIQUE (`";
|
$sql .= ",\n UNIQUE (`";
|
||||||
if (is_array ($u))
|
if (is_array ($u))
|
||||||
@@ -612,7 +613,7 @@ class dblayer extends PDO
|
|||||||
}
|
}
|
||||||
// Foreign keys
|
// Foreign keys
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach ($foreign as $field=>$k)
|
foreach ($this->foreign as $field=>$k)
|
||||||
{
|
{
|
||||||
|
|
||||||
$sql .= ",\n FOREIGN KEY(`$field`) REFERENCES `".$k[0]."`(`".
|
$sql .= ",\n FOREIGN KEY(`$field`) REFERENCES `".$k[0]."`(`".
|
||||||
@@ -626,9 +627,9 @@ class dblayer extends PDO
|
|||||||
$sql .=") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
$sql .=") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
||||||
break;
|
break;
|
||||||
case "pgsql":
|
case "pgsql":
|
||||||
$sql = "CREATE TABLE \"$table\" (\n";
|
$sql = "CREATE TABLE \"$this->table\" (\n";
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach ($fields as $field=>$params)
|
foreach ($this->fields as $field=>$params)
|
||||||
{
|
{
|
||||||
if ($i > 0)
|
if ($i > 0)
|
||||||
$sql .= ",\n";
|
$sql .= ",\n";
|
||||||
@@ -661,7 +662,7 @@ class dblayer extends PDO
|
|||||||
throw new Exception (_("Unknown type provided for field"));
|
throw new Exception (_("Unknown type provided for field"));
|
||||||
}
|
}
|
||||||
// Primary key
|
// Primary key
|
||||||
if ($primary === $field)
|
if ($this->primary === $field)
|
||||||
$sql .= " PRIMARY KEY";
|
$sql .= " PRIMARY KEY";
|
||||||
// Others parameters for field
|
// Others parameters for field
|
||||||
// Sort to put the autoincrement field in front of params, if it is
|
// Sort to put the autoincrement field in front of params, if it is
|
||||||
@@ -681,9 +682,9 @@ class dblayer extends PDO
|
|||||||
$i ++;
|
$i ++;
|
||||||
}
|
}
|
||||||
// Unique fields
|
// Unique fields
|
||||||
if ($unique !== null)
|
if ($this->unique !== null)
|
||||||
{
|
{
|
||||||
foreach ($unique as $u)
|
foreach ($this->unique as $u)
|
||||||
{
|
{
|
||||||
$sql .= ",\n UNIQUE (\"";
|
$sql .= ",\n UNIQUE (\"";
|
||||||
if (is_array ($u))
|
if (is_array ($u))
|
||||||
@@ -695,7 +696,7 @@ class dblayer extends PDO
|
|||||||
}
|
}
|
||||||
// Foreign keys
|
// Foreign keys
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach ($foreign as $field=>$k)
|
foreach ($this->foreign as $field=>$k)
|
||||||
{
|
{
|
||||||
$sql .= ",\n FOREIGN KEY(\"$field\") REFERENCES \"".$k[0]."\"(\"".
|
$sql .= ",\n FOREIGN KEY(\"$field\") REFERENCES \"".$k[0]."\"(\"".
|
||||||
$k[1]."\")";
|
$k[1]."\")";
|
||||||
@@ -711,7 +712,9 @@ class dblayer extends PDO
|
|||||||
throw new Exception (_("PDO Engine not supported in dbLayer", 500));
|
throw new Exception (_("PDO Engine not supported in dbLayer", 500));
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "$sql\n";
|
if ($this->debug)
|
||||||
|
echo "$sql\n";
|
||||||
|
return $this->db->exec($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user