public static function config($servername, $username, $password, $database, $dbport = 3306) { self::$servername = $servername; self::$username = $username; self::$password = $password; self::$database = $database; self::$dbport = $dbport; }
/** * [instance singleton] * @return [object] [class database] */ public static function instance() { if (!isset(self::$_instance)) { $class = __CLASS__; self::$_instance = new $class(); } return self::$_instance; }
/** * Get an instance of a database * @return Database */ public static function getInstance() { // create an instance if there is no instance if (self::$instance == null) { self::$instance = new Database(); } return self::$instance; }
/** * Инициализация подклчения к БД * @throws \Core\Database\Exception * @return boolean */ public function init() { if (is_null(self::$db)) { try { // коннектимся к mysql self::$db = mysqli_connect(config('db_hostname'), config('db_username'), config('db_password')); mysqli_select_db(self::$db, config('db_database')); mysqli_query(self::$db, "SET NAMES " . config('db_char_set') . " COLLATE " . config('db_dbcollat')); var_dump("DB is loaded!"); } catch (Exception $e) { throw new \Core\Database\Exception("Ошибка БД: " . mysqli_error(self::$db)); return false; } } return true; }
/** * Create and return the instance of Illuminate\Database\Capsule\Manager * * @return object the instance of the Capsule if database enabled * in the configuration file (config/database.php), null * if disabled * * @static * @access public * @since Method available since Release 0.1.0 */ public static function connect() { if (self::$config === null) { self::$config = Config::get('database'); } if (self::$config['enabled'] === true) { $capsule = new Capsule(); $capsule->addConnection(self::$config['settings']); $capsule->bootEloquent(); $capsule->setAsGlobal(); self::$capsule = $capsule; return self::$capsule; } else { return null; } }
/** * Initialize database config * @link http://php.net/manual/pdo.construct.php * @param string $dsn Data Source Name, contains the information required to connect to the database. * @param string $username Username for the DSN string * @param string $password Password for the DSN string * @param array $options A key=>value array of driver-specific connection options. * @throws Error */ public static function initialize($dsn, $username = null, $password = null, $options = array()) { if (self::$instance) { throw new Error('Cannot re-initialize database'); } self::$instance = new Database($dsn, $username, $password, $options); self::$instance->setAttribute(self::ATTR_ERRMODE, self::ERRMODE_EXCEPTION); $currentSqlMode = Database::getInstance()->query('SELECT @@GLOBAL.SQL_MODE')->fetchColumn(); if (strpos($currentSqlMode, 'STRICT_TRANS_TABLES')) { $currentSqlMode = explode(',', $currentSqlMode); $strictTransTable = array_search('STRICT_TRANS_TABLES', $currentSqlMode); unset($currentSqlMode[$strictTransTable]); $statement = self::$instance->prepare('SET SESSION sql_mode = ?'); $statement->bindValue('1', implode(',', $currentSqlMode)); $statement->execute(); } }
/** * Return an instance of the database as a \PDO Object instance * * @return Current database instance (\PDO) */ public static function getInstance() { // If the current database and the current config are null if (is_null(self::$db) && is_null(self::$config)) { // Getting the database config from the config file self::$config = Config::get('database'); // Trying to create a \PDO object from the configuration try { $db = new \PDO('mysql:dbname=' . self::$config['database'] . ';host=' . self::$config['host'] . '', self::$config['username'], self::$config['password']); } catch (PDOException $e) { // If it failed, printing the error message echo $e->getMessage(); } // Setting the current database self::$db = $db; } // Returning the current database return self::$db; }
public function __construct($checkTables = false) { $this->subPrefix .= 'D_'; parent::__construct(); }
/** * Rolls back a transaction * @link http://php.net/manual/pdo.rollback.php * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. */ public static function rollBack() { $connection = self::GetServer(self::MASTER); $result = $connection->rollBack(); if ($result) { self::$inTransaction = false; } return $result; }
/** * 执行sql入口 * * @param string $sql query to run * @param array $params the prepared query params * @return PDOStatement */ public function query($sql, array $params = NULL, $cache_statement = FALSE) { $time = microtime(TRUE); self::$last_query = $sql; if (!$this->pdo) { $this->connect(); } if ($cache_statement) { $hash = md5($sql); if (isset($this->statements[$hash])) { $statement = $this->statements[$hash]; } else { $statement = $this->statements[$hash] = $this->pdo->prepare($sql); } } else { $statement = $this->pdo->prepare($sql); } $statement->execute($params); self::$queries[] = array(microtime(TRUE) - $time, $sql); return $statement; }
/** * Run a SQL query and return the statement object * * @param string $sql query to run * @param array $params the prepared query params * @return PDOStatement */ public function query($sql, array $params = NULL, $cache_statement = FALSE) { $time = microtime(TRUE); self::$last_query = $sql; // Connect if needed if (!$this->pdo) { $this->connect(); } // Should we cached PDOStatements? (Best for batch inserts/updates) if ($cache_statement) { $hash = md5($sql); if (isset($this->statements[$hash])) { $statement = $this->statements[$hash]; } else { $statement = $this->statements[$hash] = $this->pdo->prepare($sql); } } else { $statement = $this->pdo->prepare($sql); } $statement->execute($params); //$statement = $this->pdo->query($sql); // Save query results by database type self::$queries[$this->type][] = array(microtime(TRUE) - $time, $sql); return $statement; }