1: <?php
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: class DboOdbc extends DboSource {
37: 38: 39: 40: 41:
42: var $description = "ODBC DBO Driver";
43: 44: 45: 46: 47:
48: var $startQuote = "`";
49: 50: 51: 52: 53:
54: var $endQuote = "`";
55: 56: 57: 58: 59:
60: var $_baseConfig = array(
61: 'persistent' => true,
62: 'login' => 'root',
63: 'password' => '',
64: 'database' => 'cake',
65: 'connect' => 'odbc_pconnect'
66: );
67: 68: 69: 70: 71:
72: var $columns = array();
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85: 86: 87: 88: 89:
90: function connect() {
91: $config = $this->config;
92: $connect = $config['connect'];
93: if (!$config['persistent']) {
94: $connect = 'odbc_connect';
95: }
96: if (!function_exists($connect)) {
97: exit('no odbc?');
98: }
99: $this->connected = false;
100: $this->connection = $connect($config['database'], $config['login'], $config['password'], SQL_CUR_USE_ODBC);
101: if ($this->connection) {
102: $this->connected = true;
103: }
104:
105: return $this->connected;
106: }
107: 108: 109: 110: 111:
112: function enabled() {
113: return extension_loaded('odbc');
114: }
115: 116: 117: 118: 119:
120: function disconnect() {
121: return @odbc_close($this->connection);
122: }
123: 124: 125: 126: 127: 128: 129:
130: function _execute($sql) {
131: switch ($sql) {
132: case 'BEGIN':
133: return odbc_autocommit($this->connection, false);
134: case 'COMMIT':
135: return odbc_commit($this->connection);
136: case 'ROLLBACK':
137: return odbc_rollback($this->connection);
138: }
139:
140: return odbc_exec($this->connection, $sql);
141: }
142: 143: 144: 145: 146:
147: function listSources() {
148: $cache = parent::listSources();
149: if ($cache != null) {
150: return $cache;
151: }
152:
153: $result = odbc_tables($this->connection);
154:
155: $tables = array();
156: while (odbc_fetch_row($result)) {
157: array_push($tables, odbc_result($result, 'TABLE_NAME'));
158: }
159:
160: parent::listSources($tables);
161: return $tables;
162: }
163: 164: 165: 166: 167: 168:
169: function &describe(&$model) {
170: $cache=parent::describe($model);
171:
172: if ($cache != null) {
173: return $cache;
174: }
175:
176: $fields = array();
177: $sql = 'SELECT * FROM ' . $this->fullTableName($model);
178: $result = odbc_exec($this->connection, $sql);
179:
180: $count = odbc_num_fields($result);
181:
182: for ($i = 1; $i <= $count; $i++) {
183: $cols[$i - 1] = odbc_field_name($result, $i);
184: }
185:
186: foreach ($cols as $column) {
187: $type = odbc_field_type(odbc_exec($this->connection, 'SELECT ' . $column . ' FROM ' . $this->fullTableName($model)), 1);
188: $fields[$column] = array('type' => $type);
189: }
190:
191: $this->__cacheDescription($model->tablePrefix . $model->table, $fields);
192: return $fields;
193: }
194: 195: 196: 197: 198: 199: 200: 201:
202: function value($data, $column = null) {
203: $parent=parent::value($data, $column);
204:
205: if ($parent != null) {
206: return $parent;
207: }
208:
209: if ($data === null) {
210: return 'NULL';
211: }
212:
213: if (!is_numeric($data)) {
214: return "'" . $data . "'";
215: }
216:
217: return $data;
218: }
219: 220: 221: 222: 223:
224: function lastError() {
225: if ($error = odbc_errormsg($this->connection)) {
226: return odbc_error($this->connection) . ': ' . $error;
227: }
228: return null;
229: }
230: 231: 232: 233: 234: 235:
236: function lastAffected() {
237: if ($this->hasResult()) {
238: return odbc_num_rows($this->_result);
239: }
240: return null;
241: }
242: 243: 244: 245: 246: 247:
248: function lastNumRows() {
249: if ($this->hasResult()) {
250: return odbc_num_rows($this->_result);
251: }
252: return null;
253: }
254: 255: 256: 257: 258: 259:
260: function lastInsertId($source = null) {
261: $result = $this->fetchRow('SELECT @@IDENTITY');
262: return $result[0];
263: }
264: 265: 266: 267: 268:
269: function column($real) {
270: if (is_array($real)) {
271: $col=$real['name'];
272: if (isset($real['limit'])) {
273: $col .= '(' . $real['limit'] . ')';
274: }
275: return $col;
276: }
277: return $real;
278: }
279: 280: 281: 282: 283:
284: function resultSet(&$results) {
285: $this->results =& $results;
286: $num_fields = odbc_num_fields($results);
287: $this->map = array();
288: $index = 0;
289: $j = 0;
290: while ($j < $num_fields) {
291: $column = odbc_field_name($results, $j+1);
292:
293: if (strpos($column, '_dot_') !== false) {
294: list($table, $column) = explode('_dot_', $column);
295: $this->map[$index++] = array($table, $column);
296: } else {
297: $this->map[$index++] = array(0, $column);
298: }
299: $j++;
300: }
301: }
302: 303: 304: 305: 306: 307: 308: 309:
310: function fields(&$model, $alias = null, $fields = null, $quote = true) {
311: if (empty($alias)) {
312: $alias = $model->name;
313: }
314: if (!is_array($fields)) {
315: if ($fields != null) {
316: $fields = array_map('trim', explode(',', $fields));
317: } else {
318: foreach($model->tableToModel as $tableName => $modelName) {
319: foreach($this->__descriptions[$model->tablePrefix .$tableName] as $field => $type) {
320: $fields[] = $modelName .'.' .$field;
321: }
322: }
323: }
324: }
325:
326: $count = count($fields);
327:
328: if ($count >= 1 && $fields[0] != '*' && strpos($fields[0], 'COUNT(*)') === false) {
329: for ($i = 0; $i < $count; $i++) {
330: if (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
331: $prepend = '';
332: if (strpos($fields[$i], 'DISTINCT') !== false) {
333: $prepend = 'DISTINCT ';
334: $fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));
335: }
336:
337: if (strrpos($fields[$i], '.') === false) {
338: $fields[$i] = $prepend . $this->name($alias) . '.' . $this->name($fields[$i]) . ' AS ' . $this->name($alias . '_dot_' . $fields[$i]);
339: } else {
340: $build = explode('.', $fields[$i]);
341: $fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]) . ' AS ' . $this->name($build[0] . '_dot_' . $build[1]);
342: }
343: }
344: }
345: }
346: return $fields;
347: }
348: 349: 350: 351: 352:
353: function fetchResult() {
354: if ($row = odbc_fetch_row($this->results)) {
355: $resultRow = array();
356: $numFields = odbc_num_fields($this->results);
357: $i = 0;
358: for($i = 0; $i < $numFields; $i++) {
359: list($table, $column) = $this->map[$i];
360: $resultRow[$table][$column] = odbc_result($this->results, $i + 1);
361: }
362: return $resultRow;
363: }
364: return false;
365: }
366: }
367: ?>