PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/v3/mod_goodrelations/arc/store/ARC2_StoreSelectQueryHandler.php

https://code.google.com/p/goodrelations-for-joomla/
PHP | 1799 lines | 1546 code | 124 blank | 129 comment | 397 complexity | 758dcbbd988787fa7771d925c4f00035 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * ARC2 RDF Store SELECT Query Handler
  4. *
  5. * @author Benjamin Nowack
  6. * @license http://arc.semsol.org/license
  7. * @homepage <http://arc.semsol.org/>
  8. * @package ARC2
  9. * @version 2010-11-16
  10. *
  11. */
  12. ARC2::inc('StoreQueryHandler');
  13. class ARC2_StoreSelectQueryHandler extends ARC2_StoreQueryHandler {
  14. function __construct($a, &$caller) {/* caller has to be a store */
  15. parent::__construct($a, $caller);
  16. }
  17. function __init() {/* db_con */
  18. parent::__init();
  19. $this->store = $this->caller;
  20. $con = $this->store->getDBCon();
  21. $this->handler_type = 'select';
  22. $this->engine_type = $this->v('store_engine_type', 'MyISAM', $this->a);
  23. $this->cache_results = $this->v('store_cache_results', 0, $this->a);
  24. }
  25. /* */
  26. function runQuery($infos) {
  27. $con = $this->store->getDBCon();
  28. $rf = $this->v('result_format', '', $infos);
  29. $this->infos = $infos;
  30. $this->infos['null_vars'] = array();
  31. $this->indexes = array();
  32. $this->pattern_order_offset = 0;
  33. $q_sql = $this->getSQL();
  34. /* debug result formats */
  35. if ($rf == 'sql') return $q_sql;
  36. if ($rf == 'structure') return $this->infos;
  37. if ($rf == 'index') return $this->indexes;
  38. /* create intermediate results (ID-based) */
  39. $tmp_tbl = $this->createTempTable($q_sql);
  40. /* join values */
  41. $r = $this->getFinalQueryResult($q_sql, $tmp_tbl);
  42. /* remove intermediate results */
  43. if (!$this->cache_results) {
  44. $this->queryDB('DROP TABLE IF EXISTS ' . $tmp_tbl, $con);
  45. }
  46. return $r;
  47. }
  48. function getSQL() {
  49. $r = '';
  50. $nl = "\n";
  51. $this->buildInitialIndexes();
  52. foreach ($this->indexes as $i => $index) {
  53. $this->index = array_merge($this->getEmptyIndex(), $index);
  54. $this->analyzeIndex($this->getPattern('0'));
  55. $sub_r = $this->getQuerySQL();
  56. $r .= $r ? $nl . 'UNION' . $this->getDistinctSQL() . $nl : '';
  57. $r .= $this->is_union_query ? '(' . $sub_r . ')' : $sub_r;
  58. $this->indexes[$i] = $this->index;
  59. }
  60. $r .= $this->is_union_query ? $this->getLIMITSQL() : '';
  61. if ($this->v('order_infos', 0, $this->infos['query'])) {
  62. $r = preg_replace('/SELECT(\s+DISTINCT)?\s*/', 'SELECT\\1 NULL AS `_pos_`, ', $r);
  63. }
  64. $pd_count = $this->problematicDependencies();
  65. if ($pd_count) {
  66. /* re-arranging the patterns sometimes reduces the LEFT JOIN dependencies */
  67. $set_sql = 0;
  68. if (!$this->pattern_order_offset) $set_sql = 1;
  69. if (!$set_sql && ($pd_count < $this->opt_sql_pd_count)) $set_sql = 1;
  70. if (!$set_sql && ($pd_count == $this->opt_sql_pd_count) && (strlen($r) < strlen($this->opt_sql))) $set_sql = 1;
  71. if ($set_sql) {
  72. $this->opt_sql = $r;
  73. $this->opt_sql_pd_count = $pd_count;
  74. }
  75. $this->pattern_order_offset++;
  76. if ($this->pattern_order_offset > 5) {
  77. return $this->opt_sql;
  78. }
  79. return $this->getSQL();
  80. }
  81. return $r;
  82. }
  83. function buildInitialIndexes() {
  84. $this->dependency_log = array();
  85. $this->index = $this->getEmptyIndex();
  86. $this->buildIndex($this->infos['query']['pattern'], 0);
  87. $tmp = $this->index;
  88. $this->analyzeIndex($this->getPattern('0'));
  89. $this->initial_index = $this->index;
  90. $this->index = $tmp;
  91. $this->is_union_query = $this->index['union_branches'] ? 1 : 0;
  92. $this->indexes = $this->is_union_query ? $this->getUnionIndexes($this->index) : array($this->index);
  93. }
  94. function createTempTable($q_sql) {
  95. $con = $this->store->getDBCon();
  96. $v = $this->store->getDBVersion();
  97. if ($this->cache_results) {
  98. $tbl = $this->store->getTablePrefix() . 'Q' . md5($q_sql);
  99. }
  100. else {
  101. $tbl = $this->store->getTablePrefix() . 'Q' . md5($q_sql . time() . uniqid(rand()));
  102. }
  103. if (strlen($tbl) > 64) $tbl = 'Q' . md5($tbl);
  104. $tmp_sql = 'CREATE TEMPORARY TABLE ' . $tbl . ' ( ' . $this->getTempTableDef($tbl, $q_sql) . ') ';
  105. $tmp_sql .= (($v < '04-01-00') && ($v >= '04-00-18')) ? 'ENGINE' : (($v >= '04-01-02') ? 'ENGINE' : 'TYPE');
  106. $tmp_sql .= '=' . $this->engine_type;/* HEAP doesn't support AUTO_INCREMENT, and MySQL breaks on MEMORY sometimes */
  107. if (!$this->queryDB($tmp_sql, $con) && !$this->queryDB(str_replace('CREATE TEMPORARY', 'CREATE', $tmp_sql), $con)) {
  108. return $this->addError(mysql_error($con));
  109. }
  110. mysql_unbuffered_query('INSERT INTO ' . $tbl . ' ' . "\n" . $q_sql, $con);
  111. if ($er = mysql_error($con)) $this->addError($er);
  112. return $tbl;
  113. }
  114. function getEmptyIndex() {
  115. return array(
  116. 'from' => array(),
  117. 'join' => array(),
  118. 'left_join' => array(),
  119. 'vars' => array(), 'graph_vars' => array(), 'graph_uris' => array(),
  120. 'bnodes' => array(),
  121. 'triple_patterns' => array(),
  122. 'sub_joins' => array(),
  123. 'constraints' => array(),
  124. 'union_branches'=> array(),
  125. 'patterns' => array(),
  126. 'havings' => array()
  127. );
  128. }
  129. function getTempTableDef($tmp_tbl, $q_sql) {
  130. $col_part = preg_replace('/^SELECT\s*(DISTINCT)?(.*)FROM.*$/s', '\\2', $q_sql);
  131. $parts = explode(',', $col_part);
  132. $has_order_infos = $this->v('order_infos', 0, $this->infos['query']);
  133. $r = '';
  134. $added = array();
  135. foreach ($parts as $part) {
  136. if (preg_match('/\.?(.+)\s+AS\s+`(.+)`/U', trim($part), $m) && !isset($added[$m[2]])) {
  137. $col = $m[1];
  138. $alias = $m[2];
  139. if ($alias == '_pos_') continue;
  140. $r .= $r ? ',' : '';
  141. $r .= "\n `" . $alias . "` int UNSIGNED";
  142. $added[$alias] = 1;
  143. }
  144. }
  145. if ($has_order_infos) {
  146. $r = "\n" . '`_pos_` mediumint NOT NULL AUTO_INCREMENT PRIMARY KEY, ' . $r;
  147. }
  148. return $r ? $r . "\n" : '';
  149. }
  150. function getFinalQueryResult($q_sql, $tmp_tbl) {
  151. /* var names */
  152. $vars = array();
  153. $aggregate_vars = array();
  154. foreach ($this->infos['query']['result_vars'] as $entry) {
  155. if ($entry['aggregate']) {
  156. $vars[] = $entry['alias'];
  157. $aggregate_vars[] = $entry['alias'];
  158. }
  159. else {
  160. $vars[] = $entry['var'];
  161. }
  162. }
  163. /* result */
  164. $r = array('variables' => $vars);
  165. $v_sql = $this->getValueSQL($tmp_tbl, $q_sql);
  166. //echo "\n\n" . $v_sql;
  167. $t1 = ARC2::mtime();
  168. $con = $this->store->getDBCon();
  169. $rs = mysql_unbuffered_query($v_sql, $con);
  170. if ($er = mysql_error($con)) {
  171. $this->addError($er);
  172. }
  173. $t2 = ARC2::mtime();
  174. $rows = array();
  175. $types = array(0 => 'uri', 1 => 'bnode', 2 => 'literal');
  176. if ($rs) {
  177. while ($pre_row = mysql_fetch_array($rs)) {
  178. $row = array();
  179. foreach ($vars as $var) {
  180. if (isset($pre_row[$var])) {
  181. $row[$var] = $pre_row[$var];
  182. $row[$var . ' type'] = isset($pre_row[$var . ' type']) ? $types[$pre_row[$var . ' type']] : (in_array($var, $aggregate_vars) ? 'literal' : 'uri');
  183. if (isset($pre_row[$var . ' lang_dt']) && ($lang_dt = $pre_row[$var . ' lang_dt'])) {
  184. if (preg_match('/^([a-z]+(\-[a-z0-9]+)*)$/i', $lang_dt)) {
  185. $row[$var . ' lang'] = $lang_dt;
  186. }
  187. else {
  188. $row[$var . ' datatype'] = $lang_dt;
  189. }
  190. }
  191. }
  192. }
  193. if ($row || !$vars) {
  194. $rows[] = $row;
  195. }
  196. }
  197. }
  198. $r['rows'] = $rows;
  199. return $r;
  200. }
  201. /* */
  202. function buildIndex($pattern, $id) {
  203. $pattern['id'] = $id;
  204. $type = $this->v('type', '', $pattern);
  205. if (($type == 'filter') && $this->v('constraint', 0, $pattern)) {
  206. $sub_pattern = $pattern['constraint'];
  207. $sub_pattern['parent_id'] = $id;
  208. $sub_id = $id . '_0';
  209. $this->buildIndex($sub_pattern, $sub_id);
  210. $pattern['constraint'] = $sub_id;
  211. }
  212. else {
  213. $sub_patterns = $this->v('patterns', array(), $pattern);
  214. $keys = array_keys($sub_patterns);
  215. $spc = count($sub_patterns);
  216. if (($spc > 4) && $this->pattern_order_offset) {
  217. $keys = array();
  218. for ($i = 0 ; $i < $spc; $i++) {
  219. $keys[$i] = $i + $this->pattern_order_offset;
  220. while ($keys[$i] >= $spc) $keys[$i] -= $spc;
  221. }
  222. }
  223. foreach ($keys as $i => $key) {
  224. $sub_pattern = $sub_patterns[$key];
  225. $sub_pattern['parent_id'] = $id;
  226. $sub_id = $id . '_' . $key;
  227. $this->buildIndex($sub_pattern, $sub_id);
  228. $pattern['patterns'][$i] = $sub_id;
  229. if ($type == 'union') {
  230. $this->index['union_branches'][] = $sub_id;
  231. }
  232. }
  233. }
  234. $this->index['patterns'][$id] = $pattern;
  235. }
  236. /* */
  237. function analyzeIndex($pattern) {
  238. $type = $this->v('type', '', $pattern);
  239. if (!$type) {
  240. //echo '<!-- ' . var_export($this->infos, 1) . ' -->';
  241. return false;
  242. }
  243. $type = $pattern['type'];
  244. $id = $pattern['id'];
  245. /* triple */
  246. if ($type == 'triple') {
  247. foreach (array('s', 'p', 'o') as $term) {
  248. if ($pattern[$term . '_type'] == 'var') {
  249. $val = $pattern[$term];
  250. $this->index['vars'][$val] = array_merge($this->v($val, array(), $this->index['vars']), array(array('table' => $pattern['id'], 'col' =>$term)));
  251. }
  252. if ($pattern[$term . '_type'] == 'bnode') {
  253. $val = $pattern[$term];
  254. $this->index['bnodes'][$val] = array_merge($this->v($val, array(), $this->index['bnodes']), array(array('table' => $pattern['id'], 'col' =>$term)));
  255. }
  256. }
  257. $this->index['triple_patterns'][] = $pattern['id'];
  258. /* joins */
  259. if ($this->isOptionalPattern($id)) {
  260. $this->index['left_join'][] = $id;
  261. }
  262. elseif (!$this->index['from']) {
  263. $this->index['from'][] = $id;
  264. }
  265. elseif (!$this->getJoinInfos($id)) {
  266. $this->index['from'][] = $id;
  267. }
  268. else {
  269. $this->index['join'][] = $id;
  270. }
  271. /* graph infos, graph vars */
  272. $this->index['patterns'][$id]['graph_infos'] = $this->getGraphInfos($id);
  273. foreach ($this->index['patterns'][$id]['graph_infos'] as $info) {
  274. if ($info['type'] == 'graph') {
  275. if ($info['var']) {
  276. $val = $info['var']['value'];
  277. $this->index['graph_vars'][$val] = array_merge($this->v($val, array(), $this->index['graph_vars']), array(array('table' => $id)));
  278. }
  279. elseif ($info['uri']) {
  280. $val = $info['uri'];
  281. $this->index['graph_uris'][$val] = array_merge($this->v($val, array(), $this->index['graph_uris']), array(array('table' => $id)));
  282. }
  283. }
  284. }
  285. }
  286. $sub_ids = $this->v('patterns', array(), $pattern);
  287. foreach ($sub_ids as $sub_id) {
  288. $this->analyzeIndex($this->getPattern($sub_id));
  289. }
  290. }
  291. /* */
  292. function getGraphInfos($id) {
  293. $r = array();
  294. if ($id) {
  295. $pattern = $this->index['patterns'][$id];
  296. $type = $pattern['type'];
  297. /* graph */
  298. if ($type == 'graph') {
  299. $r[] = array('type' => 'graph', 'var' => $pattern['var'], 'uri' => $pattern['uri']);
  300. }
  301. $p_pattern = $this->index['patterns'][$pattern['parent_id']];
  302. if (isset($p_pattern['graph_infos'])) {
  303. return array_merge($p_pattern['graph_infos'], $r);
  304. }
  305. return array_merge($this->getGraphInfos($pattern['parent_id']), $r);
  306. }
  307. /* FROM / FROM NAMED */
  308. else {
  309. if (isset($this->infos['query']['dataset'])) {
  310. foreach ($this->infos['query']['dataset'] as $set) {
  311. $r[] = array_merge(array('type' => 'dataset'), $set);
  312. }
  313. }
  314. }
  315. return $r;
  316. }
  317. /* */
  318. function getPattern($id) {
  319. if (is_array($id)) {
  320. return $id;
  321. }
  322. return $this->v($id, array(), $this->index['patterns']);
  323. }
  324. function getInitialPattern($id) {
  325. return $this->v($id, array(), $this->initial_index['patterns']);
  326. }
  327. /* */
  328. function getUnionIndexes($pre_index) {
  329. $r = array();
  330. $branches = array();
  331. $min_depth = 1000;
  332. /* only process branches with minimum depth */
  333. foreach ($pre_index['union_branches'] as $id) {
  334. $branches[$id] = count(preg_split('/\_/', $id));
  335. $min_depth = min($min_depth, $branches[$id]);
  336. }
  337. foreach ($branches as $branch_id => $depth) {
  338. if ($depth == $min_depth) {
  339. $union_id = preg_replace('/\_[0-9]+$/', '', $branch_id);
  340. $index = array('keeping' => $branch_id, 'union_branches' => array(), 'patterns' => $pre_index['patterns']);
  341. $old_branches = $index['patterns'][$union_id]['patterns'];
  342. $skip_id = ($old_branches[0] == $branch_id) ? $old_branches[1] : $old_branches[0];
  343. $index['patterns'][$union_id]['type'] = 'group';
  344. $index['patterns'][$union_id]['patterns'] = array($branch_id);
  345. $has_sub_unions = 0;
  346. foreach ($index['patterns'] as $pattern_id => $pattern) {
  347. if (preg_match('/^' .$skip_id. '/', $pattern_id)) {
  348. unset($index['patterns'][$pattern_id]);
  349. }
  350. elseif ($pattern['type'] == 'union') {
  351. foreach ($pattern['patterns'] as $sub_union_branch_id) {
  352. $index['union_branches'][] = $sub_union_branch_id;
  353. }
  354. }
  355. }
  356. if ($index['union_branches']) {
  357. $r = array_merge($r, $this->getUnionIndexes($index));
  358. }
  359. else {
  360. $r[] = $index;
  361. }
  362. }
  363. }
  364. return $r;
  365. }
  366. /* */
  367. function isOptionalPattern($id) {
  368. $pattern = $this->getPattern($id);
  369. if ($this->v('type', '', $pattern) == 'optional') {
  370. return 1;
  371. }
  372. if ($this->v('parent_id', '0', $pattern) == '0') {
  373. return 0;
  374. }
  375. return $this->isOptionalPattern($pattern['parent_id']);
  376. }
  377. function getOptionalPattern($id) {
  378. $pn = $this->getPattern($id);
  379. do {
  380. $pn = $this->getPattern($pn['parent_id']);
  381. } while ($pn['parent_id'] && ($pn['type'] != 'optional'));
  382. return $pn['id'];
  383. }
  384. function sameOptional($id, $id2) {
  385. return $this->getOptionalPattern($id) == $this->getOptionalPattern($id2);
  386. }
  387. /* */
  388. function isUnionPattern($id) {
  389. $pattern = $this->getPattern($id);
  390. if ($this->v('type', '', $pattern) == 'union') {
  391. return 1;
  392. }
  393. if ($this->v('parent_id', '0', $pattern) == '0') {
  394. return 0;
  395. }
  396. return $this->isUnionPattern($pattern['parent_id']);
  397. }
  398. /* */
  399. function getValueTable($col) {
  400. return $this->store->getTablePrefix() . (preg_match('/^(s|o)$/', $col) ? $col . '2val' : 'id2val');
  401. }
  402. function getGraphTable() {
  403. return $this->store->getTablePrefix() . 'g2t';
  404. }
  405. /* */
  406. function getQuerySQL() {
  407. $nl = "\n";
  408. $where_sql = $this->getWHERESQL(); /* pre-fills $index['sub_joins'] $index['constraints'] */
  409. $order_sql = $this->getORDERSQL(); /* pre-fills $index['sub_joins'] $index['constraints'] */
  410. return '' .
  411. ($this->is_union_query ? 'SELECT' : 'SELECT' . $this->getDistinctSQL()) . $nl .
  412. $this->getResultVarsSQL() . $nl . /* fills $index['sub_joins'] */
  413. $this->getFROMSQL() .
  414. $this->getAllJoinsSQL() .
  415. $this->getWHERESQL() .
  416. $this->getGROUPSQL() .
  417. $this->getORDERSQL() .
  418. ($this->is_union_query ? '' : $this->getLIMITSQL()) .
  419. $nl .
  420. '';
  421. }
  422. /* */
  423. function getDistinctSQL() {
  424. if ($this->is_union_query) {
  425. return ($this->v('distinct', 0, $this->infos['query']) || $this->v('reduced', 0, $this->infos['query'])) ? '' : ' ALL';
  426. }
  427. return ($this->v('distinct', 0, $this->infos['query']) || $this->v('reduced', 0, $this->infos['query'])) ? ' DISTINCT' : '';
  428. }
  429. /* */
  430. function getResultVarsSQL() {
  431. $r = '';
  432. $vars = $this->infos['query']['result_vars'];
  433. $nl = "\n";
  434. $added = array();
  435. foreach ($vars as $var) {
  436. $var_name = $var['var'];
  437. $tbl_alias = '';
  438. if ($tbl_infos = $this->getVarTableInfos($var_name, 0)) {
  439. $tbl = $tbl_infos['table'];
  440. $col = $tbl_infos['col'];
  441. $tbl_alias = $tbl_infos['table_alias'];
  442. }
  443. elseif ($var_name == 1) {/* ASK query */
  444. $r .= '1 AS `success`';
  445. }
  446. else {
  447. $this->addError('Result variable "' .$var_name. '" not used in query.');
  448. }
  449. if ($tbl_alias) {
  450. /* aggregate */
  451. if ($var['aggregate']) {
  452. $conv_code = '';
  453. if (strtolower($var['aggregate']) != 'count') {
  454. $tbl_alias = 'V_' . $tbl . '_' . $col . '.val';
  455. $conv_code = '0 + ';
  456. }
  457. if (!isset($added[$var['alias']])) {
  458. $r .= $r ? ',' . $nl . ' ' : ' ';
  459. $distinct_code = (strtolower($var['aggregate']) == 'count') && $this->v('distinct', 0, $this->infos['query']) ? 'DISTINCT ' : '';
  460. $r .= $var['aggregate'] . '(' . $conv_code . $distinct_code . $tbl_alias. ') AS `' . $var['alias'] . '`';
  461. $added[$var['alias']] = 1;
  462. }
  463. }
  464. /* normal var */
  465. else {
  466. if (!isset($added[$var_name])) {
  467. $r .= $r ? ',' . $nl . ' ' : ' ';
  468. $r .= $tbl_alias . ' AS `' . $var_name . '`';
  469. $is_s = ($col == 's');
  470. $is_p = ($col == 'p');
  471. $is_o = ($col == 'o');
  472. if ($tbl_alias == 'NULL') {
  473. /* type / add in UNION queries? */
  474. if ($is_s || $is_o) {
  475. $r .= ', ' . $nl . ' NULL AS `' . $var_name . ' type`';
  476. }
  477. /* lang_dt / always add it in UNION queries, the var may be used as s/p/o */
  478. if ($is_o || $this->is_union_query) {
  479. $r .= ', ' . $nl . ' NULL AS `' . $var_name . ' lang_dt`';
  480. }
  481. }
  482. else {
  483. /* type */
  484. if ($is_s || $is_o) {
  485. $r .= ', ' . $nl . ' ' .$tbl_alias . '_type AS `' . $var_name . ' type`';
  486. }
  487. /* lang_dt / always add it in UNION queries, the var may be used as s/p/o */
  488. if ($is_o) {
  489. $r .= ', ' . $nl . ' ' .$tbl_alias . '_lang_dt AS `' . $var_name . ' lang_dt`';
  490. }
  491. elseif ($this->is_union_query) {
  492. $r .= ', ' . $nl . ' NULL AS `' . $var_name . ' lang_dt`';
  493. }
  494. }
  495. $added[$var_name] = 1;
  496. }
  497. }
  498. if (!in_array($tbl_alias, $this->index['sub_joins'])) {
  499. $this->index['sub_joins'][] = $tbl_alias;
  500. }
  501. }
  502. }
  503. return $r ? $r : '1 AS `success`';
  504. }
  505. function getVarTableInfos($var, $ignore_initial_index = 1) {
  506. if ($var == '*') {
  507. return array('table' => '', 'col' => '', 'table_alias' => '*');
  508. }
  509. if ($infos = $this->v($var, 0, $this->index['vars'])) {
  510. $infos[0]['table_alias'] = 'T_' . $infos[0]['table'] . '.' . $infos[0]['col'];
  511. return $infos[0];
  512. }
  513. if ($infos = $this->v($var, 0, $this->index['graph_vars'])) {
  514. $infos[0]['col'] = 'g';
  515. $infos[0]['table_alias'] = 'G_' . $infos[0]['table'] . '.' . $infos[0]['col'];
  516. return $infos[0];
  517. }
  518. if ($this->is_union_query && !$ignore_initial_index) {
  519. if (($infos = $this->v($var, 0, $this->initial_index['vars'])) || ($infos = $this->v($var, 0, $this->initial_index['graph_vars']))) {
  520. if (!in_array($var, $this->infos['null_vars'])) {
  521. $this->infos['null_vars'][] = $var;
  522. }
  523. $infos[0]['table_alias'] = 'NULL';
  524. $infos[0]['col'] = !isset($infos[0]['col']) ? '' : $infos[0]['col'];
  525. return $infos[0];
  526. }
  527. }
  528. return 0;
  529. }
  530. /* */
  531. function getFROMSQL() {
  532. $from_ids = $this->index['from'];
  533. $r = '';
  534. foreach ($from_ids as $from_id) {
  535. $r .= $r ? ', ' : '';
  536. $r .= $this->getTripleTable($from_id) . ' T_' . $from_id;
  537. }
  538. /* MySQL 5 requires parentheses in case of multiple tables */
  539. /* MySQL >5.5 (?) does not allow parentheses in case of a single table anymore! */
  540. $r = (count($from_ids) > 1) ? '(' . $r . ')' : $r;
  541. return $r ? 'FROM ' . $r : '';
  542. }
  543. /* */
  544. function getOrderedJoinIDs() {
  545. return array_merge($this->index['from'], $this->index['join'], $this->index['left_join']);
  546. }
  547. function getJoinInfos($id) {
  548. $r = array();
  549. $tbl_ids = $this->getOrderedJoinIDs();
  550. $pattern = $this->getPattern($id);
  551. foreach ($tbl_ids as $tbl_id) {
  552. $tbl_pattern = $this->getPattern($tbl_id);
  553. if ($tbl_id != $id) {
  554. foreach (array('s', 'p', 'o') as $tbl_term) {
  555. foreach (array('var', 'bnode', 'uri') as $term_type) {
  556. if ($tbl_pattern[$tbl_term . '_type'] == $term_type) {
  557. foreach (array('s', 'p', 'o') as $term) {
  558. if (($pattern[$term . '_type'] == $term_type) && ($tbl_pattern[$tbl_term] == $pattern[$term])) {
  559. $r[] = array('term' => $term, 'join_tbl' => $tbl_id, 'join_term' => $tbl_term);
  560. }
  561. }
  562. }
  563. }
  564. }
  565. }
  566. }
  567. return $r;
  568. }
  569. function getAllJoinsSQL() {
  570. $js = $this->getJoins();
  571. $ljs = $this->getLeftJoins();
  572. $entries = array_merge($js, $ljs);
  573. $id2code = array();
  574. foreach ($entries as $entry) {
  575. if (preg_match('/([^\s]+) ON (.*)/s', $entry, $m)) {
  576. $id2code[$m[1]] = $entry;
  577. }
  578. }
  579. $deps = array();
  580. foreach ($id2code as $id => $code) {
  581. $deps[$id]['rank'] = 0;
  582. foreach ($id2code as $other_id => $other_code) {
  583. $deps[$id]['rank'] += ($id != $other_id) && preg_match('/' . $other_id . '/', $code) ? 1 : 0;
  584. $deps[$id][$other_id] = ($id != $other_id) && preg_match('/' . $other_id . '/', $code) ? 1 : 0;
  585. }
  586. }
  587. $r = '';
  588. do {
  589. /* get next 0-rank */
  590. $next_id = 0;
  591. foreach ($deps as $id => $infos) {
  592. if ($infos['rank'] == 0) {
  593. $next_id = $id;
  594. break;
  595. }
  596. }
  597. if ($next_id) {
  598. $r .= "\n" . $id2code[$next_id];
  599. unset($deps[$next_id]);
  600. foreach ($deps as $id => $infos) {
  601. $deps[$id]['rank'] = 0;
  602. unset($deps[$id][$next_id]);
  603. foreach ($infos as $k => $v) {
  604. if (!in_array($k, array('rank', $next_id))) {
  605. $deps[$id]['rank'] += $v;
  606. $deps[$id][$k] = $v;
  607. }
  608. }
  609. }
  610. }
  611. }
  612. while ($next_id);
  613. if ($deps) {
  614. $this->addError('Not all patterns could be rewritten to SQL JOINs');
  615. }
  616. return $r;
  617. }
  618. function getJoins() {
  619. $r = array();
  620. $nl = "\n";
  621. foreach ($this->index['join'] as $id) {
  622. $sub_r = $this->getJoinConditionSQL($id);
  623. $r[] = 'JOIN ' . $this->getTripleTable($id) . ' T_' . $id . ' ON (' . $sub_r . $nl . ')';
  624. }
  625. foreach (array_merge($this->index['from'], $this->index['join']) as $id) {
  626. if ($sub_r = $this->getRequiredSubJoinSQL($id)) {
  627. $r[] = $sub_r;
  628. }
  629. }
  630. return $r;
  631. }
  632. function getLeftJoins() {
  633. $r = array();
  634. $nl = "\n";
  635. foreach ($this->index['left_join'] as $id) {
  636. $sub_r = $this->getJoinConditionSQL($id);
  637. $r[] = 'LEFT JOIN ' . $this->getTripleTable($id) . ' T_' . $id . ' ON (' . $sub_r . $nl . ')';
  638. }
  639. foreach ($this->index['left_join'] as $id) {
  640. if ($sub_r = $this->getRequiredSubJoinSQL($id, 'LEFT')) {
  641. $r[] = $sub_r;
  642. }
  643. }
  644. return $r;
  645. }
  646. function getJoinConditionSQL($id) {
  647. $r = '';
  648. $nl = "\n";
  649. $infos = $this->getJoinInfos($id);
  650. $pattern = $this->getPattern($id);
  651. $tbl = 'T_' . $id;
  652. /* core dependency */
  653. $d_tbls = $this->getDependentJoins($id);
  654. foreach ($d_tbls as $d_tbl) {
  655. if (preg_match('/^T_([0-9\_]+)\.[spo]+/', $d_tbl, $m) && ($m[1] != $id)) {
  656. if ($this->isJoinedBefore($m[1], $id) && !in_array($m[1], array_merge($this->index['from'], $this->index['join']))) {
  657. $r .= $r ? $nl . ' AND ' : $nl . ' ';
  658. $r .= '(' . $d_tbl . ' IS NOT NULL)';
  659. }
  660. $this->logDependency($id, $d_tbl);
  661. }
  662. }
  663. /* triple-based join info */
  664. foreach ($infos as $info) {
  665. if ($this->isJoinedBefore($info['join_tbl'], $id) && $this->joinDependsOn($id, $info['join_tbl'])) {
  666. $r .= $r ? $nl . ' AND ' : $nl . ' ';
  667. $r .= '(' . $tbl . '.' . $info['term'] . ' = T_' . $info['join_tbl'] . '.' . $info['join_term'] . ')';
  668. }
  669. }
  670. /* filters etc */
  671. if ($sub_r = $this->getPatternSQL($pattern, 'join__T_' . $id)) {
  672. $r .= $r ? $nl . ' AND ' . $sub_r : $nl . ' ' . '(' . $sub_r . ')';
  673. }
  674. return $r;
  675. }
  676. /**
  677. * A log of identified table join dependencies in getJoinConditionSQL
  678. *
  679. */
  680. function logDependency($id, $tbl) {
  681. if (!isset($this->dependency_log[$id])) $this->dependency_log[$id] = array();
  682. if (!in_array($tbl, $this->dependency_log[$id])) {
  683. $this->dependency_log[$id][] = $tbl;
  684. }
  685. }
  686. /**
  687. * checks whether entries in the dependecy log could perhaps be optimized
  688. * (triggers re-ordering of patterns
  689. */
  690. function problematicDependencies() {
  691. foreach ($this->dependency_log as $id => $tbls) {
  692. if (count($tbls) > 1) return count($tbls);
  693. }
  694. return 0;
  695. }
  696. function isJoinedBefore($tbl_1, $tbl_2) {
  697. $tbl_ids = $this->getOrderedJoinIDs();
  698. foreach ($tbl_ids as $id) {
  699. if ($id == $tbl_1) {
  700. return 1;
  701. }
  702. if ($id == $tbl_2) {
  703. return 0;
  704. }
  705. }
  706. }
  707. function joinDependsOn($id, $id2) {
  708. if (in_array($id2, array_merge($this->index['from'], $this->index['join']))) {
  709. return 1;
  710. }
  711. $d_tbls = $this->getDependentJoins($id2);
  712. //echo $id . ' :: ' . $id2 . '=>' . print_r($d_tbls, 1);
  713. foreach ($d_tbls as $d_tbl) {
  714. if (preg_match('/^T_' .$id. '\./', $d_tbl)) {
  715. return 1;
  716. }
  717. }
  718. return 0;
  719. }
  720. function getDependentJoins($id) {
  721. $r = array();
  722. /* sub joins */
  723. foreach ($this->index['sub_joins'] as $alias) {
  724. if (preg_match('/^(T|V|G)_' . $id . '/', $alias)) {
  725. $r[] = $alias;
  726. }
  727. }
  728. /* siblings in shared optional */
  729. $o_id = $this->getOptionalPattern($id);
  730. foreach ($this->index['sub_joins'] as $alias) {
  731. if (preg_match('/^(T|V|G)_' . $o_id . '/', $alias) && !in_array($alias, $r)) {
  732. $r[] = $alias;
  733. }
  734. }
  735. foreach ($this->index['left_join'] as $alias) {
  736. if (preg_match('/^' . $o_id . '/', $alias) && !in_array($alias, $r)) {
  737. $r[] = 'T_' . $alias . '.s';
  738. }
  739. }
  740. return $r;
  741. }
  742. /* */
  743. function getRequiredSubJoinSQL($id, $prefix = '') {/* id is a triple pattern id. Optional FILTERS and GRAPHs are getting added to the join directly */
  744. $nl = "\n";
  745. $r = '';
  746. foreach ($this->index['sub_joins'] as $alias) {
  747. if (preg_match('/^V_' . $id . '_([a-z\_]+)\.val$/', $alias, $m)) {
  748. $col = $m[1];
  749. $sub_r = '';
  750. if ($this->isOptionalPattern($id)) {
  751. $pattern = $this->getPattern($id);
  752. do {
  753. $pattern = $this->getPattern($pattern['parent_id']);
  754. } while ($pattern['parent_id'] && ($pattern['type'] != 'optional'));
  755. $sub_r = $this->getPatternSQL($pattern, 'sub_join__V_' . $id);
  756. }
  757. $sub_r = $sub_r ? $nl . ' AND (' . $sub_r . ')' : '';
  758. /* lang dt only on literals */
  759. if ($col == 'o_lang_dt') {
  760. $sub_sub_r = 'T_' . $id . '.o_type = 2';
  761. $sub_r .= $nl . ' AND (' . $sub_sub_r . ')';
  762. }
  763. //$cur_prefix = $prefix ? $prefix . ' ' : 'STRAIGHT_';
  764. $cur_prefix = $prefix ? $prefix . ' ' : '';
  765. if ($col == 'g') {
  766. $r .= trim($cur_prefix . 'JOIN '. $this->getValueTable($col) . ' V_' .$id . '_' . $col. ' ON (' .$nl. ' (G_' . $id . '.' . $col. ' = V_' . $id. '_' . $col. '.id) ' . $sub_r . $nl . ')');
  767. }
  768. else {
  769. $r .= trim($cur_prefix . 'JOIN '. $this->getValueTable($col) . ' V_' .$id . '_' . $col. ' ON (' .$nl. ' (T_' . $id . '.' . $col. ' = V_' . $id. '_' . $col. '.id) ' . $sub_r . $nl . ')');
  770. }
  771. }
  772. elseif (preg_match('/^G_' . $id . '\.g$/', $alias, $m)) {
  773. $pattern = $this->getPattern($id);
  774. $sub_r = $this->getPatternSQL($pattern, 'graph_sub_join__G_' . $id);
  775. $sub_r = $sub_r ? $nl . ' AND ' . $sub_r : '';
  776. /* dataset restrictions */
  777. $gi = $this->getGraphInfos($id);
  778. $sub_sub_r = '';
  779. $added_gts = array();
  780. foreach ($gi as $set) {
  781. if (isset($set['graph']) && !in_array($set['graph'], $added_gts)) {
  782. $sub_sub_r .= $sub_sub_r !== '' ? ',' : '';
  783. $sub_sub_r .= $this->getTermID($set['graph'], 'g');
  784. $added_gts[] = $set['graph'];
  785. }
  786. }
  787. $sub_r .= ($sub_sub_r !== '') ? $nl . ' AND (G_' . $id . '.g IN (' . $sub_sub_r . '))' : ''; // /* ' . str_replace('#' , '::', $set['graph']) . ' */';
  788. /* other graph join conditions */
  789. foreach ($this->index['graph_vars'] as $var => $occurs) {
  790. $occur_tbls = array();
  791. foreach ($occurs as $occur) {
  792. $occur_tbls[] = $occur['table'];
  793. if ($occur['table'] == $id) break;
  794. }
  795. foreach($occur_tbls as $tbl) {
  796. if (($tbl != $id) && in_array($id, $occur_tbls) && $this->isJoinedBefore($tbl, $id)) {
  797. $sub_r .= $nl . ' AND (G_' .$id. '.g = G_' .$tbl. '.g)';
  798. }
  799. }
  800. }
  801. //$cur_prefix = $prefix ? $prefix . ' ' : 'STRAIGHT_';
  802. $cur_prefix = $prefix ? $prefix . ' ' : '';
  803. $r .= trim($cur_prefix . 'JOIN '. $this->getGraphTable() . ' G_' .$id . ' ON (' .$nl. ' (T_' . $id . '.t = G_' .$id. '.t)' . $sub_r . $nl . ')');
  804. }
  805. }
  806. return $r;
  807. }
  808. /* */
  809. function getWHERESQL() {
  810. $r = '';
  811. $nl = "\n";
  812. /* standard constraints */
  813. $sub_r = $this->getPatternSQL($this->getPattern('0'), 'where');
  814. /* additional constraints */
  815. foreach ($this->index['from'] as $id) {
  816. if ($sub_sub_r = $this->getConstraintSQL($id)) {
  817. $sub_r .= $sub_r ? $nl . ' AND ' . $sub_sub_r : $sub_sub_r;
  818. }
  819. }
  820. $r .= $sub_r ? $sub_r : '';
  821. /* left join dependencies */
  822. foreach ($this->index['left_join'] as $id) {
  823. $d_joins = $this->getDependentJoins($id);
  824. $added = array();
  825. $d_aliases = array();
  826. //echo $id . ' =>' . print_r($d_joins, 1);
  827. $id_alias = 'T_' . $id . '.s';
  828. foreach ($d_joins as $alias) {
  829. if (preg_match('/^(T|V|G)_([0-9\_]+)(_[spo])?\.([a-z\_]+)/', $alias, $m)) {
  830. $tbl_type = $m[1];
  831. $tbl_pattern_id = $m[2];
  832. $suffix = $m[3];
  833. if (($tbl_pattern_id >= $id) && $this->sameOptional($tbl_pattern_id, $id)) {/* get rid of dependency permutations and nested optionals */
  834. if (!in_array($tbl_type . '_' . $tbl_pattern_id . $suffix, $added)) {
  835. $sub_r .= $sub_r ? ' AND ' : '';
  836. $sub_r .= $alias . ' IS NULL';
  837. $d_aliases[] = $alias;
  838. $added[] = $tbl_type . '_' . $tbl_pattern_id . $suffix;
  839. $id_alias = ($tbl_pattern_id == $id) ? $alias : $id_alias;
  840. }
  841. }
  842. }
  843. }
  844. if (count($d_aliases) > 2) {/* @@todo fix this! */
  845. $sub_r1 = ' /* '.$id_alias.' dependencies */';
  846. $sub_r2 = '((' . $id_alias . ' IS NULL) OR (CONCAT(' . join(', ', $d_aliases) . ') IS NOT NULL))';
  847. $r .= $r ? $nl . $sub_r1 . $nl . ' AND ' .$sub_r2 : $sub_r1 . $nl . $sub_r2;
  848. }
  849. }
  850. return $r ? $nl . 'WHERE ' . $r : '';
  851. }
  852. /* */
  853. function addConstraintSQLEntry($id, $sql) {
  854. if (!isset($this->index['constraints'][$id])) {
  855. $this->index['constraints'][$id] = array();
  856. }
  857. if (!in_array($sql, $this->index['constraints'][$id])) {
  858. $this->index['constraints'][$id][] = $sql;
  859. }
  860. }
  861. function getConstraintSQL($id) {
  862. $r = '';
  863. $nl = "\n";
  864. $constraints = $this->v($id, array(), $this->index['constraints']);
  865. foreach ($constraints as $constraint) {
  866. $r .= $r ? $nl . ' AND ' . $constraint : $constraint;
  867. }
  868. return $r;
  869. }
  870. /* */
  871. function getPatternSQL($pattern, $context) {
  872. $type = $this->v('type', '', $pattern);
  873. if (!$type) {
  874. return '';
  875. }
  876. $m = 'get' . ucfirst($type) . 'PatternSQL';
  877. return method_exists($this, $m) ? $this->$m($pattern, $context) : $this->getDefaultPatternSQL($pattern, $context);
  878. }
  879. function getDefaultPatternSQL($pattern, $context) {
  880. $r = '';
  881. $nl = "\n";
  882. $sub_ids = $this->v('patterns', array(), $pattern);
  883. foreach ($sub_ids as $sub_id) {
  884. $sub_r = $this->getPatternSQL($this->getPattern($sub_id), $context);
  885. $r .= ($r && $sub_r) ? $nl . ' AND (' . $sub_r . ')' : ($sub_r ? $sub_r : '');
  886. }
  887. return $r ? $r : '';
  888. }
  889. function getTriplePatternSQL($pattern, $context) {
  890. $r = '';
  891. $nl = "\n";
  892. $id = $pattern['id'];
  893. /* s p o */
  894. $vars = array();
  895. foreach (array('s', 'p', 'o') as $term) {
  896. $sub_r = '';
  897. $type = $pattern[$term . '_type'];
  898. if ($type == 'uri') {
  899. $term_id = $this->getTermID($pattern[$term], $term);
  900. $sub_r = '(T_' . $id . '.' . $term . ' = ' . $term_id . ') /* ' . str_replace('#' , '::', $pattern[$term]) . ' */';
  901. }
  902. elseif ($type == 'literal') {
  903. $term_id = $this->getTermID($pattern[$term], $term);
  904. $sub_r = '(T_' . $id . '.' . $term . ' = ' . $term_id . ') /* ' . preg_replace('/[\#\n]/' , ' ', $pattern[$term]) . ' */';
  905. if (($lang_dt = $this->v1($term . '_lang', '', $pattern)) || ($lang_dt = $this->v1($term . '_datatype', '', $pattern))) {
  906. $lang_dt_id = $this->getTermID($lang_dt);
  907. $sub_r .= $nl . ' AND (T_' . $id . '.' .$term. '_lang_dt = ' . $lang_dt_id . ') /* ' . str_replace('#' , '::', $lang_dt) . ' */';
  908. }
  909. }
  910. elseif ($type == 'var') {
  911. $val = $pattern[$term];
  912. if (isset($vars[$val])) {/* repeated var in pattern */
  913. $sub_r = '(T_' . $id . '.' . $term . '=' . 'T_' . $id . '.' . $vars[$val] . ')';
  914. }
  915. $vars[$val] = $term;
  916. if ($infos = $this->v($val, 0, $this->index['graph_vars'])) {/* graph var in triple pattern */
  917. $sub_r .= $sub_r ? $nl . ' AND ' : '';
  918. $tbl = $infos[0]['table'];
  919. $sub_r .= 'G_' . $tbl . '.g = T_' . $id . '.' . $term;
  920. }
  921. }
  922. if ($sub_r) {
  923. if (preg_match('/^(join)/', $context) || (preg_match('/^where/', $context) && in_array($id, $this->index['from']))) {
  924. $r .= $r ? $nl . ' AND ' . $sub_r : $sub_r;
  925. }
  926. }
  927. }
  928. /* g */
  929. if ($infos = $pattern['graph_infos']) {
  930. $tbl_alias = 'G_' . $id . '.g';
  931. if (!in_array($tbl_alias, $this->index['sub_joins'])) {
  932. $this->index['sub_joins'][] = $tbl_alias;
  933. }
  934. $sub_r = array('graph_var' => '', 'graph_uri' => '', 'from' => '', 'from_named' => '');
  935. foreach ($infos as $info) {
  936. $type = $info['type'];
  937. if ($type == 'graph') {
  938. if ($info['uri']) {
  939. $term_id = $this->getTermID($info['uri'], 'g');
  940. $sub_r['graph_uri'] .= $sub_r['graph_uri'] ? $nl . ' AND ' : '';
  941. $sub_r['graph_uri'] .= '(' .$tbl_alias. ' = ' . $term_id . ') /* ' . str_replace('#' , '::', $info['uri']) . ' */';
  942. }
  943. }
  944. }
  945. if ($sub_r['from'] && $sub_r['from_named']) {
  946. $sub_r['from_named'] = '';
  947. }
  948. if (!$sub_r['from'] && !$sub_r['from_named']) {
  949. $sub_r['graph_var'] = '';
  950. }
  951. if (preg_match('/^(graph_sub_join)/', $context)) {
  952. foreach ($sub_r as $g_type => $g_sql) {
  953. if ($g_sql) {
  954. $r .= $r ? $nl . ' AND ' . $g_sql : $g_sql;
  955. }
  956. }
  957. }
  958. }
  959. /* optional sibling filters? */
  960. if (preg_match('/^(join|sub_join)/', $context) && $this->isOptionalPattern($id)) {
  961. $o_pattern = $pattern;
  962. do {
  963. $o_pattern = $this->getPattern($o_pattern['parent_id']);
  964. } while ($o_pattern['parent_id'] && ($o_pattern['type'] != 'optional'));
  965. if ($sub_r = $this->getPatternSQL($o_pattern, 'optional_filter' . preg_replace('/^(.*)(__.*)$/', '\\2', $context))) {
  966. $r .= $r ? $nl . ' AND ' . $sub_r : $sub_r;
  967. }
  968. /* created constraints */
  969. if ($sub_r = $this->getConstraintSQL($id)) {
  970. $r .= $r ? $nl . ' AND ' . $sub_r : $sub_r;
  971. }
  972. }
  973. /* result */
  974. if (preg_match('/^(where)/', $context) && $this->isOptionalPattern($id)) {
  975. return '';
  976. }
  977. return $r;
  978. }
  979. /* */
  980. function getFilterPatternSQL($pattern, $context) {
  981. $r = '';
  982. $id = $pattern['id'];
  983. $constraint_id = $this->v1('constraint', '', $pattern);
  984. $constraint = $this->getPattern($constraint_id);
  985. $constraint_type = $constraint['type'];
  986. if ($constraint_type == 'built_in_call') {
  987. $r = $this->getBuiltInCallSQL($constraint, $context);
  988. }
  989. elseif ($constraint_type == 'expression') {
  990. $r = $this->getExpressionSQL($constraint, $context, '', 'filter');
  991. }
  992. else {
  993. $m = 'get' . ucfirst($constraint_type) . 'ExpressionSQL';
  994. if (method_exists($this, $m)) {
  995. $r = $this->$m($constraint, $context, '', 'filter');
  996. }
  997. }
  998. if ($this->isOptionalPattern($id) && !preg_match('/^(join|optional_filter)/', $context)) {
  999. return '';
  1000. }
  1001. /* unconnected vars in FILTERs eval to false */
  1002. $sub_r = $this->hasUnconnectedFilterVars($id);
  1003. if ($sub_r) {
  1004. if ($sub_r == 'alias') {
  1005. if (!in_array($r, $this->index['havings'])) $this->index['havings'][] = $r;
  1006. return '';
  1007. }
  1008. elseif (preg_match('/^T([^\s]+\.)g (.*)$/s', $r, $m)) {/* graph filter */
  1009. return 'G' . $m[1] . 't ' . $m[2];
  1010. }
  1011. elseif (preg_match('/^\(*V[^\s]+_g\.val .*$/s', $r, $m)) {/* graph value filter, @@improveMe */
  1012. //return $r;
  1013. }
  1014. else {
  1015. return 'FALSE';
  1016. }
  1017. }
  1018. /* some really ugly tweaks */
  1019. /* empty language filter: FILTER ( lang(?v) = '' ) */
  1020. $r = preg_replace('/\(\/\* language call \*\/ ([^\s]+) = ""\)/s', '((\\1 = "") OR (\\1 LIKE "%:%"))', $r);
  1021. return $r;
  1022. }
  1023. /**
  1024. * Checks if vars in the given (filter) pattern are used within the filter's scope.
  1025. */
  1026. function hasUnconnectedFilterVars($filter_pattern_id) {
  1027. $scope_id = $this->getFilterScope($filter_pattern_id);
  1028. $vars = $this->getFilterVars($filter_pattern_id);
  1029. $r = 0;
  1030. foreach ($vars as $var_name) {
  1031. if ($this->isUsedTripleVar($var_name, $scope_id)) continue;
  1032. if ($this->isAliasVar($var_name)) {
  1033. $r = 'alias';
  1034. break;
  1035. }
  1036. $r = 1;
  1037. break;
  1038. }
  1039. return $r;
  1040. }
  1041. /**
  1042. * Returns the given filter pattern's scope (the id of the parent group pattern).
  1043. */
  1044. function getFilterScope($filter_pattern_id) {
  1045. $patterns = $this->initial_index['patterns'];
  1046. $r = '';
  1047. foreach ($patterns as $id => $p) {
  1048. /* the id has to be sub-part of the given filter id */
  1049. if (!preg_match('/^' . $id . '.+/', $filter_pattern_id)) continue;
  1050. /* we are looking for a group or union */
  1051. if (!preg_match('/^(group|union)$/', $p['type'])) continue;
  1052. /* we are looking for the longest/deepest match */
  1053. if (strlen($id) > strlen($r)) $r = $id;
  1054. }
  1055. return $r;
  1056. }
  1057. /**
  1058. * Builds a list of vars used in the given (filter) pattern.
  1059. */
  1060. function getFilterVars($filter_pattern_id) {
  1061. $r = array();
  1062. $patterns = $this->initial_index['patterns'];
  1063. /* find vars in the given filter (i.e. the given id is part of their pattern id) */
  1064. foreach ($patterns as $id => $p) {
  1065. if (!preg_match('/^' . $filter_pattern_id . '.+/', $id)) continue;
  1066. $var_name = '';
  1067. if ($p['type'] == 'var') {
  1068. $var_name = $p['value'];
  1069. }
  1070. elseif (($p['type'] == 'built_in_call') && ($p['call'] == 'bound')) {
  1071. $var_name = $p['args'][0]['value'];
  1072. }
  1073. if ($var_name && !in_array($var_name, $r)) {
  1074. $r[] = $var_name;
  1075. }
  1076. }
  1077. return $r;
  1078. }
  1079. /**
  1080. * Checks if $var_name appears as result projection alias.
  1081. */
  1082. function isAliasVar($var_name) {
  1083. foreach ($this->infos['query']['result_vars'] as $r_var) {
  1084. if ($r_var['alias'] == $var_name) return 1;
  1085. }
  1086. return 0;
  1087. }
  1088. /**
  1089. * Checks if $var_name is used in a triple pattern in the given scope
  1090. */
  1091. function isUsedTripleVar($var_name, $scope_id = '0') {
  1092. $patterns = $this->initial_index['patterns'];
  1093. foreach ($patterns as $id => $p) {
  1094. if ($p['type'] != 'triple') continue;
  1095. if (!preg_match('/^' . $scope_id . '.+/', $id)) continue;
  1096. foreach (array('s', 'p', 'o') as $term) {
  1097. if ($p[$term . '_type'] != 'var') continue;
  1098. if ($p[$term] == $var_name) return 1;
  1099. }
  1100. }
  1101. }
  1102. /* */
  1103. function getExpressionSQL($pattern, $context, $val_type = '', $parent_type = '') {
  1104. $r = '';
  1105. $nl = "\n";
  1106. $type = $this->v1('type', '', $pattern);
  1107. $sub_type = $this->v1('sub_type', $type, $pattern);
  1108. if (preg_match('/^(and|or)$/', $sub_type)) {
  1109. foreach ($pattern['patterns'] as $sub_id) {
  1110. $sub_pattern = $this->getPattern($sub_id);
  1111. $sub_pattern_type = $sub_pattern['type'];
  1112. if ($sub_pattern_type == 'built_in_call') {
  1113. $sub_r = $this->getBuiltInCallSQL($sub_pattern, $context, '', $parent_type);
  1114. }
  1115. else {
  1116. $sub_r = $this->getExpressionSQL($sub_pattern, $context, '', $parent_type);
  1117. }
  1118. if ($sub_r) {
  1119. $r .= $r ? ' ' . strtoupper($sub_type). ' (' .$sub_r. ')' : '(' . $sub_r . ')';
  1120. }
  1121. }
  1122. }
  1123. elseif ($sub_type == 'built_in_call') {
  1124. $r = $this->getBuiltInCallSQL($pattern, $context, $val_type, $parent_type);
  1125. }
  1126. elseif (preg_match('/literal/', $sub_type)) {
  1127. $r = $this->getLiteralExpressionSQL($pattern, $context, $val_type, $parent_type);
  1128. }
  1129. elseif ($sub_type) {
  1130. $m = 'get' . ucfirst($sub_type) . 'ExpressionSQL';
  1131. if (method_exists($this, $m)) {
  1132. $r = $this->$m($pattern, $context, '', $parent_type);
  1133. }
  1134. }
  1135. /* skip expressions that reference non-yet-joined tables */
  1136. if (preg_match('/__(T|V|G)_(.+)$/', $context, $m)) {
  1137. $context_pattern_id = $m[2];
  1138. $context_table_type = $m[1];
  1139. if (preg_match_all('/((T|V|G)(\_[0-9])+)/', $r, $m)) {
  1140. $aliases = $m[1];
  1141. $keep = 1;
  1142. foreach ($aliases as $alias) {
  1143. if (preg_match('/(T|V|G)_(.*)$/', $alias, $m)) {
  1144. $tbl_type = $m[1];
  1145. $tbl = $m[2];
  1146. if (!$this->isJoinedBefore($tbl, $context_pattern_id)) {
  1147. $keep = 0;
  1148. }
  1149. elseif (($context_pattern_id == $tbl) && preg_match('/(TV)/', $context_table_type . $tbl_type)) {
  1150. $keep = 0;
  1151. }
  1152. }
  1153. }
  1154. $r = $keep ? $r : '';
  1155. }
  1156. }
  1157. return $r ? '(' . $r . ')' : $r;
  1158. }
  1159. function detectExpressionValueType($pattern_ids) {
  1160. foreach ($pattern_ids as $id) {
  1161. $pattern = $this->getPattern($id);
  1162. $type = $this->v('type', '', $pattern);
  1163. if (($type == 'literal') && isset($pattern['datatype'])) {
  1164. if (in_array($pattern['datatype'], array($this->xsd . 'integer', $this->xsd . 'float', $this->xsd . 'double'))) {
  1165. return 'numeric';
  1166. }
  1167. }
  1168. }
  1169. return '';
  1170. }
  1171. /* */
  1172. function getRelationalExpressionSQL($pattern, $context, $val_type = '', $parent_type = '') {
  1173. $r = '';
  1174. $val_type = $this->detectExpressionValueType($pattern['patterns']);
  1175. $op = $pattern['operator'];
  1176. foreach ($pattern['patterns'] as $sub_id) {
  1177. $sub_pattern = $this->getPattern($sub_id);
  1178. $sub_pattern['parent_op'] = $op;
  1179. $sub_type = $sub_pattern['type'];
  1180. $m = ($sub_type == 'built_in_call') ? 'getBuiltInCallSQL' : 'get' . ucfirst($sub_type) . 'ExpressionSQL';
  1181. $m = str_replace('ExpressionExpression', 'Expression', $m);
  1182. $sub_r = method_exists($this, $m) ? $this->$m($sub_pattern, $context, $val_type, 'relational') : '';
  1183. $r .= $r ? ' ' . $op . ' ' . $sub_r : $sub_r;
  1184. }
  1185. return $r ? '(' . $r . ')' : $r;
  1186. }
  1187. function getAdditiveExpressionSQL($pattern, $context, $val_type = '', $parent_type = '') {
  1188. $r = '';
  1189. $val_type = $this->detectExpressionValueType($pattern['patterns']);
  1190. foreach ($pattern['patterns'] as $sub_id) {
  1191. $sub_pattern = $this->getPattern($sub_id);
  1192. $sub_type = $this->v('type', '', $sub_pattern);
  1193. $m = ($sub_type == 'built_in_call') ? 'getBuiltInCallSQL' : 'get' . ucfirst($sub_type) . 'ExpressionSQL';
  1194. $m = str_replace('ExpressionExpression', 'Expression', $m);
  1195. $sub_r = method_exists($this, $m) ? $this->$m($sub_pattern, $context, $val_type, 'additive') : '';
  1196. $r .= $r ? ' ' . $sub_r : $sub_r;
  1197. }
  1198. return $r;
  1199. }
  1200. function getMultiplicativeExpressionSQL($pattern, $context, $val_type = '', $parent_type = '') {
  1201. $r = '';
  1202. $val_type = $this->detectExpressionValueType($pattern['patterns']);
  1203. foreach ($pattern['patterns'] as $sub_id) {
  1204. $sub_pattern = $this->getPattern($sub_id);
  1205. $sub_type = $sub_pattern['type'];
  1206. $m = ($sub_type == 'built_in_call') ? 'getBuiltInCallSQL' : 'get' . ucfirst($sub_type) . 'ExpressionSQL';
  1207. $m = str_replace('ExpressionExpression', 'Expression', $m);
  1208. $sub_r = method_exists($this, $m) ? $this->$m($sub_pattern, $context, $val_type, 'multiplicative') : '';
  1209. $r .= $r ? ' ' . $sub_r : $sub_r;
  1210. }
  1211. return $r;
  1212. }
  1213. /* */
  1214. function getVarExpressionSQL($pattern, $context, $val_type = '', $parent_type = '') {
  1215. $var = $pattern['value'];
  1216. $info = $this->getVarTableInfos($var);
  1217. if (!$tbl = $info['table']) {
  1218. /* might be an aggregate var */
  1219. $vars = $this->infos['query']['result_vars'];
  1220. foreach ($vars as $test_var) {
  1221. if ($test_var['alias'] == $pattern['value']) {
  1222. return '`' . $pattern['value'] . '`';
  1223. }
  1224. }
  1225. return '';
  1226. }
  1227. $col = $info['col'];
  1228. if (($context == 'order') && ($col == 'o')) {
  1229. $tbl_alias = 'T_' . $tbl . '.o_comp';
  1230. }
  1231. elseif ($context == 'sameterm') {
  1232. $tbl_alias = 'T_' . $tbl . '.' . $col;
  1233. }
  1234. elseif (($parent_type == 'relational') && ($col == 'o') && (preg_match('/[\<\>]/', $this->v('parent_op', '', $pattern)))) {
  1235. $tbl_alias = 'T_' . $tbl . '.o_comp';
  1236. }
  1237. else {
  1238. $tbl_alias = 'V_' . $tbl . '_' . $col . '.val';
  1239. if (!in_array($tbl_alias, $this->index['sub_joins'])) {
  1240. $this->index['sub_joins'][] = $tbl_alias;
  1241. }
  1242. }
  1243. $op = $this->v('operator', '', $pattern);
  1244. if (preg_match('/^(filter|and)/', $parent_type)) {
  1245. if ($op == '!') {
  1246. $r = '(((' . $tbl_alias . ' = 0) AND (CONCAT("1", ' . $tbl_alias . ') != 1))'; /* 0 and no string */
  1247. $r .= ' OR (' . $tbl_alias . ' IN ("", "false")))'; /* or "", or "false" */
  1248. }
  1249. else {
  1250. $r = '((' . $tbl_alias . ' != 0)'; /* not null */
  1251. $r .= ' OR ((CONCAT("1", ' . $tbl_alias . ') = 1) AND (' . $tbl_alias . ' NOT IN ("", "false"))))'; /* string, and not "" or "false" */
  1252. }
  1253. }
  1254. else {
  1255. $r = trim($op . ' ' . $tbl_alias);
  1256. if ($val_type == 'numeric') {
  1257. if (preg_match('/__(T|V|G)_(.+)$/', $context, $m)) {
  1258. $context_pattern_id = $m[2];
  1259. $context_table_type = $m[1];
  1260. }
  1261. else {
  1262. $context_pattern_id = $pattern['id'];
  1263. $context_table_type = 'T';
  1264. }
  1265. if ($this->isJoinedBefore($tbl, $context_pattern_id)) {
  1266. $add = ($tbl != $context_pattern_id) ? 1 : 0;
  1267. $add = (!$add && ($context_table_type == 'V')) ? 1 : 0;
  1268. if ($add) {
  1269. $this->addConstraintSQLEntry($context_pattern_id, '(' .$r. ' = "0" OR ' . $r . '*1.0 != 0)');
  1270. }
  1271. }
  1272. }
  1273. }
  1274. return $r;
  1275. }
  1276. /* */
  1277. function getUriExpressionSQL($pattern, $context, $val_type = '') {
  1278. $val = $pattern['uri'];
  1279. $r = $pattern['operator'];
  1280. $r .= is_numeric($val) ? ' ' . $val : ' "' . mysql_real_escape_string($val, $this->store->getDBCon()) . '"';
  1281. return $r;
  1282. }
  1283. /* */
  1284. function getLiteralExpressionSQL($pattern, $context, $val_type = '', $parent_type = '') {
  1285. $val = $pattern['value'];
  1286. $r = $pattern['operator'];
  1287. if (is_numeric($val) && $this->v('datatype', 0, $pattern)) {
  1288. $r .= ' ' . $val;
  1289. }
  1290. elseif (preg_match('/^(true|false)$/i', $val) && ($this->v1('datatype', '', $pattern) == 'http://www.w3.org/2001/XMLSchema#boolean')) {
  1291. $r .= ' ' . strtoupper($val);
  1292. }
  1293. elseif ($parent_type == 'regex') {
  1294. $sub_r = mysql_real_escape_string($val, $this->store->getDBCon());
  1295. $r .= ' "' . preg_replace('/\x5c\x5c/', '\\', $sub_r) . '"';
  1296. }
  1297. else {
  1298. $r .= ' "' . mysql_real_escape_string($val, $this->store->getDBCon()) . '"';
  1299. }
  1300. if (($lang_dt = $this->v1('lang', '', $pattern)) || ($lang_dt = $this->v1('datatype', '', $pattern))) {
  1301. /* try table/alias via var in siblings */
  1302. if ($var = $this->findSiblingVarExpression($pattern['id'])) {
  1303. if (isset($this->index['vars'][$var])) {
  1304. $infos = $this->index['vars'][$var];
  1305. foreach ($infos as $info) {
  1306. if ($info['col'] == 'o') {
  1307. $tbl = $info['table'];
  1308. $term_id = $this->getTermID($lang_dt);
  1309. if ($pattern['operator'] != '!=') {
  1310. if (preg_match('/__(T|V|G)_(.+)$/', $context, $m)) {
  1311. $context_pattern_id = $m[2];
  1312. $context_table_type = $m[1];
  1313. }
  1314. elseif ($context == 'where') {
  1315. $context_pattern_id = $tbl;
  1316. }
  1317. else {
  1318. $context_pattern_id = $pattern['id'];
  1319. }
  1320. if ($tbl == $context_pattern_id) {/* @todo better dependency check */
  1321. if ($term_id || ($lang_dt != 'http://www.w3.org/2001/XMLSchema#integer')) {/* skip if simple int, but no id */
  1322. $this->addConstraintSQLEntry($context_pattern_id, 'T_' . $tbl . '.o_lang_dt = ' . $term_id . ' /* ' . str_replace('#' , '::', $lang_dt) . ' */');
  1323. }
  1324. }
  1325. }
  1326. break;
  1327. }
  1328. }
  1329. }
  1330. }
  1331. }
  1332. return trim($r);
  1333. }
  1334. function findSiblingVarExpression($id) {
  1335. $pattern = $this->getPattern($id);
  1336. do

Large files files are truncated, but you can click here to view the full file