123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
-
- namespace Luticate\Utils\DataAccess;
-
- use Illuminate\Database\Capsule\Manager as Capsule;
- use Illuminate\Database\Connection;
- use Illuminate\Database\Query\Builder;
- use Luticate\Utils\Business\LuArrayUtils;
- use Luticate\Utils\Dbo\LuDbo;
-
- abstract class LuDataAccess {
-
- protected static $_connection = "mydb";
- protected static $_table = "mydb_table";
- protected static $_table_as = "__t__";
- protected static $_dboClass = LuDbo::class;
- protected static $_dboArrayClass = LuDbo::class;
- protected static $_dboIgnoreList = [];
-
- public static function transact(\Closure $function)
- {
- return static::getConnection()->transaction($function);
- }
-
- /**
- * @return \PDO
- */
- public static function getPdo()
- {
- return Capsule::connection(static::$_connection)->getPdo();
- }
-
- /**
- * @return Connection
- */
- public static function getConnection()
- {
- return Capsule::connection(static::$_connection);
- }
-
- /**
- * @return Builder
- */
- public static function getTableAs()
- {
- return static::getConnection()->table(static::$_table . " as " . static::$_table_as);
- }
-
- /**
- * @return Builder
- */
- public static function getTable()
- {
- return static::getConnection()->table(static::$_table);
- }
-
- /**
- * @param $query Builder|callable
- * @return Builder
- */
- public static function resolveQuery($query)
- {
- if (is_callable($query)) {
- $query = $query(static::getTableAs());
- }
- return $query;
- }
-
- /**
- * @param $dbo LuDbo
- * @param array $ignoreList
- * @return array
- */
- public static function prepareDbo($dbo, $ignoreList = [])
- {
- $json = $dbo->jsonSerialize();
- foreach (array_merge(static::$_dboIgnoreList, $ignoreList) as $ignore) {
- unset($json[$ignore]);
- }
- $json = LuArrayUtils::camelCaseToSnake($json);
- return $json;
- }
-
- /**
- * @param $query Builder|callable
- * @return LuDBo[]
- */
- public static function getMultiple($query)
- {
- $query = static::resolveQuery($query);
- $string = $query->aggregate("json_agg", [static::$_table_as]);
- $json = json_decode($string, true);
- $data = LuArrayUtils::snakeToCamelCase($json);
-
- return call_user_func([static::$_dboArrayClass, 'jsonDeserialize'], $data);
- }
-
- /**
- * @param $query Builder|callable
- * @return LuDBo
- */
- public static function getSingle($query)
- {
- $query = static::resolveQuery($query);
- $string = $query->take(1)->aggregate("json_agg", [static::$_table_as]);
- if (is_null($string)) {
- return null;
- }
- $json = json_decode($string, true);
- $data = LuArrayUtils::snakeToCamelCase($json);
- if (count($data) < 1) {
- return null;
- }
-
- return call_user_func([static::$_dboClass, 'jsonDeserialize'], $data[0]);
- }
-
- /**
- * @param $id
- * @return LuDbo
- */
- public static function getSingleById($id)
- {
- return static::getSingle(static::getTableAs()->where("id", "=", $id));
- }
-
- /**
- * @param $id
- * @return int
- */
- public static function deleteSingleById($id)
- {
- return static::getTableAs()->delete($id);
- }
-
- /**
- * @param $query Builder|callable
- * @return int
- */
- public static function deleteMultiple($query)
- {
- $query = static::resolveQuery($query);
- return $query->delete();
- }
-
- /**
- * @param $dbo LuDbo
- * @param string[] $ignoreList
- * @return bool
- */
- public static function addSingle($dbo, $ignoreList = [])
- {
- $json = static::prepareDbo($dbo, $ignoreList);
- return static::getTable()->insert($json);
- }
-
- /**
- * @param $dbo LuDbo
- * @param string[] $ignoreList
- * @return int
- */
- public static function addSingleId($dbo, $ignoreList = [])
- {
- $json = static::prepareDbo($dbo, $ignoreList);
- unset($json['id']);
- return static::getTable()->insertGetId($json);
- }
-
- /**
- * @param $dbo LuDbo
- * @param string[] $ignoreList
- * @return int
- */
- public static function editSingleById($dbo, $ignoreList = [])
- {
- $json = static::prepareDbo($dbo, $ignoreList);
- $id = $json['id'];
- unset($json['id']);
- return static::getTable()->where("id", "=", $id)->update($json);
- }
-
- /**
- * @param $dbo LuDbo
- * @param $query Builder|callable
- * @param string[] $ignoreList
- * @return int
- */
- public static function editMultiple($dbo, $query, $ignoreList = [])
- {
- $query = static::resolveQuery($query);
- $json = static::prepareDbo($dbo, $ignoreList);
- return $query->update($json);
- }
- }
|