File: odbc.php

package info (click to toggle)
php4 4.0.3pl1-0potato3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 15,168 kB
  • ctags: 20,556
  • sloc: ansic: 155,237; php: 10,827; sh: 9,608; yacc: 1,874; lex: 1,742; makefile: 788; java: 424; awk: 359; cpp: 335; perl: 181; xml: 57
file content (246 lines) | stat: -rw-r--r-- 6,421 bytes parent folder | download
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
<?php
//
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Stig Bakken <ssb@fast.no>                                   |
// |                                                                      |
// +----------------------------------------------------------------------+
//
// Database independent query interface definition for PHP's ODBC
// extension.
//

//
// XXX legend:
//
// XXX ERRORMSG: The error message from the odbc function should
//				 be registered here.
// XXX ADDREF:	 As soon as Zend/PHP gets support for returning
//				 references, this return value should be made into
//				 a reference.
//

require_once 'DB/common.php';

class DB_odbc extends DB_common {
    // {{{ properties

	var $connection;
	var $phptype, $dbsyntax;

    // }}}

    // {{{ constructor

	function DB_odbc() {
		$this->phptype = 'odbc';
		$this->dbsyntax = 'unknown';
		// Let's be very pessimistic about the features of an odbc
		// backend.  The user has to specify the dbsyntax to access
		// other features.
		$this->features = array(
			'prepare' => false,
			'pconnect' => true,
			'transactions' => false
		);
		$this->errorcode_map = array(
			"01004" => DB_ERROR_TRUNCATED,
			"07001" => DB_ERROR_MISMATCH,
			"21S01" => DB_ERROR_MISMATCH,
			"21S02" => DB_ERROR_MISMATCH,
			"22003" => DB_ERROR_INVALID_NUMBER,
			"22008" => DB_ERROR_INVALID_DATE,
			"22012" => DB_ERROR_DIVZERO,
			"23000" => DB_ERROR_CONSTRAINT,
			"24000" => DB_ERROR_INVALID,
			"34000" => DB_ERROR_INVALID,
			"37000" => DB_ERROR_SYNTAX,
			"42000" => DB_ERROR_SYNTAX,
			"IM001" => DB_ERROR_UNSUPPORTED,
			"S0001" => DB_ERROR_NOT_FOUND,
			"S0002" => DB_ERROR_NOT_FOUND,
			"S0011" => DB_ERROR_ALREADY_EXISTS,
			"S0012" => DB_ERROR_NOT_FOUND,
			"S0021" => DB_ERROR_ALREADY_EXISTS,
			"S0022" => DB_ERROR_NOT_FOUND,
			"S1009" => DB_ERROR_INVALID,
			"S1090" => DB_ERROR_INVALID,
			"S1C00" => DB_ERROR_NOT_CAPABLE
		);
	}

    // }}}

    // {{{ connect()

	/**
	 * Connect to a database and log in as the specified user.
	 *
	 * @param $dsn the data source name (see DB::parseDSN for syntax)
	 * @param $persistent (optional) whether the connection should
	 *        be persistent
	 *
	 * @return int DB_OK on success, a DB error code on failure
	 */
	function connect(&$dsn, $user, $pw) {
		if (is_array($dsn)) {
			$dsninfo = &$dsn;
		} else {
			$dsninfo = DB::parseDSN($dsn);
		}
		if (!$dsninfo || !$dsninfo['phptype']) {
			return $this->raiseError(); // XXX ERRORMSG
		}
		$this->dbsyntax = $dsninfo['dbsyntax'];
		switch ($this->dbsyntax) {
			case 'solid':
				$this->features = array(
					'prepare' => true,
					'pconnect' => true,
					'transactions' => true
				);
				$default_dsn = 'localhost';
				break;
			default:
				break;
		}
		$dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
		$user = $dsninfo['username'];
		$pw = $dsninfo['password'];
		if ($this->provides('pconnect')) {
			$connect_function = $persistent ? 'odbc_pconnect' : 'odbc_connect';
		} else {
			$connect_function = 'odbc_connect';
		}
		if ($dbhost && $user && $pw) {
			$conn = $connect_function($dbhost, $user, $pw);
		} elseif ($dbhost && $user) {
			$conn = $connect_function($dbhost, $user);
		} elseif ($dbhost) {
			$conn = $connect_function($dbhost);
		} else {
			$conn = false;
		}
		if ($conn == false) {
			return $this->raiseError(); // XXX ERRORMSG
		}
		$this->connection = $conn;
		return DB_OK;



		$this->connection = odbc_connect($dsn, $user, $pw);
	}

    // }}}
    // {{{ disconnect()

	function disconnect() {
		$err = odbc_close($this->connection); // XXX ERRORMSG
		return $err;
	}

    // }}}
    // {{{ query()

	function query($query) {
		$this->last_query = $query;
		$result = odbc_exec($this->connection, $query);
		if (!$result) {
			return $this->raiseError(); // XXX ERRORMSG
		}
		// Determine which queries that should return data, and which
		// should return an error code only.
		if (preg_match('/SELECT/i', $query)) {
			$resultObj = new DB_result($this, $result);
			return $resultObj; // XXX ADDREF
		} else {
			return DB_OK;
		}
	}

	// }}}
    // {{{ simpleQuery()

	/**
	 * Send a query to ODBC and return the results as a ODBC resource
	 * identifier.
	 *
	 * @param $query the SQL query
	 *
	 * @return int returns a valid ODBC result for successful SELECT
	 * queries, DB_OK for other successful queries.  A DB error code
	 * is returned on failure.
	 */
	function simpleQuery($query) {
		$this->last_query = $query;
		$result = odbc_exec($this->connection, $query);
		if (!$result) {
			return $this->raiseError(); // XXX ERRORMSG
		}
		// Determine which queries that should return data, and which
		// should return an error code only.
		if (preg_match('/SELECT/i', $query)) {
			return $result;
		} else {
			return DB_OK;
		}
	}

	// }}}
    // {{{ fetchRow()

	function fetchRow($result) {
		$cols = odbc_fetch_into($result, &$row);
		if ($cols == 0) {
			// XXX ERRORMSG
			return false;
		}
		return $row; // XXX ADDREF
	}

	// }}}
    // {{{ freeResult()

	function freeResult($result) {
		$err = odbc_free_result($result); // XXX ERRORMSG
		return $err;
	}

	// }}}
    // {{{ quoteString()

	function quoteString($string) {
		return str_replace("'", "''", $string);
	}

	// }}}

    // prepare
    // execute
    // errorCode
    // errorMsg
    // errorNative
    // longReadlen
    // binMode
    // autoCommit
    // commit
    // rollback
}

// Local variables:
// tab-width: 4
// c-basic-offset: 4
// End:
?>