1313
1414use CodeIgniter \Database \Exceptions \DatabaseException ;
1515
16- /**
17- * Class BaseUtils
18- */
1916abstract class BaseUtils
2017{
2118 /**
22- * Database object
23- *
2419 * @var object
2520 */
2621 protected $ db ;
@@ -46,24 +41,18 @@ abstract class BaseUtils
4641 */
4742 protected $ repairTable = false ;
4843
49- /**
50- * Class constructor
51- */
5244 public function __construct (ConnectionInterface &$ db )
5345 {
5446 $ this ->db = &$ db ;
5547 }
5648
5749 /**
58- * List databases
59- *
6050 * @throws DatabaseException
6151 *
6252 * @return array|bool
6353 */
6454 public function listDatabases ()
6555 {
66- // Is there a cached result?
6756 if (isset ($ this ->db ->dataCache ['db_names ' ])) {
6857 return $ this ->db ->dataCache ['db_names ' ];
6958 }
@@ -79,6 +68,7 @@ public function listDatabases()
7968 $ this ->db ->dataCache ['db_names ' ] = [];
8069
8170 $ query = $ this ->db ->query ($ this ->listDatabases );
71+
8272 if ($ query === false ) {
8373 return $ this ->db ->dataCache ['db_names ' ];
8474 }
@@ -90,17 +80,12 @@ public function listDatabases()
9080 return $ this ->db ->dataCache ['db_names ' ];
9181 }
9282
93- /**
94- * Determine if a particular database exists
95- */
9683 public function databaseExists (string $ databaseName ): bool
9784 {
9885 return in_array ($ databaseName , $ this ->listDatabases (), true );
9986 }
10087
10188 /**
102- * Optimize Table
103- *
10489 * @throws DatabaseException
10590 *
10691 * @return bool
@@ -121,8 +106,6 @@ public function optimizeTable(string $tableName)
121106 }
122107
123108 /**
124- * Optimize Database
125- *
126109 * @throws DatabaseException
127110 *
128111 * @return mixed
@@ -145,17 +128,16 @@ public function optimizeDatabase()
145128 return $ res ;
146129 }
147130
148- // Build the result array...
149-
150131 $ res = $ res ->getResultArray ();
151132
152- // Postgre & SQLite3 returns empty array
153133 if (empty ($ res )) {
134+ // Postgre & SQLite3 returns empty array
154135 $ key = $ tableName ;
155136 } else {
156137 $ res = current ($ res );
157138 $ key = str_replace ($ this ->db ->database . '. ' , '' , current ($ res ));
158139 $ keys = array_keys ($ res );
140+
159141 unset($ res [$ keys [0 ]]);
160142 }
161143
@@ -166,8 +148,6 @@ public function optimizeDatabase()
166148 }
167149
168150 /**
169- * Repair Table
170- *
171151 * @throws DatabaseException
172152 *
173153 * @return mixed
@@ -183,6 +163,7 @@ public function repairTable(string $tableName)
183163 }
184164
185165 $ query = $ this ->db ->query (sprintf ($ this ->repairTable , $ this ->db ->escapeIdentifiers ($ tableName )));
166+
186167 if (is_bool ($ query )) {
187168 return $ query ;
188169 }
@@ -193,26 +174,23 @@ public function repairTable(string $tableName)
193174 }
194175
195176 /**
196- * Generate CSV from a query result object
197- *
198177 * @return string
199178 */
200179 public function getCSVFromResult (ResultInterface $ query , string $ delim = ', ' , string $ newline = "\n" , string $ enclosure = '" ' )
201180 {
202181 $ out = '' ;
203- // First generate the headings from the table column names
182+
204183 foreach ($ query ->getFieldNames () as $ name ) {
205184 $ out .= $ enclosure . str_replace ($ enclosure , $ enclosure . $ enclosure , $ name ) . $ enclosure . $ delim ;
206185 }
207186
208187 $ out = substr ($ out , 0 , -strlen ($ delim )) . $ newline ;
209188
210- // Next blast through the result array and build out the rows
211189 while ($ row = $ query ->getUnbufferedRow ('array ' )) {
212190 $ line = [];
213191
214192 foreach ($ row as $ item ) {
215- $ line [] = $ enclosure . str_replace ($ enclosure , $ enclosure . $ enclosure , $ item ) . $ enclosure ;
193+ $ line [] = $ enclosure . str_replace ($ enclosure , $ enclosure . $ enclosure , $ item ?? '' ) . $ enclosure ;
216194 }
217195
218196 $ out .= implode ($ delim , $ line ) . $ newline ;
@@ -221,9 +199,6 @@ public function getCSVFromResult(ResultInterface $query, string $delim = ',', st
221199 return $ out ;
222200 }
223201
224- /**
225- * Generate XML data from a query result object
226- */
227202 public function getXMLFromResult (ResultInterface $ query , array $ params = []): string
228203 {
229204 foreach (['root ' => 'root ' , 'element ' => 'element ' , 'newline ' => "\n" , 'tab ' => "\t" ] as $ key => $ val ) {
@@ -256,8 +231,6 @@ public function getXMLFromResult(ResultInterface $query, array $params = []): st
256231 }
257232
258233 /**
259- * Database Backup
260- *
261234 * @param array|string $params
262235 *
263236 * @throws DatabaseException
@@ -274,7 +247,7 @@ public function backup($params = [])
274247 'tables ' => [],
275248 'ignore ' => [],
276249 'filename ' => '' ,
277- 'format ' => 'gzip ' , // gzip, txt
250+ 'format ' => 'gzip ' ,
278251 'add_drop ' => true ,
279252 'add_insert ' => true ,
280253 'newline ' => "\n" ,
0 commit comments