/** * setUpBeforeClass * * @throws \LogicException * @return void */ public static function setUpBeforeClass() { if (!static::$driver) { throw new \LogicException('static::$driver variable is empty.'); } static::$dsn = $dsn = DsnResolver::getDsn(static::$driver); if (!$dsn) { return; } static::$dbname = $dbname = isset($dsn['dbname']) ? $dsn['dbname'] : null; if (!$dbname) { throw new \LogicException(sprintf('No dbname in %s DSN', static::$driver)); } // Id db exists, return. if (static::$dbo) { static::$dbo->select($dbname); return; } // Use factory create dbo, only create once and will be singleton. $db = self::$dbo = DatabaseFactory::getDbo(static::$driver, array('host' => isset($dsn['host']) ? $dsn['host'] : null, 'user' => isset($dsn['user']) ? $dsn['user'] : null, 'password' => isset($dsn['pass']) ? $dsn['pass'] : null, 'port' => isset($dsn['port']) ? $dsn['port'] : null, 'prefix' => isset($dsn['prefix']) ? $dsn['prefix'] : null)); $db->getDatabase($dbname)->create(true); $db->select($dbname); $queries = file_get_contents(__DIR__ . '/Stub/' . static::$driver . '.sql'); $queries = $db->splitSql($queries); foreach ($queries as $query) { $query = trim($query); if ($query) { $db->setQuery($query)->execute(); } } }
/** * Object constructor to set table and key fields. In most cases this will * be overridden by child classes to explicitly set the table and key fields * for a particular database table. * * @param string $table Name of the table to model. * @param mixed $keys Name of the primary key field in the table or array of field names that * compose the primary key. * @param DatabaseDriver $db DatabaseDriver object. * * @since 2.0 */ public function __construct($table = null, $keys = 'id', DatabaseDriver $db = null) { $db = $db ?: DatabaseFactory::getDbo(); // Set internal variables. $this->table = $table; $this->db = $db; $this->data = new \stdClass(); // Set the key to be an array. if (is_string($keys)) { $keys = array($keys); } elseif (is_object($keys)) { $keys = (array) $keys; } $this->keys = $keys; $this->autoIncrement = count($keys) == 1 ? true : false; // Initialise the table properties. $fields = $this->getFields(); if ($fields) { foreach ($fields as $name => $v) { // Add the field if it is not already present. $this->data->{$name} = null; } } if (!$this->table) { throw new \InvalidArgumentException('Table name should not empty.'); } }
/** * Constructor. * * @param string $alias Table alias. * @param string $table Table name. * @param string|array $pk Primary key. * @param DatabaseDriver $db Database adapter. * @param QueryHelper $queryHelper Query helper object. */ public function __construct($alias, $table, $pk = 'id', DatabaseDriver $db = null, QueryHelper $queryHelper = null) { $this->db = $db ?: DatabaseFactory::getDbo(); $this->pk = $pk ?: $alias . '.' . $pk; $this->queryHelper = $queryHelper ?: new QueryHelper($this->db); $this->addTable($alias, $table); $this->configure(); }
/** * Method to test setDefaultDbo(). * * @return void * * @covers Windwalker\Database\DatabaseFactory::setDefaultDbo */ public function testSetDefaultDbo() { $options = $this->getOptions(); $db = new MysqlDriver(null, $options); DatabaseFactory::setDefaultDbo($db); $this->assertSame($db, DatabaseFactory::getDbo()); $this->resetDatabaseFactory(); }
/** * Method to test setDefaultDbo(). * * @return void * * @covers Windwalker\Database\DatabaseFactory::setDefaultDbo */ public function testSetDefaultDbo() { $options = $this->getOptions(); if ($options === false) { $this->markTestSkipped('No mysql test DSN'); } $db = new MysqlDriver(null, $options); DatabaseFactory::setDefaultDbo($db); $this->assertSame($db, DatabaseFactory::getDbo()); $this->resetDatabaseFactory(); }
/** * getInstance * * @throws \UnexpectedValueException * @return AbstractDatabaseAdapter */ public static function getInstance() { if (!static::$instance) { static::$instance = new WindwalkerAdapter(DatabaseFactory::getDbo()); } if (is_callable(static::$instance)) { static::$instance = call_user_func(static::$instance); } if (!static::$instance instanceof AbstractDatabaseAdapter) { throw new \UnexpectedValueException('DB Adapter instance must be callable or extends DatabaseAdapter.'); } return static::$instance; }
/** * setUpBeforeClass * * @return void */ public static function setUpBeforeClass() { // First let's look to see if we have a DSN defined or in the environment variables. if (defined('DB_HOST') || getenv('DB_HOST')) { $dsn = defined('DB_HOST') ? DB_HOST : getenv('DB_HOST'); } else { return; } $db = self::$dbo = DatabaseFactory::getDbo(); $db->setQuery('CREATE DATABASE IF NOT EXISTS ' . DB_DBNAME)->execute(); $db->select(DB_DBNAME); $queries = file_get_contents(__DIR__ . '/Stubs/data.sql'); $queries = $db->splitSql($queries); foreach ($queries as $query) { $query = trim($query); if ($query) { $db->setQuery($query)->execute(); } } }
/** * getDb * * @return DatabaseDriver */ public function getDb() { if (!$this->db) { $this->db = DatabaseFactory::getDbo(); } return $this->db; }
/** * setUpBeforeClass * * @throws \LogicException * @return void */ public static function setUpBeforeClass() { if (!static::$driver) { throw new \LogicException('static::$driver variable is empty.'); } static::$dsn = $dsn = TestDsnResolver::getDsn(static::$driver); if (!$dsn) { static::markTestSkipped('DSN of driver ' . static::$driver . ' not available'); } static::$dbname = $dbname = isset($dsn['dbname']) ? $dsn['dbname'] : null; if (!$dbname) { throw new \LogicException(sprintf('No dbname in %s DSN', static::$driver)); } // Id db exists, return. if (static::$dbo) { static::$dbo->select($dbname); return; } try { // Use factory create dbo, only create once and will be singleton. $db = self::$dbo = DatabaseFactory::getDbo(static::$driver, array('host' => isset($dsn['host']) ? $dsn['host'] : null, 'user' => isset($dsn['user']) ? $dsn['user'] : null, 'password' => isset($dsn['pass']) ? $dsn['pass'] : null, 'port' => isset($dsn['port']) ? $dsn['port'] : null, 'prefix' => isset($dsn['prefix']) ? $dsn['prefix'] : null)); } catch (\RangeException $e) { static::markTestSkipped($e->getMessage()); return; } $database = $db->getDatabase($dbname); if (static::$debug) { $database->drop(true); } $database->create(true); $db->select($dbname); $queries = file_get_contents(__DIR__ . '/Stub/' . static::$driver . '.sql'); DatabaseHelper::batchQuery($db, $queries); }
/** * Constructor. * * @param AbstractDatabaseDriver $db Database adapter. * @param QueryHelper $queryHelper Query helper object. */ public function __construct(AbstractDatabaseDriver $db = null, QueryHelper $queryHelper = null) { $this->db = $db ?: DatabaseFactory::getDbo(); $this->queryHelper = $queryHelper ?: new QueryHelper($this->db); }
adapter: mysql host: localhost name: testing_db user: root pass: '' port: 3306 charset: utf8 YML; $phinx = \Symfony\Component\Yaml\Yaml::parse($phinx); $phinx['paths']['migrations'] = __DIR__ . '/../resources/migrations'; $phinx['environments']['production']['adapter'] = $config['database']['driver']; $phinx['environments']['production']['host'] = $config['database']['host']; $phinx['environments']['production']['name'] = $config['database']['name']; $phinx['environments']['production']['user'] = $config['database']['user']; $phinx['environments']['production']['pass'] = $config['database']['password']; $phinx['environments']['production']['adapter'] = $config['database']['driver']; $phinx['environments']['development']['host'] = $config['database']['host']; $phinx['environments']['development']['name'] = $config['database']['name']; $phinx['environments']['development']['user'] = $config['database']['user']; $phinx['environments']['development']['pass'] = $config['database']['password']; $phinx['environments']['production']['adapter'] = $config['database']['driver']; $phinx['environments']['testing']['host'] = $config['database']['host']; $phinx['environments']['testing']['name'] = $config['database']['name']; $phinx['environments']['testing']['user'] = $config['database']['user']; $phinx['environments']['testing']['pass'] = $config['database']['password']; // Prepare Windwalker DB $db = \Windwalker\Database\DatabaseFactory::getDbo($config['database']['driver'], $config['database']); \Windwalker\DataMapper\Adapter\DatabaseAdapter::setInstance(new \Windwalker\DataMapper\Adapter\WindwalkerAdapter($db)); $db->setQuery('CREATE DATABASE IF NOT EXISTS ' . $db->qn($config['database']['name']))->execute(); $db->select($config['database']['name']); return $phinx;
<?php /** * Part of Windwalker project. * * @copyright Copyright (C) 2011 - 2014 SMS Taiwan, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE */ include_once dirname(__DIR__) . '/vendor/autoload.php'; // First let's look to see if we have a DSN defined or in the environment variables. if (defined('DB_HOST') || getenv('DB_HOST')) { $dsn = defined('DB_HOST') ? DB_HOST : getenv('DB_HOST'); } else { return; } // Make the database driver. \Windwalker\Database\DatabaseFactory::getDbo(array('driver' => 'mysql', 'host' => DB_HOST, 'user' => DB_USER, 'password' => DB_PASSWD));
<?php /** * Part of Windwalker project. * * @copyright Copyright (C) 2011 - 2014 SMS Taiwan, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE */ include __DIR__ . '/../../vendor/autoload.php'; // Make the database driver. $db = \Windwalker\Database\DatabaseFactory::getDbo(array('driver' => 'mysql', 'host' => 'localhost', 'user' => 'root', 'password' => '1234', 'port' => null, 'socket' => null, 'database' => 'joomla321', 'prefix' => 'j321_'));
/** * Constructor. * * @param string $table Table name. * @param string|array $pk Primary key. * @param DatabaseDriver $db Database adapter. * @param QueryHelper $queryHelper Query helper object. */ public function __construct($table = null, $pk = 'id', DatabaseDriver $db = null, QueryHelper $queryHelper = null) { $this->db = $db ?: DatabaseFactory::getDbo(); $this->queryHelper = $queryHelper ?: new QueryHelper($this->db); parent::__construct($table, $pk); }