protected static function exec($sql, $p = [], $limit = 1000, $offset = 0, $cache = false) { try { $p = is_null($p) ? [] : $p; $limit = is_null($limit) ? 1000 : $limit; $offset = is_null($offset) ? 0 : $offset; $bean = get_called_class(); $query = DB::query($sql . " limit " . $limit . " offset " . $offset)->parameters($p); if ($cache) { $sec = intval(Config::get("const.query_cache_seconds")); $res = $query->cached($sec)->as_object($bean)->execute()->as_array(); } else { $res = $query->as_object($bean)->execute()->as_array(); Log::info("\r\n*** SQL ***********************************************************\r\n" . DB::last_query() . "\r\n" . "*******************************************************************\r\n"); } return $res; } catch (Exception $e) { list($unified_code, $platform_code, $error_text) = DB::error_info(); Log::error("unified\t:" . $unified_code); Log::error("platform\t:" . $platform_code); Log::error("ERROR\t:" . $error_text); Log::error("SQL\t:" . DB::last_query()); throw $e; } }
public static function get_error_message($message = null, $is_db_error = false, $default_message = null) { if (is_null($default_message) && $is_db_error) { $default_message = 'データベースエラーが発生しました。'; } if ($is_db_error && is_prod_env()) { return $default_message; } if ($message) { if (is_string($message)) { return $message; } elseif (is_callable(array($message, 'getMessage'))) { $message = $message->getMessage(); } } if (!$message && $is_db_error && ($error_info = DB::error_info())) { $message = sprintf('unified_code:[%s] platform_code:[%s] message: %s', $error_info[0], $error_info[1], $error_info[2]); } return $message ?: $default_message; }
/** * @param caleg_id * @param user_email * @param title * @param content * @param rating */ function get_rate_comment_caleg() { //Check whether this user_email has rated this caleg_id $calegId = Input::get('caleg_id'); $userEmail = Input::get('user_email'); $result = DB::select('rating')->from('caleg_rating')->where('caleg_id', $calegId)->where('user_email', $userEmail)->execute(); //If user has commented and rated in the past, update, otherwise insert $rating = Input::get('rating'); $title = Input::get('title'); $content = Input::get('content'); if ($result->count() > 0) { Log::debug(__FUNCTION__ . ': ' . 'update'); $result = DB::update('caleg_rating')->set(array('title' => $title, 'content' => $content, 'updated' => time(), 'rating' => $rating))->where('caleg_id', $calegId)->where('user_email', $userEmail)->execute(); } else { Log::debug(__FUNCTION__ . ': ' . 'insert'); $result = DB::insert('caleg_rating')->set(array('caleg_id' => $calegId, 'user_email' => $userEmail, 'rating' => $rating, 'title' => $title, 'content' => $content, 'created' => time(), 'updated' => time()))->execute(); } $err = DB::error_info(); Log::debug(__FUNCTION__ . ' err: ' . print_r($err, 1)); Log::debug(__FUNCTION__ . ': kueri terakhir - ' . DB::last_query()); Log::debug(__FUNCTION__ . ': ' . print_r($result, 1)); $return = array(); if (!empty($err[1])) { $return = array('status' => false, 'err' => $err); } else { $return = array('status' => true); } //Get dapil id from the given $calegId $caleg = file_get_contents('http://api.pemiluapi.org/candidate/api/caleg/' . $calegId . '?apiKey=' . self::$apiKey); $caleg = json_decode($caleg); $caleg = $caleg->data->results->caleg[0]; if (!empty($caleg) && !empty($caleg->dapil)) { $dapilId = $caleg->dapil->id; Util::notifyUsers($dapilId, $caleg, Input::all()); } $this->response($return); }