/** * @throws SpoonDatabaseException */ public function testExecute() { // clear all tables if (count($this->db->getTables()) != 0) { $this->db->drop($this->db->getTables()); } // create table users $this->db->execute("\n\t\t\tCREATE TABLE users (\n\t\t\tid INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t\tusername VARCHAR( 255 ) NOT NULL ,\n\t\t\temail VARCHAR( 255 ) NOT NULL ,\n\t\t\tdeveloper ENUM( 'Y', 'N' ) NOT NULL\n\t\t\t) ENGINE = MYISAM;"); }
/** * Validate if a database connection can be made * * @param InstallationData $data The form data * @param ExecutionContextInterface $context The forms validation context * * @todo Replace SpoonDatabase */ public function checkDatabaseConnection(InstallationData $data, ExecutionContextInterface $context) { try { // create instance $db = new \SpoonDatabase('mysql', $data->getDbHostname(), $data->getDbUsername(), $data->getDbPassword(), $data->getDbDatabase(), $data->getDbPort()); // test table $table = 'test' . time(); // attempt to create table $db->execute('DROP TABLE IF EXISTS ' . $table); $db->execute('CREATE TABLE ' . $table . ' (id int(11) NOT NULL) ENGINE=MyISAM'); // drop table $db->drop($table); } catch (\Exception $e) { $context->addViolation('Problem with database credentials'); } }
/** * Validate the form based on the variables in $_POST */ private function validateForm() { // form submitted if ($this->frm->isSubmitted()) { // database settings $this->frm->getField('hostname')->isFilled('This field is required.'); $this->frm->getField('database')->isFilled('This field is required.'); $this->frm->getField('username')->isFilled('This field is required.'); $this->frm->getField('password')->isFilled('This field is required.'); // all filled out if ($this->frm->getField('hostname')->isFilled() && $this->frm->getField('database')->isFilled() && $this->frm->getField('username')->isFilled() && $this->frm->getField('password')->isFilled()) { // test the database connection details try { // get port $port = $this->frm->getField('port')->isFilled() ? $this->frm->getField('port')->getValue() : 3306; // create instance $db = new SpoonDatabase('mysql', $this->frm->getField('hostname')->getValue(), $this->frm->getField('username')->getValue(), $this->frm->getField('password')->getValue(), $this->frm->getField('database')->getValue(), $port); // test table $table = 'test' . time(); // attempt to create table $db->execute('DROP TABLE IF EXISTS ' . $table); $db->execute('CREATE TABLE ' . $table . ' (id int(11) NOT NULL) ENGINE=MyISAM'); // drop table $db->drop($table); } catch (Exception $e) { // add errors $this->frm->addError('Problem with database credentials'); // show error $this->tpl->assign('formError', $e->getMessage()); } // all valid if ($this->frm->isCorrect()) { // update session SpoonSession::set('db_hostname', $this->frm->getField('hostname')->getValue()); SpoonSession::set('db_database', $this->frm->getField('database')->getValue()); SpoonSession::set('db_username', $this->frm->getField('username')->getValue()); SpoonSession::set('db_password', $this->frm->getField('password')->getValue()); SpoonSession::set('db_port', $this->frm->getField('port')->getValue()); // redirect SpoonHTTP::redirect('index.php?step=6'); } } } }
/** * @depends testExecute */ public function testGetQueries() { $this->db->setDebug(true); $this->db->execute('SELECT id FROM users'); $this->assertEquals(1, count($this->db->getQueries())); }
/** * Executes sql in the database * * @param \SpoonDatabase $database * @param string $sql */ protected function importSQL($database, $sql) { $database->execute(trim($sql)); }
/** * @param InstallationData $data */ protected function buildDatabase(InstallationData $data) { // put a new instance of the database in the container $database = new \SpoonDatabase('mysql', $data->getDbHostname(), $data->getDbUsername(), $data->getDbPassword(), $data->getDbDatabase(), $data->getDbPort()); $database->execute('SET CHARACTER SET :charset, NAMES :charset, time_zone = "+0:00"', array('charset' => 'utf8')); $this->container->set('database', $database); }
/** * Get (or create and get) a database-connection * @later split the write and read connection * * @param bool[optional] $write Do you want the write-connection or not? * @return SpoonDatabase */ public static function getDB($write = false) { $write = (bool) $write; // do we have a db-object ready? if (!Spoon::exists('database')) { // create instance $db = new SpoonDatabase(DB_TYPE, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PORT); // utf8 compliance & MySQL-timezone $db->execute('SET CHARACTER SET utf8, NAMES utf8, time_zone = "+0:00"'); // store Spoon::set('database', $db); } // return db-object return Spoon::get('database'); }