<?php
//////////////////////////////////////////////////////////
⁄**
SQLiteDatabaseの拡張と結果セット作成時の例外を投げる
ユーティリティ関数の作成
*⁄
class SQLiteDatabasePlus extends SQLiteDatabase{
private $tablenames;
//////////////////////////////////////////////////////////
//クエリに関連したpublic function
⁄**
関数のオーバーライド
*⁄
public function query($strsql, $type = SQLITE_BOTH){
//SQLiteResult query ( string query [, int result_type
// [, string &error_msg]] )
if ( false === $result =
parent::query($strsql, $type = SQLITE_BOTH)){
//no sql details with last error
throw new SQLiteException (
sqlite_error_string($this->lastError()));
}
return $result;
}
//////////////////////////////////////////////////////////
⁄**
関数のオーバーライド
*⁄
public function unbufferedQuery($strsql,
$type = SQLITE_BOTH){
//SQLiteUnbuffered unbufferedQuery ( string query
// [,int result_type [, string &error_msg]] )
if ( false === $result =
parent::unbufferedQuery($strsql, $type)){
throw new SQLiteException (
sqlite_error_string($this->lastError()));
}
return $result;
}
//////////////////////////////////////////////////////////
⁄**
関数のオーバーライド
*⁄
public function singleQuery($strsql, $first_column =
true, $bin_decode = false){
//array sqlite_single_query ( resource db,
// string query [, bool first_row_only
// [, bool decode_binary]] )
if ( false === $result = parent::singleQuery(
$strsql, $first_column, $bin_decode)){
throw new SQLiteException (
sqlite_error_string($this->lastError()));
}
return $result;
}
//////////////////////////////////////////////////////////
⁄**
関数のオーバーライド
*⁄
public function queryExec($strsql){
//bool queryExec ( string query
// [, string &error_msg] )
if ( !parent::queryExec($strsql)){
throw new SQLiteException (
sqlite_error_string($this->lastError()));
}
return true;
}
//////////////////////////////////////////////////////////
⁄**
関数のオーバーライド
*⁄
public function arrayQuery($strsql,
$type = SQLITE_BOTH, $bin_decode = false ){
//array arrayQuery ( string query [, int result_type
// [, bool decode_binary]] )
if ( false === $result =
parent::arrayQuery($strsql, $result_type,
$bin_decode)){
throw new SQLiteException (
sqlite_error_string($this->lastError()));
}
return $result;
}
//////////////////////////////////////////////////////////
⁄**
ポストされたデータをエスケープして返却する
変数の名前とフィールド名を比較する
*⁄
public function cleanData($post, $tablename){
if (!isset($this->tablenames)){
$this->setTablenames();
}
$this->matchNames($post, $tablename);
//if on remove slashes
if(get_magic_quotes_gpc()){
foreach ($post as $key=>$value){
$post[$key]=stripslashes($value);
}
}
foreach ($post as $key=>$value){
$post[$key] = htmlentities(
sqlite_escape_string($value));
}
return $post;
}
//////////////////////////////////////////////////////////
⁄**
ポストされたフォームの要素の名前がテーブル名と一致するか
をチェックする
*⁄
public function matchNames($post, $tablename){
//check is set
if (!isset($this->tablenames)){
$this->setTablenames();
}
if (count($post) == 0){
throw new SQLiteException("Array not set.");
}
$fields = $this->getFields($tablename);
foreach ($post as $name=>$value){
if (!array_key_exists($name, $fields)){
$message = "No matching column for ".
"$name in table $tablename.";
throw new SQLiteException($message);
}
}
}
//////////////////////////////////////////////////////////
⁄**
データベースのテーブル名を取得する
*⁄
public function getTableNames(){
if (!isset($this->tablenames)){
$this->setTablenames();
}
return $this->tablenames;
}
/////////////////////////////////////////////////////////
⁄**
指定されたテーブルのフィールドの名前と型を取得する
*⁄
public function getFields($tablename){
if (!isset($this->tablenames)){
$this->setTablenames();
}
if (!in_array($tablename, $this->tablenames)){
throw new SQLiteException(
"Table $tablename not in database.");
}
$fieldnames = array();
$sql = "PRAGMA table_info('$tablename')";
$result = $this->unbufferedQuery($sql);
foreach ($result as $row){
$fieldnames[$row['name']] = $row['type'];
}
return $fieldnames;
}
//////////////////////////////////////////////////////////
//private methods
⁄**
private method - テーブル名の配列の初期化
*⁄
private function setTableNames(){
$sql = "SELECT name ".
"FROM sqlite_master ".
"WHERE type = 'table' ".
"OR type = 'view'";
$result = $this->unbufferedQuery($sql);
foreach ($result as $row){
$this->tablenames[] = $row['name'];
}
}
}//end class
?>
|