-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_oracle.php
More file actions
205 lines (188 loc) · 7 KB
/
Copy pathclass_oracle.php
File metadata and controls
205 lines (188 loc) · 7 KB
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
<?php
/**
* Created by PhpStorm.
* User: ghostwwl
*/
class ClassOracle
{
public $o_dbhost = null;
public $o_dbport = '1521';
public $o_dbuser = null;
public $o_dbpwd = null;
public $o_sid = null;
public $o_conn_charset = 'AL32UTF8';
public $link_id = false;
public $connect_flag = false;
public $debug_flag = true;
public function __construct($o_dbh, $o_dbp, $o_dbuser, $o_dbpwd, $o_sid, $o_conn_char='AL32UTF8')
{
try
{
if (empty($o_dbh)) throw new Exception('unknown oracle db host');
if (empty($o_dbp)) throw new Exception('unknown oracle db port');
if (empty($o_dbuser)) throw new Exception('unknown oracle db user');
if (empty($o_dbpwd)) throw new Exception('unknown oracle db password');
if (empty($o_sid)) throw new Exception('unknown oracle db sid');
$this->o_dbhost = $o_dbh;
$this->o_dbport = $o_dbp;
$this->o_dbuser = $o_dbuser;
$this->o_dbpwd = $o_dbpwd;
$this->o_sid = $o_sid;
if (!empty($o_conn_char)) $this->o_conn_charset = $o_conn_char;
}
catch(Exception $err)
{
$err_msg = sprintf("ERROR %s] %s\n", date('Y-m-d H:i:s'), $err->getMessage());
if ($this->debug_flag)
{
echo $err_msg;
}
else throw new Exception($err_msg);
}
}
public function is_connect()
{
if ($this->link_id === false) return false;
return true;
}
public function close()
{
oci_close($this->link_id);
}
public function connect()
{
try{
$this->link_id = @oci_connect($this->o_dbuser, $this->o_dbpwd, "{$this->o_dbhost}:{$this->o_dbport}/{$this->o_sid}", $this->o_conn_charset);
if (!$this->link_id){
$e = oci_error();
throw new Exception($e['message']);
}
return true;
}
catch(Exception $err){
oci_close($this->link_id);
$err_msg = sprintf("ERROR %s] %s\n", date('Y-m-d H:i:s'), $err->getMessage());
if ($this->debug_flag) echo $err_msg;
else throw new Exception($err_msg);
}
return false;
}
// 执行一条语句 执行完成后自动提交 query($sql, argv1, argv2, ...,argvn)
// query('select * from xx where created>:pxc and deposit>:pxdep', ':pxc', 1381234563111, ':pxdep', 2000)
public function &query()
{
$args_num = func_num_args();
$args_list = func_get_args();
if ($args_num < 1) throw new Exception('not found argument');
if ($args_num % 2 !== 1) throw new Exception('invaild argument');
$sql = $args_list[0];
try
{
if (!$this->is_connect()) $this->connect();
$stmt = @oci_parse($this->link_id, $sql);
if (!$stmt){
$e = oci_error($stmt);
throw new Exception("oci_parse err:{$e['message']}");
}
// 绑定oracle 参数传入值
if ($args_num > 1){
for($i=1; $i<$args_num; $i=$i+2){
if (!isset($args_list[$i])) break;
$arg_p = $args_list[$i];
$arg_v = $args_list[$i+1];
oci_bind_by_name($stmt, $arg_p, $arg_v);
}
}
$run_flag = oci_execute($stmt, OCI_COMMIT_ON_SUCCESS);
if (!$run_flag){
$e = oci_error($stmt);
throw new Exception("<pre>execute err:{$e['message']}<br />\n".htmlentities($e['sqltext'])."</pre>");
}
$row_num = oci_fetch_all($stmt, $result, null, null, OCI_FETCHSTATEMENT_BY_ROW);
@oci_free_statement($stmt);
unset($stmt);
return array('num'=>$row_num, 'result'=>$result);
}
catch(Exception $e){
oci_close($this->link_id);
$err_msg = sprintf("ERROR %s] %s\n", date('Y-m-d H:i:s'), $e->getMessage());
if ($this->debug_flag) echo $err_msg;
else throw new Exception($err_msg);
}
return null;
}
// 用于列表也翻页
// select 要查询的语句 pageno 第几页 rows_per_page 每页取多少条
public function &paged_result($select, $pageno, $rows_per_page)
{
try{
if (!$this->is_connect()) $this->connect();
$sql = "SELECT * FROM (SELECT r.*, ROWNUM as row_number FROM ({$select}) r WHERE ROWNUM<=:end_row) WHERE :start_row<=row_number";
$stmt = oci_parse($this->link_id, $sql);
if (!$stmt){
$e = oci_error($stmt);
throw new Exception("oci_parse err:{$e['message']}");
}
$start_row = ($pageno-1) * $rows_per_page + 1;
oci_bind_by_name($stmt, ':start_row', $start_row);
// Calculate the number of the last row in the page
$end_row = $start_row + $rows_per_page - 1;
oci_bind_by_name($stmt, ':end_row', $end_row);
$run_flag = oci_execute($stmt);
if (!$run_flag)
{
$e = oci_error($stmt);
throw new Exception("<pre>execute err:{$e['message']}<br />\n".htmlentities($e['sqltext'])."</pre>");
}
// Prefetch the number of rows per page
oci_set_prefetch($stmt, $rows_per_page);
$row_num = oci_fetch_all($stmt, $result, null, null, OCI_FETCHSTATEMENT_BY_ROW);
@oci_free_statement($stmt);
unset($stmt);
return $result;
}
catch(Exception $e)
{
oci_close($this->link_id);
$err_msg = sprintf("ERROR %s] %s\n", date('Y-m-d H:i:s'), $e->getMessage());
if ($this->debug_flag) echo $err_msg;
else throw new Exception($err_msg);
}
return null;
}
// 执行update 或者insert 这个必须手动提交使用了事物
// 如果单条insert 或者update 不使用事物 直接用 query
public function execute($sqls)
{
if (!$this->is_connect()) $this->connect();
foreach ($sqls as $sql)
{
$stmt = oci_parse($this->link_id, $sql);
if (!oci_execute($stmt, OCI_DEFAULT))
{
oci_rollback($this->link_id);
return false;
}
}
return $this->commit();
}
public function commit()
{
try{
$commit_flag = oci_commit($this->link_id);
if (!$commit_flag) {
$error = oci_error($this->link_id);
throw new Exception('Commit failed. Oracle reports: ' . $error['message']);
}
return true;
}
catch(Exception $err){
oci_close($this->link_id);
$err_msg = sprintf("ERROR %s] %s\n", date('Y-m-d H:i:s'), $err->getMessage());
if ($this->debug_flag) echo $err_msg;
else throw new Exception($err_msg);
}
return false;
}
}
?>