-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpudlOdbc.php
More file actions
148 lines (102 loc) · 3.92 KB
/
pudlOdbc.php
File metadata and controls
148 lines (102 loc) · 3.92 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
<?php
if (!class_exists('pudl',false)) require_once(__DIR__.'/../pudl.php');
require_once(is_owner(__DIR__.'/pudlOdbcResult.php'));
class pudlOdbc
extends pudl {
////////////////////////////////////////////////////////////////////////////
// DESTRUCTOR
////////////////////////////////////////////////////////////////////////////
public function __destruct() {
$this->disconnect();
parent::__destruct();
}
////////////////////////////////////////////////////////////////////////////
// CONNECT TO THE ODBC SQL DATABASE
// http://php.net/manual/en/function.odbc-connect.php
// http://php.net/manual/en/function.odbc-pconnect.php
////////////////////////////////////////////////////////////////////////////
public function connect() {
$auth = $this->auth();
//USE EXISTING CONNECTION IF AVAILABLE
if (is_resource($auth['server'])) {
$this->connection = $auth['server'];
//ATTEMPT TO CREATE A PERSISTANT CONNECTION
} else if ($auth['persistent']) {
$this->connection = @odbc_pconnect(
$auth['database'],
$auth['username'],
$auth['password']
);
//ATTEMPT TO CREATE A NON-PERSISTANT CONNECTION
} else {
$this->connection = @odbc_connect(
$auth['database'],
$auth['username'],
$auth['password']
);
}
//CANNOT CONNECT - ERROR OUT
if (empty($this->connection)) {
throw new pudlConnectionException($this,
'Unable to connect to ODBC database ' .
'"' . $auth['database'] . '"' .
' with the username ' .
'"' . $auth['username'] . '"' .
"\nError " . $this->errno() .
': ' . $this->error()
);
}
// STORE WHICH SERVER WE'RE CONNECTED TO
if (!empty($auth['server'])) {
$this->connected = $auth['server'];
}
}
////////////////////////////////////////////////////////////////////////////
// PROCESS THE SQL QUERY
// http://php.net/manual/en/function.odbc-exec.php
////////////////////////////////////////////////////////////////////////////
protected function process($query) {
if (!$this->connection) return new pudlOdbcResult($this);
$result = @odbc_exec($this->connection, $query);
$this->numrows = ($result !== false) ? @odbc_num_rows($result) : 0;
return new pudlOdbcResult($this, $result);
}
////////////////////////////////////////////////////////////////////////////
// GET THE MOST RECENT AUTO-INCREMENT ID
////////////////////////////////////////////////////////////////////////////
public function insertId() {
if (!$this->connection) return 0;
$result = @odbc_exec($this->connection, 'SELECT @@IDENTITY');
if ($result === false) return false;
@odbc_fetch_row($result, 0);
$return = @odbc_result($result, 0);
@odbc_free_result($result);
return $return;
}
////////////////////////////////////////////////////////////////////////////
// GET THE NUMBER OF ROWS AFFECTED BY THE MOST RECENT SQL QUERY
////////////////////////////////////////////////////////////////////////////
public function updated() {
return $this->numrows;
}
////////////////////////////////////////////////////////////////////////////
// RETURNS THE ERROR CODE FOR THE MOST RECENT FUNCTION CALL
// http://php.net/manual/en/function.odbc-error.php
////////////////////////////////////////////////////////////////////////////
public function errno() {
if (!$this->connection) return (int) odbc_error();
return (int) odbc_error($this->connection);
}
////////////////////////////////////////////////////////////////////////////
// RETURNS A STRING DESCRIPTION OF THE LAST ERROR
// http://php.net/manual/en/function.odbc-errormsg.php
////////////////////////////////////////////////////////////////////////////
public function error() {
if (!$this->connection) return odbc_errormsg();
return odbc_errormsg($this->connection);
}
////////////////////////////////////////////////////////////////////////////
// MEMBER VARIABLES
////////////////////////////////////////////////////////////////////////////
private $numrows = 0;
}