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: class DboSybase extends DboSource {
35: 36: 37: 38: 39:
40: var $description = "Sybase DBO Driver";
41: 42: 43: 44: 45:
46: var $startQuote = "";
47: 48: 49: 50: 51:
52: var $endQuote = "";
53: 54: 55: 56: 57:
58: var $_baseConfig = array(
59: 'persistent' => true,
60: 'host' => 'localhost',
61: 'login' => 'sa',
62: 'password' => '',
63: 'database' => 'cake',
64: 'port' => '4100'
65: );
66: 67: 68: 69: 70:
71: var $columns = array(
72: 'primary_key' => array('name' => 'numeric(9,0) IDENTITY PRIMARY KEY'),
73: 'string' => array('name' => 'varchar', 'limit' => '255'),
74: 'text' => array('name' => 'text'),
75: 'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
76: 'float' => array('name' => 'float', 'formatter' => 'floatval'),
77: 'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
78: 'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
79: 'time' => array('name' => 'datetime', 'format' => 'H:i:s', 'formatter' => 'date'),
80: 'date' => array('name' => 'datetime', 'format' => 'Y-m-d', 'formatter' => 'date'),
81: 'binary' => array('name' => 'image'),
82: 'boolean' => array('name' => 'bit')
83: );
84: 85: 86: 87: 88:
89: function connect() {
90: $config = $this->config;
91:
92: $port = '';
93: if ($config['port'] !== null) {
94: $port = ':' . $config['port'];
95: }
96: if ($config['persistent']) {
97: $this->connection = sybase_pconnect($config['host'] . $port, $config['login'], $config['password']);
98: } else {
99: $this->connection = sybase_connect($config['host'] . $port, $config['login'], $config['password'], true);
100: }
101: $this->connected = sybase_select_db($config['database'], $this->connection);
102: return $this->connected;
103: }
104: 105: 106: 107: 108:
109: function enabled() {
110: return extension_loaded('sybase') || extension_loaded('sybase_ct');
111: }
112: 113: 114: 115: 116:
117: function disconnect() {
118: $this->connected = !@sybase_close($this->connection);
119: return !$this->connected;
120: }
121: 122: 123: 124: 125: 126: 127:
128: function _execute($sql) {
129: return sybase_query($sql, $this->connection);
130: }
131: 132: 133: 134: 135:
136: function listSources() {
137: $cache = parent::listSources();
138: if ($cache != null) {
139: return $cache;
140: }
141:
142: $result = $this->_execute("SELECT name FROM sysobjects WHERE type IN ('U', 'V')");
143: if (!$result) {
144: return array();
145: } else {
146:
147: $tables = array();
148: while ($line = sybase_fetch_array($result)) {
149: $tables[] = $line[0];
150: }
151:
152: parent::listSources($tables);
153: return $tables;
154: }
155: }
156: 157: 158: 159: 160: 161:
162: function describe(&$model) {
163:
164: $cache = parent::describe($model);
165: if ($cache != null) {
166: return $cache;
167: }
168:
169: $fields = false;
170: $cols = $this->query('DESC ' . $this->fullTableName($model));
171:
172: foreach ($cols as $column) {
173: $colKey = array_keys($column);
174: if (isset($column[$colKey[0]]) && !isset($column[0])) {
175: $column[0] = $column[$colKey[0]];
176: }
177: if (isset($column[0])) {
178: $fields[$column[0]['Field']] = array(
179: 'type' => $this->column($column[0]['Type']),
180: 'null' => $column[0]['Null'],
181: 'length' => $this->length($column[0]['Type']),
182: );
183: }
184: }
185:
186: $this->__cacheDescription($model->tablePrefix.$model->table, $fields);
187: return $fields;
188: }
189: 190: 191: 192: 193: 194: 195: 196:
197: function value($data, $column = null, $safe = false) {
198: $parent = parent::value($data, $column, $safe);
199:
200: if ($parent != null) {
201: return $parent;
202: }
203:
204: if ($data === null) {
205: return 'NULL';
206: }
207:
208: if ($data === '') {
209: return "''";
210: }
211:
212: switch ($column) {
213: case 'boolean':
214: $data = $this->boolean((bool)$data);
215: break;
216: default:
217: $data = str_replace("'", "''", $data);
218: break;
219: }
220:
221: return "'" . $data . "'";
222: }
223: 224: 225: 226: 227: 228: 229:
230: function begin(&$model) {
231: if (parent::begin($model)) {
232: if ($this->execute('BEGIN TRAN')) {
233: $this->_transactionStarted = true;
234: return true;
235: }
236: }
237: return false;
238: }
239: 240: 241: 242: 243: 244: 245: 246:
247: function commit(&$model) {
248: if (parent::commit($model)) {
249: $this->_transactionStarted = false;
250: return $this->execute('COMMIT TRAN');
251: }
252: return false;
253: }
254: 255: 256: 257: 258: 259: 260: 261:
262: function rollback(&$model) {
263: if (parent::rollback($model)) {
264: return $this->execute('ROLLBACK TRAN');
265: }
266: return false;
267: }
268: 269: 270: 271: 272: 273:
274: function lastError() {
275: return null;
276: }
277: 278: 279: 280: 281: 282:
283: function lastAffected() {
284: if ($this->_result) {
285: return sybase_affected_rows($this->connection);
286: }
287: return null;
288: }
289: 290: 291: 292: 293: 294:
295: function lastNumRows() {
296: if ($this->hasResult()) {
297: return @sybase_num_rows($this->_result);
298: }
299: return null;
300: }
301: 302: 303: 304: 305: 306:
307: function lastInsertId($source = null) {
308: $result=$this->fetchRow('SELECT @@IDENTITY');
309: return $result[0];
310: }
311: 312: 313: 314: 315: 316:
317: function column($real) {
318: if (is_array($real)) {
319: $col = $real['name'];
320: if (isset($real['limit']))
321: {
322: $col .= '('.$real['limit'].')';
323: }
324: return $col;
325: }
326:
327: $col = str_replace(')', '', $real);
328: $limit = null;
329: if (strpos($col, '(') !== false) {
330: list($col, $limit) = explode('(', $col);
331: }
332:
333: if (in_array($col, array('datetime', 'smalldatetime'))) {
334: return 'datetime';
335: } elseif (in_array($col, array('int', 'bigint', 'smallint', 'tinyint'))) {
336: return 'integer';
337: } elseif (in_array($col, array('float', 'double', 'real', 'decimal', 'money', 'numeric', 'smallmoney'))) {
338: return 'float';
339: } elseif (strpos($col, 'text') !== false) {
340: return 'text';
341: } elseif (in_array($col, array('char', 'nchar', 'nvarchar', 'string', 'varchar'))) {
342: return 'binary';
343: } elseif (in_array($col, array('binary', 'image', 'varbinary'))) {
344: return 'binary';
345: }
346:
347: return 'text';
348: }
349: 350: 351: 352: 353:
354: function resultSet(&$results) {
355: $this->results =& $results;
356: $this->map = array();
357: $num_fields = sybase_num_fields($results);
358: $index = 0;
359: $j = 0;
360:
361: while ($j < $num_fields) {
362:
363: $column = sybase_fetch_field($results,$j);
364: if (!empty($column->table)) {
365: $this->map[$index++] = array($column->table, $column->name);
366: } else {
367: $this->map[$index++] = array(0, $column->name);
368: }
369: $j++;
370: }
371: }
372: 373: 374: 375: 376:
377: function fetchResult() {
378: if ($row = sybase_fetch_row($this->results)) {
379: $resultRow = array();
380: $i = 0;
381: foreach ($row as $index => $field) {
382: list($table, $column) = $this->map[$index];
383: $resultRow[$table][$column] = $row[$index];
384: $i++;
385: }
386: return $resultRow;
387: } else {
388: return false;
389: }
390: }
391: }
392: ?>