dblayer : Add verification of format in insert selected by field type
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1621 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
51
dblayer.php
51
dblayer.php
@@ -206,15 +206,60 @@ class dblayer extends PDO
|
|||||||
if (in_array ("not null", $params) && !array_key_exists ($key, $datas))
|
if (in_array ("not null", $params) && !array_key_exists ($key, $datas))
|
||||||
throw new Exception (sprintf (_("Mandatory field '%s' not provided"),
|
throw new Exception (sprintf (_("Mandatory field '%s' not provided"),
|
||||||
$key), 405);
|
$key), 405);
|
||||||
|
if (in_array ("not null", $params) && $datas[$key] === "")
|
||||||
|
throw new Exception (sprintf (_("Mandatory field '%s' is empty"),
|
||||||
|
$key), 405);
|
||||||
if (!array_key_exists ($key, $datas))
|
if (!array_key_exists ($key, $datas))
|
||||||
continue;
|
continue;
|
||||||
// Verify the fields, if $verify is defined, before doing insertion
|
// Verify the fields, if $verify is defined, before doing insertion
|
||||||
$verify = $this->verifyOne ($key, $datas[$key]);
|
$verify = $this->verifyOne ($key, $datas[$key]);
|
||||||
if (is_array ($verify) && count ($verify))
|
if (is_array ($verify) && count ($verify))
|
||||||
throw new Exception ($verify[0]." ".$verify[1]." in ".$key);
|
throw new Exception ($verify[0]." ".$verify[1]." in ".$key);
|
||||||
// TODO : Check for type inconsistencies before create $datasOK
|
// Check for type inconsistencies if the value is provided
|
||||||
if (array_key_exists ($key, $datas))
|
if ($datas[$key] !== "" && $params[0] === "integer")
|
||||||
$datasOK[$key] = $datas[$key];
|
{
|
||||||
|
if (strspn ($datas[$key], "0123456789") !== strlen ($datas[$key]))
|
||||||
|
throw new Exception (sprintf (
|
||||||
|
_("Errors in consistency : '%s' is not an integer"),
|
||||||
|
$key), 405);
|
||||||
|
}
|
||||||
|
elseif ($datas[$key] !== "" && $params[0] === "varchar")
|
||||||
|
{
|
||||||
|
if (! isset ($params[1]))
|
||||||
|
throw new Exception (sprintf (
|
||||||
|
_("The length of varchar field '%s' is not provided"),
|
||||||
|
$key), 500);
|
||||||
|
if (strlen ($datas[$key]) > $params[1])
|
||||||
|
throw new Exception (sprintf (
|
||||||
|
_("Errors in consistency : '%s' data is too long"),
|
||||||
|
$key), 405);
|
||||||
|
}
|
||||||
|
elseif ($datas[$key] !== "" && $params[0] === "datetime")
|
||||||
|
{
|
||||||
|
// The date format must be in ANSI SQL : YYYY-MM-DD HH:MM:SS
|
||||||
|
$d = DateTime::createFromFormat("Y-m-d H:i:s", $datas[$key]);
|
||||||
|
if (!$d || $d->format("Y-m-d H:i:s") !== $datas[$key])
|
||||||
|
throw new Exception (sprintf (
|
||||||
|
_("Incorrect datetime provided for field '%s'"),
|
||||||
|
$key), 500);
|
||||||
|
}
|
||||||
|
elseif ($datas[$key] !== "" && $params[0] === "date")
|
||||||
|
{
|
||||||
|
// The date format must be in ANSI SQL : YYYY-MM-DD
|
||||||
|
$d = DateTime::createFromFormat("Y-m-d", $datas[$key]);
|
||||||
|
if (!$d || $d->format("Y-m-d") !== $datas[$key])
|
||||||
|
throw new Exception (sprintf (
|
||||||
|
_("Incorrect date provided for field '%s'"),
|
||||||
|
$key), 500);
|
||||||
|
}
|
||||||
|
elseif ($datas[$key] !== "")
|
||||||
|
throw new Exception (sprintf (_("Unknown field type for '%s'"), $key),
|
||||||
|
500);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Nothing to do if the value is empty : just save it
|
||||||
|
}
|
||||||
|
$datasOK[$key] = $datas[$key];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for inconsistency
|
// Check for inconsistency
|
||||||
|
|||||||
Reference in New Issue
Block a user