anvil_php/app/libraries/sqlite_library.php

307 lines
7.9 KiB
PHP

<?php
class Anvil_SQLite {
private $handle;
private $db_file_name;
private $config_db_name = '/db/my_db.sqlite';
private $sel_cols = array();
private $where_arr = array();
private $or_where_arr = array();
private $like_arr = array();
private $or_like_arr = array();
private $order_qry = '';
private $limit = -1;
private $offset = 0;
private $result;
public $last_query='';
function __construct($no_open=FALSE) {
if($no_open===FALSE) {
$this->open_db();
}
}
function open_db() {
$this->db_file_name = APP_ROOT.$this->config_db_name;
$this->handle = new SQLite3($this->db_file_name);
}
function lastId($table) {
$r = $this->handle->query('SELECT MAX(id) FROM '.$table);
$ret = $this->fetch_array($r);
$ret = array_shift($ret);
$ret = $ret['MAX(id)'];
if(empty($ret)) { return 0; }
return $ret;
}
function query($query, $escape=TRUE) {
$do_query = ($escape===TRUE)?$this->handle->escapeString($query):$query;
$this->result = $this->handle->query($do_query);
return $this;//$this->result;
}
function select($colnames) {
$colnames=(is_array($colnames)?$colnames:array($colnames));
$this->sel_cols = $colnames;
return $this;
}
function like(array $like_arr) {
$this->like_arr = $like_arr;
return $this;
}
function likeOr(array $like_arr) {
$this->or_like_arr = $like_arr;
return $this;
}
function where(array $where_arr) {
$this->where_arr = $where_arr;
return $this;
}
function whereOr(array $where_arr) {
$this->or_where_arr = $where_arr;
return $this;
}
function order($colname, $ord='DESC') {
$this->order_qry = ' ORDER BY '.$colname.' '.$ord;
return $this;
}
function limit($num, $offset=0) {
$this->limit = $num;
$this->offset = $offset;
}
function get($tablename) {
// Build the 'SELECT' part of the query
$select_q= 'SELECT ';
$num_sel_cols = count($this->sel_cols);
if($num_sel_cols == 0) {
$select_q.='* ';
} else {
foreach($this->sel_cols as $a_col) {
$select_q.=$a_col;
if(--$num_sel_cols > 0)
$select_q.=', ';
}
}
// Build the 'FROM' part of the query
$from_q = 'FROM '.$tablename.' ';
// Build the 'WHERE' part of the query
$where_q = '';
$bind_arr = array();
$num_where_arr = count($this->where_arr);
if($num_where_arr > 0) {
$where_q = 'WHERE ';
foreach($this->where_arr as $a_col => $a_where) {
if(is_array($a_where)) {
$num_where_subarr = count($a_where);
foreach($a_where as $a) {
$where_q.=$a_col.' = ? ';
$bind_arr[]=$a;
if(--$num_where_subarr > 0) {
$where_q.='AND ';
}
}
} else {
$where_q.=$a_col.' = ? ';
$bind_arr[]=$a_where;
if(--$num_where_arr > 0) {
$where_q.='AND ';
}
}
}
}
$num_where_arr = count($this->or_where_arr);
if($num_where_arr > 0) {
if(empty($where_q)) {
$where_q = 'WHERE ';
} else {
$where_q = 'OR ';
}
foreach($this->where_arr as $a_col => $a_where) {
if(is_array($a_where)) {
$num_where_subarr = count($a_where);
foreach($a_where as $a) {
$where_q.=$a_col.' = ? ';
$bind_arr[]=$a;
if(--$num_where_subarr > 0) {
$where_q.='OR ';
}
}
} else {
$where_q.=$a_col.' = ? ';
$bind_arr[]=$a_where;
if(--$num_where_arr > 0) {
$where_q.='OR ';
}
}
}
}
$num_where_arr = count($this->like_arr);
if($num_where_arr > 0) {
if(empty($where_q)) {
$where_q = 'WHERE ';
} else {
$where_q = 'AND ';
}
foreach($this->like_arr as $a_col => $a_like) {
if(is_array($a_like)) {
$num_like_subarr = count($a_like);
foreach($a_like as $a) {
$where_q.=$a_col.' LIKE ? ';
$bind_arr[]='%'.$a.'%';
if(--$num_like_subarr > 0) {
$where_q.='AND ';
}
}
} else {
$where_q.=$a_col.' LIKE ? ';
$bind_arr[]='%'.$a_like.'%';
if(--$num_like_arr > 0) {
$where_q.='AND ';
}
}
}
}
$num_where_arr = count($this->or_like_arr);
if($num_where_arr > 0) {
if(empty($where_q)) {
$where_q = 'WHERE ';
} else {
$where_q = 'OR ';
}
foreach($this->or_like_arr as $a_col => $a_like) {
if(is_array($a_like)) {
$num_like_subarr = count($a_like);
foreach($a_like as $a) {
$where_q.=$a_col.' LIKE ? ';
$bind_arr[]='%'.$a.'%';
if(--$num_like_subarr > 0) {
$where_q.='OR ';
}
}
} else {
$where_q.=$a_col.' LIKE ? ';
$bind_arr[]='%'.$a_like.'%';
if(--$num_like_arr > 0) {
$where_q.='OR ';
}
}
}
}
$st = $this->handle->prepare($select_q.$from_q.$where_q);
$t_cnt = 1;
foreach($bind_arr as $val) {
$st->bindParam($t_cnt, $val);
$t_cnt++;
}
$order_q = $this->order_qry;
$limit_q = '';
if($this->limit > -1) {
$limit_q = ' LIMIT '.$this->limit.', '.$this->offset;
$this->limit = -1;
$this->offset = 0;
}
$this->where_arr = array();
$this->order_qry = '';
$this->last_query = $select_q.$from_q.$where_q.$order_q.$limit_q;
$this->result = $st->execute();
return $this;
}
function fetch_array($res=NULL) {
$res=(isset($res)?$res:$this->result);
$i = 0;
$ret_arr = array();
while($resx = $res->fetchArray(SQLITE3_ASSOC)) {
$ret_arr[] = $resx;
}
return $ret_arr;
}
function insert($tablename, array $val_arr) {
$ins_q = 'INSERT INTO '.$tablename;
$ins_col1 = '(';
$ins_col2 = ' VALUES (';
$num_cols = count($val_arr);
if($num_cols <= 0) { return false; }
foreach($val_arr as $col_n => $val) {
$ins_col1 .= $col_n;
$ins_col2 .= ':'.$col_n;
if(--$num_cols > 0) {
$ins_col1 .= ", ";
$ins_col2 .= ", ";
}
}
$ins_col1 .= ")";
$ins_col2 .= ")";
$st = $this->handle->prepare($ins_q.$ins_col1.$ins_col2);
foreach($val_arr as $col_n => $val) {
$st->bindValue(':'.$col_n, $val);
}
$this->last_query = $ins_q.$ins_col1.$ins_col2;
$this->result = $st->execute();
return $this;
}
function update($tablename, array $val_arr) {
$upd_q = 'UPDATE '.$tablename;
// Build the 'SET' part of the query
$set_q = ' SET ';
$num_set_arr = count($val_arr);
$bind_set_arr = array();
if($num_set_arr > 0) {
foreach($val_arr as $a_col => $a_set) {
if(is_array($a_set) && isset($a_set['val'])) {
if(isset($a_set['query']) && $a_set['query'] == 'raw') {
$set_q.=$a_col.' = '.$a_set['val'];
} else {
$set_q.=$a_col.' = ?';
$bind_set_arr[]=$a_set['val'];
}
if(--$num_set_arr > 0) {
$set_q.='AND ';
}
} else {
$set_q.=$a_col.' = ?';
$bind_set_arr[]=$a_set;
}
}
}
// Build the 'WHERE' part of the query
$where_q = '';
$num_where_arr = count($this->where_arr);
$bind_arr = array();
if($num_where_arr > 0) {
$where_q = ' WHERE ';
foreach($this->where_arr as $a_col => $a_where) {
$where_q.=$a_col.' = ? ';
$bind_arr[]=$a_where;
if(--$num_where_arr > 0) {
$where_q.='AND ';
}
}
}
$st = $this->handle->prepare($upd_q.$set_q.$where_q);
$t_cnt = 1;
foreach($bind_set_arr as $val) {
$st->bindParam($t_cnt++, $val);
}
foreach($bind_arr as $val) {
$st->bindParam($t_cnt++, $val);
}
$this->last_query = $upd_q.$set_q.$where_q;
$this->result = $st->execute();
return $this;
}
}