예제 #1
0
 /**
  * Load the title from the database
  *
  * @return null|string the current title, null if not exists
  */
 protected function loadTitle()
 {
     $sql = "SELECT title FROM titles WHERE pid = ?";
     $res = $this->sqlite->query($sql, $this->pid);
     $title = $this->sqlite->res2single($res);
     $this->sqlite->res_close($res);
     if ($title === false) {
         return null;
     }
     return $title;
 }
 /**
  *
  */
 public function test_build_new()
 {
     // arrange
     $testdata = array();
     $testdata['new']['new1']['sort'] = 70;
     $testdata['new']['new1']['label'] = 'testcolumn';
     $testdata['new']['new1']['ismulti'] = 0;
     $testdata['new']['new1']['config'] = '{"prefix": "", "postfix": ""}';
     $testdata['new']['new1']['class'] = 'Text';
     $testdata['new']['new1']['isenabled'] = '1';
     $testdata['new']['new2']['sort'] = 40;
     $testdata['new']['new2']['label'] = 'testMulitColumn';
     $testdata['new']['new2']['ismulti'] = 1;
     $testdata['new']['new2']['config'] = '{"prefix": "pre", "postfix": "post"}';
     $testdata['new']['new2']['class'] = 'Text';
     $testdata['new']['new2']['isenabled'] = '1';
     $testname = 'testTable';
     $testname = Schema::cleanTableName($testname);
     // act
     $builder = new SchemaBuilder($testname, $testdata);
     $result = $builder->build();
     /** @noinspection SqlResolve */
     $res = $this->sqlite->query("SELECT sql FROM sqlite_master WHERE type='table' AND name=?", 'data_' . $testname);
     $tableSQL = $this->sqlite->res2single($res);
     $this->sqlite->res_close($res);
     $expected_tableSQL = "CREATE TABLE data_testtable (\n                    pid NOT NULL,\n                    rev INTEGER NOT NULL,\n                    latest BOOLEAN NOT NULL DEFAULT 0, col1 DEFAULT '', col2 DEFAULT '',\n                    PRIMARY KEY(pid, rev)\n                )";
     $res = $this->sqlite->query("SELECT * FROM types");
     $actual_types = $this->sqlite->res2arr($res);
     $this->sqlite->res_close($res);
     $expected_types = array(array('id' => "1", 'class' => 'Text', 'ismulti' => "0", 'label' => "testcolumn", 'config' => '{"prefix": "", "postfix": ""}'), array('id' => "2", 'class' => 'Text', 'ismulti' => "1", 'label' => "testMulitColumn", 'config' => '{"prefix": "pre", "postfix": "post"}'));
     $res = $this->sqlite->query("SELECT * FROM schema_cols");
     $actual_cols = $this->sqlite->res2arr($res);
     $this->sqlite->res_close($res);
     $expected_cols = array(array('sid' => "1", 'colref' => "1", 'enabled' => "1", 'tid' => "1", 'sort' => "70"), array('sid' => "1", 'colref' => "2", 'enabled' => "1", 'tid' => "2", 'sort' => "40"));
     $res = $this->sqlite->query("SELECT * FROM schemas");
     $actual_schema = $this->sqlite->res2row($res);
     $this->sqlite->res_close($res);
     $this->assertSame('1', $result);
     $this->assertEquals($expected_tableSQL, $tableSQL);
     $this->assertEquals($expected_types, $actual_types);
     $this->assertEquals($expected_cols, $actual_cols);
     $this->assertEquals('1', $actual_schema['id']);
     $this->assertEquals($testname, $actual_schema['tbl']);
     $this->assertEquals('', $actual_schema['chksum']);
     $this->assertTrue((int) $actual_schema['ts'] > 0, 'timestamp should be larger than 0');
 }
 /**
  * Adds new columns to the new schema
  *
  * @return bool
  */
 protected function addColumns()
 {
     if (!isset($this->data['new'])) {
         return true;
     }
     $colref = count($this->oldschema->getColumns()) + 1;
     foreach ($this->data['new'] as $column) {
         if (!$column['isenabled']) {
             continue;
         }
         // we do not add a disabled column
         // todo this duplicates the hardcoding as in  the function above
         $newEntry = array();
         $newEntry['config'] = $column['config'];
         $newEntry['label'] = $column['label'];
         $newEntry['ismulti'] = $column['ismulti'];
         $newEntry['class'] = $column['class'];
         $sort = $column['sort'];
         // only save if the column got a name
         if (!$newEntry['label']) {
             continue;
         }
         // add new column to the data table
         if (!$this->addDataTableColumn($colref)) {
             return false;
         }
         // save the type
         $ok = $this->sqlite->storeEntry('types', $newEntry);
         if (!$ok) {
             return false;
         }
         $res = $this->sqlite->query('SELECT last_insert_rowid()');
         if (!$res) {
             return false;
         }
         $newTid = $this->sqlite->res2single($res);
         $this->sqlite->res_close($res);
         // add this type to the schema columns
         $schemaEntry = array('sid' => $this->newschemaid, 'colref' => $colref, 'enabled' => true, 'tid' => $newTid, 'sort' => $sort);
         $ok = $this->sqlite->storeEntry('schema_cols', $schemaEntry);
         if (!$ok) {
             return false;
         }
         $colref++;
     }
     return true;
 }
 private function getTestPageId()
 {
     $res = $this->db->query('SELECT pid FROM pages WHERE page = ?', 'test');
     $pid = (int) $this->db->res2single($res);
     return $pid;
 }