1*308cc83fSAndreas Gohr<?php 2*308cc83fSAndreas Gohr 3*308cc83fSAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 4*308cc83fSAndreas Gohr 5*308cc83fSAndreas Gohr/** 6*308cc83fSAndreas Gohr * Class AccessTableGlobal 7*308cc83fSAndreas Gohr * 8*308cc83fSAndreas Gohr * Load and (more importantly) save data for Global Schemas 9*308cc83fSAndreas Gohr * 10*308cc83fSAndreas Gohr * @package dokuwiki\plugin\struct\meta 11*308cc83fSAndreas Gohr */ 12*308cc83fSAndreas Gohrclass AccessTableGlobal extends AccessTable 13*308cc83fSAndreas Gohr{ 14*308cc83fSAndreas Gohr public function __construct($table, $pid, $ts = 0, $rid = 0) 15*308cc83fSAndreas Gohr { 16*308cc83fSAndreas Gohr parent::__construct($table, $pid, $ts, $rid); 17*308cc83fSAndreas Gohr } 18*308cc83fSAndreas Gohr 19*308cc83fSAndreas Gohr /** 20*308cc83fSAndreas Gohr * Remove the current data 21*308cc83fSAndreas Gohr */ 22*308cc83fSAndreas Gohr public function clearData() 23*308cc83fSAndreas Gohr { 24*308cc83fSAndreas Gohr if (!$this->rid) return; // no data 25*308cc83fSAndreas Gohr 26*308cc83fSAndreas Gohr /** @noinspection SqlResolve */ 27*308cc83fSAndreas Gohr $sql = 'DELETE FROM ? WHERE rid = ?'; 28*308cc83fSAndreas Gohr $this->sqlite->query($sql, 'data_' . $this->schema->getTable(), $this->rid); 29*308cc83fSAndreas Gohr $this->sqlite->query($sql, 'multi_' . $this->schema->getTable(), $this->rid); 30*308cc83fSAndreas Gohr } 31*308cc83fSAndreas Gohr 32*308cc83fSAndreas Gohr protected function getLastRevisionTimestamp() 33*308cc83fSAndreas Gohr { 34*308cc83fSAndreas Gohr return 0; 35*308cc83fSAndreas Gohr } 36*308cc83fSAndreas Gohr 37*308cc83fSAndreas Gohr /** 38*308cc83fSAndreas Gohr * @inheritDoc 39*308cc83fSAndreas Gohr */ 40*308cc83fSAndreas Gohr protected function buildGetDataSQL($idColumn = 'rid') 41*308cc83fSAndreas Gohr { 42*308cc83fSAndreas Gohr return parent::buildGetDataSQL($idColumn); 43*308cc83fSAndreas Gohr } 44*308cc83fSAndreas Gohr 45*308cc83fSAndreas Gohr /** 46*308cc83fSAndreas Gohr * @inheritDoc 47*308cc83fSAndreas Gohr */ 48*308cc83fSAndreas Gohr protected function getSingleSql() 49*308cc83fSAndreas Gohr { 50*308cc83fSAndreas Gohr $cols = array_merge($this->getSingleNoninputCols(), $this->singleCols); 51*308cc83fSAndreas Gohr $cols = join(',', $cols); 52*308cc83fSAndreas Gohr $vals = array_merge($this->getSingleNoninputValues(), $this->singleValues); 53*308cc83fSAndreas Gohr $rid = $this->getRid() ?: "(SELECT (COALESCE(MAX(rid), 0 ) + 1) FROM $this->stable)"; 54*308cc83fSAndreas Gohr 55*308cc83fSAndreas Gohr return "REPLACE INTO $this->stable (rid, $cols) VALUES ($rid," . trim(str_repeat('?,', count($vals)), ',') . ');'; 56*308cc83fSAndreas Gohr } 57*308cc83fSAndreas Gohr 58*308cc83fSAndreas Gohr /** 59*308cc83fSAndreas Gohr * @inheritDoc 60*308cc83fSAndreas Gohr */ 61*308cc83fSAndreas Gohr protected function getMultiSql() 62*308cc83fSAndreas Gohr { 63*308cc83fSAndreas Gohr return "REPLACE INTO $this->mtable (pid, rid, rev, latest, colref, row, value) VALUES (?,?,?,?,?,?,?)"; 64*308cc83fSAndreas Gohr } 65*308cc83fSAndreas Gohr 66*308cc83fSAndreas Gohr /** 67*308cc83fSAndreas Gohr * @inheritDoc 68*308cc83fSAndreas Gohr */ 69*308cc83fSAndreas Gohr protected function validateTypeData($data) 70*308cc83fSAndreas Gohr { 71*308cc83fSAndreas Gohr // we do not store completely empty rows 72*308cc83fSAndreas Gohr $isempty = array_reduce($data, function ($isempty, $cell) { 73*308cc83fSAndreas Gohr return $isempty && ($cell === '' || $cell === [] || $cell === null); 74*308cc83fSAndreas Gohr }, true); 75*308cc83fSAndreas Gohr 76*308cc83fSAndreas Gohr return !$isempty; 77*308cc83fSAndreas Gohr } 78*308cc83fSAndreas Gohr 79*308cc83fSAndreas Gohr /** 80*308cc83fSAndreas Gohr * @inheritDoc 81*308cc83fSAndreas Gohr */ 82*308cc83fSAndreas Gohr protected function getSingleNoninputCols() 83*308cc83fSAndreas Gohr { 84*308cc83fSAndreas Gohr return ['pid', 'rev', 'latest']; 85*308cc83fSAndreas Gohr } 86*308cc83fSAndreas Gohr 87*308cc83fSAndreas Gohr /** 88*308cc83fSAndreas Gohr * @inheritDoc 89*308cc83fSAndreas Gohr */ 90*308cc83fSAndreas Gohr protected function getSingleNoninputValues() 91*308cc83fSAndreas Gohr { 92*308cc83fSAndreas Gohr return [$this->pid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST]; 93*308cc83fSAndreas Gohr } 94*308cc83fSAndreas Gohr 95*308cc83fSAndreas Gohr /** 96*308cc83fSAndreas Gohr * @inheritDoc 97*308cc83fSAndreas Gohr */ 98*308cc83fSAndreas Gohr protected function getMultiNoninputValues() 99*308cc83fSAndreas Gohr { 100*308cc83fSAndreas Gohr return [$this->pid, $this->rid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST]; 101*308cc83fSAndreas Gohr } 102*308cc83fSAndreas Gohr 103*308cc83fSAndreas Gohr /** 104*308cc83fSAndreas Gohr * Set new rid if this is a new insert 105*308cc83fSAndreas Gohr 106*308cc83fSAndreas Gohr * @return bool 107*308cc83fSAndreas Gohr */ 108*308cc83fSAndreas Gohr protected function afterSingleSave() 109*308cc83fSAndreas Gohr { 110*308cc83fSAndreas Gohr $ok = true; 111*308cc83fSAndreas Gohr if (!$this->rid) { 112*308cc83fSAndreas Gohr $res = $this->sqlite->query("SELECT rid FROM $this->stable WHERE ROWID = last_insert_rowid()"); 113*308cc83fSAndreas Gohr $this->rid = $this->sqlite->res2single($res); 114*308cc83fSAndreas Gohr $this->sqlite->res_close($res); 115*308cc83fSAndreas Gohr if (!$this->rid) { 116*308cc83fSAndreas Gohr $ok = false; 117*308cc83fSAndreas Gohr } 118*308cc83fSAndreas Gohr } 119*308cc83fSAndreas Gohr return $ok; 120*308cc83fSAndreas Gohr } 121*308cc83fSAndreas Gohr} 122