[4001] | 1 | <?php
|
---|
| 2 | require_once("db_common.php");
|
---|
| 3 | /*! Implementation of DataWrapper for PDO
|
---|
| 4 |
|
---|
| 5 | if you plan to use it for Oracle - use Oracle connection type instead
|
---|
| 6 | **/
|
---|
| 7 | class PDODBDataWrapper extends DBDataWrapper{
|
---|
| 8 | private $last_result;//!< store result or last operation
|
---|
| 9 |
|
---|
| 10 | public function query($sql){
|
---|
| 11 | LogMaster::log($sql);
|
---|
| 12 |
|
---|
| 13 | $res=$this->connection->query($sql);
|
---|
| 14 | if ($res===false) throw new Exception("PDO - sql execution failed\n".$this->connection->errorInfo());
|
---|
| 15 |
|
---|
| 16 | return new PDOResultSet($res);
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | protected function select_query($select,$from,$where,$sort,$start,$count){
|
---|
| 20 | $sql="SELECT ".$select." FROM ".$from;
|
---|
| 21 | if ($where) $sql.=" WHERE ".$where;
|
---|
| 22 | if ($sort) $sql.=" ORDER BY ".$sort;
|
---|
| 23 | if ($start || $count) {
|
---|
| 24 | if ($this->connection->getAttribute(PDO::ATTR_DRIVER_NAME)=="pgsql")
|
---|
| 25 | $sql.=" OFFSET ".$start." LIMIT ".$count;
|
---|
| 26 | else
|
---|
| 27 | $sql.=" LIMIT ".$start.",".$count;
|
---|
| 28 | }
|
---|
| 29 | return $sql;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | public function get_next($res){
|
---|
| 34 | $data = $res->next();
|
---|
| 35 | return $data;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | protected function get_new_id(){
|
---|
| 39 | return $this->connection->lastInsertId();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public function escape($str){
|
---|
| 43 | $res=$this->connection->quote($str);
|
---|
| 44 | if ($res===false) //not supported by pdo driver
|
---|
| 45 | return str_replace("'","''",$str);
|
---|
| 46 | return substr($res,1,-1);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | class PDOResultSet{
|
---|
| 52 | private $res;
|
---|
| 53 | public function __construct($res){
|
---|
| 54 | $this->res = $res;
|
---|
| 55 | }
|
---|
| 56 | public function next(){
|
---|
| 57 | $data = $this->res->fetch(PDO::FETCH_ASSOC);
|
---|
| 58 | if (!$data){
|
---|
| 59 | $this->res->closeCursor();
|
---|
| 60 | return null;
|
---|
| 61 | }
|
---|
| 62 | return $data;
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | ?> |
---|