OSDN Git Service

change: EPG取得並列化が可能かどうかを判定するように変更
[epgrec/epgrec.git] / DBRecord.class.php
1 <?php
2 include_once( 'config.php' );
3 include_once( 'Settings.class.php' );
4
5 class DBRecord {
6         protected $table;
7         protected $settings;
8         
9         protected $dbh;
10         public $id;
11         
12     function __construct( $table, $property = null, $value = null ) {
13                 $this->settings = Settings::factory();
14                 
15                 $this->table = $this->settings->tbl_prefix.$table;
16                 
17                 $this->dbh = @mysql_connect( $this->settings->db_host , $this->settings->db_user, $this->settings->db_pass );
18                 if( $this->dbh === FALSE ) throw new exception( "construct:データベースに接続できない" );
19                 
20                 $sqlstr = "use ".$this->settings->db_name;
21                 $res = $this->__query($sqlstr);
22                 if( $res === false ) throw new exception("construct: " . $sqlstr );
23                 $sqlstr = "set NAMES utf8";
24                 $res = $this->__query($sqlstr);
25
26                 if( ($property == null) || ($value == null) ) {
27                         // レコードを特定する要素が指定されない場合はid=0として空のオブジェクトを作成する
28                         $this->id = 0;
29                 }
30                 else {
31                         $sqlstr = "SELECT * FROM ".$this->table.
32                                     " WHERE ".mysql_real_escape_string( $property ).
33                                       "='".mysql_real_escape_string( $value )."'";
34                         
35                         $res = $this->__query( $sqlstr );
36                         $arr = mysql_fetch_array( $res , MYSQL_ASSOC );
37                         if( $arr === FALSE ) throw new exception( "construct:無効な行" );
38                         // 最初にヒットした行のidを使用する
39                         $this->id = $arr['id'];
40                 }
41                 
42                 return;
43         }
44         
45         function createTable( $tblstring ) {
46                 $sqlstr = "use ".$this->settings->db_name;
47                 $res = $this->__query($sqlstr);
48                 if( $res === false ) throw new exception("createTable: " . $sqlstr );
49                 $sqlstr = "CREATE TABLE IF NOT EXISTS ".$this->table." (" .$tblstring.") DEFAULT CHARACTER SET 'utf8'";
50                 $result = $this->__query( $sqlstr );
51                 if( $result === false ) throw new exception( "createTable:テーブル作成失敗" );
52         }
53         
54         protected function __query( $sqlstr ) {
55                 $res = @mysql_query( $sqlstr, $this->dbh );
56                 if( $res === FALSE ) throw new exception( "__query:DBクエリ失敗:".$sqlstr );
57                 return $res;
58         }
59         
60         function fetch_array( $property , $value, $options = null ) {
61                 $retval = array();
62                 
63                 $sqlstr = "SELECT * FROM ".$this->table.
64                             " WHERE ".mysql_real_escape_string( $property ).
65                               "='".mysql_real_escape_string( $value )."'";
66                 
67                 if( $options != null ) {
68                         $sqlstr .= "AND ".$options;
69                 }
70                 $res = $this->__query( $sqlstr );
71                 while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
72                         array_push( $retval, $row );
73                 }
74                 
75                 return $retval;
76         }
77         
78         function __set( $property, $value ) {
79                 if( $property == "id" ) throw new exception( "set:idの変更は不可" );
80                 // id = 0なら空の新規レコード作成
81                 if( $this->id == 0 ) {
82                         $sqlstr = "INSERT INTO ".$this->table." VALUES ( )";
83                         $res = $this->__query( $sqlstr );
84                         $this->id = mysql_insert_id();
85                 }
86                 $sqlstr = "UPDATE ".$this->table." SET ".
87                              mysql_real_escape_string($property)."='".
88                              mysql_real_escape_string($value)."' WHERE id='".$this->id."'";
89                 $res = $this->__query( $sqlstr );
90                 if( $res == FALSE )  throw new exception("set:セット失敗" );
91         }
92         
93         function __get( $property ) {
94                 if( $this->id == 0 ) throw new exception( "get:無効なid" );
95                 if( $property == "id" ) return $this->id;
96                 
97                 $sqlstr = "SELECT ".mysql_real_escape_string($property)." FROM ".$this->table." WHERE id='".$this->id."'";
98                 $res = $this->__query($sqlstr);
99                 $arr = mysql_fetch_row( $res );
100                 if( $arr === FALSE ) throw new exception( "get:".$property."は存在しない" );
101                 
102                 return stripslashes($arr[0]);
103         }
104         
105         function delete() {
106                 if( $this->id == 0 ) throw new exception( "delete:無効なid" );
107                 
108                 $sqlstr = "DELETE FROM ".$this->table." WHERE id='".$this->id."'";
109                 $this->__query( $sqlstr );
110                 $this->id = 0;
111         }
112         
113         // countを実行する
114         static function countRecords( $table, $options = "" ) {
115                 try{
116                         $tbl = new self( $table );
117                         $sqlstr = "SELECT COUNT(*) FROM " . $tbl->table ." " . $options;
118                         $result = $tbl->__query( $sqlstr );
119                 }
120                 catch( Exception $e ) {
121                         throw $e;
122                 }
123                 if( $result === false ) throw new exception("COUNT失敗");
124                 $retval = mysql_fetch_row( $result );
125                 return $retval[0];
126         }
127         
128         // DBRecordオブジェクトを返すstaticなメソッド
129         static function createRecords( $table, $options = "" ) {
130                 $retval = array();
131                 $arr = array();
132                 try{
133                         $tbl = new self( $table );
134                         $sqlstr = "SELECT * FROM ".$tbl->table." " .$options;
135                         $result = $tbl->__query( $sqlstr );
136                 }
137                 catch( Exception $e ) {
138                         throw $e;
139                 }
140                 if( $result === false ) throw new exception("レコードが存在しません");
141                 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
142                         array_push( $retval, new self( $table,  'id', $row['id'] ) );
143                 }
144                 return $retval;
145         }
146         
147         function __destruct() {
148                 $this->id = 0;
149         }
150 }
151 ?>