PHP で SQLite3 を 使う
次に 各種 関数です
コレは ほぼWordPressの 丸パクリですね…
でも SQLite3 も Query も 全然 わからなかったので
かなり 時間がかかりました…
<?php //=========================================================================== // コア関数 //=========================================================================== //--------------------------------------------------------------------------- // サニタイズされているか調べる //--------------------------------------------------------------------------- if( !function_exists( 'is_serialized' ) ) { function is_serialized( $data, $strict = true ) { if ( ! is_string( $data ) ) { return false; } $data = trim( $data ); if ( 'N;' == $data ) { return true; } if ( strlen( $data ) < 4 ) { return false; } if ( ':' !== $data[1] ) { return false; } if ( $strict ) { $lastc = substr( $data, -1 ); if ( ';' !== $lastc && '}' !== $lastc ) { return false; } } else { $semicolon = strpos( $data, ';' ); $brace = strpos( $data, '}' ); if ( false === $semicolon && false === $brace ) return false; if ( false !== $semicolon && $semicolon < 3 ) return false; if ( false !== $brace && $brace < 4 ) return false; } $token = $data[0]; switch ( $token ) { case 's' : if ( $strict ) { if ( '"' !== substr( $data, -2, 1 ) ) { return false; } } elseif ( false === strpos( $data, '"' ) ) { return false; } case 'a' : case 'O' : return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data ); case 'b' : case 'i' : case 'd' : $end = $strict ? '$' : ''; return (bool) preg_match( "/^{$token}:[0-9.E-]+;$end/", $data ); } return false; } } //--------------------------------------------------------------------------- // シリアライズ //--------------------------------------------------------------------------- if( !function_exists( 'maybe_serialize' ) ) { function maybe_serialize( $data ) { if ( is_array( $data ) || is_object( $data ) ) return serialize( $data ); if ( is_serialized( $data, false ) ) return serialize( $data ); return $data; } } //--------------------------------------------------------------------------- // アンシリアライズ //--------------------------------------------------------------------------- if( !function_exists( 'maybe_unserialize' ) ) { function maybe_unserialize( $original ) { if ( is_serialized( $original ) ) return @unserialize( $original ); return $original; } } //=========================================================================== // DB 関数 //=========================================================================== //--------------------------------------------------------------------------- // テーブルが有るか調べる //--------------------------------------------------------------------------- if( !function_exists( 'check_table' ) ) { function check_table( $table, $db_name = 'DB' ) { global $GLOBAL; $table = trim( $table ); if( empty( $table ) ) return false; $query = "SELECT * FROM sqlite_master WHERE type='table' AND name='$table'"; $return = $GLOBAL[$db_name]->querySingle( $query ); return $return; } } //--------------------------------------------------------------------------- // オプションを登録する //--------------------------------------------------------------------------- if( !function_exists( 'add_option' ) ) { function add_option( $option, $value = '' ) { global $GLOBAL; $option = trim( $option ); if( empty( $option ) ) return false; if( is_object( $value ) ) $value = clone $value; $serialized_value = maybe_serialize( $value ); $results = check_table( 'option', 'DB_OPTION' ); if( is_null( $results ) ) : $query = "CREATE TABLE option ( key TEXT, value TEXT, PRIMARY KEY( key ) );"; $GLOBAL['DB_OPTION']->exec( $query ); elseif( $results == false ) : return false; endif; $old_value = get_option( $option ); if ( !is_null( $old_value ) ) return false; $query = "INSERT INTO option VALUES ( '" . $option . "', '" . $serialized_value . "' );"; $return = $GLOBAL['DB_OPTION']->exec( $query ); return true; } } //--------------------------------------------------------------------------- // オプションを更新する //--------------------------------------------------------------------------- if( !function_exists( 'update_option' ) ) { function update_option( $option, $value = '' ) { global $GLOBAL; $option = trim( $option ); if( empty( $option ) ) return false; if( is_object( $value ) ) $value = clone $value; $serialized_value = maybe_serialize( $value ); $results = check_table( 'option', 'DB_OPTION' ); if( is_null( $results ) ) : $query = "CREATE TABLE option ( key TEXT, value TEXT, PRIMARY KEY( key ) );"; $GLOBAL['DB_OPTION']->exec( $query ); elseif( $results == false ) : return false; endif; $old_value = get_option( $option ); if ( $value === $old_value ) return false; if ( is_null( $old_value ) ) return add_option( $option, $value ); $query = "UPDATE option SET value = '" . $serialized_value . "' WHERE key = '" . $option . "';"; $return = $GLOBAL['DB_OPTION']->exec( $query ); return true; } } //--------------------------------------------------------------------------- // オプションを取得する //--------------------------------------------------------------------------- if( !function_exists( 'get_option' ) ) { function get_option( $option ) { global $GLOBAL; $option = trim( $option ); if( empty( $option ) ) return false; $results = check_table( 'option', 'DB_OPTION' ); if( is_null( $results ) ) : $query = "CREATE TABLE option ( key TEXT, value TEXT, PRIMARY KEY( key ) );"; $GLOBAL['DB_OPTION']->exec( $query ); elseif( $results == false ) : return false; endif; $query = "SELECT value FROM option WHERE key = '" . $option . "';"; $results = $GLOBAL['DB_OPTION']->querySingle( $query ); $return = maybe_unserialize( $results ); return $return; } }