dbo_pear.php
Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . PEAR);
00035 vendor ('Pear/DB');
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 class DboPear extends DboSource{
00046
00047
00048
00049
00050
00051
00052
00053 var $_pear = null;
00054
00055
00056
00057
00058
00059
00060
00061 function connect($config) {
00062 $this->config =$config;
00063 $dsn =$config['driver'] . ':
00064 . $config['host'] . '/' . $config['database'];
00065 $options=array('debug' => Configure::read() - 1,
00066 'portability' => DB_PORTABILITY_ALL,);
00067
00068 $this->_pear =&DB::connect($dsn, $options);
00069 $this->connected=$this->_pear ? true : false;
00070 return !(PEAR::isError($this->_pear));
00071 }
00072
00073
00074
00075
00076
00077
00078 function disconnect() {
00079 die (__('Please implement DBO::disconnect() first.'));
00080 }
00081
00082
00083
00084
00085
00086
00087
00088 function execute($sql) {
00089 return $this->_pear->query($sql);
00090 }
00091
00092
00093
00094
00095
00096
00097 function fetchRow($sql = null) {
00098 if (!empty($sql) && is_string($sql) && strlen($sql) > 5) {
00099 if (!$this->execute($sql)) {
00100 return null;
00101 }
00102 }
00103 return $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
00104 }
00105
00106
00107
00108
00109
00110
00111
00112 function tablesList() {
00113 $driver=$this->config['driver'];
00114 $tables=array();
00115
00116 if ('postgres' == $driver) {
00117 $sql ="SELECT a.relname AS name
00118 FROM pg_class a, pg_user b
00119 WHERE ( relkind = 'r') and relname !~ '^pg_' AND relname !~ '^sql_'
00120 AND relname !~ '^xin[vx][0-9]+' AND b.usesysid = a.relowner
00121 AND NOT (EXISTS (SELECT viewname FROM pg_views WHERE viewname=a.relname));";
00122
00123 $result=$this->all($sql);
00124
00125 foreach ($result as $item) {
00126 $tables[] = $item['name'];
00127 }
00128 } elseif ('mysql' == $driver) {
00129 $result=array();
00130 $result=mysql_list_tables($this->config['database']);
00131
00132 while ($item = mysql_fetch_array($result)) {
00133 $tables[] = $item[0];
00134 }
00135 } else {
00136 die (__('Please implement DBO_Pear::tablesList() for your database driver.'));
00137 }
00138
00139 if (!$result) {
00140 trigger_error(ERROR_NO_TABLE_LIST, E_USER_ERROR);
00141 exit;
00142 } else {
00143 return $tables;
00144 }
00145 }
00146
00147
00148
00149
00150
00151
00152
00153 function fields($tableName) {
00154 $data =$this->_pear->tableInfo($tableName);
00155 $fields=false;
00156
00157 foreach ($data as $item) {
00158 $fields[] = array('name' => $item['name'],
00159 'type' => $item['type']);
00160 }
00161
00162 return $fields;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171 function prepareValue($data) {
00172 return $this->_pear->quoteSmart($data);
00173 }
00174
00175
00176
00177
00178
00179
00180 function lastError() {
00181 return PEAR::isError($this->_result) ? $this->_result->getMessage() : null;
00182 }
00183
00184
00185
00186
00187
00188
00189 function lastAffected() {
00190 return $this->_pear->affectedRows();
00191 }
00192
00193
00194
00195
00196
00197
00198
00199 function lastNumRows() {
00200 if (method_exists($this->_result, 'numRows')) {
00201 return $this->_result->numRows();
00202 } else {
00203 return false;
00204 }
00205 }
00206
00207
00208
00209
00210
00211
00212
00213 function lastInsertId($table) {
00214 return $this->field('id', "SELECT MAX(id) FROM {$table}");
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224 function selectLimit($limit, $offset = '0') {
00225 return ' ' . $this->_pear->modifyLimitQuery('', $offset, $limit);
00226 }
00227
00228
00229
00230
00231
00232
00233
00234 function insertMulti($table, $fields, $values) {
00235 $count = count($values);
00236 for ($x = 0; $x < $count; $x++) {
00237 $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values[$x]}");
00238 }
00239 }
00240 }
00241 ?>