protected static function _findBySql($sql, array $params = [], $multiple = true) { $data = \DB::query(Database::SELECT, $sql)->parameters($params)->execute(); if ($multiple) { $result = []; foreach ($data as $row) { $result[] = static::_createObject($row); } return $result; } else { if (!count($data)) { return null; } return static::_createObject($data->current()); } }
public static function makeSql($statement, array $criteria) { $statements = [Database::SELECT => 'SELECT', Database::DELETE => 'DELETE']; $action = $statements[$statement]; isset($criteria['OFFSET']) or $criteria['OFFSET'] = 0; isset($criteria['params']) or $criteria['params'] = []; if (!isset($criteria[$action])) { $criteria[$action] = $statement === Database::SELECT ? ['*'] : []; } $select = is_array($criteria[$action]) ? implode(",\n\t", $criteria[$action]) : $criteria[$action]; $from = is_array($criteria['FROM']) ? implode(', ', $criteria['FROM']) : $criteria['FROM']; $join = isset($criteria['JOIN']) ? is_array($criteria['JOIN']) ? "\n" . implode("\n", $criteria['JOIN']) : "\n" . $criteria['JOIN'] : ''; $group_by = isset($criteria['GROUP_BY']) ? "\nGROUP BY \n\t" . (is_array($criteria['GROUP_BY']) ? implode(",\n\t", $criteria['GROUP_BY']) : $criteria['GROUP_BY']) : ''; $having = isset($criteria['HAVING']) ? "\nHAVING \n\t(" . implode(") \n\tAND (", $criteria['HAVING']) . ')' : ''; $where = !empty($criteria['WHERE']) ? is_array($criteria['WHERE']) ? "\nWHERE \n\t(" . implode(") \n\tAND (", $criteria['WHERE']) . ')' : "\nWHERE " . $criteria['WHERE'] : ''; $order_by = isset($criteria['ORDER_BY']) ? "\nORDER BY " . (is_array($criteria['ORDER_BY']) ? implode(', ', $criteria['ORDER_BY']) : $criteria['ORDER_BY']) : ''; $limit = isset($criteria['LIMIT']) ? "\nLIMIT {$criteria['LIMIT']} OFFSET {$criteria['OFFSET']}" : ''; $sql = "{$statements[$statement]} \n\t{$select} \nFROM {$from} {$join} {$where} {$group_by} {$having} {$order_by} {$limit}"; return \DB::query($statement, $sql)->parameters($criteria['params']); }