function testExecute() { // query with output $command = "SELECT * FROM \"SiteTree\""; $query = new DBP_Sql($command); $result = $query->execute(); $this->assertType('array', $result, 'Result is array'); $this->assertEquals($command, $result['Query'], 'Return command correctly'); $this->assertEquals("DBP_Field", get_class($result['Fields']->First()), 'Result contains fields'); $this->assertEquals("DBP_Record", get_class($result['Records']->First()), 'Result contains records'); $this->assertNotEquals('error', $result['Message']['type'], 'No errors message'); // query without output $command = "UPDATE \"SiteTree\" SET \"URLSegment\" = 'someurl' WHERE \"ID\" > 5000;"; $query = new DBP_Sql($command); $result = $query->execute(); $this->assertEquals($command, $result['Query'], 'Return command correctly'); $this->assertNotEquals('error', $result['Message']['type'], 'No errors message'); // query with escaped songle quoute $command = "INSERT INTO \"SiteTree\" (\"ClassName\", \"URLSegment\", \"Title\", \"Content\") VALUES ('ErrorPage', 'page-not-found', 'Page not found', '<p>Sorry, it seems you were trying to access a page that doesn''t exist.</p><p>Please check the spelling of the URL you were trying to access and try again.</p>');"; $query = new DBP_Sql($command); $result = $query->execute(); $this->assertNotEquals('error', $result['Message']['type'], 'Escaping quotes works: ' . print_r($result, true)); // force an error $command = "force error"; $query = new DBP_Sql($command); $result = $query->execute(); $this->assertEquals($command, $result['Query'], 'Return command correctly'); $this->assertEquals('error', $result['Message']['type'], 'Return error'); $this->assertType('string', $result['Message']['text'], 'Return error message'); }
static function execute_script($queries) { @ini_set('max_execution_time', '0'); $queries = DBP_Sql::split_script($queries); switch (count($queries)) { case 0: return array(); case 1: $query = new DBP_Sql($queries[0]); $records = $query->execute(); return $records; default: $status = 'highlight'; if (DB::getConn()->supportsTransactions()) { DB::getConn()->startTransaction(); } foreach ($queries as $query) { $query = new DBP_Sql($query); $result = $query->execute(); if ($result['Message']['type'] == 'error') { $msg[] = $result['Query'] . '<br />' . $result['Message']['text']; $status = 'error'; break; } $msg[] = $query->type() . ' ' . ($result['Message']['text'] ? $result['Message']['text'] : 'no error'); } if (DB::getConn()->supportsTransactions()) { if ($status == 'error') { DB::getConn()->transactionRollback(); $msg[] = 'Transaction rolled back'; } else { DB::getConn()->endTransaction(); } } $result = array('Query' => implode("\r\n", $queries), 'Message' => array('text' => implode("<br />\n", $msg), 'type' => $status)); return $result; } }