You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuDataAccess.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace Luticate\Utils\DataAccess;
  3. use Illuminate\Database\Capsule\Manager as Capsule;
  4. use Illuminate\Database\Connection;
  5. use Illuminate\Database\Query\Builder;
  6. use Luticate\Utils\Business\LuArrayUtils;
  7. use Luticate\Utils\Dbo\LuDbo;
  8. abstract class LuDataAccess {
  9. protected static $_connection = "mydb";
  10. protected static $_table = "mydb_table";
  11. protected static $_table_as = "__t__";
  12. protected static $_dboClass = LuDbo::class;
  13. protected static $_dboArrayClass = LuDbo::class;
  14. protected static $_dboIgnoreList = [];
  15. public static function transact(\Closure $function)
  16. {
  17. return static::getConnection()->transaction($function);
  18. }
  19. /**
  20. * @return \PDO
  21. */
  22. public static function getPdo()
  23. {
  24. return Capsule::connection(static::$_connection)->getPdo();
  25. }
  26. /**
  27. * @return Connection
  28. */
  29. public static function getConnection()
  30. {
  31. return Capsule::connection(static::$_connection);
  32. }
  33. /**
  34. * @return Builder
  35. */
  36. public static function getTableAs()
  37. {
  38. return static::getConnection()->table(static::$_table . " as " . static::$_table_as);
  39. }
  40. /**
  41. * @return Builder
  42. */
  43. public static function getTable()
  44. {
  45. return static::getConnection()->table(static::$_table);
  46. }
  47. /**
  48. * @param $query Builder|callable
  49. * @return Builder
  50. */
  51. public static function resolveQuery($query)
  52. {
  53. if (is_callable($query)) {
  54. $query = $query(static::getTableAs());
  55. }
  56. return $query;
  57. }
  58. /**
  59. * @param $dbo LuDbo
  60. * @param array $ignoreList
  61. * @return array
  62. */
  63. public static function prepareDbo($dbo, $ignoreList = [])
  64. {
  65. $json = $dbo->jsonSerialize();
  66. foreach (array_merge(static::$_dboIgnoreList, $ignoreList) as $ignore) {
  67. unset($json[$ignore]);
  68. }
  69. $json = LuArrayUtils::camelCaseToSnake($json);
  70. return $json;
  71. }
  72. /**
  73. * @param $query Builder|callable
  74. * @return LuDBo[]
  75. */
  76. public static function getMultiple($query)
  77. {
  78. $query = static::resolveQuery($query);
  79. $string = $query->aggregate("json_agg", [static::$_table_as]);
  80. $json = json_decode($string, true);
  81. $data = LuArrayUtils::snakeToCamelCase($json);
  82. return call_user_func([static::$_dboArrayClass, 'jsonDeserialize'], $data);
  83. }
  84. /**
  85. * @param $query Builder|callable
  86. * @return LuDBo
  87. */
  88. public static function getSingle($query)
  89. {
  90. $query = static::resolveQuery($query);
  91. $string = $query->take(1)->aggregate("json_agg", [static::$_table_as]);
  92. if (is_null($string)) {
  93. return null;
  94. }
  95. $json = json_decode($string, true);
  96. $data = LuArrayUtils::snakeToCamelCase($json);
  97. if (count($data) < 1) {
  98. return null;
  99. }
  100. return call_user_func([static::$_dboClass, 'jsonDeserialize'], $data[0]);
  101. }
  102. /**
  103. * @param $id
  104. * @return LuDbo
  105. */
  106. public static function getSingleById($id)
  107. {
  108. return static::getSingle(static::getTableAs()->where("id", "=", $id));
  109. }
  110. /**
  111. * @param $id
  112. * @return int
  113. */
  114. public static function deleteSingleById($id)
  115. {
  116. return static::getTableAs()->delete($id);
  117. }
  118. /**
  119. * @param $query Builder|callable
  120. * @return int
  121. */
  122. public static function deleteMultiple($query)
  123. {
  124. $query = static::resolveQuery($query);
  125. return $query->delete();
  126. }
  127. /**
  128. * @param $dbo LuDbo
  129. * @param string[] $ignoreList
  130. * @return bool
  131. */
  132. public static function addSingle($dbo, $ignoreList = [])
  133. {
  134. $json = static::prepareDbo($dbo, $ignoreList);
  135. return static::getTable()->insert($json);
  136. }
  137. /**
  138. * @param $dbo LuDbo
  139. * @param string[] $ignoreList
  140. * @return int
  141. */
  142. public static function addSingleId($dbo, $ignoreList = [])
  143. {
  144. $json = static::prepareDbo($dbo, $ignoreList);
  145. unset($json['id']);
  146. return static::getTable()->insertGetId($json);
  147. }
  148. /**
  149. * @param $dbo LuDbo
  150. * @param string[] $ignoreList
  151. * @return int
  152. */
  153. public static function editSingleById($dbo, $ignoreList = [])
  154. {
  155. $json = static::prepareDbo($dbo, $ignoreList);
  156. $id = $json['id'];
  157. unset($json['id']);
  158. return static::getTable()->where("id", "=", $id)->update($json);
  159. }
  160. /**
  161. * @param $dbo LuDbo
  162. * @param $query Builder|callable
  163. * @param string[] $ignoreList
  164. * @return int
  165. */
  166. public static function editMultiple($dbo, $query, $ignoreList = [])
  167. {
  168. $query = static::resolveQuery($query);
  169. $json = static::prepareDbo($dbo, $ignoreList);
  170. return $query->update($json);
  171. }
  172. }