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
<?php
/**
* AbstractDatabaseManagerJP
* 番組のクラスはこれをオーバーライドして利用します。一般DB用。
* 日本語テーブル、フィールドを利用しているデータベース用
* $Id: AbstractDatabaseManagerJP.inc,v 1.1 2015/10/08 11:18:56 wanggb Exp $
* @author iimuro
* @access public
* @package jp.aimslib2.database
*/
class AbstractDatabaseManagerJP extends AbstractDatabaseManager{
public function init($db_name, $user, $pass, $host, $port, $dbtype = "PostgreSQL"){
$this->dbtype = $dbtype;
if ($dbtype == "PostgreSQL") {
// 接続情報をアダプターに設定。以降、接続要求はこれらの値を利用。
$this->adaptor = new PostgresDatabaseAdaptorJP();
$this->adaptor->init($host, $port, $db_name, $user, $pass);
// SQL文はPostgres用
$this->builder = new PostgresSQLBuilderJP();
// 型、関数もPostgres用→executeQueryを使うときの文章生成に利用。
$this->util = new PostgresUtil();
} else {
ErrorLogger::doOutput("Unsupported Databse Type!");
}
}
/**
* @abstract
*/
public static function getConnection(){
return null;
}
/**
* SQL文を実行します。
*/
public function executeQuery($sql){
return $this->adaptor->db_exec($sql);
}
/**
*
*/
public static function db_copy_from($table, $data, $delimiter = "\t"){
return $this->adaptor->db_copy_from($table, $data, $delimiter);
}
/**
*
*/
public static function doSelect($table, $w_param, $orderkey = null, $direction = "ASC", $offset = null, $limit = null){
$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 * FROM \"" . $table . "\"" . $option . $order . $subquery . ";";
$rowset = $this->executeQuery($sql);
return $rowset;
}
/**
*
*/
public static function doInsert($table, $v_param){
$sql1 = "INSERT INTO \"" . $table . "\" ";
$sql1 .= $this->builder->createInsertValueOption($v_param);
$this->executeQuery($sql1);
}
/**
*
*/
public static 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 static function doBegin(){
$sql = "BEGIN";
$this->executeQuery($sql);
}
/**
* 明示的にトランザクションを管理したいときに利用します。
* トランザクション終了。
*/
public static function doEnd(){
$sql = "COMMIT";
$this->executeQuery($sql);
}
/**
* 明示的にトランザクションを管理したいときに利用します。
* トランザクション破棄して終了。
*/
public static function doRollback(){
$sql = "ROLLBACK";
$this->executeQuery($sql);
}
/**
* resultset を一括挿入
*/
public static 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 static function writeLargeObject($data, $oid = null) {
return $this->adaptor->writeLargeObject($data, $oid);
}
/**
* ラージオブジェクトを読み出します。
* この処理を行う前にはかならずトランザク ションブロックの中に括る必要があります。
* @access public
* @param int $oid ラージオブジェクトOID
* @return int ラージオブジェクトバイナリデータ。失敗時はnull。
*/
public static function readLargeObject($oid) {
return $this->adaptor->readLargeObject($oid);
}
/**
* データベースのデータの変換や定型文の生成などのUtilクラスを取得します。
*/
public static function getUtility() {
return $this->util;
}
}
?>