PageRenderTime 33ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/core/db/sqlite/query.class.php

https://github.com/konfirm/konsolidate
PHP | 158 lines | 51 code | 14 blank | 93 comment | 4 complexity | 7113565cef5142a8be4e5f7b1dd84e98 MD5 | raw file
  1. <?php
  2. /*
  3. * ________ ___
  4. * / / /\ /\ Konsolidate
  5. * ____/ /___/ \/ \
  6. * / /\ / http://www.konsolidate.nl
  7. * /___ ___/ \ /
  8. * \ / /\ \ / \ Class: CoreDBSQLiteQuery
  9. * \/___/ \___\/ \ Tier: Core
  10. * \ \ /\ \ /\ / Module: DB/SQLite/Query
  11. * \___\/ \___\/ \/
  12. * \ \ / $Rev$
  13. * \___ ___\/ $Author$
  14. * \ \ / $Date$
  15. * \___\/
  16. */
  17. /**
  18. * SQLite result set (this object is instanced and returned for every query)
  19. * @name CoreDBSQLiteQuery
  20. * @type class
  21. * @package Konsolidate
  22. * @author Rogier Spieker <rogier@konsolidate.nl>
  23. */
  24. class CoreDBSQLiteQuery extends Konsolidate
  25. {
  26. /**
  27. * Internal auto-replacements, in order to gain common SQL functionality (e.g. 'NOW()')
  28. * @name _replace
  29. * @type array
  30. * @access protected
  31. */
  32. protected $_replace;
  33. /**
  34. * The error number
  35. * @name errno
  36. * @type int
  37. * @access public
  38. */
  39. public $errno;
  40. /**
  41. * The error message
  42. * @name error
  43. * @type string
  44. * @access public
  45. */
  46. public $error;
  47. /**
  48. * execute given query on given connection
  49. * @name execute
  50. * @type method
  51. * @access public
  52. * @param string query
  53. * @param resource connection
  54. * @return void
  55. * @syntax void CoreDBSQLiteQuery->execute( string query, resource connection )
  56. */
  57. public function execute( $sQuery, &$rConnection )
  58. {
  59. $this->_replace = Array(
  60. "NOW()"=>microtime( true )
  61. );
  62. $this->query = str_replace( array_keys( $this->_replace ), array_values( $this->_replace ), $sQuery );
  63. $this->_conn = $rConnection;
  64. $this->_result = @sqlite_query( $this->query, $this->_conn, SQLITE_BOTH, $sError );
  65. if ( is_resource( $this->_result ) )
  66. $this->rows = sqlite_num_rows( $this->_result );
  67. // We want the exception object to tell us everything is going extremely well, don't throw it!
  68. $this->import( "../exception.class.php" );
  69. $this->exception = new CoreDBSQLiteException( sqlite_last_error( $this->_conn ) );
  70. $this->errno = &$this->exception->errno;
  71. $this->error = &$this->exception->error;
  72. }
  73. /**
  74. * rewind the internal resultset
  75. * @name rewind
  76. * @type method
  77. * @access public
  78. * @return bool success
  79. * @syntax bool CoreDBSQLiteQuery->rewind()
  80. */
  81. public function rewind()
  82. {
  83. if ( is_resource( $this->_result ) && sqlite_num_rows( $this->_result ) > 0 )
  84. return sqlite_rewind( $this->_result );
  85. return false;
  86. }
  87. /**
  88. * get the next result from the internal resultset
  89. * @name next
  90. * @type method
  91. * @access public
  92. * @return object resultrow
  93. * @syntax object CoreDBSQLiteQuery->next()
  94. */
  95. public function next()
  96. {
  97. if ( is_resource( $this->_result ) )
  98. return sqlite_fetch_object( $this->_result );
  99. return false;
  100. }
  101. /**
  102. * get the ID of the last inserted record
  103. * @name lastInsertID
  104. * @type method
  105. * @access public
  106. * @return int id
  107. * @syntax int CoreDBSQLiteQuery->lastInsertID()
  108. */
  109. public function lastInsertID()
  110. {
  111. return sqlite_last_insert_rowid( $this->_conn );
  112. }
  113. /**
  114. * get the ID of the last inserted record
  115. * @name lastInsertID
  116. * @type method
  117. * @access public
  118. * @return int id
  119. * @syntax int CoreDBSQLiteQuery->lastId()
  120. * @note alias for lastInsertID
  121. * @see lastInsertID
  122. */
  123. public function lastId()
  124. {
  125. return $this->lastInsertID();
  126. }
  127. /**
  128. * Retrieve an array containing all resultrows as objects
  129. * @name fetchAll
  130. * @type method
  131. * @access public
  132. * @return array result
  133. * @syntax array CoreDBSQLiteQuery->fetchAll()
  134. */
  135. public function fetchAll()
  136. {
  137. $aReturn = Array();
  138. while( $oRecord = $this->next() )
  139. array_push( $aReturn, $oRecord );
  140. $this->rewind();
  141. return $aReturn;
  142. }
  143. }
  144. ?>