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 115511bd5bSAndreas Gohr /** 125511bd5bSAndreas Gohr * The list of known and allowed comparators 135511bd5bSAndreas Gohr */ 1474461852SAndreas Gohr static public $COMPARATORS = array( 155511bd5bSAndreas Gohr '<', '>', '<=', '>=', '!=', '!~', '~' 165511bd5bSAndreas Gohr ); 175511bd5bSAndreas 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 935511bd5bSAndreas 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') { 9774461852SAndreas Gohr if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS)); 985511bd5bSAndreas 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 /** 107*b2ed727aSAndreas Gohr * Execute this search and return the result 108*b2ed727aSAndreas Gohr * 109*b2ed727aSAndreas Gohr * The result is a two dimensional array of array. Each cell contains an array with 110*b2ed727aSAndreas Gohr * the keys 'col' (containing a Column object) and 'val' containing the value(s) 111*b2ed727aSAndreas Gohr */ 112*b2ed727aSAndreas Gohr public function execute() { 113*b2ed727aSAndreas Gohr list($sql, $opts) = $this->getSQL(); 114*b2ed727aSAndreas Gohr 115*b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 116*b2ed727aSAndreas Gohr $data = $this->sqlite->res2arr($res); 117*b2ed727aSAndreas Gohr $this->sqlite->res_close($res); 118*b2ed727aSAndreas Gohr 119*b2ed727aSAndreas Gohr $result = array(); 120*b2ed727aSAndreas Gohr foreach($data as $row) { 121*b2ed727aSAndreas Gohr $C = 0; 122*b2ed727aSAndreas Gohr $resrow = array(); 123*b2ed727aSAndreas Gohr foreach($this->columns as $col) { 124*b2ed727aSAndreas Gohr $rescol = array(); 125*b2ed727aSAndreas Gohr $rescol['col'] = $col; 126*b2ed727aSAndreas Gohr $rescol['val'] = $row["C$C"]; 127*b2ed727aSAndreas Gohr if($col->isMulti()) { 128*b2ed727aSAndreas Gohr $rescol['val'] = explode(self::CONCAT_SEPARATOR,$rescol['val']); 129*b2ed727aSAndreas Gohr } 130*b2ed727aSAndreas Gohr $resrow[] = $rescol; 131*b2ed727aSAndreas Gohr $C++; 132*b2ed727aSAndreas Gohr } 133*b2ed727aSAndreas Gohr $result[] = $resrow; 134*b2ed727aSAndreas Gohr } 135*b2ed727aSAndreas Gohr return $result; 136*b2ed727aSAndreas Gohr } 137*b2ed727aSAndreas Gohr 138*b2ed727aSAndreas Gohr /** 13915929be2SAndreas Gohr * Transform the set search parameters into a statement 14015929be2SAndreas Gohr * 141*b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 14215929be2SAndreas Gohr */ 14315929be2SAndreas Gohr public function getSQL() { 1445511bd5bSAndreas Gohr if(!$this->columns) throw new StructException('nocolname'); 14515929be2SAndreas Gohr 14615929be2SAndreas Gohr $from = ''; 1479d7a36f9SAndreas Gohr $select = ''; 1489d7a36f9SAndreas Gohr $order = ''; 1499d7a36f9SAndreas Gohr $grouping = array(); 1509d7a36f9SAndreas Gohr $opts = array(); 1519d7a36f9SAndreas Gohr $where = '1 = 1'; 15215929be2SAndreas Gohr 1539d7a36f9SAndreas Gohr // basic tables 1549d7a36f9SAndreas Gohr $first = ''; 1559d7a36f9SAndreas Gohr foreach($this->schemas as $schema) { 1569d7a36f9SAndreas Gohr if($first) { 1579d7a36f9SAndreas Gohr // follow up tables 1589d7a36f9SAndreas Gohr $from .= "\nLEFT OUTER JOIN data_{$schema->getTable()} ON data_$first.pid = data_{$schema->getTable()}.pid"; 1599d7a36f9SAndreas Gohr } else { 1609d7a36f9SAndreas Gohr // first table 1619d7a36f9SAndreas Gohr $select .= "data_{$schema->getTable()}.pid as PID, "; 1629d7a36f9SAndreas Gohr $from .= "data_{$schema->getTable()}"; 1639d7a36f9SAndreas Gohr $first = $schema->getTable(); 16415929be2SAndreas Gohr } 1657059e7e1SAndreas Gohr 1667059e7e1SAndreas Gohr $where .= "\nAND data_{$schema->getTable()}.latest = 1"; 1679d7a36f9SAndreas Gohr } 16815929be2SAndreas Gohr 16915929be2SAndreas Gohr // columns to select, handling multis 1709d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 17115929be2SAndreas Gohr $n = 0; 17215929be2SAndreas Gohr foreach($this->columns as $col) { 17315929be2SAndreas Gohr $CN = 'C' . $n++; 17415929be2SAndreas Gohr 17515929be2SAndreas Gohr if($col->isMulti()) { 17615929be2SAndreas Gohr $tn = 'M' . $col->getColref(); 1779d7a36f9SAndreas Gohr $select .= "GROUP_CONCAT($tn.value, '$sep') AS $CN, "; 1780fe33e72SAndreas Gohr $from .= "\nLEFT OUTER JOIN multi_{$col->getTable()} AS $tn"; 1799d7a36f9SAndreas Gohr $from .= " ON data_{$col->getTable()}.pid = $tn.pid AND data_{$col->getTable()}.rev = $tn.rev"; 1800fe33e72SAndreas Gohr $from .= " AND $tn.colref = {$col->getColref()}\n"; 18115929be2SAndreas Gohr } else { 18215929be2SAndreas Gohr $select .= 'data_' . $col->getTable() . '.col' . $col->getColref() . " AS $CN, "; 1839d7a36f9SAndreas Gohr $grouping[] = $CN; 18415929be2SAndreas Gohr } 18515929be2SAndreas Gohr } 18615929be2SAndreas Gohr $select = rtrim($select, ', '); 18715929be2SAndreas Gohr 1889d7a36f9SAndreas Gohr // where clauses 189b8fe6730SAndreas Gohr foreach($this->filter as $filter) { 190b8fe6730SAndreas Gohr list($col, $value, $comp, $type) = $filter; 191b8fe6730SAndreas Gohr 1929d7a36f9SAndreas Gohr /** @var $col Column */ 1939d7a36f9SAndreas Gohr if($col->isMulti()) { 1949d7a36f9SAndreas Gohr $tn = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before 1950fe33e72SAndreas Gohr $from .= "\nLEFT OUTER JOIN multi_{$col->getTable()} AS $tn"; 1969d7a36f9SAndreas Gohr $from .= " ON data_{$col->getTable()}.pid = $tn.pid AND data_{$col->getTable()}.rev = $tn.rev"; 1970fe33e72SAndreas Gohr $from .= " AND $tn.colref = {$col->getColref()}\n"; 19815929be2SAndreas Gohr 1999d7a36f9SAndreas Gohr $column = "$tn.value"; 2009d7a36f9SAndreas Gohr } else { 2019d7a36f9SAndreas Gohr $column = "data_{$col->getTable()}.col{$col->getColref()}"; 2029d7a36f9SAndreas Gohr } 2039d7a36f9SAndreas Gohr 2049d7a36f9SAndreas Gohr list($wsql, $wopt) = $col->getType()->compare($column, $comp, $value); 2059d7a36f9SAndreas Gohr $opts = array_merge($opts, $wopt); 2069d7a36f9SAndreas Gohr 2079d7a36f9SAndreas Gohr $where .= " $type $wsql"; 2089d7a36f9SAndreas Gohr } 2099d7a36f9SAndreas Gohr 2109d7a36f9SAndreas Gohr // sorting 211b8fe6730SAndreas Gohr foreach($this->sortby as $sort) { 212b8fe6730SAndreas Gohr list($col, $asc) = $sort; 213b8fe6730SAndreas Gohr 2149d7a36f9SAndreas Gohr /** @var $col Column */ 2159d7a36f9SAndreas Gohr if($col->isMulti()) { 2169d7a36f9SAndreas Gohr // FIXME how to sort by multival? 2179d7a36f9SAndreas Gohr // FIXME what if sort by non merged multival? 2189d7a36f9SAndreas Gohr } else { 2199d7a36f9SAndreas Gohr $order .= "data_{$col->getTable()}.col{$col->getColref()} "; 2209d7a36f9SAndreas Gohr $order .= ($asc) ? 'ASC' : 'DESC'; 2219d7a36f9SAndreas Gohr $order .= ', '; 2229d7a36f9SAndreas Gohr } 2239d7a36f9SAndreas Gohr } 2249d7a36f9SAndreas Gohr $order = rtrim($order, ', '); 2259d7a36f9SAndreas Gohr 2269d7a36f9SAndreas Gohr $sql = "SELECT $select\n FROM $from\nWHERE $where\nGROUP BY " . join(', ', $grouping); 2279d7a36f9SAndreas Gohr if($order) $sql .= "\nORDER BY $order"; 2289d7a36f9SAndreas Gohr 229*b2ed727aSAndreas Gohr return array($sql, $opts); 23015929be2SAndreas Gohr } 23115929be2SAndreas Gohr 23215929be2SAndreas Gohr /** 23315929be2SAndreas Gohr * Find a column to be used in the search 23415929be2SAndreas Gohr * 23515929be2SAndreas Gohr * @param string $colname may contain an alias 23615929be2SAndreas Gohr * @return bool|Column 23715929be2SAndreas Gohr */ 23815929be2SAndreas Gohr protected function findColumn($colname) { 2395511bd5bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 24015929be2SAndreas Gohr 24115929be2SAndreas Gohr // resolve the alias or table name 24215929be2SAndreas Gohr list($table, $colname) = explode('.', $colname, 2); 24315929be2SAndreas Gohr if(!$colname) { 24415929be2SAndreas Gohr $colname = $table; 24515929be2SAndreas Gohr $table = ''; 24615929be2SAndreas Gohr } 24715929be2SAndreas Gohr if($table && isset($this->aliases[$table])) { 24815929be2SAndreas Gohr $table = $this->aliases[$table]; 24915929be2SAndreas Gohr } 25015929be2SAndreas Gohr 2515511bd5bSAndreas Gohr if(!$colname) throw new StructException('nocolname'); 25215929be2SAndreas Gohr 25315929be2SAndreas Gohr // if table name given search only that, otherwiese try all for matching column name 25415929be2SAndreas Gohr if($table) { 25515929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 25615929be2SAndreas Gohr } else { 25715929be2SAndreas Gohr $schemas = $this->schemas; 25815929be2SAndreas Gohr } 25915929be2SAndreas Gohr 26015929be2SAndreas Gohr // find it 26115929be2SAndreas Gohr $col = false; 26215929be2SAndreas Gohr foreach($schemas as $schema) { 26315929be2SAndreas Gohr $col = $schema->findColumn($colname); 26415929be2SAndreas Gohr if($col) break; 26515929be2SAndreas Gohr } 26615929be2SAndreas Gohr 26715929be2SAndreas Gohr return $col; 26815929be2SAndreas Gohr } 26915929be2SAndreas Gohr 27015929be2SAndreas Gohr} 27115929be2SAndreas Gohr 2725511bd5bSAndreas Gohr 273