@@ -34,95 +34,152 @@ function setTimeout(int $timeout) {
34
34
$ this ->timeout = $ timeout ;
35
35
}
36
36
37
- private function mysqli ( ) {
38
- $ link = mysqli_init ();
39
- $ link ->options (11 /*MYSQL_OPT_READ_TIMEOUT*/ , $ this ->timeout );
40
- $ ret = $ link ->real_connect ($ this ->host , $ this ->user , $ this ->password , $ this ->database , $ this ->port , $ this ->socket );
37
+ private function runMysql ( string $ statment , $ paramTypes = null , &... $ params ) {
38
+ $ mysqli = mysqli_init ();
39
+ $ mysqli ->options (11 /*MYSQL_OPT_READ_TIMEOUT*/ , $ this ->timeout );
40
+ $ ret = $ mysqli ->real_connect ($ this ->host , $ this ->user , $ this ->password , $ this ->database , $ this ->port , $ this ->socket );
41
41
if ($ ret === false ) {
42
- throw new Exception ('Cannot connect to mysql server: ' . mysqli_error ($ link ));
42
+ throw new Exception ('Cannot connect to mysql server: ' . mysqli_error ($ mysqli ));
43
43
}
44
- return $ link ;
45
- }
46
-
47
- function delete (string $ key ) {
48
- $ mysqli = $ this ->mysqli ();
49
- $ stmt = $ mysqli ->prepare ("DELETE FROM ` $ this ->table ` WHERE `key` = ? " );
44
+ $ stmt = $ mysqli ->prepare ($ statment );
50
45
if ($ stmt === false ) {
51
46
throw new Exception ('Cannot prepare stmt: ' . mysqli_error ($ mysqli ));
52
47
}
53
- if (! $ stmt ->bind_param (' s ' , $ key )) {
54
- throw new Exception ("Cannot bind param: $ stmt ->error " );
48
+ if ($ paramTypes !== null && ! $ stmt ->bind_param ($ paramTypes , ... $ params )) {
49
+ throw new Exception ("Cannot bind param( $ stmt -> errno ) : $ stmt ->error " );
55
50
}
56
- if (!( $ result = $ stmt ->execute () )) {
51
+ if (!$ stmt ->execute ()) {
57
52
throw new Exception ("Cannot execute query: $ stmt ->error " );
58
53
}
54
+ $ result = $ stmt ->get_result ();
59
55
$ stmt ->close ();
60
56
$ mysqli ->close ();
61
57
return $ result ;
62
58
}
63
59
64
- function set (string $ key , string $ value ) {
65
- $ mysqli = $ this ->mysqli ();
66
- $ stmt = $ mysqli ->prepare ("INSERT INTO ` $ this ->table ` (`key`,`value`) VALUES (?, ?) " );
67
- if ($ stmt === false ) {
68
- throw new Exception ('Cannot prepare stmt: ' . mysqli_error ($ mysqli ));
69
- }
70
- if (!$ stmt ->bind_param ('ss ' , $ key , $ value )) {
71
- throw new Exception ("Cannot bind param: $ stmt ->error " );
60
+ function has (string $ key ) {
61
+ $ result = $ this ->runMysql ("SELECT EXISTS(SELECT `value` FROM ` $ this ->table ` WHERE `key` = ? LIMIT 1) " , 's ' , $ key );
62
+ return $ result ->fetch_array (MYSQLI_NUM )[0 ] > 0 ;
63
+ }
64
+
65
+ function count (string $ prefix = '' ) {
66
+ $ prefix .= '% ' ;
67
+ $ result = $ this ->runMysql ("SELECT COUNT(`value`) FROM ` $ this ->table ` WHERE `key` LIKE ? " , 's ' , $ prefix );
68
+ return $ result ->fetch_array (MYSQLI_NUM )[0 ];
69
+ }
70
+
71
+ function delete (string $ key ) {
72
+ if (!$ this ->has ($ key )) {
73
+ return true ;
72
74
}
73
- if (!($ result = $ stmt ->execute ())) {
74
- throw new Exception ("Cannot execute query: $ stmt ->error " );
75
+ try {
76
+ $ this ->runMysql ("DELETE FROM ` $ this ->table ` WHERE `key` = ? " , 's ' , $ key );
77
+ } catch (Exception $ e ) {
78
+ return false ;
75
79
}
76
- $ stmt ->close ();
77
- $ mysqli ->close ();
78
- return $ result ;
80
+ return true ;
79
81
}
80
82
81
- function get (string $ key ) {
82
- $ mysqli = $ this ->mysqli ();
83
- $ stmt = $ mysqli ->prepare ("SELECT `value` FROM ` $ this ->table ` WHERE `key` = ? LIMIT 1 " );
84
- if ($ stmt === false ) {
85
- throw new Exception ('Cannot prepare stmt: ' . mysqli_error ($ mysqli ));
83
+ function set (string $ key , string $ value ) {
84
+ return $ this ->has ($ key ) ? $ this ->update ($ key , $ value ) : $ this ->add ($ key , $ value );
85
+ }
86
+
87
+ function add (string $ key , string $ value ) {
88
+ if ($ this ->has ($ key )) {
89
+ return false ;
86
90
}
87
- if (!$ stmt ->bind_param ('s ' , $ key )) {
88
- throw new Exception ("Cannot bind param: $ stmt ->error " );
91
+ try {
92
+ $ this ->runMysql ("INSERT INTO ` $ this ->table ` (`key`,`value`) VALUES (?, ?) " , 'ss ' , $ key , $ value );
93
+ } catch (Exception $ e ) {
94
+ return false ;
89
95
}
90
- if (!$ stmt ->execute ()) {
91
- throw new Exception ("Cannot execute query: $ stmt ->error " );
96
+ return true ;
97
+ }
98
+
99
+ function update (string $ key , string $ value ) {
100
+ try {
101
+ $ this ->runMysql ("UPDATE ` $ this ->table ` SET `value` = ? WHERE `key` = ? " , 'ss ' , $ value , $ key );
102
+ } catch (Exception $ e ) {
103
+ return false ;
92
104
}
93
- $ result = $ stmt ->get_result ();
94
- $ rtn = false ;
105
+ return true ;
106
+ }
107
+
108
+ function get (string $ key ) {
109
+ $ result = $ this ->runMysql ("SELECT `value` FROM ` $ this ->table ` WHERE `key` = ? LIMIT 1 " , 's ' , $ key );
95
110
$ row = $ result ->fetch_array (MYSQLI_ASSOC );
96
- if (isset ($ row ['value ' ])) {
97
- $ rtn = $ row ['value ' ];
98
- }
99
- $ stmt ->close ();
100
- $ mysqli ->close ();
101
- return $ rtn ;
111
+ return isset ($ row ['value ' ]) ? $ row ['value ' ] : false ;
102
112
}
103
113
104
114
function getPage (int $ page = 1 , int $ perPage = 100 , string $ prefix = '' ) {
105
- $ mysqli = $ this ->mysqli ();
106
- $ stmt = $ mysqli ->prepare ("SELECT * FROM ` $ this ->table ` WHERE `key` LIKE ? LIMIT ?, ? " );
107
- if ($ stmt === false ) {
108
- throw new Exception ('Cannot prepare stmt: ' . mysqli_error ($ mysqli ));
109
- }
110
115
$ prefix .= '% ' ;
111
116
$ start = ($ page - 1 ) * $ perPage ;
112
- if (!$ stmt ->bind_param ('sii ' , $ prefix , $ start , $ perPage )) {
113
- throw new Exception ("Cannot bind param: $ stmt ->error " );
117
+ $ result = $ this ->runMysql ("SELECT * FROM ` $ this ->table ` WHERE `key` LIKE ? LIMIT ?, ? " , 'sii ' , $ prefix , $ start , $ perPage );
118
+ $ rtn = [];
119
+ while ($ row = $ result ->fetch_array (MYSQLI_ASSOC )) {
120
+ $ rtn [] = $ row ;
114
121
}
115
- if (!$ stmt ->execute ()) {
116
- throw new Exception ("Cannot execute query: $ stmt ->error " );
122
+ return $ rtn ;
123
+ }
124
+
125
+ function getAll (string $ prefix = '' ) {
126
+ return new DbMySQLIterator ($ prefix , $ this );
127
+ }
128
+
129
+ function search (string $ text , int $ page , bool $ ignoreCase , array $ range ) {
130
+ $ text = '% ' . $ text . '% ' ;
131
+ if ($ text === '%% ' ) {
132
+ $ text = '% ' ;
117
133
}
118
- $ result = $ stmt ->get_result ();
134
+ $ caseProcessing = $ ignoreCase ? 'UPPER ' : '' ;
135
+ $ start = ($ page - 1 ) * DbBase::$ SEARCH_RESULT_PER_PAGE ;
136
+ $ sql = "SELECT * FROM ` $ this ->table ` WHERE " ;
137
+ $ sqlParamTypes = '' ;
138
+ $ sqlParams = [];
139
+ foreach ($ range as $ index => $ field ) {
140
+ $ range [$ index ] = "$ caseProcessing(` $ field`) LIKE $ caseProcessing(?) " ;
141
+ $ sqlParamTypes .= 's ' ;
142
+ $ sqlParams [] = $ text ;
143
+ }
144
+ $ sql .= implode (' OR ' , $ range );
145
+
146
+ $ countResult = $ this ->runMysql (str_replace ('SELECT * ' , 'SELECT COUNT(`value`) ' , $ sql ), $ sqlParamTypes , ...$ sqlParams );
147
+ $ count = $ countResult ->fetch_array (MYSQLI_NUM )[0 ];
148
+
149
+ $ sql .= " LIMIT ?, ? " ;
150
+ $ sqlParamTypes .= 'ii ' ;
151
+ $ sqlParams [] = $ start ;
152
+ $ sqlParams [] = DbBase::$ SEARCH_RESULT_PER_PAGE ;
153
+ $ result = $ this ->runMysql ($ sql , $ sqlParamTypes , ...$ sqlParams );
119
154
$ rtn = [];
120
155
while ($ row = $ result ->fetch_array (MYSQLI_ASSOC )) {
121
156
$ rtn [] = $ row ;
122
157
}
123
- $ stmt ->close ();
124
- $ mysqli ->close ();
125
- return $ rtn ;
158
+ return [ 'count ' => $ count , 'result ' => $ rtn ];
159
+ }
160
+
161
+ function eraseData () {
162
+ $ reservedPrefix = DbBase::$ KEY_RESERVED_PREFIX . '% ' ;
163
+ $ this ->runMysql ("DELETE FROM ` $ this ->table ` WHERE `key` NOT LIKE ? " , 's ' , $ reservedPrefix );
164
+ }
165
+
166
+ function eraseAll () {
167
+ $ this ->runMysql ("DELETE FROM ` $ this ->table ` " );
168
+ }
169
+
170
+ }
171
+
172
+ class DbMySQLIterator extends DbBaseIterator {
173
+
174
+ private $ dbMySQL ;
175
+
176
+ function __construct (string $ prefix , DbMySQL $ dbMySQL ) {
177
+ $ this ->dbMySQL = $ dbMySQL ;
178
+ parent ::__construct ($ prefix );
179
+ }
180
+
181
+ public function getNextPage () {
182
+ $ this ->currentValue = $ this ->dbMySQL ->getPage ($ this ->page , DbBaseIterator::$ PERPAGE , $ this ->prefix );
126
183
}
127
184
128
185
}
0 commit comments