115929be2SAndreas Gohr<?php 215929be2SAndreas Gohr 315929be2SAndreas Gohrnamespace plugin\struct\meta; 415929be2SAndreas Gohr 515929be2SAndreas Gohrclass Search { 69d7a36f9SAndreas Gohr /** 79d7a36f9SAndreas Gohr * This separator will be used to concat multi values to flatten them in the result set 89d7a36f9SAndreas Gohr */ 99d7a36f9SAndreas Gohr const CONCAT_SEPARATOR = "\n!_-_-_-_-_!\n"; 109d7a36f9SAndreas Gohr 11*5511bd5bSAndreas Gohr /** 12*5511bd5bSAndreas Gohr * The list of known and allowed comparators 13*5511bd5bSAndreas Gohr */ 14*5511bd5bSAndreas Gohr const COMPARATORS = array( 15*5511bd5bSAndreas Gohr '<', '>', '<=', '>=', '!=', '!~', '~' 16*5511bd5bSAndreas Gohr ); 17*5511bd5bSAndreas Gohr 189d7a36f9SAndreas Gohr /** @var \helper_plugin_sqlite */ 199d7a36f9SAndreas Gohr protected $sqlite; 2015929be2SAndreas Gohr 2115929be2SAndreas Gohr /** @var Schema[] list of schemas to query */ 2215929be2SAndreas Gohr protected $schemas = array(); 2315929be2SAndreas Gohr 2415929be2SAndreas Gohr /** @var Column[] list of columns to select */ 2515929be2SAndreas Gohr protected $columns = array(); 2615929be2SAndreas Gohr 2715929be2SAndreas Gohr /** @var array the sorting of the result */ 2815929be2SAndreas Gohr protected $sortby = array(); 2915929be2SAndreas Gohr 309d7a36f9SAndreas Gohr /** @var array the filters */ 319d7a36f9SAndreas Gohr protected $filter = array(); 3215929be2SAndreas Gohr 3315929be2SAndreas Gohr /** @var array list of aliases tables can be referenced by */ 3415929be2SAndreas Gohr protected $aliases = array(); 3515929be2SAndreas Gohr 3615929be2SAndreas Gohr /** 379d7a36f9SAndreas Gohr * Search constructor. 389d7a36f9SAndreas Gohr */ 399d7a36f9SAndreas Gohr public function __construct() { 409d7a36f9SAndreas Gohr /** @var \helper_plugin_struct_db $plugin */ 419d7a36f9SAndreas Gohr $plugin = plugin_load('helper', 'struct_db'); 429d7a36f9SAndreas Gohr $this->sqlite = $plugin->getDB(); 439d7a36f9SAndreas Gohr } 449d7a36f9SAndreas Gohr 459d7a36f9SAndreas Gohr /** 4615929be2SAndreas Gohr * Add a schema to be searched 4715929be2SAndreas Gohr * 4815929be2SAndreas Gohr * Call multiple times for multiple schemas. 4915929be2SAndreas Gohr * 5015929be2SAndreas Gohr * @param string $table 5115929be2SAndreas Gohr * @param string $alias 5215929be2SAndreas Gohr */ 5315929be2SAndreas Gohr public function addSchema($table, $alias = '') { 5415929be2SAndreas Gohr $this->schemas[$table] = new Schema($table); 5515929be2SAndreas Gohr if($alias) $this->aliases[$alias] = $table; 5615929be2SAndreas Gohr } 5715929be2SAndreas Gohr 5815929be2SAndreas Gohr /** 5915929be2SAndreas Gohr * Add a column to be returned by the search 6015929be2SAndreas Gohr * 6115929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 6215929be2SAndreas Gohr * added before 6315929be2SAndreas Gohr * 6415929be2SAndreas Gohr * @param string $colname may contain an alias 6515929be2SAndreas Gohr */ 6615929be2SAndreas Gohr public function addColumn($colname) { 6715929be2SAndreas Gohr $col = $this->findColumn($colname); 6815929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 6915929be2SAndreas Gohr $this->columns[] = $col; 7015929be2SAndreas Gohr } 7115929be2SAndreas Gohr 7215929be2SAndreas Gohr /** 7315929be2SAndreas Gohr * Add sorting options 7415929be2SAndreas Gohr * 7515929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 7615929be2SAndreas Gohr * added before 7715929be2SAndreas Gohr * 7815929be2SAndreas Gohr * @param string $colname may contain an alias 7915929be2SAndreas Gohr * @param bool $asc sort direction (ASC = true, DESC = false) 8015929be2SAndreas Gohr */ 8115929be2SAndreas Gohr public function addSort($colname, $asc = true) { 8215929be2SAndreas Gohr $col = $this->findColumn($colname); 8315929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 8415929be2SAndreas Gohr 8515929be2SAndreas Gohr $this->sortby[] = array($col, $asc); 8615929be2SAndreas Gohr } 8715929be2SAndreas Gohr 8815929be2SAndreas Gohr /** 899d7a36f9SAndreas Gohr * Adds a filter 9015929be2SAndreas Gohr * 9115929be2SAndreas Gohr * @param string $colname may contain an alias 9215929be2SAndreas Gohr * @param string $value 93*5511bd5bSAndreas Gohr * @param string $comp @see self::COMPARATORS 949d7a36f9SAndreas Gohr * @param string $type either 'OR' or 'AND' 9515929be2SAndreas Gohr */ 969d7a36f9SAndreas Gohr public function addFilter($colname, $value, $comp, $type = 'OR') { 97*5511bd5bSAndreas Gohr if(!in_array($comp, self::COMPARATORS)) throw new StructException("Bad comperator. Use ".join(',', self::COMPARATORS)); 98*5511bd5bSAndreas Gohr if($type != 'OR' && $type != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 999d7a36f9SAndreas Gohr 10015929be2SAndreas Gohr $col = $this->findColumn($colname); 10115929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 10215929be2SAndreas Gohr 1039d7a36f9SAndreas Gohr $this->filter[] = array($col, $value, $comp, $type); 10415929be2SAndreas Gohr } 10515929be2SAndreas Gohr 10615929be2SAndreas Gohr /** 10715929be2SAndreas Gohr * Transform the set search parameters into a statement 10815929be2SAndreas Gohr * 10915929be2SAndreas Gohr * @todo limit to the newest data! 11015929be2SAndreas Gohr * @return string 11115929be2SAndreas Gohr */ 11215929be2SAndreas Gohr public function getSQL() { 113*5511bd5bSAndreas Gohr if(!$this->columns) throw new StructException('nocolname'); 11415929be2SAndreas Gohr 11515929be2SAndreas Gohr $from = ''; 1169d7a36f9SAndreas Gohr $select = ''; 1179d7a36f9SAndreas Gohr $order = ''; 1189d7a36f9SAndreas Gohr $grouping = array(); 1199d7a36f9SAndreas Gohr $opts = array(); 1209d7a36f9SAndreas Gohr $where = '1 = 1'; 12115929be2SAndreas Gohr 1229d7a36f9SAndreas Gohr // basic tables 1239d7a36f9SAndreas Gohr $first = ''; 1249d7a36f9SAndreas Gohr foreach($this->schemas as $schema) { 1259d7a36f9SAndreas Gohr if($first) { 1269d7a36f9SAndreas Gohr // follow up tables 1279d7a36f9SAndreas Gohr $from .= "\nLEFT OUTER JOIN data_{$schema->getTable()} ON data_$first.pid = data_{$schema->getTable()}.pid"; 1289d7a36f9SAndreas Gohr } else { 1299d7a36f9SAndreas Gohr // first table 1309d7a36f9SAndreas Gohr $select .= "data_{$schema->getTable()}.pid as PID, "; 1319d7a36f9SAndreas Gohr $from .= "data_{$schema->getTable()}"; 1329d7a36f9SAndreas Gohr $first = $schema->getTable(); 13315929be2SAndreas Gohr } 1347059e7e1SAndreas Gohr 1357059e7e1SAndreas Gohr $where .= "\nAND data_{$schema->getTable()}.latest = 1"; 1369d7a36f9SAndreas Gohr } 13715929be2SAndreas Gohr 13815929be2SAndreas Gohr // columns to select, handling multis 1399d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 14015929be2SAndreas Gohr $n = 0; 14115929be2SAndreas Gohr foreach($this->columns as $col) { 14215929be2SAndreas Gohr $CN = 'C' . $n++; 14315929be2SAndreas Gohr 14415929be2SAndreas Gohr if($col->isMulti()) { 14515929be2SAndreas Gohr $tn = 'M' . $col->getColref(); 1469d7a36f9SAndreas Gohr $select .= "GROUP_CONCAT($tn.value, '$sep') AS $CN, "; 1479d7a36f9SAndreas Gohr $from .= "\nLEFT OUTER JOIN multivals AS $tn"; 1489d7a36f9SAndreas Gohr $from .= " ON data_{$col->getTable()}.pid = $tn.pid AND data_{$col->getTable()}.rev = $tn.rev"; 14915929be2SAndreas Gohr $from .= " AND $tn.tbl = '{$col->getTable()}' AND $tn.colref = {$col->getColref()}\n"; 15015929be2SAndreas Gohr } else { 15115929be2SAndreas Gohr $select .= 'data_' . $col->getTable() . ' . col' . $col->getColref() . " AS $CN, "; 1529d7a36f9SAndreas Gohr $grouping[] = $CN; 15315929be2SAndreas Gohr } 15415929be2SAndreas Gohr } 15515929be2SAndreas Gohr $select = rtrim($select, ', '); 15615929be2SAndreas Gohr 1579d7a36f9SAndreas Gohr // where clauses 158b8fe6730SAndreas Gohr foreach($this->filter as $filter) { 159b8fe6730SAndreas Gohr list($col, $value, $comp, $type) = $filter; 160b8fe6730SAndreas Gohr 1619d7a36f9SAndreas Gohr /** @var $col Column */ 1629d7a36f9SAndreas Gohr if($col->isMulti()) { 1639d7a36f9SAndreas Gohr $tn = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before 1649d7a36f9SAndreas Gohr $from .= "\nLEFT OUTER JOIN multivals AS $tn"; 1659d7a36f9SAndreas Gohr $from .= " ON data_{$col->getTable()}.pid = $tn.pid AND data_{$col->getTable()}.rev = $tn.rev"; 1669d7a36f9SAndreas Gohr $from .= " AND $tn.tbl = '{$col->getTable()}' AND $tn.colref = {$col->getColref()}\n"; 16715929be2SAndreas Gohr 1689d7a36f9SAndreas Gohr $column = "$tn.value"; 1699d7a36f9SAndreas Gohr } else { 1709d7a36f9SAndreas Gohr $column = "data_{$col->getTable()}.col{$col->getColref()}"; 1719d7a36f9SAndreas Gohr } 1729d7a36f9SAndreas Gohr 1739d7a36f9SAndreas Gohr list($wsql, $wopt) = $col->getType()->compare($column, $comp, $value); 1749d7a36f9SAndreas Gohr $opts = array_merge($opts, $wopt); 1759d7a36f9SAndreas Gohr 1769d7a36f9SAndreas Gohr $where .= " $type $wsql"; 1779d7a36f9SAndreas Gohr } 1789d7a36f9SAndreas Gohr 1799d7a36f9SAndreas Gohr // sorting 180b8fe6730SAndreas Gohr foreach($this->sortby as $sort) { 181b8fe6730SAndreas Gohr list($col, $asc) = $sort; 182b8fe6730SAndreas Gohr 1839d7a36f9SAndreas Gohr /** @var $col Column */ 1849d7a36f9SAndreas Gohr if($col->isMulti()) { 1859d7a36f9SAndreas Gohr // FIXME how to sort by multival? 1869d7a36f9SAndreas Gohr // FIXME what if sort by non merged multival? 1879d7a36f9SAndreas Gohr } else { 1889d7a36f9SAndreas Gohr $order .= "data_{$col->getTable()}.col{$col->getColref()} "; 1899d7a36f9SAndreas Gohr $order .= ($asc) ? 'ASC' : 'DESC'; 1909d7a36f9SAndreas Gohr $order .= ', '; 1919d7a36f9SAndreas Gohr } 1929d7a36f9SAndreas Gohr } 1939d7a36f9SAndreas Gohr $order = rtrim($order, ', '); 1949d7a36f9SAndreas Gohr 1959d7a36f9SAndreas Gohr $sql = "SELECT $select\n FROM $from\nWHERE $where\nGROUP BY " . join(', ', $grouping); 1969d7a36f9SAndreas Gohr if($order) $sql .= "\nORDER BY $order"; 1979d7a36f9SAndreas Gohr 1989d7a36f9SAndreas Gohr {#debugging 1999d7a36f9SAndreas Gohr $res = $this->sqlite->query($sql, $opts); 2009d7a36f9SAndreas Gohr $data = $this->sqlite->res2arr($res); 2019d7a36f9SAndreas Gohr $this->sqlite->res_close($res); 2029d7a36f9SAndreas Gohr print_r($data); 2039d7a36f9SAndreas Gohr } 2049d7a36f9SAndreas Gohr 20515929be2SAndreas Gohr return $sql; 20615929be2SAndreas Gohr } 20715929be2SAndreas Gohr 20815929be2SAndreas Gohr /** 20915929be2SAndreas Gohr * Find a column to be used in the search 21015929be2SAndreas Gohr * 21115929be2SAndreas Gohr * @param string $colname may contain an alias 21215929be2SAndreas Gohr * @return bool|Column 21315929be2SAndreas Gohr */ 21415929be2SAndreas Gohr protected function findColumn($colname) { 215*5511bd5bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 21615929be2SAndreas Gohr 21715929be2SAndreas Gohr // resolve the alias or table name 21815929be2SAndreas Gohr list($table, $colname) = explode('.', $colname, 2); 21915929be2SAndreas Gohr if(!$colname) { 22015929be2SAndreas Gohr $colname = $table; 22115929be2SAndreas Gohr $table = ''; 22215929be2SAndreas Gohr } 22315929be2SAndreas Gohr if($table && isset($this->aliases[$table])) { 22415929be2SAndreas Gohr $table = $this->aliases[$table]; 22515929be2SAndreas Gohr } 22615929be2SAndreas Gohr 227*5511bd5bSAndreas Gohr if(!$colname) throw new StructException('nocolname'); 22815929be2SAndreas Gohr 22915929be2SAndreas Gohr // if table name given search only that, otherwiese try all for matching column name 23015929be2SAndreas Gohr if($table) { 23115929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 23215929be2SAndreas Gohr } else { 23315929be2SAndreas Gohr $schemas = $this->schemas; 23415929be2SAndreas Gohr } 23515929be2SAndreas Gohr 23615929be2SAndreas Gohr // find it 23715929be2SAndreas Gohr $col = false; 23815929be2SAndreas Gohr foreach($schemas as $schema) { 23915929be2SAndreas Gohr $col = $schema->findColumn($colname); 24015929be2SAndreas Gohr if($col) break; 24115929be2SAndreas Gohr } 24215929be2SAndreas Gohr 24315929be2SAndreas Gohr return $col; 24415929be2SAndreas Gohr } 24515929be2SAndreas Gohr 24615929be2SAndreas Gohr} 24715929be2SAndreas Gohr 248*5511bd5bSAndreas Gohr 249