Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/site/cake/libs/model/dbo/dbo_pear.php
blob: 2ca4860f5cd7fd8c8de117265aaf13ac1514b028 (plain)
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
<?php
/* SVN FILE: $Id: dbo_pear.php 6305 2008-01-02 02:33:56Z phpnut $ */

/**
 * {@link http://pear.php.net/package/DB PEAR::DB} layer for DBO.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
 * Copyright 2005-2008, Cake Software Foundation, Inc.
 *								1785 E. Sahara Avenue, Suite 490-204
 *								Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package			cake
 * @subpackage		cake.cake.libs.model.dbo
 * @since			CakePHP(tm) v 0.2.9
 * @version			$Revision: 6305 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2008-01-01 20:33:56 -0600 (Tue, 01 Jan 2008) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Create an include path required PEAR libraries.
 */
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . PEAR);
vendor ('Pear/DB');

/**
 * {@link http://pear.php.net/package/DB PEAR::DB} layer for DBO.
 *
 * Long description for class
 *
 * @package		cake
 * @subpackage	cake.cake.libs.model.dbo
 */
class DboPear extends DboSource{

/**
 * PEAR::DB object with which we connect.
 *
 * @var DB The connection object.
 * @access private
 */
	 var $_pear = null;

/**
 * Connects to the database using options in the given configuration array.
 *
 * @param array $config Configuration array for connecting
 * @return boolean True if the database could be connected, else false
 */
	 function connect($config) {
		  $this->config   =$config;
		  $dsn            =$config['driver'] . '://' . $config['login'] . ':' . $config['password'] . '@'
			  . $config['host'] . '/' . $config['database'];
		  $options=array('debug' => Configure::read() - 1,
					  'portability' => DB_PORTABILITY_ALL,);

		  $this->_pear    =&DB::connect($dsn, $options);
		  $this->connected=$this->_pear ? true : false;
		  return !(PEAR::isError($this->_pear));
	 }

/**
 * Disconnects from database.
 *
 * @return boolean True if the database could be disconnected, else false
 */
	 function disconnect() {
		  die (__('Please implement DBO::disconnect() first.'));
	 }

/**
 * Executes given SQL statement.
 *
 * @param string $sql SQL statement
 * @return resource Result resource identifier
 */
	 function execute($sql) {
		  return $this->_pear->query($sql);
	 }

/**
 * Returns a row from given resultset as an array .
 *
 * @return array The fetched row as an array
 */
	function fetchRow($sql = null) {
		if (!empty($sql) && is_string($sql) && strlen($sql) > 5) {
			if (!$this->execute($sql)) {
				return null;
			}
		}
		return $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
	}

/**
 * Returns an array of tables in the database. If there are no tables, an error is raised and the application exits.
 * :WARNING: :TODO: POSTGRESQL & MYSQL ONLY! PEAR::DB doesn't support universal table listing.
 *
 * @return array Array of tablenames in the database
 */
	 function tablesList() {
		  $driver=$this->config['driver'];
		  $tables=array();

		  if ('postgres' == $driver) {
				$sql   ="SELECT a.relname AS name
						FROM pg_class a, pg_user b
						WHERE ( relkind = 'r') and relname !~ '^pg_' AND relname !~ '^sql_'
						AND relname !~ '^xin[vx][0-9]+' AND b.usesysid = a.relowner
						AND NOT (EXISTS (SELECT viewname FROM pg_views WHERE viewname=a.relname));";

				$result=$this->all($sql);

				foreach ($result as $item) {
					 $tables[] = $item['name'];
				}
		  } elseif ('mysql' == $driver) {
				$result=array();
				$result=mysql_list_tables($this->config['database']);

				while ($item = mysql_fetch_array($result)) {
					 $tables[] = $item[0];
				}
		  } else {
				die (__('Please implement DBO_Pear::tablesList() for your database driver.'));
		  }

		  if (!$result) {
				trigger_error(ERROR_NO_TABLE_LIST, E_USER_ERROR);
				exit;
		  } else {
				return $tables;
		  }
	 }

/**
 * Returns an array of the fields in given table name.
 *
 * @param string $tableName Name of database table to inspect
 * @return array Fields in table. Keys are name and type
 */
	 function fields($tableName) {
		  $data  =$this->_pear->tableInfo($tableName);
		  $fields=false;

		  foreach ($data as $item) {
				$fields[] = array('name' => $item['name'],
							'type' => $item['type']);
		  }

		  return $fields;
	 }

/**
 * Returns a quoted and escaped string of $data for use in an SQL statement.
 *
 * @param string $data String to be prepared for use in an SQL statement
 * @return string Quoted and escaped
 */
	 function prepareValue($data) {
		  return $this->_pear->quoteSmart($data);
	 }

/**
 * Returns a formatted error message from previous database operation.
 *
 * @return string Error message
 */
	 function lastError() {
		  return PEAR::isError($this->_result) ? $this->_result->getMessage() : null;
	 }

/**
 * Returns number of affected rows in previous database operation. If no previous operation exists, this returns false.
 *
 * @return int Number of affected rows
 */
	 function lastAffected() {
		  return $this->_pear->affectedRows();
	 }

/**
 * Returns number of rows in previous resultset. If no previous resultset exists,
 * this returns false.
 *
 * @return int Number of rows in resultset
 */
	 function lastNumRows() {
		  if (method_exists($this->_result, 'numRows')) {
				return $this->_result->numRows();
		  } else {
				return false;
		  }
	 }

/**
 * Returns the ID generated from the previous INSERT operation.
 *
 * @param string $table Name of the database table
 * @return int
 */
	 function lastInsertId($table) {
		  return $this->field('id', "SELECT MAX(id) FROM {$table}");
	 }

/**
 * Returns a limit statement in the correct format for the particular database.
 *
 * @param int $limit Limit of results returned
 * @param int $offset Offset from which to start results
 * @return string SQL limit/offset statement
 */
	 function selectLimit($limit, $offset = '0') {
		  return ' ' . $this->_pear->modifyLimitQuery('', $offset, $limit);
	 }
/**
 * Inserts multiple values into a join table
 *
 * @param string $table
 * @param string $fields
 * @param array $values
 */
	function insertMulti($table, $fields, $values) {
		$count = count($values);
		for ($x = 0; $x < $count; $x++) {
			$this->query("INSERT INTO {$table} ({$fields}) VALUES {$values[$x]}");
		}
	}
}
?>