]> git.mxchange.org Git - core.git/blob - framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php
Continued:
[core.git] / framework / main / classes / database / backend / lfdb_legacy / class_CachedLocalFileDatabase.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Database\Backend\Lfdb;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Criteria\Criteria;
8 use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
9 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
10 use Org\Mxchange\CoreFramework\Database\Backend\BaseDatabaseBackend;
11 use Org\Mxchange\CoreFramework\Database\Backend\DatabaseBackend;
12 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
13 use Org\Mxchange\CoreFramework\Filesystem\FileNotFoundException;
14 use Org\Mxchange\CoreFramework\Generic\FrameworkException;
15 use Org\Mxchange\CoreFramework\Handler\Stream\IoHandler;
16 use Org\Mxchange\CoreFramework\Middleware\Compressor\CompressorChannel;
17
18 // Import SPL stuff
19 use \SplFileInfo;
20
21 /**
22  * Database backend class for storing objects in locally created files.
23  *
24  * This class serializes arrays stored in the dataset instance and saves them
25  * to local files. Every file (except 'info') represents a single line. Every
26  * directory within the 'db' (base) directory represents a table.
27  *
28  * A configurable 'file_io_class' is being used as "storage backend".
29  *
30  * @author              Roland Haeder <webmaster@shipsimu.org>
31  * @version             0.0.0
32  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
33  * @license             GNU GPL 3.0 or any newer version
34  * @link                http://www.shipsimu.org
35  *
36  * This program is free software: you can redistribute it and/or modify
37  * it under the terms of the GNU General Public License as published by
38  * the Free Software Foundation, either version 3 of the License, or
39  * (at your option) any later version.
40  *
41  * This program is distributed in the hope that it will be useful,
42  * but WITHOUT ANY WARRANTY; without even the implied warranty of
43  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44  * GNU General Public License for more details.
45  *
46  * You should have received a copy of the GNU General Public License
47  * along with this program. If not, see <http://www.gnu.org/licenses/>.
48  */
49 class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
50         /**
51          * The file's extension
52          */
53         private $fileExtension = 'serialized';
54
55         /**
56          * The last read file's name
57          */
58         private $lastFile = '';
59
60         /**
61          * The last read file's content including header information
62          */
63         private $lastContents = array();
64
65         /**
66          * Whether the "connection is already up
67          */
68         private $alreadyConnected = false;
69
70         /**
71          * Table information array
72          */
73         private $tableInfo = array();
74
75         /**
76          * Element for index
77          */
78         private $indexKey = '__idx';
79
80         /**
81          * The file I/O instance for the template loader
82          */
83         private $fileIoInstance = NULL;
84
85         /**
86          * A Compressor instance
87          */
88         private $compressorChannelInstance = NULL;
89
90         /**
91          * The protected constructor. Do never instance from outside! You need to
92          * set a local file path. The class will then validate it.
93          *
94          * @return      void
95          */
96         protected function __construct () {
97                 // Call parent constructor
98                 parent::__construct(__CLASS__);
99         }
100
101         /**
102          * Create an object of CachedLocalFileDatabase and set the save path from
103          * configuration for local files.
104          *
105          * @return      $databaseInstance       An instance of CachedLocalFileDatabase
106          */
107         public static final function createCachedLocalFileDatabase () {
108                 // Get an instance
109                 $databaseInstance = new CachedLocalFileDatabase();
110
111                 // Get a new compressor channel instance
112                 $compressorChannelInstance = ObjectFactory::createObjectByConfiguredName('compressor_channel_class');
113
114                 // Set the compressor channel
115                 $databaseInstance->setCompressorChannelInstance($compressorChannelInstance);
116
117                 // Get a file IO handler
118                 $fileIoInstance = ObjectFactory::createObjectByConfiguredName('file_io_class');
119
120                 // ... and set it
121                 $databaseInstance->setFileIoInstance($fileIoInstance);
122
123                 // "Connect" to the database
124                 $databaseInstance->connectToDatabase();
125
126                 // Return database instance
127                 return $databaseInstance;
128         }
129
130         /**
131          * Setter for the last read file
132          *
133          * @param       $infoInstance   The FQFN of the last read file
134          * @return      void
135          */
136         private final function setLastFile (SplFileInfo $infoInstance) {
137                 // Cast string and set it
138                 $this->lastFile = $infoInstance;
139         }
140
141         /**
142          * Getter for last read file
143          *
144          * @return      $lastFile       The last read file's name with full path
145          */
146         public final function getLastFile () {
147                 return $this->lastFile;
148         }
149
150         /**
151          * Setter for contents of the last read file
152          *
153          * @param               $contents       An array with header and data elements
154          * @return      void
155          */
156         private final function setLastFileContents (array $contents) {
157                 // Set array
158                 $this->lastContents = $contents;
159         }
160
161         /**
162          * Getter for last read file's content as an array
163          *
164          * @return      $lastContent    The array with elements 'header' and 'data'.
165          */
166         public final function getLastContents () {
167                 return $this->lastContents;
168         }
169
170         /**
171          * Getter for file extension
172          *
173          * @return      $fileExtension  The array with elements 'header' and 'data'.
174          */
175         public final function getFileExtension () {
176                 return $this->fileExtension;
177         }
178
179         /**
180          * Getter for index key
181          *
182          * @return      $indexKey       Index key
183          */
184         public final function getIndexKey () {
185                 return $this->indexKey;
186         }
187         /**
188          * Private getter for file IO instance
189          *
190          * @return      $fileIoInstance         An instance to the file I/O sub-system
191          */
192         protected final function getFileIoInstance () {
193                 return $this->fileIoInstance;
194         }
195
196         /**
197          * Setter for file I/O instance
198          *
199          * @param       $fileIoInstance         An instance to the file I/O sub-system
200          * @return      void
201          */
202         public final function setFileIoInstance (IoHandler $fileIoInstance) {
203                 $this->fileIoInstance = $fileIoInstance;
204         }
205
206         /**
207          * Setter for compressor channel
208          *
209          * @param       $compressorChannelInstance      An instance of a CompressorChannel class
210          * @return      void
211          */
212         protected final function setCompressorChannelInstance (CompressorChannel $compressorChannelInstance) {
213                 $this->compressorChannelInstance = $compressorChannelInstance;
214         }
215
216         /**
217          * Getter for compressor channel
218          *
219          * @return      $compressorChannelInstance      An instance of a CompressorChannel class
220          */
221         protected final function getCompressorChannelInstance () {
222                 return $this->compressorChannelInstance;
223         }
224
225         /**
226          * Reads a local data file  and returns it's contents in an array
227          *
228          * @param       $infoInstance   An instance of a SplFileInfo class
229          * @return      $dataArray
230          */
231         private function getDataArrayFromFile (SplFileInfo $infoInstance) {
232                 // Init compressed data
233                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Reading elements from database file ' . $infoInstance . ' ...');
234                 $compressedData = $this->getFileIoInstance()->loadFileContents($infoInstance);
235                 $compressedData = $compressedData['data'];
236
237                 // Decompress it
238                 $serializedData = $this->getCompressorChannelInstance()->getCompressor()->decompressStream($compressedData);
239
240                 // Unserialize it
241                 $dataArray = json_decode($serializedData, true);
242
243                 // Finally return it
244                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Read ' . count($dataArray) . ' elements from database file ' . $infoInstance . '.');
245                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: dataArray=' . print_r($dataArray, true));
246                 return $dataArray;
247         }
248
249         /**
250          * Writes data array to local file
251          *
252          * @param       $infoInstance   An instance of a SplFileInfo class
253          * @param       $dataArray      An array with all the data we shall write
254          * @return      void
255          */
256         private function writeDataArrayToFqfn (SplFileInfo $infoInstance, array $dataArray) {
257                 // Serialize and compress it
258                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Flushing ' . count($dataArray) . ' elements to database file ' . $infoInstance . ' ...');
259                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: dataArray=' . print_r($dataArray, true));
260                 $compressedData = $this->getCompressorChannelInstance()->getCompressor()->compressStream(json_encode($dataArray));
261
262                 // Write this data BASE64 encoded to the file
263                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing ' . strlen($compressedData) . ' bytes ...');
264                 $this->getFileIoInstance()->saveStreamToFile($infoInstance, $compressedData, $this);
265
266                 // Debug message
267                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Flushing ' . count($dataArray) . ' elements to database file completed.');
268         }
269
270         /**
271          * Getter for table information file contents or an empty if info file was not created
272          *
273          * @param       $dataSetInstance        An instance of a database set class
274          * @return      $infoArray                      An array with all table informations
275          */
276         private function getContentsFromTableInfoFile (StoreableCriteria $dataSetInstance) {
277                 // Default content is no data
278                 $infoArray = array();
279
280                 // Create FQFN for getting the table information file
281                 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
282
283                 // Get the file contents
284                 try {
285                         $infoArray = $this->getDataArrayFromFile($infoInstance);
286                 } catch (FileNotFoundException $e) {
287                         // Not found, so ignore it here
288                 }
289
290                 // ... and return it
291                 return $infoArray;
292         }
293
294         /**
295          * Generates a file info class from given dataset instance and string
296          *
297          * @param       $dataSetInstance        An instance of a database set class
298          * @param       $rowName                        Name of the row
299          * @return      $infoInstance           An instance of a SplFileInfo class
300          */
301         private function generateFileFromDataSet (Criteria $dataSetInstance, string $rowName) {
302                 // Instanciate new file object
303                 $infoInstance = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension());
304
305                 // Return it
306                 return $infoInstance;
307         }
308
309         /**
310          * Creates the table info file from given dataset instance
311          *
312          * @param       $dataSetInstance        An instance of a database set class
313          * @return      void
314          */
315         private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
316                 // Create FQFN for creating the table information file
317                 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
318
319                 // Get the data out from dataset in a local array
320                 $this->tableInfo[$dataSetInstance->getTableName()] = array(
321                         'primary'      => $dataSetInstance->getPrimaryKey(),
322                         'created'      => time(),
323                         'last_updated' => time()
324                 );
325
326                 // Write the data to the file
327                 $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$dataSetInstance->getTableName()]);
328         }
329
330         /**
331          * Updates the table info file from given dataset instance
332          *
333          * @param       $dataSetInstance        An instance of a database set class
334          * @return      void
335          */
336         private function updateTableInfoFile (StoreableCriteria $dataSetInstance) {
337                 // Get table name from criteria
338                 $tableName = $dataSetInstance->getTableName();
339
340                 // Create FQFN for creating the table information file
341                 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
342
343                 // Get the data out from dataset in a local array
344                 $this->tableInfo[$tableName]['primary']      = $dataSetInstance->getPrimaryKey();
345                 $this->tableInfo[$tableName]['last_updated'] = time();
346
347                 // Write the data to the file
348                 $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$tableName]);
349         }
350
351         /**
352          * Updates the primary key information or creates the table info file if not found
353          *
354          * @param       $dataSetInstance        An instance of a database set class
355          * @return      void
356          */
357         private function updatePrimaryKey (StoreableCriteria $dataSetInstance) {
358                 // Get table name from criteria
359                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataSetInstance=%s - CALLED!', $dataSetInstance->__toString()));
360                 $tableName = $dataSetInstance->getTableName();
361
362                 // Get the information array from lower method
363                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s', $tableName));
364                 $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance);
365
366                 // Is the primary key there?
367                 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableInfo=' . print_r($this->tableInfo, true));
368                 if (!isset($this->tableInfo[$tableName]['primary'])) {
369                         // Then create the info file
370                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Creating info table for tableName=%s ...', $tableName));
371                         $this->createTableInfoFile($dataSetInstance);
372                 } elseif ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo[$tableName]['primary'])) {
373                         // Set the array element
374                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting primaryKey=%s for tableName=%s ...', $dataSetInstance->getPrimaryKey(), $tableName));
375                         $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
376
377                         // Update the entry
378                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Updating info table for tableName=%s ...', $tableName));
379                         $this->updateTableInfoFile($dataSetInstance);
380                 }
381
382                 // Trace message
383                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: EXIT!');
384         }
385
386         /**
387          * Makes sure that the database connection is alive
388          *
389          * @return      void
390          * @todo        Do some checks on the database directory and files here
391          */
392         public function connectToDatabase () {
393         }
394
395         /**
396          * Starts a SELECT query on the database by given return type, table name
397          * and search criteria
398          *
399          * @param       $tableName                      Name of the database table
400          * @param       $searchInstance         Local search criteria class
401          * @return      $resultData                     Result data of the query
402          * @throws      UnsupportedCriteriaException    If the criteria is unsupported
403          * @throws      SqlException                                    If an 'SQL error' occurs
404          */
405         public function querySelect (string $tableName, LocalSearchCriteria $searchInstance) {
406                 // The result is null by any errors
407                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s,searchInstance=%s - CALLED!', $tableName, $searchInstance->__toString()));
408                 $resultData = NULL;
409
410                 // Create full path name
411                 $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
412
413                 /*
414                  * A 'select' query is not that easy on local files, so first try to
415                  * find the 'table' which is in fact a directory on the server
416                  */
417                 try {
418                         // Get a directory pointer instance
419                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Getting directory_class for pathName=%s ...', $pathName));
420                         $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
421
422                         // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
423                         $resultData = array(
424                                 BaseDatabaseBackend::RESULT_INDEX_STATUS => self::RESULT_OKAY,
425                                 BaseDatabaseBackend::RESULT_INDEX_ROWS   => array()
426                         );
427
428                         // Initialize limit/skip
429                         $limitFound = 0;
430                         $skipFound = 0;
431                         $idx = 1;
432
433                         // Read the directory with some exceptions
434                         while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
435                                 // Does the extension match?
436                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: fileInfoInstance->extension=%s,this->fileExtension=%s', $fileInfoInstance->getExtension(), $this->getFileExtension()));
437                                 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
438                                         // Skip this file!
439                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Skipping fileInfoInstance->filename=%s ...', $fileInfoInstance->getFilename()));
440                                         $directoryInstance->getDirectoryIteratorInstance()->next();
441                                         continue;
442                                 } // END - if
443
444                                 // Read the file
445                                 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
446
447                                 // Is this an array?
448                                 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
449                                 if (is_array($dataArray)) {
450                                         // Default is nothing found
451                                         $isFound = true;
452
453                                         // Search in the criteria with FMFW (First Matches, First Wins)
454                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataArray()=%d', count($dataArray)));
455                                         foreach ($dataArray as $key => $value) {
456                                                 // Make sure value is not bool
457                                                 assert(!is_bool($value));
458
459                                                 // Found one entry?
460                                                 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
461                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: key=%s,value[%s]=%s,isFound=%s', $key, gettype($value), $value, intval($isFound)));
462                                         } // END - foreach
463
464                                         // Is all found?
465                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: isFound=%d,limitFound=%d,limit=%d', intval($isFound), $limitFound, $searchInstance->getLimit()));
466                                         if ($isFound === true) {
467                                                 // Shall we skip this entry?
468                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: searchInstance->skip=%d', $searchInstance->getSkip()));
469                                                 if ($searchInstance->getSkip() > 0) {
470                                                         // We shall skip some entries
471                                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: skipFound=%s', $skipFound));
472                                                         if ($skipFound < $searchInstance->getSkip()) {
473                                                                 // Skip this entry
474                                                                 $skipFound++;
475                                                                 break;
476                                                         } // END - if
477                                                 } // END - if
478
479                                                 // Set id number
480                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting dataArray[%s]=%d', $this->getIndexKey(), $idx));
481                                                 $dataArray[$this->getIndexKey()] = $idx;
482
483                                                 // Entry found!
484                                                 array_push($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS], $dataArray);
485
486                                                 // Count found entries up
487                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: resultData[%s]()=%d', BaseDatabaseBackend::RESULT_INDEX_ROWS, count($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS])));
488                                                 $limitFound++;
489                                         } // END - if
490                                 } else {
491                                         // Throw an exception here
492                                         throw new SqlException(array($this, sprintf('File &#39;%s&#39; contains invalid data.', $fileInfoInstance->getPathname()), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
493                                 }
494
495                                 // Count entry up
496                                 $idx++;
497
498                                 // Advance to next entry
499                                 $directoryInstance->getDirectoryIteratorInstance()->next();
500                         } // END - while
501
502                         // Close directory and throw the instance away
503                         $directoryInstance->closeDirectory();
504                         unset($directoryInstance);
505
506                         // Reset last exception
507                         $this->resetLastException();
508                 } catch (PathIsNoDirectoryException $e) {
509                         // Path not found means "table not found" for real databases...
510                         $this->setLastException($e);
511
512                         // So throw an SqlException here with faked error message
513                         throw new SqlException (array($this, sprintf('Table &#39;%s&#39; not found', $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
514                 } catch (FrameworkException $e) {
515                         // Catch all exceptions and store them in last error
516                         $this->setLastException($e);
517                 }
518
519                 // Return the gathered result
520                 return $resultData;
521         }
522
523         /**
524          * "Inserts" a data set instance into a local file database folder
525          *
526          * @param       $dataSetInstance        A storeable data set
527          * @return      void
528          * @throws      SqlException    If an SQL error occurs
529          */
530         public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
531                 // Try to save the request away
532                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataSetInstance=%s - CALLED!', $dataSetInstance->__toString()));
533                 try {
534                         // Create full path name
535                         $infoInstance = $this->generateFileFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
536
537                         // Write the data away
538                         $this->writeDataArrayToFqfn($infoInstance, $dataSetInstance->getCriteriaArray());
539
540                         // Update the primary key
541                         $this->updatePrimaryKey($dataSetInstance);
542
543                         // Reset last exception
544                         $this->resetLastException();
545                 } catch (FrameworkException $e) {
546                         // Catch all exceptions and store them in last error
547                         $this->setLastException($e);
548
549                         // Throw an SQL exception
550                         throw new SqlException(array(
551                                         $this,
552                                         sprintf('Cannot write data to table &#39;%s&#39;, is the table created?', $dataSetInstance->getTableName()),
553                                         self::DB_CODE_TABLE_UNWRITEABLE
554                                 ),
555                                 self::EXCEPTION_SQL_QUERY
556                         );
557                 }
558
559                 // Trace message
560                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: EXIT!');
561         }
562
563         /**
564          * "Updates" a data set instance with a database layer
565          *
566          * @param       $dataSetInstance        An instance of a StorableCriteria class
567          * @return      void
568          * @throws      SqlException    If an SQL error occurs
569          */
570         public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
571                 // Create full path name
572                 $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR;
573
574                 // Try all the requests
575                 try {
576                         // Get a file pointer instance
577                         $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
578
579                         // Initialize limit/skip
580                         $limitFound = 0;
581                         $skipFound = 0;
582
583                         // Get the criteria array from the dataset
584                         $searchArray = $dataSetInstance->getCriteriaArray();
585
586                         // Get search criteria
587                         $searchInstance = $dataSetInstance->getSearchInstance();
588
589                         // Read the directory with some exceptions
590                         while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
591                                 // Does the extension match?
592                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
593                                 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
594                                         // Skip this file!
595                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
596                                         $directoryInstance->getDirectoryIteratorInstance()->next();
597                                         continue;
598                                 } // END - if
599
600                                 // Open this file for reading
601                                 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
602                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
603
604                                 // Is this an array?
605                                 if (is_array($dataArray)) {
606                                         // Default is nothing found
607                                         $isFound = true;
608
609                                         // Search in the criteria with FMFW (First Matches, First Wins)
610                                         foreach ($dataArray as $key => $value) {
611                                                 // Make sure value is not bool
612                                                 assert(!is_bool($value));
613
614                                                 // Found one entry?
615                                                 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
616                                         } // END - foreach
617
618                                         // Is all found?
619                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: isFound=' . intval($isFound));
620                                         if ($isFound === true) {
621                                                 // Shall we skip this entry?
622                                                 if ($searchInstance->getSkip() > 0) {
623                                                         // We shall skip some entries
624                                                         if ($skipFound < $searchInstance->getSkip()) {
625                                                                 // Skip this entry
626                                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Found entry, but skipping ...');
627                                                                 $skipFound++;
628                                                                 break;
629                                                         } // END - if
630                                                 } // END - if
631
632                                                 // Entry found, so update it
633                                                 foreach ($searchArray as $searchKey => $searchValue) {
634                                                         // Make sure the value is not bool again
635                                                         assert(!is_bool($searchValue));
636                                                         assert($searchKey != $this->indexKey);
637
638                                                         // Debug message + add/update it
639                                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
640                                                         $dataArray[$searchKey] = $searchValue;
641                                                 } // END - foreach
642
643                                                 // Write the data to a local file
644                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing dataArray()=' . count($dataArray) . ' to ' . $fileInfoInstance->getPathname() . ' ...');
645                                                 $this->writeDataArrayToFqfn($fileInfoInstance, $dataArray);
646
647                                                 // Count found entries up
648                                                 $limitFound++;
649                                         } // END - if
650                                 } // END - if
651
652                                 // Advance to next entry
653                                 $directoryInstance->getDirectoryIteratorInstance()->next();
654                         } // END - while
655
656                         // Close the file pointer
657                         $directoryInstance->closeDirectory();
658
659                         // Update the primary key
660                         $this->updatePrimaryKey($dataSetInstance);
661
662                         // Reset last exception
663                         $this->resetLastException();
664                 } catch (FrameworkException $e) {
665                         // Catch all exceptions and store them in last error
666                         $this->setLastException($e);
667
668                         // Throw an SQL exception
669                         throw new SqlException(array($this, sprintf('Cannot write data to table &#39;%s&#39;, is the table created? Exception: %s, message:%s', $dataSetInstance->getTableName(), $e->__toString(), $e->getMessage()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
670                 }
671         }
672
673         /**
674          * Getter for primary key of specified table or if not found null will be
675          * returned. This must be database-specific.
676          *
677          * @param       $tableName              Name of the table we need the primary key from
678          * @return      $primaryKey             Primary key column of the given table
679          * @todo        Rename method to getPrimaryKeyFromTableInfo()
680          */
681         public function getPrimaryKeyOfTable (string $tableName) {
682                 // Default key is null
683                 $primaryKey = NULL;
684
685                 // Does the table information exist?
686                 if (isset($this->tableInfo[$tableName])) {
687                         // Then return the primary key
688                         $primaryKey = $this->tableInfo[$tableName]['primary'];
689                 } // END - if
690
691                 // Return the column
692                 return $primaryKey;
693         }
694
695         /**
696          * Removes non-public data from given array.
697          *
698          * @param       $data   An array with possible non-public data that needs to be removed.
699          * @return      $data   A cleaned up array with only public data.
700          * @todo        Add more generic non-public data for removal
701          */
702         public function removeNonPublicDataFromArray (array $data) {
703                 // Remove '__idx'
704                 unset($data[$this->indexKey]);
705
706                 // Return it
707                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: data[' . gettype($data) . ']='.print_r($data, true));
708                 return $data;
709         }
710
711         /**
712          * Counts total rows of given table
713          *
714          * @param       $tableName      Table name
715          * @return      $count          Total rows of given table
716          */
717         public function countTotalRows (string $tableName) {
718                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ' - CALLED!');
719
720                 // Create full path name
721                 $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
722
723                 // Try all the requests
724                 try {
725                         // Get a file pointer instance
726                         $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
727
728                         // Initialize counter
729                         $count = 0;
730
731                         // Read the directory with some exceptions
732                         while ($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) {
733                                 // Does the extension match?
734                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
735                                 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
736                                         // Debug message
737                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
738                                         // Skip this file!
739                                         continue;
740                                 } // END - if
741
742                                 // Count this row up
743                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
744                                 $count++;
745                         } // END - while
746                 } catch (FrameworkException $e) {
747                         // Catch all exceptions and store them in last error
748                         $this->setLastException($e);
749
750                         // Throw an SQL exception
751                         throw new SqlException(array($this, sprintf('Cannot count on table &#39;%s&#39;, is the table created?', $dataSetInstance->getTableName()), self::DB_CODE_TABLE_NOT_FOUND), self::EXCEPTION_SQL_QUERY);
752                 }
753
754                 // Return count
755                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');
756                 return $count;
757         }
758
759 }