je voudrais créer une classe d'accès à des sgbd et faisant des traitements standards (ex :CRUD ) à l'aide de PDO
je voudrais une structure comme ça
class BaseDBAccess {
function Insert($sql);
}
class PostgreSQLDBAccess extends BaseDBAccess {
function Insert($sql)
{
//postgresql specific code
}
}
class MySQLDBAccess extends BaseDBAccess {
function Insert($sql)
{
//MySQL specific code
}
}
que j'utilise dans une autre classe comme ça :class Serializer
{
$dbAccess = new MySQLDBAccess(.....)
function SaveSomethingIntoDB($something)
{
$dbAccess-> Insert($something)
}
}
Pour la première classe j'ai trouvé ça :
<?php
class BaseDBAccess
{
private $db;
/**
*
* Set variables
*
*/
public function __set($name, $value)
{
switch($name)
{
case 'username':
$this->username = $value;
break;
case 'password':
$this->password = $value;
break;
case 'dsn':
$this->dsn = $value;
break;
default:
throw new Exception("$name is invalid");
}
}
/**
*
* @check variables have default value
*
*/
public function __isset($name)
{
switch($name)
{
case 'username':
$this->username = null;
break;
case 'password':
$this->password = null;
break;
}
}
/**
*
* @Connect to the database and set the error mode to Exception
*
* @Throws PDOException on failure
*
*/
public function conn()
{
isset($this->username);
isset($this->password);
if (!$this->db instanceof PDO)
{
try{
$this->db = new PDO($this->dsn, $this->username, $this->password);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(Exception $e){
echo 'Erreur : '.$e->getMessage().'<br />';
echo 'N° : '.$e->getCode();
}
}
}
/***
*
* @select values from table
*
* @access public
*
* @param string $table The name of the table
*
* @param string $fieldname
*
* @param string $id
*
* @return array on success or throw PDOException on failure
*
*/
public function dbSelect($table, $fieldname=null, $id=null)
{
$this->conn();
$sql = "SELECT * FROM `$table` WHERE `$fieldname`=:id";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
/**
*
* @execute a raw query
*
* @access public
*
* @param string $sql
*
* @return array
*
*/
public function rawSelect($sql)
{
$this->conn();
return $this->db->query($sql);
}
/**
*
* @run a raw query
*
* @param string The query to run
*
*/
public function rawQuery($sql)
{
$this->conn();
$this->db->query($sql);
}
/**
*
* @Insert a value into a table
*
* @acces public
*
* @param string $table
*
* @param array $values
*
* @return int The last Insert Id on success or throw PDOexeption on failure
*
*/
public function dbInsert($table, $values)
{
$this->conn();
/*** snarg the field names from the first array member ***/
$fieldnames = array_keys($values[0]);
/*** now build the query ***/
$size = sizeof($fieldnames);
$i = 1;
$sql = "INSERT INTO $table";
/*** set the field names ***/
$fields = '( ' . implode(' ,', $fieldnames) . ' )';
/*** set the placeholders ***/
$bound = '(:' . implode(', :', $fieldnames) . ' )';
/*** put the query together ***/
$sql .= $fields.' VALUES '.$bound;
/*** prepare and execute ***/
$stmt = $this->db->prepare($sql);
foreach($values as $vals)
{
$stmt->execute($vals);
}
}
/**
*
* @Update a value in a table
*
* @access public
*
* @param string $table
*
* @param string $fieldname, The field to be updated
*
* @param string $value The new value
*
* @param string $pk The primary key
*
* @param string $id The id
*
* @throws PDOException on failure
*
*/
public function dbUpdate($table, $fieldname, $value, $pk, $id)
{
$this->conn();
$sql = "UPDATE `$table` SET `$fieldname`='{$value}' WHERE `$pk` = :id";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_STR);
$stmt->execute();
}
/**
*
* @Delete a record from a table
*
* @access public
*
* @param string $table
*
* @param string $fieldname
*
* @param string $id
*
* @throws PDOexception on failure
*
*/
public function dbDelete($table, $fieldname, $id)
{
$this->conn();
$sql = "DELETE FROM `$table` WHERE `$fieldname` = :id";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_STR);
$stmt->execute();
}
} /*** end of class ***/
?>
Pour les 3 autres classes j'ai tenter de faire comme ça mais je pense que je suis mal parti : <?php
class MysqlDBAccess extends BaseDBAccess
{
public function __construct()
{
}
public function MysqlConnectDb()
{
$MysqlConnect = new BaseDBAccess();
/*** The DSN ***/
$MysqlConnect->dsn = "mysql:dbname=mabase;host=localhost";
/*** MySQL username and password ***/
$MysqlConnect->username = 'username';
$MysqlConnect->password = 'password';
}
public function Mysqlinsert()
{
$Connect = new BaseDBAccess();
/*** array of values to insert ***/
$values = array(
array('animal_name'=>'bruce', 'animal_type'=>'dingo'),
array('animal_name'=>'bruce', 'animal_type'=>'wombat'),
array('animal_name'=>'bruce', 'animal_type'=>'kiwi'),
array('animal_name'=>'bruce', 'animal_type'=>'kangaroo')
);
/*** insert the array of values ***/
$MysqlConnect->dbInsert('animals', $values);
}
public function exec_nquery($p_query)
{
try{
$stmt = $this->dbh->prepare($p_query);
$this->dbh->beginTransaction();
$stmt->execute();
$this->dbh->commit();
}catch (PDOException $e){
echo "__warning\nErreur [DB] => " . $e->getMessage();
}
}
}
/*** end of class ***/
?>
class PostgresqlDBAccess extends BaseDBAccess
{
public function __construct()
{
}
public function PgsqlConnectDb()
{
$PgSQLConnect = new BaseDBAccess();
/*** The DSN ***/
$PgSQLConnect->dsn = "pgsql:dbname=mabase;host=localhost";
/*** MySQL username and password ***/
$PgSQLConnect->username = 'username';
$PgSQLConnect->password = 'password';
}
public function Mysqlinsert()
{
$PgSQLConnect = new BaseDBAccess();
/*** array of values to insert ***/
$values = array(
array('animal_name'=>'bruce', 'animal_type'=>'dingo'),
array('animal_name'=>'bruce', 'animal_type'=>'wombat'),
array('animal_name'=>'bruce', 'animal_type'=>'kiwi'),
array('animal_name'=>'bruce', 'animal_type'=>'kangaroo')
);
/*** insert the array of values ***/
$PgSQLConnect->dbInsert('evostream', $values);
}
}
class Serializer
{
var $Pgsqlconnet;
var $urlXML;
public function OpenPgsqlConnect($_Pgsqlconnet)
{
$this->Pgsqlconnet = $_Pgsqlconnet;
$Postgres = new PostgresqlDBAccess();
$this->Pgsqlconnet = $Postgres->PgsqlConnectDb();
}
public function parseXml($urlXMLtoparse)
{
$this->urlXML = $urlXMLtoparse;
//Charging data of the XML
$xml = simplexml_load_file($this->urlXML);
if($xml)
{
return $xml;
}
}
}
Etant débutant dans le concept POO en général je suis un peu bloqué pour la suiteSi quelqun a des idées ,tuto ou url
je suis preneurs
merci d'avance pour votre aide