Subversion Repositories oidplus

Rev

Rev 863 | Rev 1086 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 863 Rev 1050
Line 15... Line 15...
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
17
 * limitations under the License.
18
 */
18
 */
19
 
19
 
20
if (!defined('INSIDE_OIDPLUS')) die();
20
namespace ViaThinkSoft\OIDplus;
21
 
21
 
22
class OIDplusDatabaseConnectionSQLite3 extends OIDplusDatabaseConnection {
22
class OIDplusDatabaseConnectionSQLite3 extends OIDplusDatabaseConnection {
23
        private $conn = null;
23
        private $conn = null;
24
        private $prepare_cache = array();
24
        private $prepare_cache = array();
25
        private $last_error = null; // do the same like MySQL+PDO, just to be equal in the behavior
25
        private $last_error = null; // do the same like MySQL+PDO, just to be equal in the behavior
Line 27... Line 27...
27
        public function doQuery(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult {
27
        public function doQuery(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult {
28
                $this->last_error = null;
28
                $this->last_error = null;
29
                if (is_null($prepared_args)) {
29
                if (is_null($prepared_args)) {
30
                        try {
30
                        try {
31
                                $res = $this->conn->query($sql);
31
                                $res = $this->conn->query($sql);
32
                        } catch (Exception $e) {
32
                        } catch (\Exception $e) {
33
                                $res = false;
33
                                $res = false;
34
                        }
34
                        }
35
                        if ($res === false) {
35
                        if ($res === false) {
36
                                $this->last_error = $this->conn->lastErrorMsg();
36
                                $this->last_error = $this->conn->lastErrorMsg();
37
                                throw new OIDplusSQLException($sql, $this->error());
37
                                throw new OIDplusSQLException($sql, $this->error());
Line 53... Line 53...
53
                        if (isset($this->prepare_cache[$sql])) {
53
                        if (isset($this->prepare_cache[$sql])) {
54
                                $stmt = $this->prepare_cache[$sql];
54
                                $stmt = $this->prepare_cache[$sql];
55
                        } else {
55
                        } else {
56
                                try {
56
                                try {
57
                                        $stmt = $this->conn->prepare($sql);
57
                                        $stmt = $this->conn->prepare($sql);
58
                                } catch (Exception $e) {
58
                                } catch (\Exception $e) {
59
                                        $stmt = false;
59
                                        $stmt = false;
60
                                }
60
                                }
61
                                if ($stmt === false) {
61
                                if ($stmt === false) {
62
                                        $this->last_error = $this->conn->lastErrorMsg();
62
                                        $this->last_error = $this->conn->lastErrorMsg();
63
                                        throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error());
63
                                        throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error());
Line 73... Line 73...
73
                                $stmt->bindValue(':param'.$i, $value, SQLITE3_TEXT);
73
                                $stmt->bindValue(':param'.$i, $value, SQLITE3_TEXT);
74
                        }
74
                        }
75
 
75
 
76
                        try {
76
                        try {
77
                                $ps = $stmt->execute();
77
                                $ps = $stmt->execute();
78
                        } catch (Exception $e) {
78
                        } catch (\Exception $e) {
79
                                $ps = false;
79
                                $ps = false;
80
                        }
80
                        }
81
                        if ($ps === false) {
81
                        if ($ps === false) {
82
                                $this->last_error = $this->conn->lastErrorMsg();
82
                                $this->last_error = $this->conn->lastErrorMsg();
83
                                throw new OIDplusSQLException($sql, $this->error());
83
                                throw new OIDplusSQLException($sql, $this->error());
Line 91... Line 91...
91
                        // Note: This will always give results even for tables that do not
91
                        // Note: This will always give results even for tables that do not
92
                        // have autoincrements, because SQLite3 assigns an "autoindex" for every table,
92
                        // have autoincrements, because SQLite3 assigns an "autoindex" for every table,
93
                        // e.g. the config table. Therefore, our testcase will fail.
93
                        // e.g. the config table. Therefore, our testcase will fail.
94
                        return (int)$this->conn->lastInsertRowID();
94
                        return (int)$this->conn->lastInsertRowID();
95
                        //return (int)$this->query('select last_insert_rowid() as id')->fetch_object()->id;
95
                        //return (int)$this->query('select last_insert_rowid() as id')->fetch_object()->id;
96
                } catch (Exception $e) {
96
                } catch (\Exception $e) {
97
                        return 0;
97
                        return 0;
98
                }
98
                }
99
        }
99
        }
100
 
100
 
101
        public function error(): string {
101
        public function error(): string {
Line 117... Line 117...
117
                        if (!$is_absolute_path) {
117
                        if (!$is_absolute_path) {
118
                                // Filename must be absolute path, since OIDplus can be called from several locations (e.g. registration wizard)
118
                                // Filename must be absolute path, since OIDplus can be called from several locations (e.g. registration wizard)
119
                                $filename = OIDplus::localpath().$filename;
119
                                $filename = OIDplus::localpath().$filename;
120
                        }
120
                        }
121
 
121
 
122
                        $this->conn = new SQLite3($filename, $flags, $encryption);
122
                        $this->conn = new \SQLite3($filename, $flags, $encryption);
123
                } catch (Exception $e) {
123
                } catch (\Exception $e) {
124
                        throw new OIDplusConfigInitializationException(trim(_L('Connection to the database failed!').' ' . $e->getMessage()));
124
                        throw new OIDplusConfigInitializationException(trim(_L('Connection to the database failed!').' ' . $e->getMessage()));
125
                }
125
                }
126
 
126
 
127
                $this->conn->createCollation('NATURAL_CMP', 'strnatcmp'); // we need that for natSort()
127
                $this->conn->createCollation('NATURAL_CMP', 'strnatcmp'); // we need that for natSort()
128
                $this->conn->enableExceptions(true); // Throw exceptions instead of PHP warnings
128
                $this->conn->enableExceptions(true); // Throw exceptions instead of PHP warnings