download this file: class.sqlite.dbs.php view text/plain: class.sqlite.dbs.php file encoding: UTF-8 [goback]
<?php
##
## this file name is 'class.sqlite.dbs.php'
##
## sqlite object
##
## [author]
##  - Chilbong Kim<san2(at)linuxchannel.net>
##
## [changes]
##  - 2010.05.07 : bug fixed for sqlite_seek()
##  - 2004.05.01 : new build
##
## [references]
##
## [example]
##  require_once 'class.sqlite.dbs.php';
##  $sqlite = new sqlite; // none debug mode
##  //$sqlite->connect('localhost','user','passwd');
##  $sqlite->select_db('dbname');
##  $result = $sqlite->query('....');
##  ...
##  $sqlite->close();
##

class sqlite
{
  var 
$debug FALSE;    // boolean, 1(true), 0(false), debug mode
  
var $link;        // resource link_identifier
  
var $dbname '';    // string, selected database name
  
var $datadir '.';
  var 
$mode '0666';

  
## set debug mode
  ##
  
function &sqlite($debug=0)
  {
    
$this->debug $debug;
  }

  
## sqlite connect
  ##
  ## return :
  ##    $resource    resource id
  ##
  
function &connect($host$user$passwd$dbname='')
  {
    
define('__SQLSTART__',microtime());

    if(
$dbname)
    {
        
$this->dbname $dbname;
        
$errorstr '';
        
$this->link sqlite_open($this->datadir.'/'.$dbname,$this->mode,&$errorstr);
        
$this->error(sqlite_last_error($this->link),'connect() -- :'.$errorstr);
    }

    return 
$this->link;
  }

  
## sqlite persistent connection to sqlite server
  ##
  
function &pconnect($host$user$passwd$dbname='')
  {
    
define('__SQLSTART__',microtime());

    if(
$dbname)
    {
        
$this->dbname $dbname;
        
$errorstr '';
        
$this->link = @sqlite_popen($this->datadir.'/'.$dbname,$this->mode,&$errorstr);
        
$this->error(sqlite_last_error($this->link),'pconnect() -- :'.$errorstr);
    }

    return 
$this->link;
  }

  
## sqlite close
  ##
  
function &close($link='')
  {
    
$link $link $link $this->link;
    
$result = @sqlite_close($link);
    
$this->error(sqlite_last_error($this->link),'close()');

    
define('__SQLEND__',microtime());

    return 
$result// boolean
  
}

  
## sqlite result
  ##
  
function &result($result$row$field)
  {
    
sqlite_seek($result,$row);
    
$mixed = @sqlite_fetch_array($result);
    
$this->error(sqlite_last_error($this->link),'result()');
    
sqlite_rewind($result);

    return 
$mixed["$field"];
  }

  
## sqlite select db
  ##
  
function &select_db($dbname)
  {
    
$this->dbname $dbname;

    if(!
$this->link)
    {
        
$this->connect(0,0,0,$dbname);
    }

    return (boolean)
$this->link// boolean
  
}

  
## sqlite query
  ##
  
function &query($sql)
  {
    
$result = @sqlite_query($this->link,$sql);
    
$this->error(sqlite_last_error($this->link),'query()');

    return 
$result;
  }

  
## sqlite query
  ##
  
function &db_query($dbname$sql)
  {
    
$orig['link'] = $this->link;
    
$orig['dbname'] = $this->dbname;
    
$this->connect(0,0,0,$dbname);
    
$result $this->query($sql);
    
$this->dbname $orig['dbname'];
    
$this->link $orig['link'];

    return 
$result;
  }

  
## sqlite free result
  ##
  
function &free_result($result)
  {
    return 
TRUE;
  }

  
## alias
  ##
  
function &freeresult($result)
  {
    return 
TRUE;
  }

  
## sqlite fetch array
  ##
  
function &fetch_array($result)
  {
    
$array = @sqlite_fetch_array($result,SQLITE_BOTH);
    
$this->error(sqlite_last_error($this->link),'fetch_array()');

    return 
$array;
  }

  
## sqlite fetch row
  ##
  
function &fetch_row($result)
  {
    
$array = @sqlite_fetch_array($result,SQLITE_NUM);
    
$this->error(sqlite_last_error($this->link),'fetch_row()');

    return 
$array;
  }

  
## sqlite fetch assoc
  ##
  
function &fetch_assoc($result)
  {
    
$array = @sqlite_fetch_array($result,SQLITE_ASSOC);
    
$this->error(sqlite_last_error($this->link),'fetch_assoc()');

    return 
$array;
  }

  
## sqlite fetch object
  ##
  
function &fetch_object($result)
  {
    return 
FALSE;
  }

  
## sqlite list dbs
  ##
  
function &list_dbs($link='')
  {
    
$hdle = @opendir($this->datadir);
    if(!
$hdle) return FALSE;

    while(
$file = @readdir($hdle))
    {
        if(
preg_match('/^[.]+\.db/i',$file))
        {
            
$result[] = $file;
        }
    }
    
closedir($hdle);

    return 
$result// in further
  
}

  
## sqlite data seek
  ##
  
function &data_seek($result$number)
  {
    
$boolean = @sqlite_seek($result,$number);
    
$this->error(sqlite_last_error($this->link),sqlite_error(),'data_seek()');

    return 
$boolean;
  }

  
## alias
  ##
  
function &seek($result$number)
  {
    return 
$this->data_Seek($result,$number);
  }

  
## sqlite list tables
  ##
  
function &list_tables($dbname='')
  {
    
$sql =    'SELECT name FROM sqlite_master WHERE type="table" UNION ALL '.
        
'SELECT name FROM sqlite_temp_master WHERE type="table" ORDER BY name';

    
$result $this->query($sql);

    return 
$result;
  }

  
## sqlite get list tables
  ##
  
function &get_list_tables($dbname='')
  {
    
$result $this->list_tables($dbname);

    if(
$result)
    {
        while(
$table $this->fetch_row($result))
        { 
$tables[] = $table[0]; }

        
//$this->free_result($result);
    
}

    return 
$tables// array
  
}

  
## sqlite check table exist
  ##
  
function &table_exists($table$dbname='')
  {
    
$tables $this->get_list_tables($dbname);
    if(
$tables && is_array($tables)) $check = @in_array($table,$tables);
    unset(
$tables);

    return (boolean)
$check// boolean
  
}

  
## sqlite num rows
  ##
  
function &num_rows($result)
  {
    
$rows = @sqlite_num_rows($result) + 0;
    
$this->error(sqlite_last_error($this->link),sqlite_error(),'num_rows()');

    return 
$rows// integer
  
}

  
## get count of rows
  ##
  
function &get_count($table$where='')
  {
    if(
$where$sql ' WHERE 'preg_replace('/^\s*WHERE\s*/i','',$where);

    
$sql 'SELECT COUNT(*) FROM '.$table.$sql;
    
$result $this->query($sql);
    
$rows sqlite_fetch_single($result) + 0;
    
//$this->free_result($result);

    
return $rows// integer
  
}

  
## alias
  ##
  
function &get_rows($table$where='')
  {
    return 
$this->get_count($table,$where);
  }

  
## sqlite error report
  ##
  
function &error($errno$func)
  {
    if(
$this->debug && $errno)
    {
        echo 
'ERRNO('.$errno .') : '.sqlite_error_string($error).' : '.
        
$func.' in '.__FILE__."<BR>\n";
    }
  }
// end of class

?>