/** * 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_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); } } }
/** * 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; }
/** * Execute this search and return the result * * The result is a two dimensional array of Value()s. * * This will always query for the full result (not using offset and limit) and then * return the wanted range, setting the count (@see getCount) to the whole result number * * @return Value[][] */ public function execute() { list($sql, $opts) = $this->getSQL(); /** @var \PDOStatement $res */ $res = $this->sqlite->query($sql, $opts); if ($res === false) { throw new StructException("SQL execution failed for\n\n{$sql}"); } $this->result_pids = array(); $result = array(); $cursor = -1; $pageidAndRevOnly = array_reduce($this->columns, function ($pageidAndRevOnly, Column $col) { return $pageidAndRevOnly && $col->getTid() == 0; }, true); while ($row = $res->fetch(\PDO::FETCH_ASSOC)) { $cursor++; if ($cursor < $this->range_begin) { continue; } if ($this->range_end && $cursor >= $this->range_end) { continue; } $C = 0; $resrow = array(); $isempty = true; foreach ($this->columns as $col) { $val = $row["C{$C}"]; if ($col->isMulti()) { $val = explode(self::CONCAT_SEPARATOR, $val); } $value = new Value($col, $val); $isempty &= $this->isEmptyValue($value); $resrow[] = $value; $C++; } // skip empty rows if ($isempty && !$pageidAndRevOnly) { $cursor--; continue; } $this->result_pids[] = $row['PID']; $result[] = $resrow; } $this->sqlite->res_close($res); $this->count = $cursor + 1; return $result; }
/** * 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']; } } }