xref: /plugin/combo/ComboStrap/SqliteRequest.php (revision 1e6623d9be7c26643a021b887e027c9b902710d7)
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 ExceptionCompile
60     */
61    public function execute(): SqliteResult
62    {
63        $res = null;
64        $requestType = "";
65        $queryExecuted="";
66        if ($this->data !== null && $this->tableName !== null) {
67            $res = $this->sqlitePlugin->storeEntry($this->tableName, $this->data);
68            $requestType = "Upsert";
69            $queryExecuted = "upsert of table $this->tableName";
70        }
71
72        if ($this->query !== null) {
73            $res = $this->sqlitePlugin->query($this->query);
74            $requestType = "Query Simple";
75            $queryExecuted = $this->query;
76        }
77
78        if ($this->queryParametrized !== null) {
79            $res = $this->sqlitePlugin->getAdapter()->query($this->queryParametrized);
80            $requestType = "Query Parametrized"; // delete, insert, update, query
81            $queryExecuted = $this->queryParametrized;
82        }
83
84        if ($this->statement !== null) {
85            $res = $this->sqlitePlugin->getAdapter()->executeQuery($this->statement);
86            $requestType = "statement";
87            $queryExecuted = $this->statement;
88        }
89
90        if ($res === null) {
91            throw new ExceptionCompile("No Sql request was found to be executed");
92        }
93
94        if ($res === false) {
95            $message = $this->getErrorMessage();
96            throw new ExceptionCompile("Error in the $requestType. Message: {$message}");
97        }
98
99        if((!$res instanceof \PDOStatement)){
100            $message = $this->getErrorMessage();
101            throw new ExceptionCompile("Error in the request type `$requestType`. res is not a PDOStatement but as the value ($res). Message: {$message}, Query: {$queryExecuted}");
102        }
103
104        $this->result = new SqliteResult($this, $res);
105        return $this->result;
106    }
107
108    public function getErrorMessage(): string
109    {
110        $adapter = $this->sqlitePlugin->getAdapter();
111        if ($adapter === null) {
112            throw new ExceptionRuntimeInternal("The database adapter is null, no error info can be retrieved");
113        }
114        $do = $adapter->getDb();
115        if ($do === null) {
116            throw new ExceptionRuntimeInternal("The database object is null, it seems that the database connection has been closed. Are you in two differents execution context ?");
117        }
118        $errorInfo = $do->errorInfo();
119        $message = "";
120        $errorCode = $errorInfo[0];
121        if ($errorCode === '0000') {
122            $message = ("No rows were deleted or updated");
123        }
124        $errorInfoAsString = implode(", ", $errorInfo);
125        return "$message. : {$errorInfoAsString}";
126    }
127
128    public function getSqliteConnection(): Sqlite
129    {
130        return $this->sqlite;
131    }
132
133    public function close()
134    {
135
136        if ($this->result !== null) {
137            $this->result->close();
138            $this->result = null;
139        }
140
141    }
142
143    public function setQuery(string $string): SqliteRequest
144    {
145        $this->query = $string;
146        return $this;
147    }
148
149    /**
150     * @param string $executableSql
151     * @param array $parameters
152     * @return SqliteResult
153     */
154    public function setQueryParametrized(string $executableSql, array $parameters): SqliteRequest
155    {
156
157        $args = [$executableSql];
158        $this->queryParametrized = array_merge($args, $parameters);
159        return $this;
160
161    }
162
163    /**
164     * @param string $statement
165     * @return $this - a statement that will execute
166     */
167    public function setStatement(string $statement): SqliteRequest
168    {
169        $this->statement = $statement;
170        return $this;
171    }
172
173}
174