/projects/Fantasy/webroot/include/DataFacade.php

https://github.com/danielvaleradp/--ngara · PHP · 228 lines · 184 code · 42 blank · 2 comment · 18 complexity · cabd4cd9ff9ad05bd1d1627273b862a1 MD5 · raw file

  1. <?php
  2. class DataFacade {
  3. protected static function dolar($i) { return '$' . $i; }
  4. protected static function quote($i) { return '"' . $i . '"'; }
  5. protected static function conds($i, $j) { return self::quote($i) . ' = ' . self::dolar($j); }
  6. protected static function sets ($i, $j) { return 'SET ' . self::conds($i, $j); }
  7. public static function enum_values($type_name) {
  8. global $dbconn;
  9. global $pqs;
  10. $pq = 'ENUM';
  11. if (!isset($pqs)) $pqs = array();
  12. if (!in_array($pq, $pqs)) {
  13. $query = <<<'EOF'
  14. SELECT pg_enum.enumlabel
  15. FROM pg_catalog.pg_enum
  16. WHERE pg_enum.enumtypid = (
  17. SELECT pg_type.typelem
  18. FROM pg_type
  19. WHERE pg_type.typname = '_' || $1
  20. );
  21. EOF;
  22. $result = pg_prepare($dbconn, $pq, $query) or die('pg_prepare: ' . pg_last_error());
  23. $pqs[] = $pq;
  24. }
  25. $result = pg_execute($dbconn, $pq, array($type_name)) or die('pg_execute: ' . pg_last_error());
  26. $r = array();
  27. while ($row = pg_fetch_row($result)) $r[] = $row[0];
  28. pg_free_result($result);
  29. return $r;
  30. }
  31. // No se incluyen en el query a los campos que no estén definidos.
  32. // Si están definidos y valen null, sí se incluyen y se guarda NULL.
  33. public static function insert($entity) {
  34. global $dbconn;
  35. global $pqs;
  36. $ec = get_class($entity);
  37. $en = $ec::table();
  38. $ef = $ec::fields();
  39. $fs = array_reduce(
  40. $ef,
  41. function ($acc, $f) use (&$entity) {
  42. if ($entity->is_set($f)) $acc[$f] = $entity->get($f);
  43. return $acc;
  44. },
  45. array()
  46. );
  47. $fn = array_keys($fs);
  48. $fc = count($fs);
  49. $fr = range(1, $fc);
  50. $pq = 'INSERT ' . $en . '(' . join(', ', $fn) . ')';
  51. if (!isset($pqs)) $pqs = array();
  52. if (!in_array($pq, $pqs)) {
  53. $query =
  54. 'INSERT INTO "Fantasy"."' . $en . '" (' .
  55. join(', ', array_map('self::quote', $fn)) .
  56. ') VALUES (' .
  57. join(', ', array_map('self::dolar', $fr)) .
  58. ')';
  59. $result = pg_prepare($dbconn, $pq, $query) or die('pg_prepare: ' . pg_last_error());
  60. $pqs[] = $pq;
  61. }
  62. $result = pg_execute($dbconn, $pq, array_values($fs)) or die('pg_execute: ' . pg_last_error());
  63. return;
  64. }
  65. public static function retrieveAll($ec) {
  66. global $dbconn;
  67. $en = $ec::table();
  68. $ef = $ec::fields();
  69. $query =
  70. 'SELECT ' . join(', ', array_map('self::quote', $ef)) .
  71. ' FROM "Fantasy"."' . $en . '"';
  72. $result = pg_query($dbconn, $query) or die('pg_prepare: ' . pg_last_error());
  73. $n = pg_num_fields($result);
  74. $r = array();
  75. while ($row = pg_fetch_row($result)) {
  76. $e = new $ec;
  77. for ($i = 0; $i < $n; ++$i) {
  78. $e->set(pg_field_name($result, $i), $row[$i]);
  79. }
  80. $r[] = $e;
  81. }
  82. pg_free_result($result);
  83. return $r;
  84. }
  85. public static function select($entity) {
  86. global $dbconn;
  87. global $pqs;
  88. $ec = get_class($entity);
  89. $en = $ec::table();
  90. $ef = $ec::fields();
  91. $pk = $ec::pk();
  92. $kn = count($pk);
  93. $kr = range(1, $kn);
  94. $pq = 'SELECT ' . $en;
  95. $data = array_map(
  96. function ($i) use (&$entity) { return $entity->get($i); },
  97. $pk
  98. );
  99. if (!isset($pqs)) $pqs = array();
  100. if (!in_array($pq, $pqs)) {
  101. $query =
  102. 'SELECT ' . join(', ', array_map('self::quote', $ef)) .
  103. ' FROM "Fantasy"."' . $en . '" WHERE ' .
  104. join(' AND ', array_map('self::conds', $pk, $kr));
  105. $result = pg_prepare($dbconn, $pq, $query) or die('pg_prepare: ' . pg_last_error());
  106. $pqs[] = $pq;
  107. }
  108. $result = pg_execute($dbconn, $pq, $data) or die('pg_execute: ' . pg_last_error());
  109. if (pg_num_rows($result) === 0) return null;
  110. $row = pg_fetch_row($result) or die('pg_fetch_row: ' . pg_last_error());
  111. $e = new $ec;
  112. $n = pg_num_fields($result);
  113. for ($i = 1; $i < $n; ++$i) {
  114. $e->set(pg_field_name($result, $i), $row[$i]);
  115. }
  116. pg_free_result($result);
  117. return $e;
  118. }
  119. public static function update($entity) {
  120. global $dbconn;
  121. global $pqs;
  122. $ec = get_class($entity);
  123. $en = $ec::table();
  124. $ef = $ec::fields();
  125. $fs = array_reduce(
  126. $ef,
  127. function ($acc, $f) use (&$entity) {
  128. if ($entity->is_set($f)) $acc[$f] = $entity->get($f);
  129. return $acc;
  130. },
  131. array()
  132. );
  133. $fn = array_keys($fs);
  134. $fc = count($fs);
  135. $fr = range(1, $fc);
  136. $pk = $ec::pk();
  137. $kn = count($pk);
  138. $kr = range($fc + 1, $fc + $kn);
  139. $pq = 'UPDATE ' . $en . '(' . join(', ', $fn) . ')';
  140. $get = function ($i) use (&$entity) { return $entity->get($i); };
  141. $data = array_merge(array_values($fs), array_map($get, $pk));
  142. if (!isset($pqs)) $pqs = array();
  143. if (!in_array($pq, $pqs)) {
  144. $query =
  145. 'UPDATE "Fantasy"."' . $en . '" SET ' .
  146. join(', ' , array_map('self::conds', $fs, $fr)) . ' WHERE ' .
  147. join(' AND ', array_map('self::conds', $pk, $kr));
  148. $result = pg_prepare($dbconn, $pq, $query) or die('pg_prepare: ' . pg_last_error());
  149. $pqs[] = $pq;
  150. }
  151. $result = pg_execute($dbconn, $pq, $data) or die('pg_execute: ' . pg_last_error());
  152. return true;
  153. }
  154. public static function removeAll($ec) {
  155. global $dbconn;
  156. $en = $ec::table();
  157. $query = 'DELETE FROM "Fantasy"."' . $en . '"';
  158. $result = pg_query($dbconn, $query) or die('pg_prepare: ' . pg_last_error());
  159. return true;
  160. }
  161. public static function remove($entity) {
  162. global $dbconn;
  163. global $pqs;
  164. $ec = get_class($entity);
  165. $en = $ec::table();
  166. $pk = $ec::pk();
  167. $kn = count($pk);
  168. $kr = range(1, $kn);
  169. $pq = 'DELETE ' . $en;
  170. $data = array_map(
  171. function ($i) use (&$entity) { return $entity->get($i); },
  172. $pk
  173. );
  174. if (!isset($pqs)) $pqs = array();
  175. if (!in_array($pq, $pqs)) {
  176. $query =
  177. 'DELETE FROM "Fantasy"."' . $en . '" WHERE ' .
  178. join(' AND ', array_map('self::conds', $pk, $kr));
  179. $result = pg_prepare($dbconn, $pq, $query) or die('pg_prepare: ' . pg_last_error());
  180. $pqs[] = $pq;
  181. }
  182. $result = pg_execute($dbconn, $pq, $data) or die('pg_execute: ' . pg_last_error());
  183. return true;
  184. }
  185. }
  186. ?>