include_once( 'Settings.class.php' );
class DBRecord {
- protected $table;
- protected $settings;
+ protected $__table;
+ protected $__settings;
- protected $dbh;
- protected $record_data;
- protected $f_dirty;
+ protected $__record_data;
+ protected $__f_dirty;
- public $id;
+ public $__id;
+
+ protected static $__ref_counter = 0; // 参照カウンタ
+ protected static $__dbh = false; // 接続リソース
function __construct( $table, $property = null, $value = null ) {
- $this->f_dirty = false;
- $this->record_data = false;
-
- $this->settings = Settings::factory();
- $this->table = $this->settings->tbl_prefix.$table;
+ $this->__settings = Settings::factory();
+ $this->__f_dirty = false;
+ $this->__record_data = false;
+ $this->__table = $this->__settings->tbl_prefix.$table;
- $this->dbh = @mysql_connect( $this->settings->db_host , $this->settings->db_user, $this->settings->db_pass );
- if( $this->dbh === FALSE ) throw new exception( "construct:データベースに接続できない" );
+ if( self::$__ref_counter == 0 ) {
+ self::$__dbh = @mysql_connect( $this->__settings->db_host , $this->__settings->db_user, $this->__settings->db_pass );
+ if( self::$__dbh === false ) throw new exception( "construct:データベースに接続できない" );
+ $sqlstr = "use ".$this->__settings->db_name;
+ $res = $this->__query($sqlstr);
+ if( $res === false ) throw new exception("construct: " . $sqlstr );
+ $sqlstr = "set NAMES utf8";
+ $res = $this->__query($sqlstr);
+
+ self::$__ref_counter++;
+ }
- $sqlstr = "use ".$this->settings->db_name;
- $res = $this->__query($sqlstr);
- if( $res === false ) throw new exception("construct: " . $sqlstr );
- $sqlstr = "set NAMES utf8";
- $res = $this->__query($sqlstr);
-
if( ($property == null) || ($value == null) ) {
// レコードを特定する要素が指定されない場合はid=0として空のオブジェクトを作成する
- $this->id = 0;
+ $this->__id = 0;
}
else {
- $sqlstr = "SELECT * FROM ".$this->table.
+ $sqlstr = "SELECT * FROM ".$this->__table.
" WHERE ".mysql_real_escape_string( $property ).
"='".mysql_real_escape_string( $value )."'";
$res = $this->__query( $sqlstr );
- $this->record_data = mysql_fetch_array( $res , MYSQL_ASSOC );
- if( $this->record_data === false ) throw new exception( "construct:".$table."に".$property."=".$value."はありません" );
+ $this->__record_data = mysql_fetch_array( $res , MYSQL_ASSOC );
+ if( $this->__record_data === false ) throw new exception( "construct:".$this->__table."に".$property."=".$value."はありません" );
// 最初にヒットした行のidを使用する
- $this->id = $this->record_data['id'];
+ $this->__id = $this->__record_data['id'];
}
return;
}
function createTable( $tblstring ) {
- $sqlstr = "use ".$this->settings->db_name;
+ $sqlstr = "use ".$this->__settings->db_name;
$res = $this->__query($sqlstr);
if( $res === false ) throw new exception("createTable: " . $sqlstr );
- $sqlstr = "CREATE TABLE IF NOT EXISTS ".$this->table." (" .$tblstring.") DEFAULT CHARACTER SET 'utf8'";
+ $sqlstr = "CREATE TABLE IF NOT EXISTS ".$this->__table." (" .$tblstring.") DEFAULT CHARACTER SET 'utf8'";
$result = $this->__query( $sqlstr );
if( $result === false ) throw new exception( "createTable:テーブル作成失敗" );
}
protected function __query( $sqlstr ) {
- $res = @mysql_query( $sqlstr, $this->dbh );
- if( $res === FALSE ) throw new exception( "__query:DBクエリ失敗:".$sqlstr );
+ if( self::$__dbh === false ) throw new exception( "__query:DBに接続されていない" );
+
+ $res = @mysql_query( $sqlstr, self::$__dbh );
+ if( $res === false ) throw new exception( "__query:DBクエリ失敗:".$sqlstr );
return $res;
}
function fetch_array( $property , $value, $options = null ) {
$retval = array();
- $sqlstr = "SELECT * FROM ".$this->table.
+ $sqlstr = "SELECT * FROM ".$this->__table.
" WHERE ".mysql_real_escape_string( $property ).
"='".mysql_real_escape_string( $value )."'";
function __set( $property, $value ) {
if( $property === "id" ) throw new exception( "set:idの変更は不可" );
// id = 0なら空の新規レコード作成
- if( $this->id == 0 ) {
- $sqlstr = "INSERT INTO ".$this->table." VALUES ( )";
+ if( $this->__id == 0 ) {
+ $sqlstr = "INSERT INTO ".$this->__table." VALUES ( )";
$res = $this->__query( $sqlstr );
- $this->id = mysql_insert_id();
+ $this->__id = mysql_insert_id();
- // $this->record_data読み出し
- $sqlstr = "SELECT * FROM ".$this->table.
- " WHERE id = '".$this->id."'";
+ // $this->__record_data読み出し
+ $sqlstr = "SELECT * FROM ".$this->__table.
+ " WHERE id = '".$this->__id."'";
$res = $this->__query( $sqlstr );
- $this->record_data = mysql_fetch_array( $res , MYSQL_ASSOC );
+ $this->__record_data = mysql_fetch_array( $res , MYSQL_ASSOC );
}
- if( $this->record_data === false ) throw new exception("set: DBの異常?" );
+ if( $this->__record_data === false ) throw new exception("set: DBの異常?" );
- if( array_key_exists( $property, $this->record_data ) ) {
- $this->record_data[$property] = mysql_real_escape_string($value);
- $this->f_dirty = true;
+ if( array_key_exists( $property, $this->__record_data ) ) {
+ $this->__record_data[$property] = mysql_real_escape_string($value);
+ $this->__f_dirty = true;
}
else {
throw new exception("set:$property はありません" );
}
function __get( $property ) {
- if( $this->id == 0 ) throw new exception( "get:無効なid" );
- if( $property === "id" ) return $this->id;
- if( $this->record_data === false ) throw new exception( "get: 無効なレコード" );
- if( ! array_key_exists( $property, $this->record_data ) ) throw new exception( "get: $propertyは存在しません" );
+ if( $this->__id == 0 ) throw new exception( "get:無効なid" );
+ if( $property === "id" ) return $this->__id;
+ if( $this->__record_data === false ) throw new exception( "get: 無効なレコード" );
+ if( ! array_key_exists( $property, $this->__record_data ) ) throw new exception( "get: $propertyは存在しません" );
- return stripslashes($this->record_data[$property]);
+ return stripslashes($this->__record_data[$property]);
}
function delete() {
- if( $this->id == 0 ) throw new exception( "delete:無効なid" );
+ if( $this->__id == 0 ) throw new exception( "delete:無効なid" );
- $sqlstr = "DELETE FROM ".$this->table." WHERE id='".$this->id."'";
+ $sqlstr = "DELETE FROM ".$this->__table." WHERE id='".$this->__id."'";
$this->__query( $sqlstr );
- $this->id = 0;
- $this->record_data = false;
- $this->f_dirty = false;
+ $this->__id = 0;
+ $this->__record_data = false;
+ $this->__f_dirty = false;
}
- function close() {
- if( $this->id != 0 ) {
- if( $this->f_dirty ) {
- $sqlstr = "UPDATE ".$this->table." SET";
- foreach( $this->record_data as $property => $value ) {
+ function update() {
+ if( $this->__id != 0 ) {
+ if( $this->__f_dirty ) {
+ $sqlstr = "UPDATE ".$this->__table." SET";
+ foreach( $this->__record_data as $property => $value ) {
if( $property === "id" ) continue;
$sqlstr .= " ".$property." = '".$value."',";
}
$sqlstr = rtrim($sqlstr, "," );
- $sqlstr .= " WHERE id = '".$this->id."'";
+ $sqlstr .= " WHERE id = '".$this->__id."'";
$res = $this->__query($sqlstr);
if( $res === false ) throw new exception( "close: アップデート失敗" );
}
- $this->id = 0;
- $this->f_dirty = false;
- $this->record_data = false;
+ $this->__f_dirty = false;
}
}
static function countRecords( $table, $options = "" ) {
try{
$tbl = new self( $table );
- $sqlstr = "SELECT COUNT(*) FROM " . $tbl->table ." " . $options;
+ $sqlstr = "SELECT COUNT(*) FROM " . $tbl->__table ." " . $options;
$result = $tbl->__query( $sqlstr );
}
catch( Exception $e ) {
$arr = array();
try{
$tbl = new self( $table );
- $sqlstr = "SELECT * FROM ".$tbl->table." " .$options;
+ $sqlstr = "SELECT * FROM ".$tbl->__table." " .$options;
$result = $tbl->__query( $sqlstr );
}
catch( Exception $e ) {
return $retval;
}
+ // デストラクタ
function __destruct() {
// 呼び忘れに対応
- if( $this->id != 0 ) {
- $this->close();
+ if( $this->__id != 0 ) {
+ $this->update();
}
+ $this->__id = 0;
+ $this->__record_data = false;
}
}
?>