1<?php
2
3
4namespace ComboStrap;
5
6
7use PDO;
8
9class SqliteResult
10{
11    private $res;
12    /**
13     * @var SqliteRequest
14     */
15    private $sqlite;
16    /**
17     * @var \helper_plugin_sqlite
18     */
19    private $sqlitePlugin;
20
21    /**
22     * SqliteResult constructor.
23     */
24    public function __construct(SqliteRequest $sqlite, $res)
25    {
26        $this->sqlite = $sqlite;
27        $this->res = $res;
28        $this->sqlitePlugin = $this->sqlite->getSqliteConnection()->getSqlitePlugin();
29
30    }
31
32    public function getRows(): array
33    {
34
35        $adapter = $this->sqlitePlugin->getAdapter();
36        if (!Sqlite::isJuneVersion($adapter)) {
37            return $this->sqlitePlugin->res2arr($this->res);
38        }
39
40        /**
41         * note:
42         * * fetch mode may be also {@link PDO::FETCH_NUM}
43         * * {@link helper_plugin_sqlite::res2arr()} but without the fucking cache !
44         */
45        return $this->res->fetchAll(PDO::FETCH_ASSOC);
46
47
48    }
49
50    public function close(): SqliteResult
51    {
52        /**
53         * $this->res may be a boolean {@link }
54         * is a number in CI
55         *
56         * We get:
57         * Error: Call to a member function closeCursor() on int
58         * /home/runner/work/combo/combo/lib/plugins/sqlite/classes/adapter_pdosqlite.php:125
59         */
60        if ($this->res instanceof \PDOStatement) {
61            $this->sqlitePlugin->res_close($this->res);
62        }
63        $this->res = null;
64        return $this;
65    }
66
67    public function getInsertId(): string
68    {
69        $adapter = $this->sqlitePlugin->getAdapter();
70        if (!Sqlite::isJuneVersion($adapter)) {
71            /** @noinspection PhpUndefinedMethodInspection */
72            return $adapter->getDb()->lastInsertId();
73        } else {
74            return $adapter->getPdo()->lastInsertId();
75        }
76
77    }
78
79    public function getChangeCount()
80    {
81        return $this->sqlitePlugin->countChanges($this->res);
82    }
83
84    public function getFirstCellValue()
85    {
86        return $this->sqlitePlugin->res2single($this->res);
87    }
88
89    public function getFirstCellValueAsInt(): int
90    {
91        return intval($this->getFirstCellValue());
92    }
93
94    public function getFirstRow()
95    {
96        $rows = $this->getRows();
97        if (sizeof($rows) >= 1) {
98            return $rows[0];
99        }
100        return [];
101    }
102
103
104}
105