1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- require_once(dirname(__FILE__) . '/CsvController.php');
- class OdbcController
- {
- /** @var PDO */
- private $dbh;
- /** @var PDOStatement */
- private $qh;
- private $exception;
- public function __construct ($dsn)
- {
- try {
- $this->dbh = new PDO($dsn);
- } catch (PDOException $e) {
- $this->exception = $e;
- }
- }
- public function isConnected ()
- {
- return ($this->dbh instanceof PDO);
- }
- public function getLastException ()
- {
- return $this->exception;
- }
- public function executeAndConvertToCsv ($queryString)
- {
- $csvCtrl = new CsvController();
- $this->executeAndImport($queryString, array($csvCtrl, "encodeRow"));
- return $csvCtrl->exportToString();
- }
- public function executeAndImport($queryString, $callback)
- {
- $this->query($queryString);
- return $this->import($callback);
- }
- public function query ($queryString)
- {
- if (!$this->dbh) return false;
- try {
- $this->qh = $this->dbh->query($queryString);
- } catch (PDOException $e) {
- $this->exception = $e;
- return false;
- }
- return true;
- }
- public function fetchAll ()
- {
- if (!$this->qh) return array();
- return $this->qh->fetchAll(PDO::FETCH_NUM);
- }
- public function import ($callback)
- {
- if (!$this->qh) return false;
- $i = 0;
- while ($row = $this->qh->fetch(PDO::FETCH_ASSOC)) {
- call_user_func($callback, $row);
- $i++;
- }
- return $i;
- }
- }
|