Mercurial > hg > pnewss
view pdo.php @ 12:7917bd536187 draft
Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Wed, 12 Jun 2013 22:22:07 +0200 |
parents | 005339a1b2ce |
children |
line wrap: on
line source
<?php /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of Ivo Smits.*/ class PDOWrapper { private $numQueries = 0; private $pdo = NULL; public static function connectMysql($host, $database, $user = NULL, $pass = NULL, $initialcommand = NULL) { $options = array(); if ($initialcommand !== NULL) $options[PDO::MYSQL_ATTR_INIT_COMMAND] = $initialcommand; return new self('mysql:dbname='.$database.';host='.$host, $user, $pass, $options); //"SET NAMES utf8,time_zone = '+0:00'" } public function __construct($dsn, $user = NULL, $pass = NULL, $options = NULL) { try { $this->pdo = new PDO($dsn, $user, $pass, $options); } catch (PDOException $e) { throw new Exception('Could not connect to database: '.$e->getMessage()); } $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } public function __call($name, $arguments) { if ($name == 'eval') { $q = array_shift($arguments); return $this->evalField($q, $arguments); } } private function queryi($query, $args = NULL) { $this->numQueries++; if ($args === NULL) { //$stmt = $db_pdo->query($query); $stmt = $this->pdo->prepare($query); $stmt->execute(); } else { if (!is_array($args)) $args = array($args); $stmt = $this->pdo->prepare($query); $stmt->execute($args); } return $stmt; } function insert($query, $args = NULL) { $this->queryi($query, $args)->closeCursor(); return $this->pdo->lastInsertId(); } function update($query, $args = NULL) { global $db_pdo; $stmt = $this->queryi($query, $args); $cnt = $stmt->rowCount(); $stmt->closeCursor(); return $cnt; } function evalField($query, $args = NULL) { $stmt = $this->queryi($query, $args); $ret = $stmt->fetchColumn(); $stmt->closeCursor(); return $ret; } function evalRow($query, $args = NULL) { $stmt = $this->queryi($query, $args); $ret = $stmt->fetch(PDO::FETCH_NUM); $stmt->closeCursor(); return $ret; } function evalRowAssoc($query, $args = NULL) { $stmt = $this->queryi($query, $args); $ret = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); return $ret; } function evalAllAssoc($query, $args = NULL) { $stmt = $this->queryi($query, $args); $ret = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); return $ret; } function evalAllKVP($query, $args = NULL) { $stmt = $this->queryi($query, $args); $ret = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); $stmt->closeCursor(); return $ret; } function evalColumn($query, $args = NULL) { $stmt = $this->queryi($query, $args); $ret = $stmt->fetchAll(PDO::FETCH_COLUMN); $stmt->closeCursor(); return $ret; } }