PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/database.php

http://prails.googlecode.com/
PHP | 147 lines | 92 code | 8 blank | 47 comment | 24 complexity | 776f2ed8a02b174b8618664f43b342f7 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. Prails Web Framework
  4. Copyright (C) 2010 Robert Kunze
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /**
  17. * @desc Abstraction layer for database access
  18. */
  19. class Database extends TblClass {
  20. function Database($prefix = "tbl_") {
  21. parent::TblClass($prefix);
  22. }
  23. /**
  24. * Retrieve more complex structured data from the database
  25. * @param STRING $query
  26. * @return Array of DBEntrys
  27. */
  28. function query($query) {
  29. return $this->SqlQuery($query);
  30. }
  31. /**
  32. * Retrieve data from a table
  33. * @param STRING $table table name (with or without prefix)
  34. * @param MIXED $filter retrieve what? (example: "customer_id=12" or Array("customer_id" => 12))
  35. * @param STRING $sort what and how to sort (example: "lastModified ASC")
  36. */
  37. function get($table, $filter = "", $sort = "", $start=0, $limit=999999) {
  38. if (strpos($table, $this->str_prefix) === false) {
  39. $table = $this->str_prefix . $table;
  40. }
  41. if (is_array($filter)) {
  42. $res = "";
  43. foreach ($filter as $key => $value) {
  44. if (strlen($res) > 0) $res .= " AND ";
  45. if (substr($key, -3) == "_id") {
  46. $res .= $key."=".(int)$value;
  47. } else {
  48. $res .= $key."='".$this->escape($value)."'";
  49. }
  50. }
  51. $filter = $res;
  52. }
  53. if (strlen($sort) > 0) {
  54. $sort = " ORDER BY ".$sort;
  55. }
  56. return $this->SqlQuery("SELECT * FROM ".$table." WHERE ".if_set($filter, "1").$sort." LIMIT ".$start.", ".$limit);
  57. }
  58. function select($table, $filter = "", $sort = "", $start = 0, $limit = 999999) { return $this->get($table, $filter, $sort, $start, $limit); }
  59. function getItem($table, $id) {
  60. if (strpos($table, $this->str_prefix) === false) {
  61. $table = $this->str_prefix . $table;
  62. }
  63. $pkfield = str_replace($this->str_prefix, "", $table)."_id";
  64. return @array_pop($this->SqlQuery("SELECT * FROM ".$table." WHERE ".$pkfield."=".if_set($id, "0")));
  65. }
  66. /**
  67. * Adds data to a table
  68. * @param STRING $table table name (with or without prefix)
  69. * @param ARRAY $data new data
  70. */
  71. function add($table, $data) {
  72. if (strpos($table, $this->str_prefix) === false) {
  73. $table = $this->str_prefix . $table;
  74. }
  75. return $this->InsertQuery($table, $data);
  76. }
  77. function insert($table, $data) { return $this->add($table, $data); }
  78. function ins($table, $data) { return $this->add($table, $data); }
  79. /**
  80. * Removes data from a table
  81. * @param STRING $table table name (with or without prefix)
  82. * @param MIXED $filter remove what?
  83. */
  84. function remove($table, $filter) {
  85. if (strpos($table, $this->str_prefix) === false) {
  86. $table = $this->str_prefix . $table;
  87. }
  88. if (is_array($filter)) {
  89. $res = "";
  90. foreach ($filter as $key => $value) {
  91. if (strlen($res) > 0) $res .= " AND ";
  92. if (substr($key, -3) == "_id") {
  93. $res .= $key."=".(int)$value;
  94. } else {
  95. $res .= $key."='".$this->escape($value)."'";
  96. }
  97. }
  98. $filter = $res;
  99. }
  100. $this->DeleteQuery($table, if_set($filter, "FALSE"));
  101. }
  102. function delete($table, $filter) { $this->remove($table, $filter); }
  103. function del($table, $filter) { $this->remove($table, $filter); }
  104. /**
  105. * Updates data in a table
  106. * @param STRING $table table name (with or without prefix)
  107. * @param MIXED $filter update what?
  108. * @param ARRAY $data new data
  109. */
  110. function update($table, $data, $filter) {
  111. if (strpos($table, $this->str_prefix) === false) {
  112. $table = $this->str_prefix . $table;
  113. }
  114. if (is_array($filter)) {
  115. $res = "";
  116. foreach ($filter as $key => $value) {
  117. if (strlen($res) > 0) $res .= " AND ";
  118. if (substr($key, -3) == "_id") {
  119. $res .= $key."=".(int)$value;
  120. } else {
  121. $res .= $key."='".$this->escape($value)."'";
  122. }
  123. }
  124. $filter = $res;
  125. }
  126. $this->UpdateQuery($table, $data, if_set($filter, "FALSE"));
  127. }
  128. function edit($table, $data, $filter) { $this->update($table, $data, $filter); }
  129. function set($table, $data, $filter = null) {
  130. if ($filter == null)
  131. return $this->add($table, $data);
  132. else
  133. $this->update($table, $data, $filter);
  134. }
  135. }
  136. ?>