/** * @covers Org\Snje\Minifw\Table::create * @covers Org\Snje\Minifw\Table::drop */ public function test_create() { $table_create = TableWithAll::get(); $table_create->drop(); $table_create->create(); $db = \Org\Snje\Minifw\DB::get(); $sql = 'show create table `' . $table_create::TBNAME . '`'; $ret = $db->get_query($sql); $this->assertArrayHasKey(0, $ret); $ret = $ret[0]; $leftsql = $ret['Create Table']; $rightsql = 'CREATE TABLE `table_with_all` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT \'ID\', `intfield` int(11) NOT NULL COMMENT \'A int field\', `charfield` varchar(200) NOT NULL COMMENT \'A varchar field\', `textfield` text NOT NULL COMMENT \'A text field\', `intfield_def` int(11) NOT NULL DEFAULT \'0\' COMMENT \'A int field\', `charfield_def` varchar(200) NOT NULL DEFAULT \'\' COMMENT \'A varchar field\', PRIMARY KEY (`id`), UNIQUE KEY `uniqueindex` (`intfield`), KEY `charfield` (`charfield`), KEY `intfield` (`intfield`,`charfield`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=\'Table To Create\''; $this->assertEquals($rightsql, $leftsql); $table_create = TableWithOne::get(); $table_create->drop(); $table_create->create(); $sql = 'show create table `' . $table_create::TBNAME . '`'; $ret = $db->get_query($sql); $this->assertArrayHasKey(0, $ret); $ret = $ret[0]; $leftsql = $ret['Create Table']; $rightsql = 'CREATE TABLE `table_with_one` ( `intfield` int(11) NOT NULL COMMENT \'A int field\' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=\'Table To Create\''; $this->assertEquals($rightsql, $leftsql); }
/** * 私有构造函数 */ protected function __construct($args = []) { parent::__construct(); $ini = Minifw\Config::get('sqlite'); if (!empty($args)) { $ini['path'] = isset($args['path']) ? strval($args['path']) : $ini['path']; } if (empty($ini)) { throw new Minifw\Exception('数据库未配置'); } $this->_sqlite = new \SQLite3(WEB_ROOT . $ini['path'], SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE); }
/** * 私有构造函数 */ protected function __construct($args = []) { $this->_db = DB::get($args); }
/** * 私有构造函数 */ protected function __construct($args = []) { parent::__construct(); $ini = Minifw\Config::get('mysql'); if (!empty($args)) { $ini['host'] = isset($args['host']) ? strval($args['host']) : $ini['host']; $ini['username'] = isset($args['username']) ? strval($args['username']) : $ini['username']; $ini['password'] = isset($args['password']) ? strval($args['password']) : $ini['password']; $ini['dbname'] = isset($args['dbname']) ? strval($args['dbname']) : $ini['dbname']; $ini['encoding'] = isset($args['encoding']) ? strval($args['encoding']) : $ini['encoding']; } if (empty($ini)) { throw new Minifw\Exception('数据库未配置'); } $this->_host = $ini['host']; $this->_username = $ini['username']; $this->_password = $ini['password']; $this->_dbname = $ini['dbname']; $this->_encoding = $ini['encoding']; $this->_mysqli = new \mysqli($this->_host, $this->_username, $this->_password, $this->_dbname); if ($this->_mysqli->connect_error) { throw new Minifw\Exception('数据库连接失败'); } if (!$this->_mysqli->set_charset($this->_encoding)) { throw new Minifw\Exception('数据库查询失败'); } }