/** * Check driver * * @param string Driver name * @return bool */ public static final function driver($name = '') { $name = Janitor::get_input(__CLASS__, $name, '', TRUE); if ($name == 'postgre') { $is_driver = (bool) (Core::$db->dbdriver == $name or isset(Core::$db->pdodriver) && Core::$db->pdodriver == 'pgsql' or strpos(Core::$db->hostname, 'pgsql') !== FALSE); } else { $is_driver = (bool) (Core::$db->dbdriver == $name or isset(Core::$db->pdodriver) && Core::$db->pdodriver == $name or strpos(Core::$db->hostname, $name) !== FALSE); } return $is_driver; }
/** * Overloading static method triggered when invoking special method. * * @param string * @param array * @return mixed */ public static function __callStatic($name, $args) { // Defined DBAL component $dbal = array('forge', 'util'); if (in_array($name, $dbal)) { // Return corresponding component (DB Forge or DB Util) $dbal_component = 'db' . $name; return static::${$dbal_component}; } elseif ($name == 'insert_id' && isset(static::$db->pdodriver) && static::$db->pdodriver == 'pgsql') { return static::$db->conn_id->lastInsertId(); } elseif ($name == 'last_created') { // Get last created entry if (($last_id = static::$db->insert_id()) && empty($last_id)) { // Nothing return NULL; } // Return the corresponding model instance with last id $gas = array_shift($args); return self::find($gas, array($last_id)); } elseif (preg_match('/^find_by_([^)]+)$/', $name, $m) && count($m) == 2) { // Get the instance, passed field and value for WHERE condition $gas = array_shift($args); $field = $m[1]; $value = array_shift($args); // Build the task onto the Gas instance $gas->recorder->set('where', array($field, $value)); $multirow = is_array($args) && !empty($args) ? current($args) : TRUE; return self::all($gas, $multirow); } elseif (preg_match('/^(min|max|avg|sum)$/', $name, $m) && count($m) == 2) { // Get the instance, passed arguments for SELECT condition $gas = array_shift($args); $type = $m[1]; $value = array_shift($args); $value = empty($value) ? $gas->primary_key : $value; // Build the task onto the Gas instance $gas->recorder->set('select_' . $type, array($value)); return self::all($gas, FALSE); } elseif (preg_match('/^(first|last)$/', $name, $m) && count($m) == 2) { // Get the instance, passed arguments for ORDER BY condition $gas = array_shift($args); $order = $m[1] == 'first' ? 'asc' : 'desc'; $collumn = array_shift($args); $collumn = is_null($collumn) ? $gas->primary_key : $collumn; // Build the task onto the Gas instance $gas->recorder->set('order_by', array($collumn, $order)); $gas->recorder->set('limit', array('1')); return self::all($gas, FALSE); } elseif (($method_type = self::diagnostic($name)) && !empty($method_type)) { // Give appropriate return, based by each task node needs if ($method_type == 'condition' or $method_type == 'selector') { // Always, sanitize arguments $args = Janitor::get_input($name, $args, TRUE); // Ensure once, in case there are some deprecated method if (!is_callable(array(static::$db, $name))) { throw new \BadMethodCallException('[' . $name . ']Unknown method.'); } // Build the task onto the Gas instance $gas = array_shift($args); $gas->recorder->set($name, $args); return $gas; } elseif ($method_type == 'executor') { $executor = static::$dictionary['executor']; $write = array_slice($executor, 0, 6); $operation = array_slice($executor, 6, 4); $utility = array_slice($executor, 10, 6); if (in_array($name, $utility)) { // This not affected any row or any record return static::$db->{$name}(); } else { // Always, sanitize arguments $args = Janitor::get_input($name, $args, TRUE); // Ensure once, in case there are some deprecated method if (!is_callable(array(static::$db, $name))) { throw new \BadMethodCallException('[' . $name . ']Unknown method.'); } // Build the task onto the Gas instance $gas = array_shift($args); // Merge the table alongside with sent arguments $table = $gas->validate_table()->table; $argument = array_unshift($args, $table); $gas->recorder->set($name, $args); return self::_execute($gas); } } } else { // Last try check relationships $gas = array_shift($args); if (FALSE != ($relationship = $gas->meta->get('entities.' . $name))) { // Gotcha! // Check for any pre-process options if (!empty($args)) { $relationship['options'] = array_merge($args, $relationship['options']); } return self::generate_entity($gas, $relationship); } // Good bye throw new \BadMethodCallException('[' . $name . ']Unknown method.'); } }