PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/yadal/class.ODBC.php

https://github.com/reshadf/Library
PHP | 262 lines | 215 code | 10 blank | 37 comment | 7 complexity | 8c90a11277b1325d06b426900774a912 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Yadal interface for ODBC
  4. *
  5. * @todo This file is not finished at all!!!!
  6. *
  7. * @package Yadal
  8. */
  9. /**
  10. * NOTE!! THIS VERSION DOES NOT WORK
  11. * SEE test/test.odbc.php!!
  12. */
  13. /**
  14. * class ODBC
  15. *
  16. * Yadal - Yet Another Database Abstraction Layer
  17. * ODBC
  18. *
  19. * @author Teye Heimans
  20. * @package Yadal
  21. */
  22. class ODBC extends Yadal {
  23. var $_dsn; // dsn
  24. /**
  25. * ODBC::ODBC()
  26. *
  27. * Constructor
  28. *
  29. * @return ODBC
  30. * @access public
  31. * @author Teye Heimans
  32. */
  33. function ODBC( $db ) {
  34. $this->Yadal( $db );
  35. $this->_nameQuote = "'";
  36. }
  37. /**
  38. * ODBC::connect()
  39. *
  40. * Make a connection with the database and
  41. * select the database.
  42. *
  43. * @param string dns: the dns to connect to
  44. * @param string username: the username which should be used to login
  45. * @param string password: the password which should be used to login
  46. * @return resource: The connection resource or false on failure
  47. * @access public
  48. * @author Teye Heimans
  49. */
  50. function connect( $dsn = '', $username = '', $password = '' ) {
  51. // connect with odbc
  52. $this->_conn = odbc_connect( $dsn, $username, $password );
  53. $this->_dsn = $dsn;
  54. if( $this->_conn ) {
  55. $this->_isConnected = true;
  56. // return the connection resource
  57. return $this->_conn;
  58. }
  59. return false;
  60. }
  61. /**
  62. * ODBC::query()
  63. *
  64. * Execute the query
  65. *
  66. * @param string $query: the query
  67. * @return resource
  68. * @access public
  69. * @author Teye Heimans
  70. */
  71. function query( $query ) {
  72. $this->_lastQuery = $query;
  73. // execute the query
  74. return odbc_exec( $this->_conn, $query );
  75. }
  76. /**
  77. * ODBC::getInsertId()
  78. *
  79. * Get the id of the last inserted record
  80. *
  81. * @return int
  82. * @access public
  83. * @author Teye Heimans
  84. */
  85. function getInsertId() {
  86. //
  87. }
  88. /**
  89. * ODBC::getError()
  90. *
  91. * Return the last error
  92. *
  93. * @return string
  94. * @access public
  95. * @author Teye Heimans
  96. */
  97. function getError() {
  98. return odbc_errormsg( $this->_conn );
  99. }
  100. /**
  101. * ODBC::recordCount()
  102. *
  103. * Return the number of records found by the query
  104. *
  105. * @param resource $sql: The sql resource we have to count
  106. * @return int
  107. * @access public
  108. * @author Teye Heimans
  109. */
  110. function recordCount( $sql) {
  111. return odbc_num_rows( $sql );
  112. }
  113. /**
  114. * ODBC::getRecord()
  115. *
  116. * Fetch a record in assoc mode and return it
  117. *
  118. * @param resource $sql: The sql resource where we have to get a row from
  119. * @return: assoc array or false when there are no records left
  120. * @access public
  121. * @author Teye Heimans
  122. */
  123. function getRecord( $sql ) {
  124. return mysql_fetch_assoc( $sql );
  125. }
  126. /**
  127. * ODBC::getFieldNames()
  128. *
  129. * Return the field names of the table
  130. *
  131. * @param string $table: the table where we should fetch the field names from
  132. * @return array
  133. * @access public
  134. * @author Teye Heimans
  135. */
  136. function getFieldNames( $table ) {
  137. $sql = odbc_columns( $this->_conn );
  138. $result = array();
  139. $num = odbc_num_fields( $sql );
  140. for ( $i = 1; $i <= $num; $i++ ) {
  141. $result[$i-1] = odbc_field_name($sql, $i);
  142. }
  143. $num = odbc_num_rows( $sql );
  144. echo "Aantal rows: $num<br />\n";
  145. for( $i = 0; $i <= $num; $i++ ) {
  146. echo odbc_result( $sql, 4) ."<br >\n";
  147. }
  148. return $result;
  149. }
  150. /**
  151. * ODBC::escapeString()
  152. *
  153. * Public: escape the string we are going to save from dangerous characters
  154. *
  155. * @param string $string
  156. * @return string
  157. */
  158. function escapeString( $string ) {
  159. return mysql_real_escape_string( $string );
  160. }
  161. /**
  162. * ODBC::fetchKeys()
  163. *
  164. * Public: fetch the keys from the table
  165. *
  166. * @return array of the keys which are found
  167. */
  168. function fetchKeys( $table = null) {
  169. $table = is_null($table) ? $this->_table : $table;
  170. $tmp = $this->_sql;
  171. //odbc_primarykeys( $this->_sql
  172. $sql = $this->query("SHOW KEYS FROM `".$table."`");
  173. $keys = array();
  174. while( $r = $this->getRecord() ) {
  175. if ( $r['Key_name'] == 'PRIMARY' ) {
  176. $keys['PR'][] = $r['Column_name'];
  177. } else {
  178. $keys[$r['Key_name']][] = $r['Column_name'];
  179. }
  180. }
  181. mysql_free_result($sql);
  182. $this->_sql = $tmp;
  183. // if no keys are found...
  184. if(sizeof($keys) == 0) {
  185. trigger_error(
  186. "Error, could not fetch the indexes from table '".$table."'! ".
  187. "If you didn't define a primary key or another index type, ".
  188. "please set the name of the field (which should be used for indexing) ".
  189. "manually in the dbinfo() function!",
  190. E_USER_WARNING
  191. );
  192. return null;
  193. }
  194. if(isset($keys['PR'])) {
  195. return $keys['PR'];
  196. } else {
  197. $d = each( $keys );
  198. return $d[1];
  199. }
  200. }
  201. /**
  202. * ODBC::fetchUniqueFields()
  203. *
  204. * Public: fetch the unique fields from the table
  205. *
  206. * @param string $table
  207. * @return array
  208. */
  209. function fetchUniqueFields( $table = null ) {
  210. $table = is_null($table) ? $this->_table : $table;
  211. $tmp = $this->_sql;
  212. $sql = $this->query("SHOW KEYS FROM `".$table."`");
  213. $unique = array();
  214. while( $r = $this->getRecord() ) {
  215. if ( $r['Non_unique'] == 0) {
  216. $unique[] = $r['Column_name'];
  217. }
  218. }
  219. mysql_free_result($sql);
  220. $this->_sql = $tmp;
  221. if(sizeof($unique) > 0) {
  222. return $unique;
  223. } else {
  224. return array();
  225. }
  226. }
  227. }
  228. ?>