public static function log_runtime($req) { $end_time = microtime(true); $framework_time = (double) ($req->start_times["request"] - $req->start_times["init"]); if (isset($req->start_times["app"])) { $app_time = (double) ($end_time - $req->start_times["app"]); } $total_time = (double) ($end_time - $req->start_times["init"]); if (class_exists('\\ActiveRecord\\ConnectionManager') && \ActiveRecord\ConnectionManager::connection_count()) { $db_time = (double) \ActiveRecord\ConnectionManager::get_connection()->reset_database_time(); if (isset($app_time)) { $app_time -= $db_time; } } $status = "200"; foreach (headers_list() as $header) { if (preg_match("/^Status: (\\d+)/", $header, $m)) { $status = $m[1]; } } $log = "Completed in " . sprintf("%0.5f", $total_time); if (isset($db_time)) { $log .= " | DB: " . sprintf("%0.5f", $db_time) . " (" . intval($db_time / $total_time * 100) . "%)"; } if (isset($app_time)) { $log .= " | App: " . sprintf("%0.5f", $app_time) . " (" . intval($app_time / $total_time * 100) . "%)"; } $log .= " | Framework: " . sprintf("%0.5f", $framework_time) . " (" . intval($framework_time / $total_time * 100) . "%)"; $log .= " | " . $status . " [" . $req->url . "]"; if (isset($req->redirected_to)) { $log .= " -> [" . $req->redirected_to . "]"; } Log::info($log); }
/** * @param string $connection * @return Connection */ public static function connection($connection = null) { if (is_null($connection)) { $connection = static::config('database')->get('default_connection', 'default'); } return ConnectionManager::get_connection($connection); }
public function test_gh_91_get_connection_with_null_connection_is_always_default() { $conn_one = ConnectionManager::get_connection('mysql'); $conn_two = ConnectionManager::get_connection(); $conn_three = ConnectionManager::get_connection('mysql'); $conn_four = ConnectionManager::get_connection(); $this->assert_same($conn_one, $conn_three); $this->assert_same($conn_two, $conn_three); $this->assert_same($conn_four, $conn_three); }
public function testGh91GetConnectionWithNullConnectionIsAlwaysDefault() { $connOne = ConnectionManager::getConnection('mysql'); $connTwo = ConnectionManager::getConnection(); $connThree = ConnectionManager::getConnection('mysql'); $connFour = ConnectionManager::getConnection(); $this->assertSame($connOne, $connThree); $this->assertSame($connTwo, $connThree); $this->assertSame($connFour, $connThree); }
protected function setORM() { $app = $this; \ActiveRecord\Config::initialize(function ($cfg) use($app) { $cfg->set_model_directory(ZI . '/models'); $cfg->set_connections(array_combine(array_keys($app->db), array_map(function ($cfg) { if ($cfg['driver'] == 'sqlite') { return $cfg['driver'] . "://" . $cfg['database']; } else { return $cfg['driver'] . "://" . $cfg['username'] . ":" . $cfg['password'] . "@" . $cfg['host'] . "/" . $cfg['database']; } }, $app->db))); $cfg->set_default_connection($app->mode); }); $this->conn = \ActiveRecord\ConnectionManager::get_connection($app->mode); }
public static function connect() { ActiveRecord\Config::initialize(function ($cfg) { $dbHost = Config::getValue_('dbHost'); $dbUser = Config::getValue_('dbUser'); $dbPass = Config::getValue_('dbPass'); $dbName = Config::getValue_('dbName'); $cfg->set_model_directory(MODEL); $cfg->set_connections(array('development' => 'mysql://' . $dbUser . ':' . $dbPass . '@' . $dbHost . '/' . $dbName)); try { ConnectionManager::get_connection("development"); } catch (Exception $e) { $response = Utils::getInternalServerErrorResponse(true); $response->send(); die; } }); }
public function setUp($connection_name = null) { require_once 'DatabaseLoader.php'; Table::clearCache(); $config = Config::instance(); $this->original_default_connection = $config->getDefaultConnection(); if ($connection_name) { $config->setDefaultConnection($connection_name); } /* if ($connection_name === 'sqlite' || $config->getDefaultConnection() === 'sqlite') { // need to create the db. the adapter specifically does not create it for us. //static::$db = \substr(Config::instance()->getConnection('sqlite'), 9); //new Sqlite(static::$db); //$file_db = new PDO('sqlite:messaging.sqlite3'); if ($GLOBALS['OS'] !== 'WIN') { new \PDO('sqlite:../Fixtures/test.db'); } else { new \PDO('sqlite:../Fixtures/test.db'); } var_dump($config); var_dump($GLOBALS['OS']); exit(); } */ $this->connection_name = $connection_name; try { $this->conn = ConnectionManager::getConnection($connection_name); } catch (ExceptionDatabase $e) { $this->markTestSkipped($connection_name . ' failed to connect. ' . $e->getMessage()); } $GLOBALS['Activerecord_LOG'] = false; $loader = new DatabaseLoader($this->conn); $loader->resetTableData(); if (self::$log) { $GLOBALS['Activerecord_LOG'] = true; } }
public function __construct($class_name) { $this->class = Reflections::instance()->add($class_name)->get($class_name); // if connection name property is null the connection manager will use the default connection $connection = $this->class->getStaticPropertyValue('connection', null); $this->conn = ConnectionManager::get_connection($connection); $this->set_table_name(); $this->set_sequence_name(); $this->get_meta_data(); $this->set_primary_key(); $this->set_delegates(); $this->set_setters_and_getters(); $this->callback = new CallBack($class_name); $this->callback->register('before_save', function (Model $model) { $model->set_timestamps(); }, array('prepend' => true)); $this->callback->register('after_save', function (Model $model) { $model->reset_dirty(); }, array('prepend' => true)); }
public static function delete_all($options = array()) { $cm = \ActiveRecord\ConnectionManager::instance(); $conn = $cm::get_connection('master'); $table = static::table(); $sql = new SQLBuilder($conn, $table->get_fully_qualified_table_name()); $conditions = is_array($options) ? $options['conditions'] : $options; if (is_array($conditions) && !eh_um_hash($conditions)) { call_user_func_array(array($sql, 'delete'), $conditions); } else { $sql->delete($conditions); } if (isset($options['limit'])) { $sql->limit($options['limit']); } if (isset($options['order'])) { $sql->order($options['order']); } $values = $sql->bind_values(); $ret = $conn->query($table->last_sql = $sql->to_s(), $values); return $ret->rowCount(); }
public function test_get_connection_uses_existing_object() { $a = ConnectionManager::get_connection('mysql'); $a->harro = 'harro there'; $this->assert_same($a, ConnectionManager::get_connection('mysql')); }
/** * @return \ActiveRecord\Connection */ public function getDatabase() { return \ActiveRecord\ConnectionManager::get_connection(); }
/** * * @param string $connection * @return PDO */ public static function getPDOObject($connection = "nope") { return $connection == "nope" ? ActiveRecord\ConnectionManager::get_connection()->connection : ActiveRecord\ConnectionManager::get_connection($connection); }
public function test_substitute_escape_quotes_with_connections_escape_method() { try { $conn = ConnectionManager::get_connection(); } catch (DatabaseException $e) { $this->mark_test_skipped('failed to connect. ' . $e->getMessage()); } $a = new Expressions(null, 'name=?', "Tito's Guild"); $a->set_connection($conn); $escaped = $conn->escape("Tito's Guild"); $this->assert_equals("name={$escaped}", $a->to_s(true)); }
/** * Get last query * * @return string */ public function lastInsertId() { $connection = \ActiveRecord\ConnectionManager::get_connection(); $id = $connection->insert_id(); return $id; }
public function testSubstituteEscapeQuotesWithConnectionsEscapeMethod() { try { $conn = ConnectionManager::getConnection(); } catch (ExceptionDatabase $e) { $this->markTestSkipped('failed to connect. ' . $e->getMessage()); } $a = new Expressions(null, 'name=?', "Tito's Guild"); $a->setConnection($conn); $escaped = $conn->escape("Tito's Guild"); $this->assertEquals("name={$escaped}", $a->toString(true)); }
private function print_harian($params) { try { $condition = array(); $data = array(); if (!empty($params['a']) && !empty($params['z']) && !empty($params['cdate'])) { $col_date = $params['cdate']; $query = sprintf("%s BETWEEN '%s' and '%s' ", $params['cdate'], $params['a'], $params['z']); $condition['conditions'] = $query; } $ar_adapter = \ActiveRecord\ConnectionManager::get_connection($this->app->mode); $connection = $ar_adapter->connection; $stmt = $connection->prepare("select (stocks.get_stock_harian(:awal, :akhir)).*"); $stmt->bindParam(':awal', $params['a']); $stmt->bindParam(':akhir', $params['z']); $stmt->execute(); $stocks = $stmt->fetchAll(PDO::FETCH_CLASS); // print_r($stocks); $data["rows"] = $stocks; $data["title"] = $this->template_header; $data["posting_range"] = sprintf(" %s sampai %s", $params['a'], $params['z']); $data["params"] = sprintf("a=%s&z=%s&cdate=%s", $params['a'], $params['z'], "posting_date"); $data["source_url"] = APP::urlFor('stok.export'); } catch (Exception $e) { App::flash('error', $e); } APP::render('stock/print_laporan_harian', $data); }
public function reestablishConnection($close = true) { // if connection name property is null the connection manager will use the default connection $connection = $this->class->getStaticPropertyValue('connection', null); if ($close) { ConnectionManager::dropConnection($connection); static::clearCache(); } return $this->conn = ConnectionManager::getConnection($connection); }