/sql/db2
[return to app]1
<?php
2 /**
3 * Converts the procedural IBM DB2 PHP class to object-oriented
4 */
5 class db2 {
6 /**
7 * Database resource ID
8 * @var object
9 */
10 public $resource;
11
12 /**
13 * The database method used to sanitize user input strings
14 */
15 const ESCAPE_STRING = 'escape_string';
16
17 /**
18 * Connect to IBM DB2
19 *
20 * @param string $username
21 * @param string $passwd
22 * @param string $dbname Optional
23 * @param array $options Optional
24 */
25 public function __construct($username, $password, $dbname, $options = null) {
26 try {
27 $this->resource = db2_connect($dbname, $username, $password, $options);
28 if (!$this->resource) {
29 throw new Exception('Cannot connect to DB2');
30 }
31 } catch (Exception $e) {
32 $this->error = $e->getMessage();
33 }
34 }
35
36 /**
37 * Alias for db2_exec() to update DB2 to use the modern naming convention
38 * and not require passing the connection to every statement via argument
39 *
40 * @param string $query
41 * @return resource
42 */
43 public function query($query) {
44 return db2_exec($this->resource, $query);
45 }
46
47 /**
48 * Catch-all method allowing you to call any DB2 function via camel-cap object-oriented syntax.
49 *
50 * From your models you could access:
51 *
52 * $this->db->fetchAssoc()
53 * $this->db->fetchBoth()
54 * $this->db->commit()
55 * $this->db->prepare()
56 * $this->db->primaryKeys()
57 * $this->db->result()
58 * $this->db->rollback()
59 * $this->db->close()
60 * etc.
61 *
62 * @param string $name
63 * @param mixed $val
64 */
65 public function __call($name, $val) {
66 $name = strtolower('db2_' . preg_replace('/[A-Z]/', '_$0', $name));
67 if (function_exists($name)) {
68 return call_user_func_array($name, $val);
69 }
70 }
71 }