/**
  * retrieve the data saved for the page from the database. Usually there is no need to call this function.
  * Call @see SchemaData::getData instead.
  */
 protected function getDataFromDB()
 {
     list($sql, $opt) = $this->buildGetDataSQL();
     $res = $this->sqlite->query($sql, $opt);
     $data = $this->sqlite->res2arr($res);
     return $data;
 }
 public function test_build_update()
 {
     // arrange
     $initialdata = array();
     $initialdata['new']['new1']['sort'] = 70;
     $initialdata['new']['new1']['label'] = 'testcolumn';
     $initialdata['new']['new1']['ismulti'] = 0;
     $initialdata['new']['new1']['config'] = '{"prefix": "", "postfix": ""}';
     $initialdata['new']['new1']['class'] = 'Text';
     $initialdata['new']['new1']['isenabled'] = '1';
     $testname = 'testTable';
     $testname = Schema::cleanTableName($testname);
     $builder = new SchemaBuilder($testname, $initialdata);
     $result = $builder->build();
     $this->assertSame($result, '1', 'Prerequiste setup  in order to have basis which to change during act');
     $updatedata = array();
     $updatedata['id'] = "1";
     $updatedata['cols']['1']['sort'] = 65;
     $updatedata['cols']['1']['label'] = 'testColumn';
     $updatedata['cols']['1']['ismulti'] = 1;
     $updatedata['cols']['1']['config'] = '{"prefix": "pre", "postfix": "fix"}';
     $updatedata['cols']['1']['class'] = 'Text';
     $updatedata['cols']['1']['isenabled'] = '1';
     // act
     $builder = new SchemaBuilder($testname, $updatedata);
     $result = $builder->build();
     $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' => "testColumn", 'config' => '{"prefix": "pre", "postfix": "fix"}'));
     // assert
     $this->assertSame($result, '2');
     $this->assertEquals($actual_types, $expected_types);
 }
 /**
  * fetch all pages where the schema isn't assigned, yet and reevaluate the page assignments for those pages and assign when needed
  *
  * @param $table
  */
 public function propagatePageAssignments($table)
 {
     $sql = 'SELECT pid FROM schema_assignments WHERE tbl != ? OR assigned != 1';
     $res = $this->sqlite->query($sql, $table);
     $pagerows = $this->sqlite->res2arr($res);
     $this->sqlite->res_close($res);
     foreach ($pagerows as $row) {
         $tables = $this->getPageAssignments($row['pid'], true);
         if (in_array($table, $tables)) {
             $this->assignPageSchema($row['pid'], $table);
         }
     }
 }
 public function test_saveData()
 {
     // arrange
     $testdata = array('testcolumn' => 'value1_saved', 'testMulitColumn' => array("value2.1_saved", "value2.2_saved", "value2.3_saved"));
     // act
     $schemaData = meta\AccessTable::byTableName('testtable', 'testpage', time());
     $result = $schemaData->saveData($testdata);
     // assert
     /** @noinspection SqlResolve */
     $res = $this->sqlite->query("SELECT pid, col1, col2 FROM data_testtable WHERE pid = ? ORDER BY rev DESC LIMIT 1", array('testpage'));
     $actual_saved_single = $this->sqlite->res2row($res);
     $expected_saved_single = array('pid' => 'testpage', 'col1' => 'value1_saved', 'col2' => 'value2.1_saved');
     /** @noinspection SqlResolve */
     $res = $this->sqlite->query("SELECT colref, row, value FROM multi_testtable WHERE pid = ? ORDER BY rev DESC LIMIT 3", array('testpage'));
     $actual_saved_multi = $this->sqlite->res2arr($res);
     $expected_saved_multi = array(array('colref' => '2', 'row' => '1', 'value' => "value2.1_saved"), array('colref' => '2', 'row' => '2', 'value' => "value2.2_saved"), array('colref' => '2', 'row' => '3', 'value' => "value2.3_saved"));
     $this->assertTrue($result, 'should be true on success');
     $this->assertEquals($expected_saved_single, $actual_saved_single, 'single value fields');
     $this->assertEquals($expected_saved_multi, $actual_saved_multi, 'multi value fields');
 }
 /**
  * Write the latest value from an entry in a data_ table to the corresponding multi_table
  *
  * @param string $table
  * @param int    $colref
  */
 protected function migrateSingleToMulti($table, $colref)
 {
     /** @noinspection SqlResolve */
     $sqlSelect = "SELECT pid, rev, col{$colref} AS value FROM data_{$table} WHERE latest = 1";
     $res = $this->sqlite->query($sqlSelect);
     $valueSet = $this->sqlite->res2arr($res);
     $this->sqlite->res_close($res);
     $valueString = array();
     $arguments = array();
     foreach ($valueSet as $values) {
         if (blank($values['value']) || trim($values['value']) == '') {
             continue;
         }
         $valueString[] = "(?, ?, ?, ?, ?)";
         $arguments = array_merge($arguments, array($colref, $values['pid'], $values['rev'], 1, $values['value']));
     }
     if (empty($valueString)) {
         return;
     }
     $valueString = join(',', $valueString);
     /** @noinspection SqlResolve */
     $sqlInsert = "INSERT OR REPLACE INTO multi_{$table} (colref, pid, rev, row, value) VALUES {$valueString}";
     $this->sqlite->query($sqlInsert, $arguments);
 }
Ejemplo n.º 6
0
 /**
  * Schema constructor
  *
  * @param string $table The table this schema is for
  * @param int $ts The timestamp for when this schema was valid, 0 for current
  * @param bool $islookup only used when creating a new schema, makes the new schema a lookup
  */
 public function __construct($table, $ts = 0, $islookup = false)
 {
     /** @var \helper_plugin_struct_db $helper */
     $helper = plugin_load('helper', 'struct_db');
     $info = $helper->getInfo();
     $this->structversion = $info['date'];
     $this->sqlite = $helper->getDB();
     if (!$this->sqlite) {
         return;
     }
     $table = self::cleanTableName($table);
     $this->table = $table;
     $this->ts = $ts;
     // load info about the schema itself
     if ($ts) {
         $sql = "SELECT *\n                      FROM schemas\n                     WHERE tbl = ?\n                       AND ts <= ?\n                  ORDER BY ts DESC\n                     LIMIT 1";
         $opt = array($table, $ts);
     } else {
         $sql = "SELECT *\n                      FROM schemas\n                     WHERE tbl = ?\n                  ORDER BY ts DESC\n                     LIMIT 1";
         $opt = array($table);
     }
     $res = $this->sqlite->query($sql, $opt);
     if ($this->sqlite->res2count($res)) {
         $schema = $this->sqlite->res2arr($res);
         $result = array_shift($schema);
         $this->id = $result['id'];
         $this->user = $result['user'];
         $this->chksum = $result['chksum'];
         $this->islookup = $result['islookup'];
         $this->ts = $result['ts'];
         $this->editors = $result['editors'];
     } else {
         $this->islookup = $islookup;
     }
     $this->sqlite->res_close($res);
     if (!$this->id) {
         return;
     }
     // load existing columns
     $sql = "SELECT SC.*, T.*\n                  FROM schema_cols SC,\n                       types T\n                 WHERE SC.sid = ?\n                   AND SC.tid = T.id\n              ORDER BY SC.sort";
     $res = $this->sqlite->query($sql, $this->id);
     $rows = $this->sqlite->res2arr($res);
     $this->sqlite->res_close($res);
     foreach ($rows as $row) {
         if ($row['class'] == 'Integer') {
             $row['class'] = 'Decimal';
         }
         $class = 'dokuwiki\\plugin\\struct\\types\\' . $row['class'];
         if (!class_exists($class)) {
             // This usually never happens, except during development
             msg('Unknown type "' . hsc($row['class']) . '" falling back to Text', -1);
             $class = 'dokuwiki\\plugin\\struct\\types\\Text';
         }
         $config = json_decode($row['config'], true);
         /** @var AbstractBaseType $type */
         $type = new $class($config, $row['label'], $row['ismulti'], $row['tid']);
         $column = new Column($row['sort'], $type, $row['colref'], $row['enabled'], $table);
         $type->setContext($column);
         $this->columns[] = $column;
         if ($row['sort'] > $this->maxsort) {
             $this->maxsort = $row['sort'];
         }
     }
 }