/framework/core/db/functions.php

http://zoop.googlecode.com/ · PHP · 264 lines · 97 code · 23 blank · 144 comment · 0 complexity · f44cc1df57dfefa8997b6f83d0caa573 MD5 · raw file

  1. <?php
  2. /**
  3. * Turn on echoing of sql statements
  4. *
  5. * @return nothing
  6. */
  7. function SqlEchoOn()
  8. {
  9. return DbModule::getDefaultConnection()->echoOn();
  10. }
  11. /**
  12. * Turn off echoing of sql statements
  13. *
  14. */
  15. function SqlEchoOff()
  16. {
  17. return DbModule::getDefaultConnection()->echoOff();
  18. }
  19. /**
  20. * Begin a transaction (not all database engines support transactions)
  21. *
  22. */
  23. function SqlBeginTransaction()
  24. {
  25. return DbModule::getDefaultConnection()->beginTransaction();
  26. }
  27. /**
  28. * Commit a transaction (not all database engines support transactions)
  29. *
  30. */
  31. function SqlCommitTransaction()
  32. {
  33. return DbModule::getDefaultConnection()->commitTransaction();
  34. }
  35. /**
  36. * Executes a database query. $params must be a $key => $value array of values to substitute into $sql
  37. *
  38. * @param string $sql SQL query with parameters in the format ":variablename" or ":variablename:datatype"
  39. * @param array($key=>$value) $params ($key => value) array of parameters to substitute into the SQL query. If you are not passing parameters in, params should be an empty array()
  40. * @return DbResultSet DbResultSet object
  41. */
  42. function SqlQuery($sql, $params)
  43. {
  44. return DbModule::getDefaultConnection()->query($sql, $params);
  45. }
  46. /**
  47. * Returns a DbSchema object
  48. * This function returns a DbSchema object that can be used to query the structure of the database in a DOM like fashion.
  49. *
  50. * @return DbSchema
  51. */
  52. function SqlGetSchema()
  53. {
  54. return DbModule::getDefaultConnection()->getSchema();
  55. }
  56. /**
  57. * Executes a SQL statement to alter the schema
  58. * Unlike SqlQuery, this doesn't return a DbResultSet or accept parameters
  59. *
  60. * @param string $sql SQL query to execute
  61. * @returns nothing
  62. */
  63. function SqlAlterSchema($sql)
  64. {
  65. return DbModule::getDefaultConnection()->alterSchema($sql);
  66. }
  67. /**
  68. * Executes the given query and returns the value of the first cell in the first row of the resultset.
  69. *
  70. * An error will occur if the query returns more than one row.
  71. *
  72. * @param string $sql SQL query with parameters in the format ":variablename" or ":variablename:datatype"
  73. * @param array($key=>$value) $params ($key => value) array of parameters to substitute into the SQL query. If you are not passing parameters in, params should be an empty array()
  74. * @return mixed value of cell or false if no record returned
  75. */
  76. function SqlFetchCell($sql, $params)
  77. {
  78. return DbModule::getDefaultConnection()->fetchCell($sql, $params);
  79. }
  80. /**
  81. * Executes the given query and returns the first row of the resultset.
  82. *
  83. * An error will occur if the query returns more than one row.
  84. *
  85. * @param string $sql SQL query with parameters in the format ":variablename" or ":variablename:datatype"
  86. * @param array($key=>$value) $params ($key => value) array of parameters to substitute into the SQL query. If you are not passing parameters in, params should be an empty array()
  87. * @return array row data or false if no record returned
  88. */
  89. function SqlFetchRow($sql, $params)
  90. {
  91. return DbModule::getDefaultConnection()->fetchRow($sql, $params);
  92. }
  93. /**
  94. * Returns an array containing the values from the first column of each row returned by the passed-in query
  95. *
  96. * @param string $sql SQL query with parameters in the format ":variablename" or ":variablename:datatype"
  97. * @param array($key=>$value) $params ($key => value) array of parameters to substitute into the SQL query. If you are not passing parameters in, params should be an empty array()
  98. * @return unknown
  99. */
  100. function SqlFetchColumn($sql, $params)
  101. {
  102. return DbModule::getDefaultConnection()->fetchColumn($sql, $params);
  103. }
  104. /**
  105. * Returns an array of all rows returned from the given SQL statement
  106. *
  107. * @param string $sql SQL query with parameters in the format ":variablename" or ":variablename:datatype"
  108. * @param array($key=>$value) $params ($key => value) array of parameters to substitute into the SQL query. If you are not passing parameters in, params should be an empty array()
  109. * @return unknown
  110. */
  111. function SqlFetchRows($sql, $params)
  112. {
  113. return DbModule::getDefaultConnection()->fetchRows($sql, $params);
  114. }
  115. /**
  116. * Returns a nested array, grouped by the fields (or field) listed in $mapFields
  117. * For example, if mapFields = array("person_id", "book_id"), and the resultset returns
  118. * a list of all the chapters of all the books of all the people, this will group the
  119. * records by person and by book, keeping each row in an array under
  120. * $var[$person_id][$book_id]
  121. *
  122. * @param string $sql SQL query with parameters in the format ":variablename" or ":variablename:datatype"
  123. * @param array $mapFields array of fields to group the results by
  124. * @param array($key=>$value) $params ($key => value) array of parameters to substitute into the SQL query. If you are not passing parameters in, params should be an empty array()
  125. * @return associative array structure grouped by the values in $mapFields
  126. */
  127. function SqlFetchMap($sql, $mapFields, $params)
  128. {
  129. return DbModule::getDefaultConnection()->fetchMap($sql, $mapFields, $params);
  130. }
  131. /**
  132. * Creates a simple nested array structure grouping the values of the $valueField column by the values of the columns specified in the $keyFields array.
  133. *
  134. * For example, if your query returns a list of books and you'd like to group the titles by subject and isbn number, let $keyFields = array("subject", "isbn") and $valueField = "title".
  135. * The format thus created will be $var[$subject][$isbn] = $title;
  136. *
  137. * @param string $sql SQL query with parameters in the format ":variablename" or ":variablename:datatype"
  138. * @param array $keyFields array of fields to group the results by
  139. * @param array $valueField name of the field containing the value to be grouped
  140. * @param array($key=>$value) $params ($key => value) array of parameters to substitute into the SQL query. If you are not passing parameters in, params should be an empty array()
  141. * @return associative array structure grouped by the values in $mapFields
  142. */
  143. function SqlFetchSimpleMap($sql, $keyFields, $valueField, $params)
  144. {
  145. return DbModule::getDefaultConnection()->fetchSimpleMap($sql, $keyFields, $valueField, $params);
  146. }
  147. /**
  148. * Enter description here...
  149. *
  150. * @param string $tableName Table into which we will insert the data
  151. * @param array $values ($key => $value) array in the format ($fieldName => $fieldValue)
  152. * @param boolean $serial True if the last inserted ID should be returned
  153. * @return mixed ID of the inserted row or false if $serial == false
  154. */
  155. function SqlInsertArray($tableName, $values, $serial = true)
  156. {
  157. return DbModule::getDefaultConnection()->insertArray($tableName, $values, $serial);
  158. }
  159. /**
  160. * Executes an update statement on the database (identical to SqlUpdateRow)
  161. *
  162. * Unlike SqlQuery, this method does not return a DbResultSet objects
  163. *
  164. * @param string $sql SQL query with parameters in the format ":variablename" or ":variablename:datatype"
  165. * @param array($key=>$value) $params ($key => value) array of parameters to substitute into the SQL query. If you are not passing parameters in, params should be an empty array()
  166. * @return number Number of affected rows (not all database engines support this)
  167. */
  168. function SqlModifyRow($sql, $params)
  169. {
  170. // Is this supposed to work different from SqlUpdateRow?
  171. //return DbModule::getDefaultConnection()->modifyRow($sql, $params);
  172. return SqlUpdateRow($sql, $params);
  173. }
  174. /**
  175. * This function is not yet implimented
  176. *
  177. * @param string $sql SQL query with parameters in the format ":variablename" or ":variablename:datatype"
  178. * @param array($key=>$value) $params ($key => value) array of parameters to substitute into the SQL query. If you are not passing parameters in, params should be an empty array()
  179. * @return unknown
  180. */
  181. function SqlModifyRowValues($tableName, $values)
  182. {
  183. return DbModule::getDefaultConnection()->modifyRowValues($tableName, $values);
  184. }
  185. /**
  186. * Execute an insert statement
  187. *
  188. * Unlike SqlQuery, this method does not return a DbResultSet
  189. *
  190. * @param string $sql SQL query with parameters in the format ":variablename" or ":variablename:datatype"
  191. * @param array($key=>$value) $params ($key => value) array of parameters to substitute into the SQL query. If you are not passing parameters in, params should be an empty array()
  192. * @return number Number of affected rows (not all database engines support this)
  193. */
  194. function SqlInsertRow($sql, $params)
  195. {
  196. return DbModule::getDefaultConnection()->insertRow($sql, $params);
  197. }
  198. function SqlInsertRows($sql, $params)
  199. {
  200. return DbModule::getDefaultConnection()->insertRows($sql, $params);
  201. }
  202. /**
  203. * This function has not yet been implemented
  204. *
  205. * @param string $sql SQL query with parameters in the format ":variablename" or ":variablename:datatype"
  206. * @param array($key=>$value) $params ($key => value) array of parameters to substitute into the SQL query. If you are not passing parameters in, params should be an empty array()
  207. * @return unknown
  208. */
  209. function SqlInsertRowValues($tableName, $values)
  210. {
  211. return DbModule::getDefaultConnection()->insertRowValues($tableName, $values);
  212. }
  213. /**
  214. * Executes an update statement on the database
  215. *
  216. * Unlike the method query(), this method does not return a DbResultSet objects
  217. *
  218. * @param string $sql SQL query with parameters in the format ":variablename" or ":variablename:datatype"
  219. * @param array($key=>$value) $params ($key => value) array of parameters to substitute into the SQL query. If you are not passing parameters in, params should be an empty array()
  220. * @return unknown
  221. */
  222. function SqlUpdateRow($sql, $params)
  223. {
  224. return DbModule::getDefaultConnection()->updateRow($sql, $params);
  225. }
  226. function SqlUpdateRows($sql, $params)
  227. {
  228. return DbModule::getDefaultConnection()->updateRows($sql, $params);
  229. }
  230. function SqlSelsertRow($tableName, $fieldNames, $conditions, $defaults = NULL, $lock = 0)
  231. {
  232. return DbModule::getDefaultConnection()->selsertRow($tableName, $fieldNames, $conditions, $defaults, $lock);
  233. }
  234. function SqlUpsertRow($tableName, $conditions, $values)
  235. {
  236. return DbModule::getDefaultConnection()->upsertRow($tableName, $conditions, $values);
  237. }
  238. function SqlDeleteRows($sql, $params)
  239. {
  240. return DbModule::getDefaultConnection()->deleteRows($sql, $params);
  241. }