EDB
[ class tree: EDB ] [ index: EDB ] [ all elements ]

Source for file EDB_SQLITE2.php

Documentation is available at EDB_SQLITE2.php

  1. <?php
  2. /**
  3.  * Project: EDB_SQLITE2 :: SQLITE2 abstraction layer
  4.  * File:    EDB/EDB_SQLITE2.php
  5.  *
  6.  * The EDB_SQLITE2 class is sqlite2 abstraction layer that used internally
  7.  * on EDB class.
  8.  *
  9.  * @category    Database
  10.  * @package     EDB
  11.  * @subpackage  EDB_ABSTRACT
  12.  * @author      JoungKyun.Kim <http://oops.org>
  13.  * @copyright   (c) 2018, JoungKyun.Kim
  14.  * @license     BSD License
  15.  * @version     $Id$
  16.  * @link        http://pear.oops.org/package/EDB
  17.  * @filesource
  18.  */
  19.  
  20. /**
  21.  * SQLite2 engine for EDB API
  22.  *
  23.  * This class support abstracttion DB layer for SQLite3 Engine
  24.  *
  25.  * @package     EDB
  26.  */
  27. Class EDB_SQLITE2 extends EDB_Common {
  28.     // {{{ properties
  29.     /**#@+
  30.      * @access private
  31.      */
  32.     /**
  33.      * db handler of EDB_SQLITE2 class
  34.      * @var    object 
  35.      */
  36.     private $db;
  37.     /**
  38.      * The number of query parameter
  39.      * @var    integer 
  40.      */
  41.     private $pno 0;
  42.     /**#@-*/
  43.     // }}}
  44.  
  45.     // {{{ (object) EDB_SQLITE2::__construct ($path[, $mode = 0666])
  46.     /** 
  47.      * Instantiates an EDB_SQLITE2 object and opens an SQLite 3 database
  48.      *
  49.      * For examples:
  50.      * <code>
  51.      * $db = new EDB_SQLITE2 ('sqlite2:///path/file.db');
  52.      * $db = new EDB_SQLITE2 ('sqlite2:///path/file.db', 0666)
  53.      * </code>
  54.      *
  55.      * If you add prefix 'p~' before host, you can connect with persistent
  56.      * connection.
  57.      *
  58.      * For Examples:
  59.      * <code>
  60.      * $db = new EDB_SQLTE2 ('sqlite2://p~/path/file.db');
  61.      * </code>
  62.      *
  63.      * @access public
  64.      * @return EDB_SQLITE2 
  65.      * @param  string  $path  sqlite2 database file
  66.      * @param  integer $mode  The mode of the file. Intended to be used to open
  67.      *                         the database in read-only mode. Presently, this
  68.      *                         parameter is ignored by the sqlite library. The
  69.      *                         default value for mode is the octal value 0666
  70.      *                         and this is the recommended value.
  71.      */
  72.     function __construct ({
  73.         if extension_loaded ('sqlite') )
  74.             throw new myException ('sqlite extension is not loaded on PHP!'E_USER_ERROR);
  75.  
  76.         try {
  77.             $_argv func_get_args ();
  78.             $argv is_array ($_argv[0]$_argv[0$_argv;;
  79.  
  80.             $o = (object) array (
  81.                 'path' => preg_replace ('!^sqlite2://!'''$argv[0]),
  82.                 'mode' => $argv[2],
  83.             );
  84.  
  85.             if $o->flag )
  86.                 $o->mode 0666;
  87.  
  88.             // for persistent connection
  89.             if preg_match ('!^p~!'$o->path) ) {
  90.                 $o->path preg_replace ('!^p~!'''$o->path);
  91.                 $func 'sqlite_popen';
  92.             else
  93.                 $func 'sqlite_open';
  94.  
  95.             $this->db $func ($o->path$o->mode$error);
  96.         catch Exception $e {
  97.             if $error )
  98.                 throw new myException ($error$e->getCode()$e);
  99.             else
  100.                 throw new myException ($e->getMessage ()$e->getCode()$e);
  101.         }
  102.     }
  103.     // }}}
  104.  
  105.     // {{{ (string) EDB_SQLITE2::get_charset (void)
  106.     /** 
  107.      * Get character set of current database
  108.      *
  109.      * This method is not allow on SQLite2 Engine
  110.      *
  111.      * @access public
  112.      * @return string Current character set name
  113.      */
  114.     function get_charset ({
  115.         if function_exists ('sqlite_libencoding') )
  116.             return sqlite_libencoding ();
  117.         else
  118.             throw new myException ('Unsupported method on SQLITE2 engine'E_USER_ERROR);
  119.     }
  120.     // }}}
  121.  
  122.     // {{{ (bool) EDB_SQLITE2::set_charset ($charset)
  123.     /** 
  124.      * Set character set of current database
  125.      *
  126.      * This method is not allow on SQLite2 Engine, and always
  127.      * returns true
  128.      *
  129.      * @access public
  130.      * @return bool   always returns true
  131.      * @param  string $char name of character set that supported from database
  132.      */
  133.     function set_charset ({
  134.         return true;
  135.     }
  136.     // }}}
  137.  
  138.     // {{{ (string) EDB_SQLITE2::escape ($string)
  139.     /** 
  140.      * Escape special characters in a string for use in an SQL statement
  141.      *
  142.      * @access public
  143.      * @return string 
  144.      * @param  string  The string that is to be escaped.
  145.      */
  146.     function escape ($string{
  147.         return sqlite_escape_string ($string);
  148.     }
  149.     // }}}
  150.  
  151.     // {{{ (int) EDB_SQLITE2::query ($query, $param_type, $param1, $param2 ...)
  152.     /** 
  153.      * Performs a query on the database
  154.      *
  155.      * Executes an SQL query, returning number of affected rows
  156.      *
  157.      * @access public
  158.      * @return integer The number of affected rows or false. If is not delete/insert/update
  159.      *                  query, always returns 0.
  160.      * @param  string $query  The query strings
  161.      * @param  string $type   (optional) Bind parameter type. See also
  162.      *  {@link http://www.php.net/manual/en/sqlite3stmt.bindparam.php SQLite3Stmt::bindparam()}.
  163.      *  <code>
  164.      *  i => integer SQLITE2_INTEGER
  165.      *  d => double  SQLITE2_FLOAT
  166.      *  s => string  SQLITE2_TEXT
  167.      *  b => blob    SQLITE2_BLOB
  168.      *  n => null    SQLITE2_NULL
  169.      *  </code>
  170.      * @param  mixed  $param1 (optional) Bind parameter 1
  171.      * @param  mixed  $param2,... (optional) Bind parameter 2 ..
  172.      */
  173.     function query ({
  174.         $_argv func_get_args ();
  175.         $argv is_array ($_argv[0]$_argv[0$_argv;;
  176.  
  177.         try {
  178.             $sql array_shift ($argv);
  179.             $this->pno count ($argv$this->get_param_number ($sql0;
  180.  
  181.             if $this->free )
  182.                 $this->free_result ();
  183.  
  184.             // store query in log variable
  185.             $this->queryLog[$sql;
  186.  
  187.             /*
  188.              * For no bind query
  189.              */
  190.             if $this->pno++ == )
  191.                 return $this->no_bind_query ($sql);
  192.  
  193.             /*
  194.              * For bind query
  195.              */
  196.             return $this->bind_query ($sql$argv);
  197.         catch Exception $e {
  198.             throw new myException ($e->getMessage ()$e->getCode()$e);
  199.             return false;
  200.         }
  201.     }
  202.     // }}}
  203.  
  204.     // {{{ (int) EDB_SQLITE2::lastId (void)
  205.     /**
  206.      * 가장 마지막 입력 row ID를 반환한다.
  207.      *
  208.      * @since  2.0.4
  209.      * @access public
  210.      * @return int|false
  211.      */
  212.     function lastId ({
  213.         return sqlite_last_insert_rowid ($this->db);
  214.     }
  215.     // }}}
  216.  
  217.     // {{{ (bool) EDB_SQLITE2::seek ($offset)
  218.     /**
  219.      * Adjusts the result pointer to an arbitrary row in the result
  220.      *
  221.      * @access public
  222.      * @return boolean 
  223.      * @param  integer Must be between zero and the total number of rows minus one
  224.      */
  225.     function seek ($offset{
  226.         if is_resource ($this->result) )
  227.             return false;
  228.  
  229.         try {
  230.             return sqlite_seek ($this->result$offset);
  231.         catch Exception $e {
  232.             throw new myException ($e->getMessage ()$e->getCode()$e);
  233.             return false;
  234.         }
  235.     }
  236.     // }}}
  237.  
  238.     // {{{ (object) EDB_SQLITE2::fetch (void)
  239.     /**
  240.      * Fetch a result row as an associative object
  241.      *
  242.      * @access public
  243.      * @return object The object of fetched a result row or false
  244.      * @param  boolean (optional) fetch 수행 후 result를 free한다.
  245.      *                  (기본값: false) EDB >= 2.0.3
  246.      */
  247.     function fetch ($free false{
  248.         try {
  249.             $r sqlite_fetch_object ($this->result);
  250.             if $free )
  251.                 $this->free_result ();
  252.             return $r;
  253.         catch Exception $e {
  254.             throw new myException ($e->getMessage ()$e->getCode()$e);
  255.             return false;
  256.         }
  257.     }
  258.     // }}}
  259.  
  260.     // {{{ (array) EDB_SQLITE2::fetch_all ($free = true)
  261.     /**
  262.      * Fetch all result rows as an associative object
  263.      *
  264.      * @access public
  265.      * @return array The fetched result rows
  266.      * @param  boolean (optional) free result set after fetch.
  267.      *                  Defaluts is true.
  268.      */
  269.     function fetch_all ($free true{
  270.         try {
  271.             $rows sqlite_fetch_all ($this->resultSQLITE_ASSOC);
  272.             if $free )
  273.                 $this->free_result ();
  274.  
  275.             return $rows;
  276.         catch Exception $e {
  277.             throw new myException ($e->getMessage ()$e->getCode()$e);
  278.             return array ();
  279.         }
  280.     }
  281.     // }}}
  282.  
  283.     // {{{ (bool) EDB_SQLITE2::free_result (void)
  284.     /**
  285.      * Frees stored result memory for the given statement handle
  286.      *
  287.      * @access public
  288.      * @return boolean always returns true
  289.      */
  290.     function free_result ({
  291.         if $this->free return true;
  292.         $this->free = false;
  293.  
  294.         if isset ($this->result) )
  295.             unset ($this->result);
  296.  
  297.         $this->result = null;
  298.  
  299.         return true;
  300.     }
  301.     // }}}
  302.  
  303.     // {{{ (string) EDB_SQLITE2::field_name ($index)
  304.     /**
  305.      * Get the name of the specified field in a result
  306.      *
  307.      * Given the ordinal column number, field_index, sqlite_field_name()
  308.      * returns the name of that field in the result set result.
  309.      *
  310.      * @access public
  311.      * @return string|false
  312.      * @param  integer The numerical field offset. The index starts at 0.
  313.      * @see http://php.net/manual/en/function.sqlite-field-name.php sqlite_field_name()
  314.      */
  315.     function field_name ($index{
  316.         try {
  317.             if is_resource ($this->result) )
  318.                 return false;
  319.             return sqlite_field_name ($this->result$index);
  320.         catch Exception $e {
  321.             throw new myException ($e->getMessage ()$e->getCode()$e);
  322.             return false;
  323.         }
  324.     }
  325.     // }}}
  326.  
  327.     // {{{ (string) EDB_SQLITE2::field_type ($index, $table)
  328.     /**
  329.      * Get the type of the specified field in a result
  330.      *
  331.      * @access public
  332.      * @return string|false
  333.      * @param  integer The numerical field offset. The index starts at 0.
  334.      * @param  string  name of table
  335.      * @see http://php.net/manual/en/function.sqlite-fetch-column-types.php sqlite_fetch_column_types()
  336.      */
  337.     function field_type ($index$table{
  338.         try {
  339.             if ( ($r sqlite_fetch_column_types ($table$this->dbSQLITE_NUM)) === false )
  340.                 return false;
  341.  
  342.             return $r[$index];
  343.         catch Exception $e {
  344.             throw new myException ($e->getMessage ()$e->getCode()$e);
  345.             return false;
  346.         }
  347.     }
  348.     // }}}
  349.  
  350.     // {{{ (int) EDB_SQLITE2::num_fields (void)
  351.     /**
  352.      * Returns the number of fields in the result set.
  353.      *
  354.      * @access public
  355.      * @return integer|false
  356.      */
  357.     function num_fields ({
  358.         try {
  359.             if is_resource ($this->result) )
  360.                 return false;
  361.  
  362.             return sqlite_num_fields ($this->result);
  363.         catch Exception $e {
  364.             throw new myException ($e->getMessage ()$e->getCode()$e);
  365.             return false;
  366.         }
  367.     }
  368.     // }}}
  369.  
  370.     // {{{ (void) EDB_SQLITE2::trstart (void)
  371.     /**
  372.      * DB transaction 을 시작한다.
  373.      *
  374.      * @access public
  375.      * @return void 
  376.      */
  377.     function trstart ({
  378.         $this->db->query ('BEGIN TRANSACTION');
  379.     }
  380.     // }}}
  381.  
  382.     // {{{ (void) EDB_SQLITE2::trend ($v)
  383.     /**
  384.      * DB transaction 을 종료한다.
  385.      *
  386.      * @access public
  387.      * @return void 
  388.      * @param bool false일경우 rollback을 수행한다.
  389.      */
  390.     function trend ($v true{
  391.         $sql ($v === false'ROLLBACK' 'COMMIT';
  392.         $this->db->query ($sql ' TRANSACTION');
  393.     }
  394.     // }}}
  395.  
  396.     // {{{ (void) EDB_SQLITE2::close (void)
  397.     /**
  398.      * Close the db handle
  399.      *
  400.      * @access public
  401.      * @return void 
  402.      */
  403.     function close ({
  404.         if is_resource ($this->db) ) {
  405.             sqlite_close ($this->db);
  406.             unset ($this->db);
  407.         }
  408.     }
  409.     // }}}
  410.  
  411.     /*
  412.      * Priavte functions
  413.      */
  414.     // {{{ private (int) EDB_SQLITE2::no_bind_query ($sql)
  415.     /** 
  416.      * Performs a query on the database
  417.      *
  418.      * @access private
  419.      * @return integer The number of affected rows or false
  420.      * @param  string  The query strings
  421.      */
  422.     private function no_bind_query ($sql{
  423.         try {
  424.             $this->result = sqlite_query ($this->db$sqlSQLITE_ASSOC);
  425.             if is_resource ($this->result) ) {
  426.                 $this->free = false;
  427.                 throw new myException (sqlite_last_error ($this->db)E_USER_WARNING);
  428.                 return false;
  429.             }
  430.         catch Exception $e {
  431.             $this->free = false;
  432.             throw new myException ($e->getMessage ()$e->getCode()$e);
  433.             return false;
  434.         }
  435.  
  436.         $this->switch_freemark ();
  437.  
  438.         if preg_match ('/^(update|insert|delete|replace)/i'trim ($sql)) )
  439.             return sqlite_changes ($this->db);
  440.         if strncasecmp ('create|drop'trim ($sql)6) )
  441.             return 1;
  442.  
  443.         return sqlite_num_rows ($this->result);
  444.     }
  445.     // }}}
  446.  
  447.     // {{{ private (int) EDB_SQLITE2::bind_query ($sql, $parameters)
  448.     /** 
  449.      * Performs a bind query on the database
  450.      *
  451.      * @access private
  452.      * @return integer The number of affected rows or false
  453.      * @param  string  The query strings
  454.      * @param  array   (optional) Bind parameter type
  455.      */
  456.     private function bind_query ($sql$params{
  457.         if $this->pno != count ($params|| $this->check_param ($params=== false {
  458.             throw new myException (
  459.                 'Number of elements in query doesn\'t match number of bind variables',
  460.                 E_USER_WARNING
  461.             );
  462.             return false;
  463.         }
  464.  
  465.         $parano strlen ($params[0]);
  466.         for $i=0$j=1$i<$parano$i++$j++ {
  467.             switch ($params[0][$i]{
  468.                 case 'c' :
  469.                 case 'b' :
  470.                     if is_object ($params[$j]) )
  471.                         $params[$j$params[$j]->data;
  472.                     $params[$j$this->escape ($params[$j]);
  473.                     break;
  474.             }
  475.         }
  476.  
  477.         $query $this->bind_param ($sql$params);
  478.         return $this->no_bind_query ($query);
  479.     }
  480.     // }}}
  481.  
  482.     function __destruct ({
  483.         try {
  484.             @$this->free_result ();
  485.             $this->close ();
  486.         catch Exception $e }
  487.     }
  488. }
  489.  
  490. /*
  491.  * Local variables:
  492.  * tab-width: 4
  493.  * c-basic-offset: 4
  494.  * End:
  495.  * vim: set filetype=php noet sw=4 ts=4 fdm=marker:
  496.  * vim600: noet sw=4 ts=4 fdm=marker
  497.  * vim<600: noet sw=4 ts=4
  498.  */
  499. ?>

Documentation generated on Tue, 14 May 2019 01:59:55 +0900 by phpDocumentor 1.4.4