File: IRMDB.php

package info (click to toggle)
irm 1.5.3.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,436 kB
  • ctags: 3,308
  • sloc: php: 16,796; sh: 127; perl: 97; pascal: 56; makefile: 48
file content (243 lines) | stat: -rw-r--r-- 5,314 bytes parent folder | download | duplicates (2)
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
<?php

require_once 'MDB.php';
require_once 'include/i18n.php';

class IRMDB
{
	var $dsn = NULL;
	
	var $_dbh = NULL;
	
	function IRMDB($dsn, $socket = NULL)
	{
		$this->dsn = $dsn;
		
		if ($socket !== NULL)
		{
			ini_set('mysql.default_socket', $socket);
		}
		
		$this->_dbh = MDB::Connect($dsn);
		
		if (MDB::isError($this->_dbh))
		{
			if (ini_get('display_errors') == 0)
			{
				echo "<b>"._('FATAL')."</b>: "
					.$this->_dbh->getMessage()."\n"
					.$this->_dbh->getUserInfo();
			}

			trigger_error(sprintf(_("Could not connect to %s: %s\n%s"),
						$this->dsn,
						$this->_dbh->getMessage(),
						$this->_dbh->getUserInfo()),
					E_USER_ERROR);
		}

		$this->_dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'DBDie');
		$this->_dbh->setFetchMode(MDB_FETCHMODE_ASSOC);
		$this->_dbh->setOption('seqname_format', '%s');
	}

	/** Create an insert query and run it on the database.
	 * Similar in concept to the autoExecute method in PEAR::DB --
	 * take an assoc. array of field => value pairs in $values, and create
	 * an INSERT INTO $table query from that.
	 */
	function InsertQuery($table, $values)
	{
		// The list of fields in the insert query
		$fields = array();
		
		// The quoted values ready for insertion
		$quoted = array();
		
		foreach ($values as $f => $v)
		{
			$fields[] = $f;
			$quoted[] = $this->_dbh->getTextValue($v);
		}
		
		$fields = join(',', $fields);
		$quoted = join(',', $quoted);
		
		$q = "INSERT INTO $table ($fields) VALUES ($quoted)";
		
		$this->query($q);
	}
	
	/** Create an update query and run it on the database.
	 * Similar in concept to the autoExecute method in PEAR::DB --
	 * take an assoc. array of field => value pairs in $values, and create
	 * an UPDATE $table query from that.
	 * Any criteria to select which records to update should be specified
	 * in $where.
	 */
	function UpdateQuery($table, $values, $where = NULL)
	{
		// The list of fields in the query
		$fields = array();
		
		foreach ($values as $f => $v)
		{
			$fields[] = "$f=".$this->_dbh->getTextValue($v);
		}
		
		$fields = join(',', $fields);
		
		if ($where !== NULL)
		{
			$where = "WHERE $where";
		}

		$q = "UPDATE $table SET $fields $where";
		
		$this->query($q);
	}
	
	function nextId()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'nextId'), $args);
	}
	
	function query()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'query'), $args);
	}
	
	function getOne()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'getOne'), $args);
	}
	
	function getRow()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'getRow'), $args);
	}
	
	function getCol()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'getCol'), $args);
	}
	
	function getAll()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'getAll'), $args);
	}
	
	function getTextValue()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'getTextValue'), $args);
	}
	
	function setErrorHandling()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'setErrorHandling'), $args);
	}

	function pushErrorHandling()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'pushErrorHandling'), $args);
	}

	function popErrorHandling()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'popErrorHandling'), $args);
	}

	function autoCommit()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'autoCommit'), $args);
	}

	function commit()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'commit'), $args);
	}

	function disconnect()
	{
		$args = func_get_args();
		return call_user_func_array(array(&$this->_dbh, 'disconnect'), $args);
		
		$this->_dbh = NULL;
	}

	/* Testing only: remove all tables from the database
	 * Not something you want to try at home, kids.
	 *
	 * This method has a kink -- since a table may fail to drop
	 * because of key constraints, we need to keep dropping tables over
	 * and over until they're all gone or we can't drop any more.  This
	 * problem makes this method about an order of magnitude more complex
	 * than it otherwise would need to be.
	 */
	function _EmptyDatabase()
	{
		$tbls = $this->_dbh->getCol('SHOW TABLES');

		$continue = true;
		$this->pushErrorHandling(PEAR_ERROR_RETURN);
		while ($continue)
		{
			$continue = false;
			$faileds = array();
			
			foreach ($tbls as $t)
			{
				$err = $this->_dbh->query("DROP TABLE $t");
				if (MDB::isError($err))
				{
					$faileds[] = $t;
				}
				else
				{
					$continue = true;
				}
			}
			
			$tbls = $faileds;
		}

		$this->popErrorHandling();

		if (count($tbls))
		{
			// Didn't drop all tables
			trigger_error("Failed to drop the following tables: ".join(' ', $tbls), E_USER_ERROR);
		}
	}
	
	/** Initialise a (presumably) empty database
	 */
	function InitDatabase()
	{
		global $INSTALL;
		require_once 'database/install.php';
		foreach ($INSTALL as $q)
		{
			$this->_dbh->query($q);
		}
	}
}

function DBDie($err)
{
	$msg = sprintf(_("Database Error: %s (%s)"),$err->getMessage(),$err->getUserInfo());
	
	trigger_error($msg, E_USER_ERROR);
}