1<?php
2
3
4namespace ComboStrap;
5
6
7class SqliteRequest
8{
9    /**
10     * @var Sqlite
11     */
12    private $sqlite;
13    /**
14     * @var string
15     */
16    private $tableName;
17    /**
18     * @var array
19     */
20    private $data;
21    /**
22     * @var SqliteResult
23     */
24    private $result;
25    /**
26     * @var string
27     */
28    private $query;
29
30    private $sqlitePlugin;
31    /**
32     * @var array|string[]
33     */
34    private $queryParametrized;
35    /**
36     * A statement that is not a query
37     * @var string
38     */
39    private $statement;
40
41    /**
42     * SqliteRequest constructor.
43     * @param Sqlite $sqlite
44     */
45    public function __construct(Sqlite $sqlite)
46    {
47        $this->sqlite = $sqlite;
48        $this->sqlitePlugin = $sqlite->getSqlitePlugin();
49    }
50
51    public function setTableRow(string $tableName, array $data): SqliteRequest
52    {
53        $this->tableName = $tableName;
54        $this->data = $data;
55        return $this;
56    }
57
58    /**
59     * @throws ExceptionCombo
60     */
61    public function execute(): SqliteResult
62    {
63        $res = null;
64        $requestType = "";
65        if ($this->data !== null && $this->tableName !== null) {
66            $res = $this->sqlitePlugin->storeEntry($this->tableName, $this->data);
67            $requestType = "Upsert";
68        }
69
70        if ($this->query !== null) {
71            $res = $this->sqlitePlugin->query($this->query);
72            $requestType = "Query Simple";
73        }
74
75        if ($this->queryParametrized !== null) {
76            $res = $this->sqlitePlugin->getAdapter()->query($this->queryParametrized);
77            $requestType = "Query Parametrized"; // delete, insert, update, query
78        }
79
80        if($this->statement!==null){
81            $res = $this->sqlitePlugin->getAdapter()->getDb()->exec($this->statement);
82            $requestType = "statement";
83        }
84
85        if ($res === null) {
86            throw new ExceptionCombo("No Sql request was found to be executed");
87        }
88
89        if ($res === false) {
90            $message = $this->getErrorMessage();
91            throw new ExceptionCombo("Error in the $requestType. Message: {$message}");
92        }
93
94        $this->result = new SqliteResult($this, $res);
95        return $this->result;
96    }
97
98    public function getErrorMessage(): string
99    {
100        $adapter = $this->sqlitePlugin->getAdapter();
101        if ($adapter === null) {
102            LogUtility::msg("The database adapter is null, no error info can be retrieved");
103            return "";
104        }
105        $do = $adapter->getDb();
106        if ($do === null) {
107            LogUtility::msg("The database object is null, it seems that the database connection has been closed");
108            return "";
109        }
110        $errorInfo = $do->errorInfo();
111        $message = "";
112        $errorCode = $errorInfo[0];
113        if ($errorCode === '0000') {
114            $message = ("No rows were deleted or updated");
115        }
116        $errorInfoAsString = implode(", ",$errorInfo);
117        return "$message. : {$errorInfoAsString}";
118    }
119
120    public function getSqliteConnection(): Sqlite
121    {
122        return $this->sqlite;
123    }
124
125    public function close()
126    {
127
128        if ($this->result !== null) {
129            $this->result->close();
130            $this->result = null;
131        }
132
133    }
134
135    public function setQuery(string $string): SqliteRequest
136    {
137        $this->query = $string;
138        return $this;
139    }
140
141    /**
142     * @param string $executableSql
143     * @param array $parameters
144     * @return SqliteResult
145     */
146    public function setQueryParametrized(string $executableSql, array $parameters): SqliteRequest
147    {
148
149        $args = [$executableSql];
150        $this->queryParametrized = array_merge($args, $parameters);
151        return $this;
152
153    }
154
155    /**
156     * @param string $statement
157     * @return $this - a statement that will execute
158     */
159    public function setStatement(string $statement): SqliteRequest
160    {
161        $this->statement = $statement;
162        return $this;
163    }
164
165}
166