home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / EasyPHP-2.0b1-setup.exe / {app} / sqlitemanager / include / sqlite3.class.php < prev    next >
Encoding:
PHP Script  |  2006-04-18  |  9.2 KB  |  313 lines

  1. <?php
  2. include_once dirname(__FILE__) . '/sqlite.class.php';
  3. if( !defined('PDO_ATTR_TIMEOUT') )         define('PDO_ATTR_TIMEOUT',         PDO::ATTR_TIMEOUT);
  4. if( !defined('PDO_FETCH_ASSOC') )         define('PDO_FETCH_ASSOC',         PDO::FETCH_ASSOC);
  5. if( !defined('PDO_FETCH_NUM') )         define('PDO_FETCH_NUM',         PDO::FETCH_NUM);
  6. if( !defined('PDO_FETCH_BOTH') )         define('PDO_FETCH_BOTH',         PDO::FETCH_BOTH);
  7. if( !defined('PDO_ATTR_PERSISTENT') )     define('PDO_ATTR_PERSISTENT',     PDO::ATTR_PERSISTENT);
  8. if( !defined('PDO_ATTR_CASE') )         define('PDO_ATTR_CASE',         PDO::ATTR_CASE);
  9. if( !defined('PDO_CASE_NATURAL') )         define('PDO_CASE_NATURAL',         PDO::CASE_NATURAL);
  10. if( !defined('PDO_ATTR_AUTOCOMMIT') )     define('PDO_ATTR_AUTOCOMMIT',     PDO::ATTR_AUTOCOMMIT);
  11. if( !defined('PDO_ATTR_ERRMODE') )         define('PDO_ATTR_ERRMODE',         PDO::ATTR_ERRMODE);
  12. if( !defined('PDO_ERRMODE_EXCEPTION') ) define('PDO_ERRMODE_EXCEPTION', PDO::ERRMODE_EXCEPTION);
  13. if( !defined('PDO_ERRMODE_SILENT') )     define('PDO_ERRMODE_SILENT',     PDO::ERRMODE_SILENT);
  14.  
  15. if( !defined('SQLITE_BOTH') )     define('SQLITE_BOTH',     PDO_FETCH_BOTH);
  16. if( !defined('SQLITE_ASSOC') )     define('SQLITE_ASSOC',     PDO_FETCH_ASSOC);
  17. if( !defined('SQLITE_NUM') )     define('SQLITE_NUM',     PDO_FETCH_NUM);
  18.  
  19. class sqlite3 extends sqlite {
  20.  
  21.  
  22.     function sqlite3($dbPath) {
  23.          $this->dbVersion = 3;
  24.         if($dbPath == ':memory:') {
  25.             $this->readOnly = false;            
  26.         } else {
  27.             $this->readOnly = !is_writable($dbPath);
  28.         } 
  29.         parent::sqlite($dbPath);
  30.         if(class_exists('PDO') && $this->connect($dbPath)) {
  31.             return $this->connId;
  32.         } else {
  33.             $this->getError();
  34.             return false;
  35.         }
  36.     }
  37.     
  38.     function connect($dbPath) {
  39.         try {
  40.             $user = '';
  41.             $password = '';
  42.             $arrayAttrib = array();
  43.             if($dbPath == ':memory:') $arrayAttrib[PDO_ATTR_PERSISTENT] = true;
  44.             $this->connId = new PDO('sqlite:'.$dbPath, $user, $password, $arrayAttrib); 
  45.             
  46.             $this->connId->setAttribute(PDO_ATTR_CASE, PDO_CASE_NATURAL);
  47.             $this->connId->query("PRAGMA count_changes=1;");
  48.         } catch (PDOException $e) {
  49.             $this->error = true;
  50.             $this->errorMessage = $e->getMessage();
  51.             return false;
  52.         }
  53.         if(DEBUG) {
  54.             $this->connId->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
  55.         } else {
  56.             $this->connId->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_SILENT);
  57.         }
  58.         return $this->connId;
  59.     }
  60.     
  61.     function getError($errorCode = null) {
  62.         if(is_resource($this->resId)) {
  63.             $errorInfo = $this->resId->errorInfo();
  64.         } else {
  65.             $errorInfo = $this->connId->errorInfo();
  66.         }
  67.         if(is_array($errorInfo) && isset($errorInfo[2])) {
  68.             $this->error = true;
  69.             $this->errorMessage = $errorInfo[2];
  70.         } else {
  71.             $this->errorMessage = 'not an error';
  72.         }
  73.         return $this->errorMessage;
  74.     }
  75.     
  76.     function getErrorMessage() {
  77.         return $this->errorMessage;
  78.     }
  79.     
  80.     function escape($string) {
  81.         if(function_exists('sqlite_escape_string')) {
  82.             $res = sqlite_escape_string($string);
  83.         } else {
  84.             $res = str_replace("'", "''", $string);
  85.         }
  86.         return $res;
  87.     }
  88.     
  89.     function query($sqlString, $buffered = true, $assign = true) {
  90.         try {
  91.             if($assign && is_object($this->resId)) $this->resId->closeCursor();
  92.             if(substr(trim($sqlString), -1) != ';') $sqlString .= ';';
  93.             if(DEBUG) $resId = $this->connId->query($sqlString);
  94.             else $resId = @$this->connId->query($sqlString);
  95.             if($assign) $this->resId = $resId;
  96.         } catch(PDOException $e) {
  97.             $this->error = true;
  98.             $this->errorMessage = $e->getMessage();
  99.             return false; 
  100.         }
  101.         $tempErrorInfo = $this->connId->errorInfo();
  102.         if(is_array($tempErrorInfo) && isset($tempErrorInfo[0]) && ($tempErrorInfo[0] != '00000')) {
  103.             $this->error = true;
  104.             $this->errorMessage = $tempErrorInfo[2];
  105.             return false;
  106.         }
  107.         return $resId;
  108.     }
  109.     
  110.     function array_query($sqlString, $result_type=SQLITE_BOTH, $decode_binary=true) {
  111.         try {
  112.             $result_type = $this->convertResultType($result_type);
  113.             $q = $this->query($sqlString); 
  114.             $rows = $q->fetchAll($result_type);
  115.         } catch(PDOException $e) {
  116.             $this->error = true;
  117.             $this->errorMessage = $e->getMessage();
  118.             return false; 
  119.         }
  120.         return $rows;
  121.     }
  122.     
  123.     function num_rows($resId = null) {
  124.         try {
  125.             if($resId == null) $resId = $this->resId;
  126.             if(DEBUG) $out = $resId->rowCount();
  127.             else $out =  @$resId->rowCount();
  128.         } catch(PDOException $e) {
  129.             $this->error = true;
  130.             $this->errorMessage = $e->getMessage();
  131.             return false; 
  132.         }
  133.         return $out;
  134.     }
  135.     
  136.     function fetch_single($resId=null, $result_type=SQLITE_BOTH) {
  137.         try {
  138.             $result_type = $this->convertResultType($result_type);
  139.             if($resId == null) $resId = $this->resId;
  140.             if(DEBUG) $out =  $resId->fetchColumn();
  141.             else $out =  @$resId->fetchColumn();
  142.         } catch(PDOException $e) {
  143.             $this->error = true;
  144.             $this->errorMessage = $e->getMessage();
  145.             return false; 
  146.         }
  147.         return $out;
  148.     }
  149.     
  150.     function fetch_array($resId=null, $result_type=SQLITE_BOTH, $decode_binary=true) {
  151.         try {
  152.             $result_type = $this->convertResultType($result_type);
  153.             if($resId == null) $resId = $this->resId;
  154.             if(DEBUG) $out =  $resId->fetch($result_type);
  155.             else $out =  @$resId->fetch($result_type);
  156.         } catch(PDOException $e) {
  157.             $this->error = true;
  158.             $this->errorMessage = $e->getMessage();
  159.             return false; 
  160.         }
  161.         return $out;
  162.     }
  163.  
  164.     function last_insert_id() {
  165.         try {
  166.             if(DEBUG) $out = $this->connId->lastInsertId();
  167.             else $out = @$this->connId->lastInsertId();
  168.         } catch(PDOException $e) {
  169.             $this->error = true;
  170.             $this->errorMessage = $e->getMessage();
  171.             return false; 
  172.         }
  173.         return $out;    
  174.     }    
  175.     
  176.     function changes($resId = null) {
  177.         try {
  178.             if($resId == null) $resId = $this->resId;            
  179.             if(is_object($resId)) $out = $this->num_rows($resId);
  180.             else $out = 0;
  181.         } catch(PDOException $e) {
  182.             $this->error = true;
  183.             $this->errorMessage = $e->getMessage();
  184.             return false; 
  185.         }
  186.         return $out;
  187.     }
  188.         
  189.     function num_fields($resId = null) {
  190.         try {
  191.             if($resId == null) $resId = $this->resId;
  192.             if(DEBUG) $out =  $resId->columnCount();
  193.             else $out =  @$resId->columnCount();
  194.         } catch(PDOException $e) {
  195.             $this->error = true;
  196.             $this->errorMessage = $e->getMessage();
  197.             return false; 
  198.         }
  199.         return $out;        
  200.     }
  201.  
  202.     function field_name($resId = null, $index) {
  203.         try {
  204.             if($resId == null) $resId = $this->resId;
  205.             if(DEBUG) $tempColInfo = $resId->getColumnMeta($index);
  206.             else $tempColInfo = @$resId->getColumnMeta($index);
  207.         } catch(PDOException $e) {
  208.             $this->error = true;
  209.             $this->errorMessage = $e->getMessage();
  210.             return false; 
  211.         }    
  212.         return $tempColInfo["name"];
  213.     }
  214.  
  215.     function create_function($function_name, $callback, $num_arg=null) {
  216.         try {
  217.             if(method_exists($this->connId, 'sqliteCreateFunction')) {
  218.                 if(DEBUG) return $this->connId->sqliteCreateFunction($function_name, $callback, $num_arg);
  219.                 else return @$this->connId->sqliteCreateFunction($function_name, $callback, $num_arg);
  220.             } else {
  221.                 return false;
  222.             }
  223.         } catch(PDOException $e) {
  224.             $this->error = true;
  225.             $this->errorMessage = $e->getMessage();
  226.             return false; 
  227.         }
  228.     }
  229.     
  230.     function create_aggregate($function_name, $step_func, $finalize_func, $num_args=null) {
  231.         if(method_exists($this->connId, 'sqliteCreateAggregate')) {
  232.             try {
  233.                 if(DEBUG) return $this->connId->sqliteCreateAggregate($function_name, $step_func, $finalize_func, $num_args);
  234.                 else return @$this->connId->sqliteCreateAggregate($function_name, $step_func, $finalize_func, $num_args);
  235.             } catch(PDOException $e) {
  236.                 $this->error = true;
  237.                 $this->errorMessage = $e->getMessage();
  238.                 return false; 
  239.             }
  240.         } else {
  241.             return false;
  242.         }
  243.     }
  244.         
  245.     function sqlite_version() {
  246.         try {
  247.             $query = "SELECT sqlite_version();";
  248.             $res = $this->query($query, true, false);
  249.             $Result = $this->fetch_single($res, SQLITE_NUM);
  250.             if($Result) return $Result;
  251.             else return false;
  252.         } catch(PDOException $e) {
  253.             $this->error = true;
  254.             $this->errorMessage = $e->getMessage();
  255.             return false; 
  256.         }
  257.     }
  258.     
  259.     function close() {
  260.         // set obj to null
  261.     }
  262.  
  263.     function sqlitem_busy_timeout($milliseconds=0) {
  264.         try {
  265.             $this->connId->setAttribute(PDO_ATTR_TIMEOUT, $milliseconds);
  266.             return true;
  267.         } catch(PDOException $e) {
  268.             $this->error = true;
  269.             $this->errorMessage = $e->getMessage();
  270.             return false; 
  271.         }
  272.     }
  273.     
  274.     function convertResultType($type) {
  275.         $fetch_style = PDO_FETCH_BOTH;
  276.         if($type == SQLITE_ASSOC)         $fetch_style = PDO_FETCH_ASSOC;
  277.         elseif($type == SQLITE_NUM)     $fetch_style = PDO_FETCH_NUM;
  278.         elseif($type == SQLITE_BOTH)      $fetch_style = PDO_FETCH_BOTH;    
  279.         return     $fetch_style;
  280.     }
  281.     
  282.     function beginTransaction() {
  283.         try {
  284.             $this->connId->beginTransaction();
  285.         } catch(PDOException $e) {
  286.             $this->error = true;
  287.             $this->errorMessage = $e->getMessage();
  288.             return false; 
  289.         }
  290.     }
  291.     
  292.     function commitTransaction() {
  293.         try {
  294.             $this->connId->commit();
  295.         } catch(PDOException $e) {
  296.             $this->error = true;
  297.             $this->errorMessage = $e->getMessage();
  298.             return false; 
  299.         }
  300.     }
  301.     
  302.     function rollBackTransaction() {
  303.         try {
  304.             $this->connId->rollBack();
  305.         } catch(PDOException $e) {
  306.             $this->error = true;
  307.             $this->errorMessage = $e->getMessage();
  308.             return false; 
  309.         }
  310.     }
  311. }
  312. ?>
  313.