Mercurial > epgrec.yaz
annotate DBRecord.class.php @ 158:be39a7efcc87
merged with upstream
| author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
|---|---|
| date | Wed, 31 Mar 2010 06:16:04 +0900 |
| parents | 49145003e6a3 |
| children | 66eabfc1b118 |
| rev | line source |
|---|---|
| 1 | 1 <?php |
| 2 include_once( 'config.php' ); | |
|
32
01a8fdc0cebb
testing: Web base setting page.
Sushi-k <epgrec@park.mda.or.jp>
parents:
17
diff
changeset
|
3 include_once( 'Settings.class.php' ); |
| 1 | 4 |
| 5 class DBRecord { | |
| 6 protected $table; | |
|
39
8965ef108821
change: modify all scripts for web base setting.
Sushi-k <epgrec@park.mda.or.jp>
parents:
37
diff
changeset
|
7 protected $settings; |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
8 |
| 1 | 9 protected $dbh; |
| 10 public $id; | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
11 |
| 1 | 12 function __construct( $table, $property = null, $value = null ) { |
|
39
8965ef108821
change: modify all scripts for web base setting.
Sushi-k <epgrec@park.mda.or.jp>
parents:
37
diff
changeset
|
13 $this->settings = Settings::factory(); |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
14 |
|
39
8965ef108821
change: modify all scripts for web base setting.
Sushi-k <epgrec@park.mda.or.jp>
parents:
37
diff
changeset
|
15 $this->table = $this->settings->tbl_prefix.$table; |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
16 |
|
39
8965ef108821
change: modify all scripts for web base setting.
Sushi-k <epgrec@park.mda.or.jp>
parents:
37
diff
changeset
|
17 $this->dbh = @mysql_connect( $this->settings->db_host , $this->settings->db_user, $this->settings->db_pass ); |
| 1 | 18 if( $this->dbh === FALSE ) throw new exception( "construct:データベースに接続できない" ); |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
19 |
|
39
8965ef108821
change: modify all scripts for web base setting.
Sushi-k <epgrec@park.mda.or.jp>
parents:
37
diff
changeset
|
20 $sqlstr = "use ".$this->settings->db_name; |
| 1 | 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 )."'"; | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
34 |
| 1 | 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 } | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
41 |
| 1 | 42 return; |
| 43 } | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
44 |
| 1 | 45 function createTable( $tblstring ) { |
|
39
8965ef108821
change: modify all scripts for web base setting.
Sushi-k <epgrec@park.mda.or.jp>
parents:
37
diff
changeset
|
46 $sqlstr = "use ".$this->settings->db_name; |
| 1 | 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 } | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
53 |
| 1 | 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 } | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
59 |
| 1 | 60 function fetch_array( $property , $value, $options = null ) { |
| 61 $retval = array(); | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
62 |
| 1 | 63 $sqlstr = "SELECT * FROM ".$this->table. |
| 64 " WHERE ".mysql_real_escape_string( $property ). | |
|
17
d3ee3927eb3a
fix: fetch_array() in DBRecord.class.php
Sushi-k <epgrec@park.mda.or.jp>
parents:
1
diff
changeset
|
65 "='".mysql_real_escape_string( $value )."'"; |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
66 |
| 1 | 67 if( $options != null ) { |
|
17
d3ee3927eb3a
fix: fetch_array() in DBRecord.class.php
Sushi-k <epgrec@park.mda.or.jp>
parents:
1
diff
changeset
|
68 $sqlstr .= "AND ".$options; |
| 1 | 69 } |
| 70 $res = $this->__query( $sqlstr ); | |
| 71 while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) { | |
| 72 array_push( $retval, $row ); | |
| 73 } | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
74 |
|
17
d3ee3927eb3a
fix: fetch_array() in DBRecord.class.php
Sushi-k <epgrec@park.mda.or.jp>
parents:
1
diff
changeset
|
75 return $retval; |
| 1 | 76 } |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
77 |
| 1 | 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 } | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
92 |
| 1 | 93 function __get( $property ) { |
| 94 if( $this->id == 0 ) throw new exception( "get:無効なid" ); | |
| 95 if( $property == "id" ) return $this->id; | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
96 |
| 1 | 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."は存在しない" ); | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
101 |
| 1 | 102 return stripslashes($arr[0]); |
| 103 } | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
104 |
| 1 | 105 function delete() { |
| 106 if( $this->id == 0 ) throw new exception( "delete:無効なid" ); | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
107 |
| 1 | 108 $sqlstr = "DELETE FROM ".$this->table." WHERE id='".$this->id."'"; |
| 109 $this->__query( $sqlstr ); | |
| 110 $this->id = 0; | |
| 111 } | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
112 |
| 1 | 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 } | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
127 |
| 1 | 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 } | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
146 |
| 1 | 147 function __destruct() { |
| 148 $this->id = 0; | |
| 149 } | |
| 150 } | |
|
102
49145003e6a3
fix whitespace and new line code
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
39
diff
changeset
|
151 ?> |
