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
00035
00036
00037 class DboOdbc extends DboSource {
00038
00039
00040
00041
00042
00043 var $description = "ODBC DBO Driver";
00044
00045
00046
00047
00048
00049 var $startQuote = "`";
00050
00051
00052
00053
00054
00055 var $endQuote = "`";
00056
00057
00058
00059
00060
00061 var $_baseConfig = array(
00062 'persistent' => true,
00063 'login' => 'root',
00064 'password' => '',
00065 'database' => 'cake',
00066 'connect' => 'odbc_pconnect'
00067 );
00068
00069
00070
00071
00072
00073 var $columns = array();
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 function connect() {
00092 $config = $this->config;
00093 $connect = $config['connect'];
00094 if (!$config['persistent']) {
00095 $connect = 'odbc_connect';
00096 }
00097 if (!function_exists($connect)) {
00098 die('no odbc?');
00099 }
00100 $this->connected = false;
00101 $this->connection = $connect($config['database'], $config['login'], $config['password'], SQL_CUR_USE_ODBC);
00102 if ($this->connection) {
00103 $this->connected = true;
00104 }
00105
00106 return $this->connected;
00107 }
00108
00109
00110
00111
00112
00113 function disconnect() {
00114 return @odbc_close($this->connection);
00115 }
00116
00117
00118
00119
00120
00121
00122
00123 function _execute($sql) {
00124 switch ($sql) {
00125 case 'BEGIN':
00126 return odbc_autocommit($this->connection, false);
00127 case 'COMMIT':
00128 return odbc_commit($this->connection);
00129 case 'ROLLBACK':
00130 return odbc_rollback($this->connection);
00131 }
00132
00133 return odbc_exec($this->connection, $sql);
00134 }
00135
00136
00137
00138
00139
00140 function listSources() {
00141 $cache = parent::listSources();
00142 if ($cache != null) {
00143 return $cache;
00144 }
00145
00146 $result = odbc_tables($this->connection);
00147
00148 $tables = array();
00149 while (odbc_fetch_row($result)) {
00150 array_push($tables, odbc_result($result, 'TABLE_NAME'));
00151 }
00152
00153 parent::listSources($tables);
00154 return $tables;
00155 }
00156
00157
00158
00159
00160
00161
00162 function &describe(&$model) {
00163 $cache=parent::describe($model);
00164
00165 if ($cache != null) {
00166 return $cache;
00167 }
00168
00169 $fields = array();
00170 $sql = 'SELECT * FROM ' . $this->fullTableName($model);
00171 $result = odbc_exec($this->connection, $sql);
00172
00173 $count = odbc_num_fields($result);
00174
00175 for ($i = 1; $i <= $count; $i++) {
00176 $cols[$i - 1] = odbc_field_name($result, $i);
00177 }
00178
00179 foreach ($cols as $column) {
00180 $type = odbc_field_type(odbc_exec($this->connection, 'SELECT ' . $column . ' FROM ' . $this->fullTableName($model)), 1);
00181 $fields[$column] = array('type' => $type);
00182 }
00183
00184 $this->__cacheDescription($model->tablePrefix . $model->table, $fields);
00185 return $fields;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195 function value($data, $column = null) {
00196 $parent=parent::value($data, $column);
00197
00198 if ($parent != null) {
00199 return $parent;
00200 }
00201
00202 if ($data === null) {
00203 return 'NULL';
00204 }
00205
00206 if (!is_numeric($data)) {
00207 return "'" . $data . "'";
00208 }
00209
00210 return $data;
00211 }
00212
00213
00214
00215
00216
00217 function lastError() {
00218 if ($error = odbc_errormsg($this->connection)) {
00219 return odbc_error($this->connection) . ': ' . $error;
00220 }
00221 return null;
00222 }
00223
00224
00225
00226
00227
00228
00229 function lastAffected() {
00230 if ($this->hasResult()) {
00231 return odbc_num_rows($this->_result);
00232 }
00233 return null;
00234 }
00235
00236
00237
00238
00239
00240
00241 function lastNumRows() {
00242 if ($this->hasResult()) {
00243 return odbc_num_rows($this->_result);
00244 }
00245 return null;
00246 }
00247
00248
00249
00250
00251
00252
00253 function lastInsertId($source = null) {
00254 $result = $this->fetchRow('SELECT @@IDENTITY');
00255 return $result[0];
00256 }
00257
00258
00259
00260
00261
00262 function column($real) {
00263 if (is_array($real)) {
00264 $col=$real['name'];
00265 if (isset($real['limit'])) {
00266 $col .= '(' . $real['limit'] . ')';
00267 }
00268 return $col;
00269 }
00270 return $real;
00271 }
00272
00273
00274
00275
00276
00277 function resultSet(&$results) {
00278 $this->results =& $results;
00279 $num_fields = odbc_num_fields($results);
00280 $this->map = array();
00281 $index = 0;
00282 $j = 0;
00283 while ($j < $num_fields) {
00284 $column = odbc_field_name($results, $j+1);
00285
00286 if (strpos($column, '_dot_') !== false) {
00287 list($table, $column) = explode('_dot_', $column);
00288 $this->map[$index++] = array($table, $column);
00289 } else {
00290 $this->map[$index++] = array(0, $column);
00291 }
00292 $j++;
00293 }
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303 function fields(&$model, $alias = null, $fields = null, $quote = true) {
00304 if (empty($alias)) {
00305 $alias = $model->name;
00306 }
00307 if (!is_array($fields)) {
00308 if ($fields != null) {
00309 $fields = array_map('trim', explode(',', $fields));
00310 } else {
00311 foreach($model->tableToModel as $tableName => $modelName) {
00312 foreach($this->__descriptions[$model->tablePrefix .$tableName] as $field => $type) {
00313 $fields[] = $modelName .'.' .$field;
00314 }
00315 }
00316 }
00317 }
00318
00319 $count = count($fields);
00320
00321 if ($count >= 1 && $fields[0] != '*' && strpos($fields[0], 'COUNT(*)') === false) {
00322 for ($i = 0; $i < $count; $i++) {
00323 if (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
00324 $prepend = '';
00325 if (strpos($fields[$i], 'DISTINCT') !== false) {
00326 $prepend = 'DISTINCT ';
00327 $fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));
00328 }
00329
00330 if (strrpos($fields[$i], '.') === false) {
00331 $fields[$i] = $prepend . $this->name($alias) . '.' . $this->name($fields[$i]) . ' AS ' . $this->name($alias . '_dot_' . $fields[$i]);
00332 } else {
00333 $build = explode('.', $fields[$i]);
00334 $fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]) . ' AS ' . $this->name($build[0] . '_dot_' . $build[1]);
00335 }
00336 }
00337 }
00338 }
00339 return $fields;
00340 }
00341
00342
00343
00344
00345
00346 function fetchResult() {
00347 if ($row = odbc_fetch_row($this->results)) {
00348 $resultRow = array();
00349 $numFields = odbc_num_fields($this->results);
00350 $i = 0;
00351 for($i = 0; $i < $numFields; $i++) {
00352 list($table, $column) = $this->map[$i];
00353 $resultRow[$table][$column] = odbc_result($this->results, $i + 1);
00354 }
00355 return $resultRow;
00356 }
00357 return false;
00358 }
00359 }
00360 ?>