public static function connect($db) { $uri = 'mongodb://something'; $mongo = new MongoClient($uri, ['username' => 'user', 'password' => 'password', 'ssl' => true, 'w' => true]); static::$db = $mongo->{$db}; static::$connection = $mongo; static::$isConnected = static::$connection->connected; }
public function set_up($connection_name = null) { ActiveRecord\Table::clear_cache(); $config = ActiveRecord\Config::instance(); $this->original_default_connection = $config->get_default_connection(); $this->original_date_class = $config->get_date_class(); if ($connection_name) { $config->set_default_connection($connection_name); } if ($connection_name == 'sqlite' || $config->get_default_connection() == 'sqlite') { // need to create the db. the adapter specifically does not create it for us. static::$db = substr(ActiveRecord\Config::instance()->get_connection('sqlite'), 9); new SQLite3(static::$db); } $this->connection_name = $connection_name; try { $this->conn = ActiveRecord\ConnectionManager::get_connection($connection_name); } catch (ActiveRecord\DatabaseException $e) { $this->mark_test_skipped($connection_name . ' failed to connect. ' . $e->getMessage()); } $GLOBALS['ACTIVERECORD_LOG'] = false; $loader = new DatabaseLoader($this->conn); $loader->reset_table_data(); if (self::$log) { $GLOBALS['ACTIVERECORD_LOG'] = true; } }
/** @return DB */ public static function DB() { if (is_null(static::$db)) { static::$db = new DB(static::GetConnection(), PREFIX); } return static::$db; }
protected static function init() { if (!empty(static::$db)) { return; } static::$db = classSupernova::$db; }
function getManager() { if (is_null(static::$db)) { static::$db = makeDB(); } return static::$db; }
public static function connect() { if (!self::$con) { static::$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error()); static::$db = mysql_select_db(DB_NAME, self::$con) or die("Failed to connect to mysql:" . mysql_errno()); } }
/** * constructor * @param mixed param PDO instance, * or record id (if integer), * or constraints (if array) but param['pdo'] is PDO instance */ function __construct($params = null) { try { if (!static::$db && static::$adapter) { static::$db = new static::$adapter(static::$db_settings); } } catch (Exception $e) { throw new WaxDbException("Cannot Initialise DB", "Database Configuration Error"); } $class_name = get_class($this); if ($class_name != 'WaxModel' && !$this->table) { $this->table = Inflections::underscore($class_name); } $this->define($this->primary_key, $this->primary_type, $this->primary_options); $this->setup(); $this->set_identifier(); // If a scope is passed into the constructor run a method called scope_[scope](). if ($params) { $method = "scope_" . $params; if (method_exists($this, $method)) { $this->{$method}; } else { $res = $this->filter(array($this->primary_key => $params))->first(); $this->row = $res->row; $this->clear(); } } }
/** * Constructor. * * @param \wpdb $db */ public function __construct(wpdb $db) { if (!isset(static::$instance)) { static::$instance = $this; static::$db = $db; } }
/** * Main function for initialize this class. * @param \Slim\Slim */ public static function init() { date_default_timezone_set(static::config()->timezone); static::$db = static::config()->db; static::$user = new \ptejada\uFlex\User(); static::$app = new \Slim\Slim(); static::$mail = new \PHPMailer(); static::$assets = new \Assets(); static::$user->config->database->host = static::config()->db['server']; static::$user->config->database->user = static::config()->db['username']; static::$user->config->database->password = static::config()->db['password']; static::$user->config->database->name = static::config()->db['database_name']; static::$user->start(); require 'app/config/mail.php'; static::$mail->isSMTP(); static::$mail->Host = $mail['Host']; static::$mail->SMTPAuth = $mail['SMTPAuth']; static::$mail->Username = $mail['Username']; static::$mail->Password = $mail['Password']; static::$mail->SMTPSecure = $mail['SMTPSecure']; static::$mail->Port = $mail['Port']; static::$mail->From = $mail['From']; static::$mail->FromName = $mail['FromName']; static::$mail->isHTML(true); }
public static function connect() { if (is_null(static::$db)) { static::$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); } return static::$db; }
public function __construct() { static::$instance = $this; if (!isset(static::$db)) { static::$db = new DatabaseConnect(); } }
/** * Returns the *Singleton* instance of this class. * * @return Factory the singleton instance. */ public static function getInstance() { if (null === static::$instance) { static::$instance = new static(); static::$db = \Xoops::getInstance()->db(); } return static::$instance; }
/** * 返回active record使用的数据库连接。 * 默认使用'db'应用组件作为数据库连接。 * 如果你想使用不同的数据库连接的话你可以重写该方法。 * @return ConnectionPool active record使用的数据库连接。 */ public static function getDbConnection() { if (static::$db !== null) { return static::$db; } else { return static::$db = \Sky\Sky::$app->getDb(); } }
/** * Returns new cmysql class for custom SQL querying, based on current database connection * @return cMySql */ public static function getDb($fresh = false) { if ($fresh || !isset(self::$db)) { static::$db = new MySQL(static::connect()); return static::$db; } return static::$db; }
/** * getDbConnection return current DB connection or create new if not exist * * @return type */ static function getDbConnection() { if (empty(static::$db)) { $pdo = Service::get('pdo'); static::$db = new \PDO($pdo['dns'], $pdo['user'], $pdo['password']); } return static::$db; }
public static function setUpBeforeClass() { static::$client = static::createClient(); if (static::$client->getDatabases()->exists(static::getDbName(), static::$dbStorage)) { static::$client->getDatabases()->drop(static::getDbName(), static::$dbStorage); } static::$db = static::$client->getDatabases()->create(static::getDbName(), static::$dbStorage); }
/** * ลิสต์รายชื่ออีเมล์ของแอดมิน */ public static function findAdmin(Request $request) { $model = new static($request); $result = array(); foreach ($model->db()->select($model->getFullTableName('user'), array('status', 1), array('email')) as $item) { $result[] = $item['email']; } return $result; }
/** * @return EntityManager */ public static function getEm() { if (!static::$em instanceof EntityManager) { $helper = (include "src/cli_config.php"); static::$em = $helper->get("em")->getEntityManager(); static::$db = $helper->get("db"); } return static::$em; }
/** * Get a instance of DbShardingConnection of the specified connecton name. * * @param string $rule Sharding Rule is an instance of \Core\Lib\OrderShardingRule. * * @return \Core\Lib\DbShardingConnection */ public function getDbSharding($rule = null) { if (!static::$db instanceof \Db\ShardingConnection) { static::$db = \Db\ShardingConnection::instance(); } if ($rule != null) { static::$db->setRule($rule); } return static::$db; }
/** * ตรวจสอบโมดูลที่เรียก * * @return array */ public static function getModule($module, $date) { if (defined('MAIN_INIT')) { $model = new static(); return $model->db()->createQuery()->from('index I')->join('modules M', 'INNER', array('M.id', 'I.module_id'))->join('index_detail D', 'INNER', array(array('D.id', 'I.id'), array('D.module_id', 'I.module_id'), array('D.language', array(Language::name(), ''))))->where(array(array('I.index', 1), array('M.module', $module), array('I.published', 1), array('I.published_date', '<=', $date)))->cacheOn()->first('M.id module_id', 'M.module', 'M.owner', 'D.topic', 'D.description', 'M.config'); } else { // เรียก method โดยตรง new \Kotchasan\Http\NotFound('Do not call method directly'); } }
/** * อ่านรายชื่อโมดูลทั้งหมดที่ติดตั้งแล้ว * * @return array */ public static function getModules() { if (defined('MAIN_INIT')) { $model = new static(); return $model->db()->createQuery()->select('M.id', 'M.module', 'M.owner', 'I.language')->from('modules M')->join('index I', 'LEFT', array(array('I.module_id', 'M.id'), array('I.index', 1)))->where(array('I.published', 1))->cacheOn()->execute(); } else { // เรียก method โดยตรง new \Kotchasan\Http\NotFound('Do not call method directly'); } }
/** * @param $id * * @return CartModel */ protected static function getDb($id) { if (is_null(static::$db)) { static::$db = CartModel::findOne(['id' => $id]); } if (is_null(static::$db)) { static::$db = new CartModel(); static::$db->id = $id; } return static::$db; }
private static function initDbConnection() { try { $db = new PDO('pgsql:host=' . Container::get('params')->getDbData('host') . ';dbname=' . Container::get('params')->getDbData('db'), Container::get('params')->getDbData('user'), Container::get('params')->getDbData('pass')); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); static::$db = $db; } catch (PDOException $e) { echo $e->getMessage('Can\'t connect to database.'); } return; }
protected function getCollection() { if (null === static::$collection) { // connect static::$client = new \MongoClient(); // select a database static::$db = static::$client->ik2; // select a collection (analogous to a relational database's table) static::$collection = static::$db->cronario_jobsss2; } return static::$collection; }
/** * อ่านข้อมูลสมาชิกที่ $user_id * * @param int $user_id * @return object|null คืนค่า Object ของข้อมูล ไม่พบคืนค่า null */ public static function getUser($user_id) { if (is_int($user_id) && $user_id > 0) { // query ข้อมูลสมาชิกที่เลือก $model = new static(); $query = $model->db()->createQuery(); $array = array('U.id', 'U.pname', 'U.fname', 'U.lname', 'U.email', 'U.displayname', 'U.website', 'U.company', 'U.address1', 'U.address2', 'U.phone1', 'U.phone2', 'U.sex', 'U.birthday', 'U.zipcode', 'U.country', 'U.status', 'U.subscrib', 'U.admin_access', 'U.provinceID', 'U.province', 'U.icon', 'U.fb', 'V.email invite'); $result = $query->select($array)->from('user U')->join('user V', 'LEFT', array('V.id', 'U.invite_id'))->where(array('U.id', $user_id))->limit(1)->toArray()->execute(); return sizeof($result) == 1 ? (object) $result[0] : null; } return null; }
/** * Create the database connection ( when loaded) * */ public static function __init() { try { if (static::$db == null) { static::$db = new \PDO(MYSQL_HOSTNAME . ';dbname=' . MYSQL_DATABASE . ';charset=utf8', MYSQL_USERNAME, MYSQL_PASSWORD); } if (DEBUG) { static::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } } catch (Exception $e) { die($e->getMessage()); } }
/** * ค้นหาข้อมูลทั้งหมด * * @param Request $request * @param object $index * @return object */ public static function findAll(Request $request, $index) { // model $model = new static(); $db = $model->db(); // ข้อความค้นหา $index->q = $request->globals(array('POST', 'GET'), 'q')->topic(); $index->words = array(); $where1 = array(); $where2 = array(); // แยกข้อความค้นหาออกเป็นคำๆ ค้นหาข้อความที่มีความยาวมากกว่า 1 ตัวอักษร foreach (explode(' ', $index->q) as $item) { if (mb_strlen($item) > 1) { $index->words[] = $item; $where1[] = array('D.topic', 'LIKE', '%' . $item . '%'); $where1[] = array('D.detail', 'LIKE', '%' . $item . '%'); $where2[] = array('C.detail', 'LIKE', '%' . $item . '%'); } } $index->sqls = array(); $select = array('I.id', 'I.alias', 'M.module', 'M.owner', 'D.topic', 'D.description', 'I.visited', 'I.index'); $q1 = $db->createQuery()->select($select)->from('modules M')->join('index I', 'INNER', array(array('I.module_id', 'M.id'), array('I.published', 1), array('I.published_date', '<=', date('Y-m-d')), array('I.language', array(Language::name(), ''))))->join('index_detail D', 'INNER', array(array('D.id', 'I.id'), array('D.module_id', 'M.id')))->where($where1, 'OR'); $q2 = $db->createQuery()->select($select)->from('comment C')->join('modules M', 'INNER', array('M.id', 'C.module_id'))->join('index I', 'INNER', array(array('I.module_id', 'M.id'), array('I.published', 1), array('I.published_date', '<=', date('Y-m-d')), array('I.language', array(Language::name(), ''))))->join('index_detail D', 'INNER', array(array('D.id', 'I.id'), array('D.module_id', 'M.id')))->where($where2, 'OR'); // union all queries $q3 = $db->createQuery()->union(array($q1, $q2)); // groub by id $index->sqls[] = $db->createQuery()->select()->from(array($q3, 'Q'))->groupBy('Q.id'); // ค้นหาจากโมดูลอื่นๆที่ติดตั้ง foreach (Gcms::$install_owners as $item => $modules) { if ($item != 'index' && is_file(ROOT_PATH . "modules/{$item}/models/search.php")) { include ROOT_PATH . "modules/{$item}/models/search.php"; //createClass(ucfirst($item).'\Search\Model')->findAll($request, $index); } } // union all queries $query = $db->createQuery()->from(array($db->createQuery()->union($index->sqls), 'Z')); // จำนวน $index->total = $query->cacheOn()->count(); // ข้อมูลแบ่งหน้า if (empty($index->list_per_page)) { $index->list_per_page = 20; } $index->page = $request->request('page')->toInt(); $index->totalpage = ceil($index->total / $index->list_per_page); $index->page = max(1, $index->page > $index->totalpage ? $index->totalpage : $index->page); $index->start = $index->list_per_page * ($index->page - 1); $index->end = $index->start + $index->list_per_page > $index->total ? $index->total : $index->start + $index->list_per_page; // query $index->items = $query->select()->order('visited')->limit($index->list_per_page, $index->start)->cacheOn()->execute(); return $index; }
/** * อ่านข้อมูลหมวดหมู่ที่เลือก และสามารถเผยแพร่ได้ * * @param int $category_id * @param int $module_id * @return object|null ข้อมูลที่เลือก (Object) หรือ null หากไม่พบ */ public static function get($category_id, $module_id) { if (is_int($category_id) && is_int($module_id)) { $model = new static(); $query = $model->db()->createQuery()->select()->from('category')->where(array(array('category_id', $category_id), array('module_id', $module_id), array('published', '1')))->limit(1); foreach ($query->toArray()->execute() as $item) { $item['topic'] = Gcms::ser2Str($item, 'topic'); $item['detail'] = Gcms::ser2Str($item, 'detail'); $item['icon'] = Gcms::ser2Str($item, 'icon'); return (object) ArrayTool::unserialize($item['config'], $item); } } return null; }
/** * อ่านข้อมูลการเยี่ยมชมในเดือนที่เลือก * * @param string $date * @return array */ public static function get($date) { $datas = array(); if (preg_match('/^([0-9]+)\\-([0-9]+)$/', $date, $match)) { $y = (int) $match[1]; $m = (int) $match[2]; $model = new static(); $query = $model->db()->createQuery()->select('date', 'SUM(`pages_view`) AS `pages_view`')->from('counter')->where(array(array('YEAR(`date`)', $y), array('MONTH(`date`)', $m)))->groupBy('date')->order('date ASC')->toArray()->cacheOn(); foreach ($query->execute() as $item) { $datas[$item['date']] = $item; } } return $datas; }
/** * Connect to the database, with the credentials provided in the configuration file. */ public static function connect() { // Retrieve the database data from the configuration $host = Config::getValue('database', 'host', 'localhost'); $port = Config::getValue('database', 'port', 3306); $dbname = Config::getValue('database', 'database', ''); $user = Config::getValue('database', 'username', 'root'); $password = Config::getValue('database', 'password', ''); // Connect to the database using PDO, store the instance globally try { static::$db = new PDO('mysql:host=' . $host . ';port=' . $port . ';dbname=' . $dbname, $user, $password); } catch (Exception $e) { throw new Exception('An error occurred while connecting to the database!'); } }