1
1
<?php
2
2
3
3
class Database {
4
- private static $ instance = null ;
5
- private $ pdo , $ query , $ error = false , $ results , $ count ;
6
-
7
- private function __construct () {
8
- try {
9
- $ this ->pdo = new PDO ("mysql:host= " . Config::get ('mysql.host ' ) . ";dbname= " . Config::get ('mysql.database ' ), Config::get ('mysql.username ' ), Config::get ('mysql.password ' ));
10
- } catch (PDOException $ exception ) {
11
- die ($ exception ->getMessage ());
12
- }
13
- }
14
-
15
- public static function getInstance () {
16
-
17
- if (!isset (self ::$ instance )) {
18
- self ::$ instance = new Database ;
19
- }
20
-
21
- return self ::$ instance ;
22
- }
23
-
24
- public function query ($ sql , $ params = [])
25
- {
26
-
27
- $ this ->error = false ;
28
- $ this ->query = $ this ->pdo ->prepare ($ sql );
29
-
30
- if (count ($ params )) {
31
- $ i = 1 ;
32
- foreach ($ params as $ param ) {
33
- $ this ->query ->bindValue ($ i , $ param );
34
- $ i ++;
35
- }
36
- }
37
-
38
-
39
- if (!$ this ->query ->execute ()) {
40
- $ this ->error = true ;
41
- } else {
42
- $ this ->results = $ this ->query ->fetchAll (PDO ::FETCH_OBJ );
43
- $ this ->count = $ this ->query ->rowCount ();
44
- }
45
-
46
- return $ this ;
47
- }
48
-
49
- public function error ()
50
- {
51
- return $ this ->error ;
52
- }
53
-
54
- public function results ()
55
- {
56
- return $ this ->results ;
57
- }
58
-
59
- public function count ()
60
- {
61
- return $ this ->count ;
62
- }
63
-
64
- public function get ($ table , $ where = [])
65
- {
66
- return $ this ->action ('SELECT * ' , $ table , $ where );
67
- }
68
-
69
- public function delete ($ table , $ where = [])
70
- {
71
- return $ this ->action ('DELETE ' , $ table , $ where );
72
- }
73
-
74
- public function action ($ action , $ table , $ where = [])
75
- {
76
- if (count ($ where ) === 3 ) {
77
-
78
- $ operators = ['= ' , '> ' , '< ' , '>= ' , '<= ' ];
79
-
80
- $ field = $ where [0 ];
81
- $ operator = $ where [1 ];
82
- $ value = $ where [2 ];
83
-
84
- if (in_array ($ operator , $ operators )) {
85
-
86
- $ sql = "{$ action } FROM {$ table } WHERE {$ field } {$ operator } ? " ;
87
- if (!$ this ->query ($ sql , [$ value ])->error ()) { //true если есть ошибка
88
- return $ this ;
89
- }
90
- }
91
- }
92
-
93
- return false ;
94
- }
95
-
96
- public function insert ($ table , $ fields = [])
97
- {
98
- $ values = '' ;
99
- foreach ($ fields as $ field ) {
100
- $ values .= "?, " ;
101
- }
102
- $ val = rtrim ($ values , ', ' );
103
-
104
-
105
- $ sql = "INSERT INTO {$ table } ( " . '` ' . implode ('`, ` ' , array_keys ($ fields )) . '` ' . ") VALUES ( {$ val }) " ;
106
-
107
- if (!$ this ->query ($ sql , $ fields )->error ()) {
108
- return true ;
109
- }
110
- return false ;
111
-
112
- }
113
-
114
- public function update ($ table , $ id , $ fields = [])
115
- {
116
- $ set = '' ;
117
- foreach ($ fields as $ key => $ field ) {
118
- $ set .= "{$ key } = ?, " ; // username = ?, password = ?,
119
- }
120
-
121
- $ set = rtrim ($ set , ', ' ); // username = ?, password = ?
122
-
123
- $ sql = "UPDATE {$ table } SET {$ set } WHERE id = {$ id }" ;
124
-
125
- if (!$ this ->query ($ sql , $ fields )->error ()){
126
- return true ;
127
- }
128
-
129
- return false ;
130
- }
131
-
132
- public function first ()
133
- {
134
- return $ this ->results ()[0 ];
135
- }
136
- }
4
+ private static $ instance = null ;
5
+ private $ pdo , $ query , $ error = false , $ results , $ count ;
6
+
7
+ private function __construct () {
8
+ try {
9
+ if (Config::get ('dbdriver ' ) == 'sqlite ' ) {
10
+ $ this ->pdo = new PDO ("sqlite: " . Config::get ('sqlite.database ' ), '' , '' , [
11
+ PDO ::ATTR_EMULATE_PREPARES => false ,
12
+ PDO ::ATTR_ERRMODE => PDO ::ERRMODE_EXCEPTION ,
13
+ PDO ::ATTR_DEFAULT_FETCH_MODE => PDO ::FETCH_OBJ
14
+ ]);
15
+ } else if (Config::get ('dbdriver ' ) == 'mysql ' ) {
16
+ $ this ->pdo = new PDO ("mysql:host= " . Config::get ('mysql.host ' ) . ";dbname= " . Config::get ('mysql.database ' ) . "; charset=utf8 " , Config::get ('mysql.username ' ), Config::get ('mysql.password ' ), [
17
+ PDO ::ATTR_ERRMODE => PDO ::ERRMODE_EXCEPTION ,
18
+ PDO ::ATTR_DEFAULT_FETCH_MODE => PDO ::FETCH_OBJ
19
+ ]);
20
+ }
21
+ } catch (PDOException $ exception ) {
22
+ die ($ exception ->getMessage ());
23
+ }
24
+ }
25
+
26
+ public static function getInstance () {
27
+ if (!isset (self ::$ instance )) {
28
+ self ::$ instance = new Database ;
29
+ }
30
+
31
+ return self ::$ instance ;
32
+ }
33
+
34
+ public function query ($ sql , $ params = []) {
35
+ $ this ->error = false ;
36
+ $ this ->query = $ this ->pdo ->prepare ($ sql );
37
+
38
+ if (count ($ params )) {
39
+ $ i = 1 ;
40
+ foreach ($ params as $ param ) {
41
+ $ this ->query ->bindValue ($ i , $ param );
42
+ $ i ++;
43
+ }
44
+ }
45
+
46
+ if (!$ this ->query ->execute ()) {
47
+ $ this ->error = true ;
48
+ } else {
49
+ $ this ->results = $ this ->query ->fetchAll (PDO ::FETCH_OBJ );
50
+ $ this ->count = $ this ->query ->rowCount ();
51
+ }
52
+
53
+ return $ this ;
54
+ }
55
+
56
+ public function error () {
57
+ return $ this ->error ;
58
+ }
59
+
60
+ public function results () {
61
+ return $ this ->results ;
62
+ }
63
+
64
+ public function count () {
65
+ return $ this ->count ;
66
+ }
67
+
68
+ public function get ($ table , $ where = []) {
69
+ return $ this ->action ('SELECT * ' , $ table , $ where );
70
+ }
71
+
72
+ public function delete ($ table , $ where = []) {
73
+ return $ this ->action ('DELETE ' , $ table , $ where );
74
+ }
75
+
76
+ public function action ($ action , $ table , $ where = []) {
77
+ if (count ($ where ) === 3 ) {
78
+
79
+ $ operators = ['= ' , '> ' , '< ' , '>= ' , '<= ' ];
80
+
81
+ $ field = $ where [0 ];
82
+ $ operator = $ where [1 ];
83
+ $ value = $ where [2 ];
84
+
85
+ if (in_array ($ operator , $ operators )) {
86
+
87
+ $ sql = "{$ action } FROM {$ table } WHERE {$ field } {$ operator } ? " ;
88
+ if (!$ this ->query ($ sql , [$ value ])->error ()) { //true если есть ошибка
89
+ return $ this ;
90
+ }
91
+ }
92
+ }
93
+
94
+ return false ;
95
+ }
96
+
97
+ public function insert ($ table , $ fields = []) {
98
+ $ values = '' ;
99
+ foreach ($ fields as $ field ) {
100
+ $ values .= "?, " ;
101
+ }
102
+ $ val = rtrim ($ values , ', ' );
103
+
104
+ $ sql = "INSERT INTO {$ table } ( " . '` ' . implode ('`, ` ' , array_keys ($ fields )) . '` ' . ") VALUES ( {$ val }) " ;
105
+
106
+ if (!$ this ->query ($ sql , $ fields )->error ()) {
107
+ return true ;
108
+ }
109
+ return false ;
110
+ }
111
+
112
+ public function update ($ table , $ id , $ fields = []) {
113
+ $ set = '' ;
114
+ foreach ($ fields as $ key => $ field ) {
115
+ $ set .= "{$ key } = ?, " ; // username = ?, password = ?,
116
+ }
117
+
118
+ $ set = rtrim ($ set , ', ' ); // username = ?, password = ?
119
+
120
+ $ sql = "UPDATE {$ table } SET {$ set } WHERE id = {$ id }" ;
121
+
122
+ if (!$ this ->query ($ sql , $ fields )->error ()){
123
+ return true ;
124
+ }
125
+
126
+ return false ;
127
+ }
128
+
129
+ public function first () {
130
+ return $ this ->results ()[0 ];
131
+ }
132
+ }
0 commit comments