Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
GateWay
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 9
110
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 connect
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 search
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 searchTriples
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 statementToArray
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 fetchQuery
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 count
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getJoiner
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 join
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 * @author Christophe GARCIA <christopheg@taotesting.com>
21 * @license GPLv2
22 * @package generis
23 *
24 */
25
26namespace   oat\generis\model\kernel\persistence\smoothsql\search;
27
28use common_persistence_Manager;
29use common_persistence_SqlPersistence;
30use Doctrine\DBAL\Driver\Statement;
31use oat\oatbox\service\ServiceManager;
32use oat\search\base\exception\SearchGateWayExeption;
33use oat\search\base\QueryBuilderInterface;
34use oat\search\base\ResultSetInterface;
35use oat\search\TaoSearchGateWay;
36
37/**
38 * Description of GateWay
39 *
40 * @author Christophe GARCIA <christopheg@taotesting.com>
41 */
42class GateWay extends TaoSearchGateWay
43{
44    /**
45     *
46     * @var common_persistence_SqlPersistence
47     */
48    protected $connector;
49    /**
50     * parser service or className
51     * @var string
52     */
53    protected $parserList = [
54        'taoRdf' => 'search.tao.parser'
55    ];
56    /**
57     * driver escaper list
58     * @var array
59     */
60    protected $driverList = [
61        'taoRdf' => 'search.driver.tao'
62    ];
63
64    /**
65     * resultSet service or className
66     * @var string
67     */
68    protected $resultSetClassName = '\\oat\\generis\\model\\kernel\\persistence\\smoothsql\\search\\TaoResultSet';
69
70    public function init()
71    {
72        parent::init();
73
74        $this->connector = ServiceManager::getServiceManager()
75                ->get(common_persistence_Manager::SERVICE_ID)
76                ->getPersistenceById($this->options['persistence'] ?? 'default');
77
78        return $this;
79    }
80
81        /**
82     * try to connect to database. throw an exception
83     * if connection failed.
84     *
85     * @throws SearchGateWayExeption
86     * @return $this
87     */
88    public function connect()
89    {
90        return !is_null($this->connector);
91    }
92
93    /**
94     * execute Parsed Query
95     *
96     * @return type
97     */
98    public function search(QueryBuilderInterface $Builder)
99    {
100        $this->serialyse($Builder);
101        $statement = $this->connector->query($this->parsedQuery);
102        $result    = $this->statementToArray($statement);
103        $resultSetClass = $this->resultSetClassName;
104        $resultSet = new $resultSetClass($result);
105        $queryCount = $this->getSerialyser()->setCriteriaList($Builder)->count(true)->serialyse();
106        $resultSet->setParent($this)->setCountQuery($queryCount);
107        return $resultSet;
108    }
109
110    /**
111     * @param QueryBuilderInterface $Builder
112     * @param string $propertyUri
113     * @param bool $isDistinct
114     *
115     * @return ResultSetInterface
116     */
117    public function searchTriples(QueryBuilderInterface $Builder, string $propertyUri, bool $isDistinct = false)
118    {
119        $statement = $this->connector->query(parent::searchTriples($Builder, $propertyUri, $isDistinct));
120        $result    = $this->statementToArray($statement);
121        $resultSet = new $this->resultSetClassName($result, count($result));
122        $resultSet->setIsTriple(true);
123        return $resultSet;
124    }
125
126    /**
127     *
128     * @param Statement $statement
129     * @return array
130     */
131    protected function statementToArray(Statement $statement)
132    {
133        $result = [];
134        while ($row = $statement->fetch(\PDO::FETCH_OBJ)) {
135            $result[] = $row;
136        }
137        return $result;
138    }
139
140    public function fetchQuery($query)
141    {
142        $statement = $this->connector->query($query);
143        $result = $statement->fetch(\PDO::FETCH_ASSOC);
144        return $result;
145    }
146
147    /**
148     * return total count result
149     * @param QueryBuilderInterface $Builder
150     * @return integer
151     */
152    public function count(QueryBuilderInterface $Builder)
153    {
154        $this->parsedQuery = $this->getSerialyser()->setCriteriaList($Builder)->count(true)->serialyse();
155        $statement = $this->connector->query($this->parsedQuery);
156        $result    = $this->statementToArray($statement);
157        return (int)reset($result)->cpt;
158    }
159
160
161    public function getJoiner()
162    {
163        $joiner = new QueryJoiner();
164        $options = $this->getOptions();
165        $joiner->setDriverEscaper($this->getDriverEscaper())->setOptions($options);
166        $joiner->setParent($this);
167        return $joiner;
168    }
169
170    public function join(QueryJoiner $joiner)
171    {
172
173        $query = $joiner->execute();
174        $statement = $this->connector->query($query);
175        $result    = $this->statementToArray($statement);
176        $resultSetClass = $this->resultSetClassName;
177        $resultSet = new $resultSetClass($result);
178        $queryCount = $joiner->count();
179        $resultSet->setParent($this)->setCountQuery($queryCount);
180        return $resultSet;
181    }
182}