Add support to read in dblayer with order and colmuns displayed
Return the number of deleted rows in delete git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1223 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
24
dblayer.php
24
dblayer.php
@@ -102,17 +102,27 @@ class dblayer extends PDO
|
|||||||
return $this->db->lastInsertId();
|
return $this->db->lastInsertId();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read all the table or only one line defined by the select key, the select
|
/** Read the table content based on a select filter, ordered by order
|
||||||
operator and the associated select value
|
operator and the associated select value
|
||||||
$select = array (array ($key, $val, $operator))
|
- $select = array (array ($key, $val, $operator), ...)
|
||||||
$key=>column, $val=>value to found, $operator=>'LIKE', =...
|
$key=>column, $val=>value to found, $operator=>'LIKE', =...
|
||||||
$order = array (array ($key, $orientation))
|
- $display = array ($col1, $col2...);
|
||||||
|
Columns displayed
|
||||||
|
- $order = array (array ($key, $orientation), ...)
|
||||||
$key=>column, $orientation=ASC/DESC
|
$key=>column, $orientation=ASC/DESC
|
||||||
*/
|
*/
|
||||||
function read ($select=null, $order=null)
|
function read ($select=null, $display=null, $order=null)
|
||||||
{
|
{
|
||||||
if ($this->db === null)
|
if ($this->db === null)
|
||||||
throw new Exception ("Database not connected");
|
throw new Exception ("Database not connected");
|
||||||
|
if ($display !== null)
|
||||||
|
{
|
||||||
|
foreach ($display as $f)
|
||||||
|
{
|
||||||
|
if (!in_array ($f, array_keys ($this->fields)))
|
||||||
|
throw new Exception ("Field $f not allowed");
|
||||||
|
}
|
||||||
|
}
|
||||||
$req = "SELECT ";
|
$req = "SELECT ";
|
||||||
$req .= implode (",", array_keys ($this->fields));
|
$req .= implode (",", array_keys ($this->fields));
|
||||||
$req .= " FROM `".$this->table."`";
|
$req .= " FROM `".$this->table."`";
|
||||||
@@ -155,6 +165,7 @@ class dblayer extends PDO
|
|||||||
$st->bindValue (":".$s[0], $s[1]);
|
$st->bindValue (":".$s[0], $s[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$st->execute ();
|
$st->execute ();
|
||||||
$res = array ();
|
$res = array ();
|
||||||
while ($d = $st->fetch (PDO::FETCH_ASSOC))
|
while ($d = $st->fetch (PDO::FETCH_ASSOC))
|
||||||
@@ -184,6 +195,7 @@ class dblayer extends PDO
|
|||||||
$req .= "$key=:$key";
|
$req .= "$key=:$key";
|
||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$req .= " WHERE $this->primary=:primary";
|
$req .= " WHERE $this->primary=:primary";
|
||||||
if ($this->debug) echo "DEBUG : $req\n";
|
if ($this->debug) echo "DEBUG : $req\n";
|
||||||
$st = $this->db->prepare ($req);
|
$st = $this->db->prepare ($req);
|
||||||
@@ -208,7 +220,8 @@ class dblayer extends PDO
|
|||||||
$st->execute ();
|
$st->execute ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Delete a tuple identified by its primary key */
|
/** Delete a tuple identified by its primary key
|
||||||
|
Return the number of deleted rows (can be 0 !) */
|
||||||
function delete ($deletekey)
|
function delete ($deletekey)
|
||||||
{
|
{
|
||||||
if ($this->db === null)
|
if ($this->db === null)
|
||||||
@@ -221,6 +234,7 @@ class dblayer extends PDO
|
|||||||
var_export ($deletekey, TRUE)."\n";
|
var_export ($deletekey, TRUE)."\n";
|
||||||
$st->bindValue (":primary", $deletekey);
|
$st->bindValue (":primary", $deletekey);
|
||||||
$st->execute ();
|
$st->execute ();
|
||||||
|
return $st->rowCount();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user