1 | <?php
|
---|
2 | require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngineDb/SqlMerger.php';
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Determines the offset in a row of sql queries.
|
---|
6 | *
|
---|
7 | * @author Christian Weiske <cweiske@cweiske.de>
|
---|
8 | * @license http://www.gnu.org/licenses/lgpl.html LGPL
|
---|
9 | *
|
---|
10 | * @package sparql
|
---|
11 | */
|
---|
12 | class SparqlEngineDb_Offsetter
|
---|
13 | {
|
---|
14 | public function __construct(ADOConnection $dbConn, Query $query)
|
---|
15 | {
|
---|
16 | $this->dbConn = $dbConn;
|
---|
17 | $this->query = $query;
|
---|
18 | }//public function __construct(ADOConnection $dbConn, Query $query)
|
---|
19 |
|
---|
20 |
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Determines the offset in the sqls, the position to start form.
|
---|
24 | *
|
---|
25 | * @param array $arSqls Array of SQL query parts as returned by
|
---|
26 | * SparqlEngine_TypeSorter::getOrderifiedSqls()
|
---|
27 | * @return array Array of two values: The first determines the
|
---|
28 | * index of the sql query to begin with, the second
|
---|
29 | * is the row offset that should be used in the final
|
---|
30 | * SQL query.
|
---|
31 | */
|
---|
32 | public function determineOffset($arSqls)
|
---|
33 | {
|
---|
34 | $arSM = $this->query->getSolutionModifier();
|
---|
35 | if ($arSM['offset'] === null) {
|
---|
36 | return array(0, 0);
|
---|
37 | }
|
---|
38 |
|
---|
39 | $nCount = 0;
|
---|
40 | foreach ($arSqls as $nId => $arSql) {
|
---|
41 | $nCurrentCount = $this->getCount($arSql);
|
---|
42 | if ($nCurrentCount + $nCount > $arSM['offset']) {
|
---|
43 | return array($nId, $arSM['offset'] - $nCount);
|
---|
44 | }
|
---|
45 | $nCount += $nCurrentCount;
|
---|
46 | }
|
---|
47 | //nothing found - no results for this offset
|
---|
48 | return array(count($arSqls), 0);
|
---|
49 | }//public function determineOffset($arSql)
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Returns the number of rows that the given query will return.
|
---|
55 | *
|
---|
56 | * @param array $arSql Array with sql parts and at least keys
|
---|
57 | * 'from' and 'where' set.
|
---|
58 | * @return int Number of rows returned.
|
---|
59 | */
|
---|
60 | protected function getCount($arSql)
|
---|
61 | {
|
---|
62 | $sql = SparqlEngineDb_SqlMerger::getCount($this->query, $arSql);
|
---|
63 | $dbResult = $this->dbConn->execute($sql);
|
---|
64 |
|
---|
65 | $nCount = 0;
|
---|
66 | foreach ($dbResult as $row) {
|
---|
67 | $nCount = intval($row[0]);
|
---|
68 | break;
|
---|
69 | }
|
---|
70 | return $nCount;
|
---|
71 | }//protected function getCount($arSql)
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Creates a sql LIMIT statement if the sparql query needs one.
|
---|
77 | * This method is needed because AdoDb does not support limits with
|
---|
78 | * prepared statements. It's a pity.
|
---|
79 | *
|
---|
80 | * @return string SQL command to be appended to a query, to limit
|
---|
81 | * the number of result rows returned.
|
---|
82 | */
|
---|
83 | public static function getLimitSql(Query $query, ADOConnection $dbConn)
|
---|
84 | {
|
---|
85 | $arSM = $query->getSolutionModifier();
|
---|
86 | if ($arSM['limit'] === null && $arSM['offset'] === null) {
|
---|
87 | return '';
|
---|
88 | }
|
---|
89 | //this here is mysql syntax. if anyone has problems, write it
|
---|
90 | //dependent on $dbConn's type
|
---|
91 | if ($arSM['offset'] === null) {
|
---|
92 | return ' LIMIT ' . $arSM['limit'];
|
---|
93 | } else if ($arSM['limit'] === null) {
|
---|
94 | return ' LIMIT ' . $arSM['offset'] . ', 18446744073709551615';
|
---|
95 | } else {
|
---|
96 | return ' LIMIT ' . $arSM['offset'] . ', ' . $arSM['limit'];
|
---|
97 | }
|
---|
98 | }//public static function getLimitSql(Query $query, ADOConnection $dbConn)
|
---|
99 |
|
---|
100 | }//class SparqlEngineDb_Offsetter
|
---|
101 |
|
---|
102 | ?> |
---|