comparison DBRecord.class.php @ 159:66eabfc1b118

revoke all whitespace modifications
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Wed, 31 Mar 2010 06:42:51 +0900
parents 49145003e6a3
children
comparison
equal deleted inserted replaced
158:be39a7efcc87 159:66eabfc1b118
3 include_once( 'Settings.class.php' ); 3 include_once( 'Settings.class.php' );
4 4
5 class DBRecord { 5 class DBRecord {
6 protected $table; 6 protected $table;
7 protected $settings; 7 protected $settings;
8 8
9 protected $dbh; 9 protected $dbh;
10 public $id; 10 public $id;
11 11
12 function __construct( $table, $property = null, $value = null ) { 12 function __construct( $table, $property = null, $value = null ) {
13 $this->settings = Settings::factory(); 13 $this->settings = Settings::factory();
14 14
15 $this->table = $this->settings->tbl_prefix.$table; 15 $this->table = $this->settings->tbl_prefix.$table;
16 16
17 $this->dbh = @mysql_connect( $this->settings->db_host , $this->settings->db_user, $this->settings->db_pass ); 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:データベースに接続できない" ); 18 if( $this->dbh === FALSE ) throw new exception( "construct:データベースに接続できない" );
19 19
20 $sqlstr = "use ".$this->settings->db_name; 20 $sqlstr = "use ".$this->settings->db_name;
21 $res = $this->__query($sqlstr); 21 $res = $this->__query($sqlstr);
22 if( $res === false ) throw new exception("construct: " . $sqlstr ); 22 if( $res === false ) throw new exception("construct: " . $sqlstr );
23 $sqlstr = "set NAMES utf8"; 23 $sqlstr = "set NAMES utf8";
24 $res = $this->__query($sqlstr); 24 $res = $this->__query($sqlstr);
29 } 29 }
30 else { 30 else {
31 $sqlstr = "SELECT * FROM ".$this->table. 31 $sqlstr = "SELECT * FROM ".$this->table.
32 " WHERE ".mysql_real_escape_string( $property ). 32 " WHERE ".mysql_real_escape_string( $property ).
33 "='".mysql_real_escape_string( $value )."'"; 33 "='".mysql_real_escape_string( $value )."'";
34 34
35 $res = $this->__query( $sqlstr ); 35 $res = $this->__query( $sqlstr );
36 $arr = mysql_fetch_array( $res , MYSQL_ASSOC ); 36 $arr = mysql_fetch_array( $res , MYSQL_ASSOC );
37 if( $arr === FALSE ) throw new exception( "construct:無効な行" ); 37 if( $arr === FALSE ) throw new exception( "construct:無効な行" );
38 // 最初にヒットした行のidを使用する 38 // 最初にヒットした行のidを使用する
39 $this->id = $arr['id']; 39 $this->id = $arr['id'];
40 } 40 }
41 41
42 return; 42 return;
43 } 43 }
44 44
45 function createTable( $tblstring ) { 45 function createTable( $tblstring ) {
46 $sqlstr = "use ".$this->settings->db_name; 46 $sqlstr = "use ".$this->settings->db_name;
47 $res = $this->__query($sqlstr); 47 $res = $this->__query($sqlstr);
48 if( $res === false ) throw new exception("createTable: " . $sqlstr ); 48 if( $res === false ) throw new exception("createTable: " . $sqlstr );
49 $sqlstr = "CREATE TABLE IF NOT EXISTS ".$this->table." (" .$tblstring.") DEFAULT CHARACTER SET 'utf8'"; 49 $sqlstr = "CREATE TABLE IF NOT EXISTS ".$this->table." (" .$tblstring.") DEFAULT CHARACTER SET 'utf8'";
50 $result = $this->__query( $sqlstr ); 50 $result = $this->__query( $sqlstr );
51 if( $result === false ) throw new exception( "createTable:テーブル作成失敗" ); 51 if( $result === false ) throw new exception( "createTable:テーブル作成失敗" );
52 } 52 }
53 53
54 protected function __query( $sqlstr ) { 54 protected function __query( $sqlstr ) {
55 $res = @mysql_query( $sqlstr, $this->dbh ); 55 $res = @mysql_query( $sqlstr, $this->dbh );
56 if( $res === FALSE ) throw new exception( "__query:DBクエリ失敗:".$sqlstr ); 56 if( $res === FALSE ) throw new exception( "__query:DBクエリ失敗:".$sqlstr );
57 return $res; 57 return $res;
58 } 58 }
59 59
60 function fetch_array( $property , $value, $options = null ) { 60 function fetch_array( $property , $value, $options = null ) {
61 $retval = array(); 61 $retval = array();
62 62
63 $sqlstr = "SELECT * FROM ".$this->table. 63 $sqlstr = "SELECT * FROM ".$this->table.
64 " WHERE ".mysql_real_escape_string( $property ). 64 " WHERE ".mysql_real_escape_string( $property ).
65 "='".mysql_real_escape_string( $value )."'"; 65 "='".mysql_real_escape_string( $value )."'";
66 66
67 if( $options != null ) { 67 if( $options != null ) {
68 $sqlstr .= "AND ".$options; 68 $sqlstr .= "AND ".$options;
69 } 69 }
70 $res = $this->__query( $sqlstr ); 70 $res = $this->__query( $sqlstr );
71 while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) { 71 while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
72 array_push( $retval, $row ); 72 array_push( $retval, $row );
73 } 73 }
74 74
75 return $retval; 75 return $retval;
76 } 76 }
77 77
78 function __set( $property, $value ) { 78 function __set( $property, $value ) {
79 if( $property == "id" ) throw new exception( "set:idの変更は不可" ); 79 if( $property == "id" ) throw new exception( "set:idの変更は不可" );
80 // id = 0なら空の新規レコード作成 80 // id = 0なら空の新規レコード作成
81 if( $this->id == 0 ) { 81 if( $this->id == 0 ) {
82 $sqlstr = "INSERT INTO ".$this->table." VALUES ( )"; 82 $sqlstr = "INSERT INTO ".$this->table." VALUES ( )";
87 mysql_real_escape_string($property)."='". 87 mysql_real_escape_string($property)."='".
88 mysql_real_escape_string($value)."' WHERE id='".$this->id."'"; 88 mysql_real_escape_string($value)."' WHERE id='".$this->id."'";
89 $res = $this->__query( $sqlstr ); 89 $res = $this->__query( $sqlstr );
90 if( $res == FALSE ) throw new exception("set:セット失敗" ); 90 if( $res == FALSE ) throw new exception("set:セット失敗" );
91 } 91 }
92 92
93 function __get( $property ) { 93 function __get( $property ) {
94 if( $this->id == 0 ) throw new exception( "get:無効なid" ); 94 if( $this->id == 0 ) throw new exception( "get:無効なid" );
95 if( $property == "id" ) return $this->id; 95 if( $property == "id" ) return $this->id;
96 96
97 $sqlstr = "SELECT ".mysql_real_escape_string($property)." FROM ".$this->table." WHERE id='".$this->id."'"; 97 $sqlstr = "SELECT ".mysql_real_escape_string($property)." FROM ".$this->table." WHERE id='".$this->id."'";
98 $res = $this->__query($sqlstr); 98 $res = $this->__query($sqlstr);
99 $arr = mysql_fetch_row( $res ); 99 $arr = mysql_fetch_row( $res );
100 if( $arr === FALSE ) throw new exception( "get:".$property."は存在しない" ); 100 if( $arr === FALSE ) throw new exception( "get:".$property."は存在しない" );
101 101
102 return stripslashes($arr[0]); 102 return stripslashes($arr[0]);
103 } 103 }
104 104
105 function delete() { 105 function delete() {
106 if( $this->id == 0 ) throw new exception( "delete:無効なid" ); 106 if( $this->id == 0 ) throw new exception( "delete:無効なid" );
107 107
108 $sqlstr = "DELETE FROM ".$this->table." WHERE id='".$this->id."'"; 108 $sqlstr = "DELETE FROM ".$this->table." WHERE id='".$this->id."'";
109 $this->__query( $sqlstr ); 109 $this->__query( $sqlstr );
110 $this->id = 0; 110 $this->id = 0;
111 } 111 }
112 112
113 // countを実行する 113 // countを実行する
114 static function countRecords( $table, $options = "" ) { 114 static function countRecords( $table, $options = "" ) {
115 try{ 115 try{
116 $tbl = new self( $table ); 116 $tbl = new self( $table );
117 $sqlstr = "SELECT COUNT(*) FROM " . $tbl->table ." " . $options; 117 $sqlstr = "SELECT COUNT(*) FROM " . $tbl->table ." " . $options;
122 } 122 }
123 if( $result === false ) throw new exception("COUNT失敗"); 123 if( $result === false ) throw new exception("COUNT失敗");
124 $retval = mysql_fetch_row( $result ); 124 $retval = mysql_fetch_row( $result );
125 return $retval[0]; 125 return $retval[0];
126 } 126 }
127 127
128 // DBRecordオブジェクトを返すstaticなメソッド 128 // DBRecordオブジェクトを返すstaticなメソッド
129 static function createRecords( $table, $options = "" ) { 129 static function createRecords( $table, $options = "" ) {
130 $retval = array(); 130 $retval = array();
131 $arr = array(); 131 $arr = array();
132 try{ 132 try{
141 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 141 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
142 array_push( $retval, new self( $table, 'id', $row['id'] ) ); 142 array_push( $retval, new self( $table, 'id', $row['id'] ) );
143 } 143 }
144 return $retval; 144 return $retval;
145 } 145 }
146 146
147 function __destruct() { 147 function __destruct() {
148 $this->id = 0; 148 $this->id = 0;
149 } 149 }
150 } 150 }
151 ?> 151 ?>