function query($q, $result_format = '', $src = '', $keep_bnode_ids = 0) { /* Parse query */ ARC2::inc('SPARQLPlusParser'); $p =& new ARC2_SPARQLPlusParser($this->a, $this); $p->parse($q, $src); $infos = $p->getQueryInfos(); $infos['result_format'] = $result_format; if (!$p->getErrors()) { /* Check and execute query */ $qt = $infos['query']['type']; if (!in_array($qt, array('select', 'ask', 'describe', 'construct', 'load', 'insert', 'delete'))) { return $this->addError('Unsupported query type "' . $qt . '"'); } $t1 = ARC2::mtime(); $r = array('result' => $this->runQuery($qt, $q)); $t2 = ARC2::mtime(); $r['query_time'] = $t2 - $t1; if ($result_format == 'raw') { return $r['result']; } if ($result_format == 'rows') { return $r['result']['rows'] ? $r['result']['rows'] : array(); } if ($result_format == 'row') { return $r['result']['rows'] ? $r['result']['rows'][0] : array(); } return $r; } return 0; }
function query($q, $result_format = '', $src = '', $keep_bnode_ids = 0, $log_query = 0) { if ($log_query) { $this->logQuery($q); } ARC2::inc('SPARQLPlusParser'); $p =& new ARC2_SPARQLPlusParser($this->a, $this); $p->parse($q, $src); $infos = $p->getQueryInfos(); $t1 = ARC2::mtime(); if (!($errs = $p->getErrors())) { $qt = $infos['query']['type']; $r = array('query_type' => $qt, 'result' => $this->runQuery($q, $qt, $infos)); } else { $r = array('result' => ''); } $t2 = ARC2::mtime(); $r['query_time'] = $t2 - $t1; /* query result */ if ($result_format == 'raw') { return $r['result']; } if ($result_format == 'rows') { return $this->v('rows', array(), $r['result']); } if ($result_format == 'row') { if (!isset($r['result']['rows'])) { return array(); } return $r['result']['rows'] ? $r['result']['rows'][0] : array(); } return $r; }
function runQuery($infos) { $this->infos = $infos; $con = $this->store->getDBCon(); $t1 = ARC2::mtime(); /* delete */ $this->refs_deleted = false; /* graph(s) only */ if (!$this->v('construct_triples', array(), $this->infos['query'])) { $tc = $this->deleteTargetGraphs(); } elseif (!$this->v('pattern', array(), $this->infos['query'])) { $tc = $this->deleteTriples(); } else { $tc = $this->deleteConstructedGraph(); } $t2 = ARC2::mtime(); /* clean up */ if ($tc && ($this->refs_deleted || rand(1, 100) == 1)) { $this->cleanTableReferences(); } if ($tc && rand(1, 50) == 1) { $this->store->optimizeTables(); } if ($tc && rand(1, 500) == 1) { $this->cleanValueTables(); } $t3 = ARC2::mtime(); $index_dur = round($t3 - $t2, 4); $dur = round($t3 - $t1, 4); return array('t_count' => $tc, 'delete_time' => $dur, 'index_update_time' => $index_dur); }
public function testBlockUpdateWithoutGraph() { global $prefixSparql, $graph1; $q = $prefixSparql . " \n\t\t\tINSERT DATA { \n\t\t\t\ta:A b:Name \"Test2\" . \n \t\t}"; $p = ARC2::getSPARQLPlusParser(); $p->parse($q); $infos = $p->getQueryInfos(); $t1 = ARC2::mtime(); $err = $p->getErrors(); if ($err) { print_r($err); $this->assertTrue(true); } else { $this->assertTrue(false); } $q = $prefixSparql . " \n\t\t\tDELETE DATA { \n\t\t\t\ta:A b:Name \"Test2\" . \n \t\t}"; $p = ARC2::getSPARQLPlusParser(); $p->parse($q); $infos = $p->getQueryInfos(); $t1 = ARC2::mtime(); $err = $p->getErrors(); if ($err) { print_r($err); $this->assertTrue(true); } else { $this->assertTrue(false); } }
function query($q, $result_format = '', $src = '', $keep_bnode_ids = 0, $log_query = 0) { if ($log_query) { $this->logQuery($q); } $con = $this->getDBCon(); if (preg_match('/^dump/i', $q)) { $infos = array('query' => array('type' => 'dump')); } else { ARC2::inc('SPARQLPlusParser'); $p =& new ARC2_SPARQLPlusParser($this->a, $this); $p->parse($q, $src); $infos = $p->getQueryInfos(); } if ($result_format == 'infos') { return $infos; } $infos['result_format'] = $result_format; if (!isset($p) || !$p->getErrors()) { $qt = $infos['query']['type']; if (!in_array($qt, array('select', 'ask', 'describe', 'construct', 'load', 'insert', 'delete', 'dump'))) { return $this->addError('Unsupported query type "' . $qt . '"'); } $t1 = ARC2::mtime(); $r = array('query_type' => $qt, 'result' => $this->runQuery($infos, $qt, $keep_bnode_ids, $q)); $t2 = ARC2::mtime(); $r['query_time'] = $t2 - $t1; /* query result */ if ($result_format == 'raw') { return $r['result']; } if ($result_format == 'rows') { return $r['result']['rows'] ? $r['result']['rows'] : array(); } if ($result_format == 'row') { return $r['result']['rows'] ? $r['result']['rows'][0] : array(); } return $r; } return 0; }
function handleQueryRequest($q) { if (preg_match('/^dump/i', $q)) { $infos = array('query' => array('type' => 'dump')); $this->is_dump = 1; } else { ARC2::inc('SPARQLPlusParser'); $p = new ARC2_SPARQLPlusParser($this->a, $this); $p->parse($q); $infos = $p->getQueryInfos(); } /* errors? */ if ($errors = $this->getErrors()) { $this->setHeader('http', 'HTTP/1.1 400 Bad Request'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = htmlspecialchars(join("\n", $errors)); return true; } $qt = $infos['query']['type']; /* wrong read key? */ if ($this->read_key && $this->p('key') != $this->read_key && preg_match('/^(select|ask|construct|describe|dump)$/', $qt)) { $this->setHeader('http', 'HTTP/1.1 401 Access denied'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Access denied. Missing or wrong "key" parameter.'; return true; } /* wrong write key? */ if ($this->write_key && $this->p('key') != $this->write_key && preg_match('/^(load|insert|delete|update)$/', $qt)) { $this->setHeader('http', 'HTTP/1.1 401 Access denied'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Access denied. Missing or wrong "key" parameter.'; return true; } /* non-allowed query type? */ if (!in_array($qt, $this->getFeatures())) { $this->setHeader('http', 'HTTP/1.1 401 Access denied'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Access denied for "' . $qt . '" query'; return true; } /* load/insert/delete via GET */ if (in_array($qt, array('load', 'insert', 'delete')) && isset($_GET['query'])) { $this->setHeader('http', 'HTTP/1.1 501 Not Implemented'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Query type "' . $qt . '" not supported via GET'; return true; } /* unsupported query type */ if (!in_array($qt, array('select', 'ask', 'describe', 'construct', 'load', 'insert', 'delete', 'dump'))) { $this->setHeader('http', 'HTTP/1.1 501 Not Implemented'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Unsupported query type "' . $qt . '"'; return true; } /* adjust infos */ $infos = $this->adjustQueryInfos($infos); $t1 = ARC2::mtime(); $r = array('result' => $this->runQuery($infos, $qt)); $t2 = ARC2::mtime(); $r['query_time'] = $t2 - $t1; /* query errors? */ if ($errors = $this->getErrors()) { $this->setHeader('http', 'HTTP/1.1 400 Bad Request'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Error: ' . join("\n", $errors); return true; } /* result */ $m = 'get' . ucfirst($qt) . 'ResultDoc'; if (method_exists($this, $m)) { $this->result = $this->{$m}($r); } else { $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Result serializer not available, dumping raw data:' . "\n" . print_r($r, 1); } }
function queryDB($sql, $con, $log_errors = 0) { $t1 = ARC2::mtime(); $r = mysql_query($sql, $con); $t2 = ARC2::mtime() - $t1; if ($t2 > 1) { //echo "\n needed " . $t2 . ' secs for ' . $sql; } if ($log_errors && ($er = mysql_error($con))) { $this->addError($er); } return $r; }
function queryDB($sql, $con, $log_errors = 0) { $t1 = ARC2::mtime(); $r = mysqli_query($con, $sql); if (0) { $t2 = ARC2::mtime() - $t1; $call_obj = $this; $call_path = ''; while ($call_obj) { $call_path = get_class($call_obj) . ' / ' . $call_path; $call_obj = isset($call_obj->caller) ? $call_obj->caller : false; } echo "\n" . $call_path . " needed " . $t2 . ' secs for ' . str_replace("\n", ' ', $sql); } $er = mysqli_error($con); if ($log_errors && !empty($er)) { $this->addError($er); } return $r; }
function logInserts() { $t_start = $this->t_start; $t_prev = $this->t_prev; $t_now = ARC2::mtime(); $tc_prev = $this->t_count_prev; $tc_now = $this->t_count; $tc_diff = $tc_now - $tc_prev; $dur_full = $t_now - $t_start; $dur_diff = $t_now - $t_prev; $speed_full = round($tc_now / $dur_full); $speed_now = round($tc_diff / $dur_diff); $r = $tc_diff . ' in ' . round($dur_diff, 5) . ' = ' . $speed_now . ' t/s (' . $tc_now . ' in ' . round($dur_full, 5) . ' = ' . $speed_full . ' t/s )'; $fp = @fopen("arc_insert_log.txt", "a"); @fwrite($fp, $r . "\r\n"); @fclose($fp); $this->t_prev = $t_now; $this->t_count_prev = $tc_now; }
function getFinalQueryResult($q_sql, $tmp_tbl) { /* var names */ $vars = array(); $aggregate_vars = array(); foreach ($this->infos['query']['result_vars'] as $entry) { if ($entry['aggregate']) { $vars[] = $entry['alias']; $aggregate_vars[] = $entry['alias']; } else { $vars[] = $entry['var']; } } /* result */ $r = array('variables' => $vars); $v_sql = $this->getValueSQL($tmp_tbl, $q_sql); //echo "\n\n" . $v_sql; $t1 = ARC2::mtime(); $con = $this->store->getDBCon(); $rs = mysql_unbuffered_query($v_sql, $con); if ($er = mysql_error($con)) { $this->addError($er); } $t2 = ARC2::mtime(); $rows = array(); $types = array(0 => 'uri', 1 => 'bnode', 2 => 'literal'); if ($rs) { while ($pre_row = mysql_fetch_array($rs)) { $row = array(); foreach ($vars as $var) { if (isset($pre_row[$var])) { $row[$var] = $pre_row[$var]; $row[$var . ' type'] = isset($pre_row[$var . ' type']) ? $types[$pre_row[$var . ' type']] : (in_array($var, $aggregate_vars) ? 'literal' : 'uri'); if (isset($pre_row[$var . ' lang_dt']) && ($lang_dt = $pre_row[$var . ' lang_dt'])) { if (preg_match('/^([a-z]+(\\-[a-z0-9]+)*)$/i', $lang_dt)) { $row[$var . ' lang'] = $lang_dt; } else { $row[$var . ' datatype'] = $lang_dt; } } } } if ($row || !$vars) { $rows[] = $row; } } } $r['rows'] = $rows; return $r; }
/** * Check, send a request SPARQL and parse the response. Example of using : $sp_readonly = new FourStore_StorePlus($endpoint); echo "\nPrint :"; $q = "select * where { GRAPH <http://example.com> {?x ?y ?z.}} "; $rows = $sp_readonly->query($q, 'rows'); $err = $sp_readonly->getErrors(); if ($err) { print_r($err); throw new Exception(print_r($err,true)); } var_dump($rows); echo "\nASK :"; $q = "PREFIX a: <http://example.com/test/a/> PREFIX b: <http://example.com/test/b/> ask where { GRAPH <http://example.com> {a:A b:Name \"Test3\" .}} "; $res = $sp_readonly->query($q, 'raw'); $err = $sp_readonly->getErrors(); if ($err) { print_r($err); throw new Exception(print_r($err,true)); } var_dump($res); * @param string $query : Query Sparql * @param $q Query SPARQL * @param $result_format Optional, * rows to return array of results or * row to return array of first result or * raw to return boolean for request ask, insert and delete * @return array|boolean in function of parameter $result_format * @access public */ public function query($q, $result_format = '') { if ($this->_debug) { print date('Y-m-d\\TH:i:s\\Z', time()) . ' : ' . $q . '' . "\n\n"; } $p = new FourStore_SPARQLParser('', $this); $p->parse($q); $infos = $p->getQueryInfos(); $t1 = ARC2::mtime(); if (!($errs = $p->getErrors())) { $qt = $infos['query']['type']; $r = array('query_type' => $qt, 'result' => $this->runQuery($q, $qt, $infos)); } else { $r = array('result' => ''); if ($this->_debug) { print date('Y-m-d\\TH:i:s\\Z', time()) . ' : ERROR ' . $q . '' . "\n\n"; print_r($errs); } return $this->_arc2_RemoteStore->addError($p->getErrors()); } $t2 = ARC2::mtime(); $r['query_time'] = $t2 - $t1; /* query result */ if ($result_format == 'raw') { return $r['result']; } if ($result_format == 'rows') { return $this->_arc2_RemoteStore->v('rows', array(), $r['result']); } if ($result_format == 'row') { if (!isset($r['result']['rows'])) { return array(); } return $r['result']['rows'] ? $r['result']['rows'][0] : array(); } return $r; }
public function testMtime() { $actual = ARC2::mtime(); $this->assertTrue(is_float($actual)); }
function getFinalQueryResult($q_sql, $vars, $tmp_tbl, $aggregate_vars = '') { $aggregate_vars = $aggregate_vars ? $aggregate_vars : array(); $r = array('variables' => $vars); $v_sql = $this->getValueSQL($tmp_tbl); $t1 = ARC2::mtime(); $con = $this->store->getDBCon(); $rs = mysql_unbuffered_query($v_sql); if ($er = mysql_error()) { $this->addError($er); } $t2 = ARC2::mtime(); $rows = array(); $types = array(0 => 'uri', 1 => 'bnode', 2 => 'literal'); if ($rs) { while ($pre_row = mysql_fetch_array($rs)) { $row = array(); foreach ($vars as $var) { if (isset($pre_row[$var])) { $row[$var] = $pre_row[$var]; $row[$var . ' type'] = isset($pre_row[$var . ' type']) ? $types[$pre_row[$var . ' type']] : (in_array($var, $aggregate_vars) ? 'literal' : 'uri'); if (isset($pre_row[$var . ' lang_dt']) && ($lang_dt = $pre_row[$var . ' lang_dt'])) { if (preg_match('/^([a-z]+(\\-[a-z0-9]+)*)$/i', $lang_dt)) { $row[$var . ' lang'] = $lang_dt; } else { $row[$var . ' datatype'] = $lang_dt; } } } } if ($row) { $rows[] = $row; } } } $r['rows'] = $rows; return $r; }