xref: /plugin/combo/ComboStrap/SqliteResult.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1<?php
2
3
4namespace ComboStrap;
5
6
7class SqliteResult
8{
9    private $res;
10    /**
11     * @var SqliteRequest
12     */
13    private $sqlite;
14    /**
15     * @var \helper_plugin_sqlite
16     */
17    private $sqlitePlugin;
18
19    /**
20     * SqliteResult constructor.
21     */
22    public function __construct(SqliteRequest $sqlite, $res)
23    {
24        $this->sqlite = $sqlite;
25        $this->res = $res;
26        $this->sqlitePlugin = $this->sqlite->getSqliteConnection()->getSqlitePlugin();
27
28    }
29
30    public function getRows(): array
31    {
32        return $this->sqlitePlugin->res2arr($this->res);
33    }
34
35    public function close(): SqliteResult
36    {
37        /**
38         * $this->res is a number in CI
39         *
40         * We get:
41         * Error: Call to a member function closeCursor() on int
42         * /home/runner/work/combo/combo/lib/plugins/sqlite/classes/adapter_pdosqlite.php:125
43         */
44        if ($this->res instanceof \PDOStatement) {
45            $this->sqlitePlugin->res_close($this->res);
46        }
47        $this->res = null;
48        return $this;
49    }
50
51    public function getInsertId(): string
52    {
53        return $this->sqlitePlugin->getAdapter()->getDb()->lastInsertId();
54    }
55
56    public function getChangeCount()
57    {
58        return $this->sqlitePlugin->countChanges($this->res);
59    }
60
61    public function getFirstCellValue()
62    {
63        return $this->sqlitePlugin->res2single($this->res);
64    }
65
66    public function getFirstCellValueAsInt(): int
67    {
68        return intval($this->getFirstCellValue());
69    }
70
71    public function getFirstRow()
72    {
73        $rows = $this->getRows();
74        if (sizeof($rows) >= 1) {
75            return $rows[0];
76        }
77        return [];
78    }
79
80
81}
82