1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* AbstractDatabaseManager
* 番組のクラスはこれをオーバーライドして利用します。一般DB用。
* $Id: AbstractDatabaseManager.inc,v 1.1 2015/10/08 11:18:56 wanggb Exp $
* @author iimuro
* @access public
* @package jp.aimslib2.database
*/
class AbstractDatabaseManager{
/**
* 使用するデータベースの種類。
* init時にセットする。
* 指定がない場合はPostgres?
*/
public $dbtype = null;
/**
* 接続する先
*/
public $adaptor;
/**
* SQL文生成
*/
public $builder;
/**
* 型変換等データベースの差異があるもの
*/
public $util;
/**
* 接続に必要な情報でクラスを初期化します。
* @param String データベース名
* @param String データベースユーザー名
* @param String 接続パスワード
* @param String データベースホスト名。自サーバー内ならlocalhost。
* @param String ポート番号
* @param String データベース種類。デフォルトは"PostgreSQL"
*/
public function init($db_name, $user, $pass, $host, $port, $dbtype = "PostgreSQL"){
$this->dbtype = $dbtype;
if (strtolower($dbtype) == "postgresql") {
// 接続情報をアダプターに設定。以降、接続要求はこれらの値を利用。
$this->adaptor = new PostgresDatabaseAdaptor();
$this->adaptor->init($host, $port, $db_name, $user, $pass);
// SQL文はPostgres用
$this->builder = new PostgresSQLBuilder();
// 型、関数もPostgres用→executeQueryを使うときの文章生成に利用。
$this->util = new PostgresUtil();
} elseif (strtolower($dbtype) == "mysql") {
// 接続情報をアダプターに設定。以降、接続要求はこれらの値を利用。
$this->adaptor = new MySQLDatabaseAdaptor();
$this->adaptor->init($host, $port, $db_name, $user, $pass);
// SQL文はMySQL用
$this->builder = new MySQLSQLBuilder();
// 型、関数もMySQL用→executeQueryを使うときの文章生成に利用。
//$this->util = new MySQLUtil();
} else {
ErrorLogger::doOutput("Unsupported Databse Type!");
}
}
/**
* @abstract
*/
public function getConnection(){
return $this->adaptor->con;
}
/**
* SQL文を実行します。
*/
public function executeQuery($sql){
//echo "<br>".$sql;
return $this->adaptor->db_exec($sql);
}
/**
*
*/
public function db_copy_from($table, $data, $delimiter = "\t"){
return $this->adaptor->db_copy_from($table, $data, $delimiter);
}
/**
*
*/
public function doDelect($table, $w_param){
$option = "";
if($w_param != null){
$option = " WHERE " . $this->builder->createOptionCondition($w_param);
}
$sql = "DELETE FROM " . $table . $option . ";";
$this->executeQuery($sql);
}
/**
*
*/
public function doSelect($table, $w_param, $orderkey = null, $direction = "ASC", $offset = null, $limit = null, $selectkey = "*"){
$option = "";
if($w_param != null){
$option = " WHERE " . $this->builder->createOptionCondition($w_param);
}
$order = "";
if($orderkey != null){
$order .= " ORDER BY " . $orderkey . " " . $direction;
}
$subquery = $this->builder->createSubQuery($offset, $limit);
$sql = "SELECT ".$selectkey." FROM " . $table . $option . $order . $subquery . ";";
$rowset = $this->executeQuery($sql);
return $rowset;
}
/**
*
*/
public function doInsert($table, $v_param){
$sql1 = "INSERT INTO " . $table . " ";
$sql1 .= $this->builder->createInsertValueOption($v_param);
if (strtolower($this->dbtype) == "postgresql") {
$sql1 .= " returning id";
}
//return $this->executeQuery($sql1);
$tmp = $this->executeQuery($sql1);
if (isset($tmp[0]["id"])) {
return $tmp[0]["id"];
}
return $tmp;
}
/**
*
*/
public function doInsertAndReturn($table, $v_param){
$result = null;
if (strtolower($this->dbtype) == "postgresql") {
$sql1 = "INSERT INTO " . $table . " ";
$sql1 .= $this->builder->createInsertValueOption($v_param);
$sql1 .= " returning *";
$result = $this->executeQuery($sql1);
} elseif (strtolower($this->dbtype) == "mysql") {
$sql1 = "INSERT INTO " . $table . " ";
$sql1 .= $this->builder->createInsertValueOption($v_param);
$this->executeQuery($sql1);
$sql2 = "select last_insert_id() as id";
$tmp = $this->executeQuery($sql2);
if (isset($tmp[0]["id"])) {
error_log("error: new id is " . $tmp[0]["id"], 0);
$w_param = array();
$w_param["id"] = $tmp[0]["id"];
$result = $this->doSelect($table, $w_param, null, null, 0, 1);
} else {
// エラー?
error_log("error: id not found at select last_insert_id()", 0);
return null;
}
} else {
ErrorLogger::doOutput("Unsupported Databse Type!");
}
return $result;
}
/**
*
*/
public function doUpdate($table, $w_param, $v_param){
$set_param = $this->builder->createSetValueOption($v_param);
if(count($w_param) > 0){
$where_option = $this->builder->createOptionCondition($w_param);
$sql = "UPDATE " . $table . " SET $set_param WHERE $where_option";
}else{
$sql = "UPDATE " . $table . " SET $set_param";
}
$this->executeQuery($sql);
}
/**
* 明示的にトランザクションを管理したいときに利用します。
* トランザクション開始。
*/
public function doBegin(){
$sql = "BEGIN";
$this->executeQuery($sql);
}
/**
* 明示的にトランザクションを管理したいときに利用します。
* トランザクション終了。
*/
public function doEnd(){
$sql = "COMMIT";
$this->executeQuery($sql);
}
/**
* 明示的にトランザクションを管理したいときに利用します。
* トランザクション破棄して終了。
*/
public function doRollback(){
$sql = "ROLLBACK";
$this->executeQuery($sql);
}
/**
* resultset を一括挿入
*/
public function db_copy_resultset($table, $column_name, $resultset, $delimiter = "\t"){
$data = array();
foreach($resultset as $row){
$line = "";
for($i=0; $i < count($column_name); $i++) {
if(isset($row[$column_name[$i]])){
$line .= $row[$column_name[$i]];
}else{
$line .= "\N";
}
if($i < count($column_name) - 1){
$line .= $delimiter;
}
}
$line .= "\n";
array_push($data, $line);
}
array_push($data, "\\.\n");
return $this->db_copy_from($table, $data, $delimiter);
}
/**
* ラージオブジェクトを保存します。OIDが指定されない場合は新規に作成します。
* この処理を行う前にはかならずトランザク ションブロックの中に括る必要があります。
* @access public
* @param string $data ラージオブジェクトバイナリデータ
* @param int $oid ラージオブジェクトOID
* @return int ラージオブジェクトOID。失敗時はnull。
*/
public function writeLargeObject($data, $oid = null) {
return $this->adaptor->writeLargeObject($data, $oid);
}
/**
* ラージオブジェクトを読み出します。
* この処理を行う前にはかならずトランザク ションブロックの中に括る必要があります。
* @access public
* @param int $oid ラージオブジェクトOID
* @return int ラージオブジェクトバイナリデータ。失敗時はnull。
*/
public function readLargeObject($oid) {
return $this->adaptor->readLargeObject($oid);
}
/**
* ラージオブジェクトを削除します。
* この処理を行う前にはかならずトランザク ションブロックの中に括る必要があります。
* @access public
* @param int $oid ラージオブジェクトOID
* @return boolean 成功時true
*/
public function deleteLargeObject($oid = null) {
return $this->adaptor->deleteLargeObject($oid);
}
/**
* データベースのデータの変換や定型文の生成などのUtilクラスを取得します。
*/
public function getUtility() {
return $this->util;
}
}
?>