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
<?php
/**
* PostgresDatabaseAdaptorJP
* PostgreSQL接続
* データベースコネクション情報
* 日本語テーブル、フィールドを利用しているデータベース用
* $Id: PostgresDatabaseAdaptorJP.inc,v 1.1 2015/10/08 11:18:57 wanggb Exp $
* @author iimuro
* @access public
* @package jp.aimslib2.database.postgres
*/
class PostgresDatabaseAdaptorJP extends PostgresDatabaseAdaptor{
public $host;
public $port;
public $db_name;
public $user;
public $pass;
public $con = null;
public $result;
public $lastquery;
/**
* コンストラクタ
*/
function PostgresDatabaseAdaptorJP(){
}
/**
* 初期化
*/
public function init($host, $port, $db_name, $user, $pass){
$this->host = $host;
if(isset($port)){
$this->port = $port;
}else{
$this->port = "5432";
}
$this->db_name = $db_name;
$this->user = $user;
$this->pass = $pass;
}
/**
* 接続の確立
*/
public function db_connect(){
if (!is_resource($this->con)) {
$h = "host=" . $this->host . " port=5432 dbname=" . $this->db_name . " user=" . $this->user . " password=" . $this->pass;
$this->con = pg_pconnect($h);
}
}
/**
* 接続の解除(使わない)
*/
public function db_close() {
pg_close($this->con);
$this->con = null;
}
public function db_exec($query) {
$this->db_connect();
$this->lastquery = $query;
// test
ErrorLogger::doOutput("SQL:" . $query);
//$this->result = pg_query($this->con, $query);
if(@$this->result = pg_query($this->con, $query)){
$rowset = $this->private_getRowset($this->result);
pg_free_result($this->result);
return $rowset;
}else{
if ( pg_connection_status($this->con) == PGSQL_CONNECTION_OK ){
ErrorLogger::doOutput("connect ok", 0);
}
ErrorLogger::doOutput("error at:" . $this->lastquery, 0);
pg_query("ROLLBACK");
exit;
}
}
/**
* データの一括挿入
*/
public function db_copy_from($table, $data, $delimiter = "\t"){
$this->db_connect();
pg_exec($this->con, "copy \"" . $table . "\" from stdin");
foreach($data as $line){
pg_put_line($this->con, $line);
}
return pg_end_copy($this->con);
//return pg_copy_from($this->con, $table, $data, $delimiter);
}
/**
* ラージオブジェクトを保存します。OIDが指定されない場合は新規に作成します。
* この処理を行う前にはかならずトランザク ションブロックの中に括る必要があります。
* @access public
* @param string $data ラージオブジェクトバイナリデータ
* @param int $oid ラージオブジェクトOID
* @return int ラージオブジェクトOID。失敗時はnull。
*/
public static function writeLargeObject($data, $oid = null) {
$this->db_connect();
if ($oid == null) {
if (!$oid = pg_lo_create($this->con)) {
return null;
}
}
if (!$handle = pg_lo_open($this->con, $oid, "w")) {
return null;
}
if (!pg_lo_write($handle, $data)) {
pg_lo_close($handle);
return null;
}
if (!pg_lo_close($handle)) {
return null;
}
return $oid;
}
/**
* ラージオブジェクトを読み出します。
* この処理を行う前にはかならずトランザク ションブロックの中に括る必要があります。
* @access public
* @param int $oid ラージオブジェクトOID
* @return int ラージオブジェクトバイナリデータ。失敗時はnull。
*/
public static function readLargeObject($oid) {
$this->db_connect();
if (!$handle = pg_lo_open($this->con, $oid, "r")) {
return null;
}
ob_start();
if (!pg_lo_read_all($handle)) {
pg_lo_close($handle);
return null;
}
$data = ob_get_contents();
ob_end_clean();
if (!pg_lo_close($handle)) {
return null;
}
return $data;
}
/**
* ラージオブジェクトを削除します。
* この処理を行う前にはかならずトランザク ションブロックの中に括る必要があります。
* @access public
* @param int $oid ラージオブジェクトOID
* @return boolean 成功時true
*/
public static function deleteLargeObject($oid) {
ErrorLogger::doOutput("UnsupportedFunctionException.", 0);
return false;
}
/**
* 結果を2次元連想配列にして返します。
* @access private
* @param resource $result 検索の結果リソース
* @return array カラム名をkeyとした連想配列を要素として持つ配列
*/
public function private_getRowset(&$result){
$rowset = array();
if (!is_resource($result)) {
return null;
}
$bool_field = array();
for ($x = 0; $x < pg_num_fields($result); $x++ ) {
if (pg_field_type($result, $x) == "bool") {
$name = pg_field_name($result, $x);
array_push($bool_field, $name);
}
}
while ($row = pg_fetch_assoc($result)) {
foreach ($bool_field as $name) {
if ($row[$name] == "t") {
$row[$name] = true;
} else {
$row[$name] = false;
}
}
array_push($rowset, $row);
}
return $rowset;
}
}
?>