PageRenderTime 24ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/cakephp/vendors/cake/libs/model/datasources/dbo/dbo_odbc.php

https://github.com/castlino/linonico.com
PHP | 360 lines | 187 code | 21 blank | 152 comment | 38 complexity | eae13be903209f91de840169f6dc1490 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id: dbo_odbc.php 7945 2008-12-19 02:16:01Z gwoo $ */
  3. /**
  4. * ODBC for DBO
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  19. * @package cake
  20. * @subpackage cake.cake.libs.model.dbo
  21. * @since CakePHP(tm) v 0.10.5.1790
  22. * @version $Revision: 7945 $
  23. * @modifiedby $LastChangedBy: gwoo $
  24. * @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
  25. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  26. */
  27. /**
  28. * Short description for class.
  29. *
  30. * Long description for class
  31. *
  32. * @package cake
  33. * @subpackage cake.cake.libs.model.datasources.dbo
  34. */
  35. class DboOdbc extends DboSource {
  36. /**
  37. * Driver description
  38. *
  39. * @var string
  40. */
  41. var $description = "ODBC DBO Driver";
  42. /**
  43. * Table/column starting quote
  44. *
  45. * @var string
  46. */
  47. var $startQuote = "`";
  48. /**
  49. * Table/column end quote
  50. *
  51. * @var string
  52. */
  53. var $endQuote = "`";
  54. /**
  55. * Driver base configuration
  56. *
  57. * @var array
  58. */
  59. var $_baseConfig = array(
  60. 'persistent' => true,
  61. 'login' => 'root',
  62. 'password' => '',
  63. 'database' => 'cake',
  64. 'connect' => 'odbc_pconnect'
  65. );
  66. /**
  67. * Enter description here...
  68. *
  69. * @var unknown_type
  70. */
  71. var $columns = array();
  72. // var $columns = array('primary_key' => array('name' => 'int(11) DEFAULT NULL auto_increment'),
  73. // 'string' => array('name' => 'varchar', 'limit' => '255'),
  74. // 'text' => array('name' => 'text'),
  75. // 'integer' => array('name' => 'int', 'limit' => '11'),
  76. // 'float' => array('name' => 'float'),
  77. // 'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d h:i:s', 'formatter' => 'date'),
  78. // 'timestamp' => array('name' => 'datetime', 'format' => 'Y-m-d h:i:s', 'formatter' => 'date'),
  79. // 'time' => array('name' => 'time', 'format' => 'h:i:s', 'formatter' => 'date'),
  80. // 'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'),
  81. // 'binary' => array('name' => 'blob'),
  82. // 'boolean' => array('name' => 'tinyint', 'limit' => '1'));
  83. /**
  84. * Connects to the database using options in the given configuration array.
  85. *
  86. * @return boolean True if the database could be connected, else false
  87. */
  88. function connect() {
  89. $config = $this->config;
  90. $connect = $config['connect'];
  91. if (!$config['persistent']) {
  92. $connect = 'odbc_connect';
  93. }
  94. if (!function_exists($connect)) {
  95. die('no odbc?');
  96. }
  97. $this->connected = false;
  98. $this->connection = $connect($config['database'], $config['login'], $config['password'], SQL_CUR_USE_ODBC);
  99. if ($this->connection) {
  100. $this->connected = true;
  101. }
  102. return $this->connected;
  103. }
  104. /**
  105. * Disconnects from database.
  106. *
  107. * @return boolean True if the database could be disconnected, else false
  108. */
  109. function disconnect() {
  110. return @odbc_close($this->connection);
  111. }
  112. /**
  113. * Executes given SQL statement.
  114. *
  115. * @param string $sql SQL statement
  116. * @return resource Result resource identifier
  117. * @access protected
  118. */
  119. function _execute($sql) {
  120. switch ($sql) {
  121. case 'BEGIN':
  122. return odbc_autocommit($this->connection, false);
  123. case 'COMMIT':
  124. return odbc_commit($this->connection);
  125. case 'ROLLBACK':
  126. return odbc_rollback($this->connection);
  127. }
  128. // TODO: should flags be set? possible requirement: SQL_CURSOR_STATIC
  129. return odbc_exec($this->connection, $sql);
  130. }
  131. /**
  132. * Returns an array of sources (tables) in the database.
  133. *
  134. * @return array Array of tablenames in the database
  135. */
  136. function listSources() {
  137. $cache = parent::listSources();
  138. if ($cache != null) {
  139. return $cache;
  140. }
  141. $result = odbc_tables($this->connection);
  142. $tables = array();
  143. while (odbc_fetch_row($result)) {
  144. array_push($tables, odbc_result($result, 'TABLE_NAME'));
  145. }
  146. parent::listSources($tables);
  147. return $tables;
  148. }
  149. /**
  150. * Returns an array of the fields in given table name.
  151. *
  152. * @param Model $model Model object to describe
  153. * @return array Fields in table. Keys are name and type
  154. */
  155. function &describe(&$model) {
  156. $cache=parent::describe($model);
  157. if ($cache != null) {
  158. return $cache;
  159. }
  160. $fields = array();
  161. $sql = 'SELECT * FROM ' . $this->fullTableName($model);
  162. $result = odbc_exec($this->connection, $sql);
  163. $count = odbc_num_fields($result);
  164. for ($i = 1; $i <= $count; $i++) {
  165. $cols[$i - 1] = odbc_field_name($result, $i);
  166. }
  167. foreach ($cols as $column) {
  168. $type = odbc_field_type(odbc_exec($this->connection, 'SELECT ' . $column . ' FROM ' . $this->fullTableName($model)), 1);
  169. $fields[$column] = array('type' => $type);
  170. }
  171. $this->__cacheDescription($model->tablePrefix . $model->table, $fields);
  172. return $fields;
  173. }
  174. /**
  175. * Returns a quoted and escaped string of $data for use in an SQL statement.
  176. *
  177. * @param string $data String to be prepared for use in an SQL statement
  178. * @param string $column The column into which this data will be inserted
  179. * @return string Quoted and escaped
  180. * @todo Add logic that formats/escapes data based on column type
  181. */
  182. function value($data, $column = null) {
  183. $parent=parent::value($data, $column);
  184. if ($parent != null) {
  185. return $parent;
  186. }
  187. if ($data === null) {
  188. return 'NULL';
  189. }
  190. if (!is_numeric($data)) {
  191. return "'" . $data . "'";
  192. }
  193. return $data;
  194. }
  195. /**
  196. * Returns a formatted error message from previous database operation.
  197. *
  198. * @return string Error message with error number
  199. */
  200. function lastError() {
  201. if ($error = odbc_errormsg($this->connection)) {
  202. return odbc_error($this->connection) . ': ' . $error;
  203. }
  204. return null;
  205. }
  206. /**
  207. * Returns number of affected rows in previous database operation. If no previous operation exists,
  208. * this returns false.
  209. *
  210. * @return integer Number of affected rows
  211. */
  212. function lastAffected() {
  213. if ($this->hasResult()) {
  214. return odbc_num_rows($this->_result);
  215. }
  216. return null;
  217. }
  218. /**
  219. * Returns number of rows in previous resultset. If no previous resultset exists,
  220. * this returns false.
  221. *
  222. * @return int Number of rows in resultset
  223. */
  224. function lastNumRows() {
  225. if ($this->hasResult()) {
  226. return odbc_num_rows($this->_result);
  227. }
  228. return null;
  229. }
  230. /**
  231. * Returns the ID generated from the previous INSERT operation.
  232. *
  233. * @param unknown_type $source
  234. * @return int
  235. */
  236. function lastInsertId($source = null) {
  237. $result = $this->fetchRow('SELECT @@IDENTITY');
  238. return $result[0];
  239. }
  240. /**
  241. * Enter description here...
  242. *
  243. * @param string $real Real database-layer column type (i.e. "varchar(255)")
  244. */
  245. function column($real) {
  246. if (is_array($real)) {
  247. $col=$real['name'];
  248. if (isset($real['limit'])) {
  249. $col .= '(' . $real['limit'] . ')';
  250. }
  251. return $col;
  252. }
  253. return $real;
  254. }
  255. /**
  256. * Enter description here...
  257. *
  258. * @param unknown_type $results
  259. */
  260. function resultSet(&$results) {
  261. $this->results =& $results;
  262. $num_fields = odbc_num_fields($results);
  263. $this->map = array();
  264. $index = 0;
  265. $j = 0;
  266. while ($j < $num_fields) {
  267. $column = odbc_field_name($results, $j+1);
  268. if (strpos($column, '_dot_') !== false) {
  269. list($table, $column) = explode('_dot_', $column);
  270. $this->map[$index++] = array($table, $column);
  271. } else {
  272. $this->map[$index++] = array(0, $column);
  273. }
  274. $j++;
  275. }
  276. }
  277. /**
  278. * Generates the fields list of an SQL query.
  279. *
  280. * @param Model $model
  281. * @param string $alias Alias tablename
  282. * @param mixed $fields
  283. * @return array
  284. */
  285. function fields(&$model, $alias = null, $fields = null, $quote = true) {
  286. if (empty($alias)) {
  287. $alias = $model->name;
  288. }
  289. if (!is_array($fields)) {
  290. if ($fields != null) {
  291. $fields = array_map('trim', explode(',', $fields));
  292. } else {
  293. foreach($model->tableToModel as $tableName => $modelName) {
  294. foreach($this->__descriptions[$model->tablePrefix .$tableName] as $field => $type) {
  295. $fields[] = $modelName .'.' .$field;
  296. }
  297. }
  298. }
  299. }
  300. $count = count($fields);
  301. if ($count >= 1 && $fields[0] != '*' && strpos($fields[0], 'COUNT(*)') === false) {
  302. for ($i = 0; $i < $count; $i++) {
  303. if (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
  304. $prepend = '';
  305. if (strpos($fields[$i], 'DISTINCT') !== false) {
  306. $prepend = 'DISTINCT ';
  307. $fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));
  308. }
  309. if (strrpos($fields[$i], '.') === false) {
  310. $fields[$i] = $prepend . $this->name($alias) . '.' . $this->name($fields[$i]) . ' AS ' . $this->name($alias . '_dot_' . $fields[$i]);
  311. } else {
  312. $build = explode('.', $fields[$i]);
  313. $fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]) . ' AS ' . $this->name($build[0] . '_dot_' . $build[1]);
  314. }
  315. }
  316. }
  317. }
  318. return $fields;
  319. }
  320. /**
  321. * Fetches the next row from the current result set
  322. *
  323. * @return unknown
  324. */
  325. function fetchResult() {
  326. if ($row = odbc_fetch_row($this->results)) {
  327. $resultRow = array();
  328. $numFields = odbc_num_fields($this->results);
  329. $i = 0;
  330. for($i = 0; $i < $numFields; $i++) {
  331. list($table, $column) = $this->map[$i];
  332. $resultRow[$table][$column] = odbc_result($this->results, $i + 1);
  333. }
  334. return $resultRow;
  335. }
  336. return false;
  337. }
  338. }
  339. ?>