table = $table; $this->data = $data; $this->oldschema = new Schema($table); /** @var \helper_plugin_struct_db $helper */ $helper = plugin_load('helper', 'struct_db'); $this->sqlite = $helper->getDB(); } /** * Create the new schema * * @return bool|int the new schema id on success */ public function build() { $this->sqlite->query('BEGIN TRANSACTION'); $ok = true; // create the data table if new schema if(!$this->oldschema->getId()) { $ok = $this->newDataTable(); } // create a new schema $ok = $ok && $this->newSchema(); // update column info $ok = $ok && $this->updateColumns(); $ok = $ok && $this->addColumns(); if (!$ok) { $this->sqlite->query('ROLLBACK TRANSACTION'); return false; } $this->sqlite->query('COMMIT TRANSACTION'); return $this->newschemaid; } /** * Creates a new schema * * @todo use checksum or other heuristic to see if we really need a new schema OTOH we probably need one nearly always!? */ protected function newSchema() { $sql = "INSERT INTO schemas (tbl, ts) VALUES (?, ?)"; $this->sqlite->query($sql, $this->table, time()); $res = $this->sqlite->query('SELECT last_insert_rowid()'); $this->newschemaid = $this->sqlite->res2single($res); $this->sqlite->res_close($res); if(!$this->newschemaid) return false; return true; } /** * Updates all the existing column infos and adds them to the new schema */ protected function updateColumns() { foreach($this->oldschema->getColumns() as $column) { $oldEntry = $column->getType()->getAsEntry(); $oldTid = $column->getTid(); $newEntry = $oldEntry; $newTid = $oldTid; $enabled = true; $sort = $column->getSort(); if(isset($this->data['cols'][$column->getColref()])){ // todo I'm not too happy with this hardcoded here - we should probably have a list of fields at one place $newEntry['config'] = $this->data['cols'][$column->getColref()]['config']; $newEntry['label'] = $this->data['cols'][$column->getColref()]['label']; $newEntry['ismulti'] = $this->data['cols'][$column->getColref()]['ismulti']; $newEntry['class'] = $this->data['cols'][$column->getColref()]['class']; $sort = $this->data['cols'][$column->getColref()]['sort']; // when the type definition has changed, we create a new one if(array_diff_assoc($oldEntry, $newEntry)) { $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); } } else { $enabled = false; // no longer there FIXME this assumes we remove the entry from the form completely. We might not want to do that } // add this type to the schema columns $schemaEntry = array( 'sid' => $this->newschemaid, 'colref' => $column->getColref(), 'enabled' => $enabled, 'tid' => $newTid, 'sort' => $sort ); $ok = $this->sqlite->storeEntry('schema_cols', $schemaEntry); if(!$ok) return false; } return true; } /** * 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) { // 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']; $enabled = true; // 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' => $enabled, 'tid' => $newTid, 'sort' => $sort ); $ok = $this->sqlite->storeEntry('schema_cols', $schemaEntry); if(!$ok) return false; $colref++; } return true; } /** * Create a completely new data table with columns yet * * @todo how do we want to handle indexes? * @return bool */ protected function newDataTable() { $tbl = 'data_' . $this->table; $sql = "CREATE TABLE $tbl ( pid NOT NULL, rev INTEGER NOT NULL, PRIMARY KEY(pid, rev) )"; return (bool) $this->sqlite->query($sql); } /** * Add an additional column to the existing data table * * @param int $index the new column index to add * @return bool */ protected function addDataTableColumn($index) { $tbl = 'data_' . $this->table; $sql = " ALTER TABLE $tbl ADD COLUMN col$index DEFAULT ''"; if(! $this->sqlite->query($sql)) { return false; } return true; } }