3 namespace Org\Mxchange\CoreFramework\Database\Backend\Lfdb;
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\Database\Sql\SqlException;
13 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
14 use Org\Mxchange\CoreFramework\Filesystem\FileNotFoundException;
15 use Org\Mxchange\CoreFramework\Generic\FrameworkException;
16 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
17 use Org\Mxchange\CoreFramework\Result\Database\BaseDatabaseResult;
18 use Org\Mxchange\CoreFramework\Traits\Compressor\Channel\CompressorChannelTrait;
19 use Org\Mxchange\CoreFramework\Traits\Handler\Io\IoHandlerTrait;
22 use \InvalidArgumentException;
26 * Database backend class for storing objects in locally created files.
28 * This class serializes arrays stored in the dataset instance and saves them
29 * to local files. Every file (except 'info') represents a single line. Every
30 * directory within the 'db' (base) directory represents a table.
32 * A configurable 'file_io_class' is being used as "storage backend".
34 * @author Roland Haeder <webmaster@shipsimu.org>
36 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
37 * @license GNU GPL 3.0 or any newer version
38 * @link http://www.shipsimu.org
40 * This program is free software: you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation, either version 3 of the License, or
43 * (at your option) any later version.
45 * This program is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU General Public License for more details.
50 * You should have received a copy of the GNU General Public License
51 * along with this program. If not, see <http://www.gnu.org/licenses/>.
53 class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
55 use CompressorChannelTrait;
59 * The file's extension
61 private $fileExtension = 'serialized';
64 * The last read file's name
66 private $lastFile = '';
69 * The last read file's content including header information
71 private $lastContents = [];
74 * Whether the "connection is already up
76 private $alreadyConnected = false;
79 * Table information array
81 private $tableInfo = [];
86 private $indexKey = '__idx';
89 * Cached file names based on table name to avoid "expensive" invocations
90 * of FrameworkConfiguration->getConfigEntry().
92 private $pathNames = [];
95 * The protected constructor. Do never instance from outside! You need to
96 * set a local file path. The class will then validate it.
100 private function __construct () {
101 // Call parent constructor
102 parent::__construct(__CLASS__);
106 * Create an object of CachedLocalFileDatabase and set the save path from
107 * configuration for local files.
109 * @return $databaseInstance An instance of CachedLocalFileDatabase
111 public static final function createCachedLocalFileDatabase () {
113 $databaseInstance = new CachedLocalFileDatabase();
115 // Set the compressor channel
116 $databaseInstance->setCompressorChannelInstance(ObjectFactory::createObjectByConfiguredName('compressor_channel_class'));
118 // Get a file IO handler and set it
119 $databaseInstance->setFileIoInstance(ObjectFactory::createObjectByConfiguredName('file_io_class'));
121 // "Connect" to the database
122 $databaseInstance->connectToDatabase();
124 // Return database instance
125 return $databaseInstance;
129 * Setter for the last read file
131 * @param $infoInstance The FQFN of the last read file
134 private final function setLastFile (SplFileInfo $infoInstance) {
135 // Cast string and set it
136 $this->lastFile = $infoInstance;
140 * Getter for last read file
142 * @return $lastFile The last read file's name with full path
144 public final function getLastFile () {
145 return $this->lastFile;
149 * Setter for contents of the last read file
151 * @param $contents An array with header and data elements
154 private final function setLastFileContents (array $contents) {
156 $this->lastContents = $contents;
160 * Getter for last read file's content as an array
162 * @return $lastContent The array with elements 'header' and 'data'.
164 public final function getLastContents () {
165 return $this->lastContents;
169 * Getter for file extension
171 * @return $fileExtension The array with elements 'header' and 'data'.
173 public final function getFileExtension () {
174 return $this->fileExtension;
178 * Getter for index key
180 * @return $indexKey Index key
182 public final function getIndexKey () {
183 return $this->indexKey;
187 * Reads a local data file and returns it's contents in an array
189 * @param $infoInstance An instance of a SplFileInfo class
192 private function getDataArrayFromFile (SplFileInfo $infoInstance) {
193 // Init compressed data
194 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Reading elements from database file ' . $infoInstance . ' ...');
195 $compressedData = $this->getFileIoInstance()->loadFileContents($infoInstance);
196 $compressedData = $compressedData['data'];
199 $serializedData = $this->getCompressorChannelInstance()->getCompressor()->decompressStream($compressedData);
202 $dataArray = json_decode($serializedData, true);
205 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Read ' . count($dataArray) . ' elements from database file ' . $infoInstance . '.');
206 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: dataArray=' . print_r($dataArray, true));
211 * Writes data array to local file
213 * @param $infoInstance An instance of a SplFileInfo class
214 * @param $dataArray An array with all the data we shall write
217 private function writeDataArrayToFqfn (SplFileInfo $infoInstance, array $dataArray) {
218 // Serialize and compress it
219 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Flushing ' . count($dataArray) . ' elements to database file ' . $infoInstance . ' ...');
220 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: dataArray=' . print_r($dataArray, true));
221 $compressedData = $this->getCompressorChannelInstance()->getCompressor()->compressStream(json_encode($dataArray));
223 // Write this data BASE64 encoded to the file
224 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing ' . strlen($compressedData) . ' bytes ...');
225 $this->getFileIoInstance()->saveStreamToFile($infoInstance, $compressedData, $this);
228 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Flushing ' . count($dataArray) . ' elements to database file completed.');
232 * Getter for table information file contents or an empty if info file was not created
234 * @param $dataSetInstance An instance of a database set class
235 * @return $infoArray An array with all table informations
237 private function getContentsFromTableInfoFile (StoreableCriteria $dataSetInstance) {
238 // Default content is no data
241 // Create FQFN for getting the table information file
242 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
244 // Get the file contents
246 $infoArray = $this->getDataArrayFromFile($infoInstance);
247 } catch (FileNotFoundException $e) {
248 // Not found, so ignore it here
256 * Generates a file info class from given dataset instance and string
258 * @param $dataSetInstance An instance of a database set class
259 * @param $rowName Name of the row
260 * @return $infoInstance An instance of a SplFileInfo class
262 private function generateFileFromDataSet (Criteria $dataSetInstance, string $rowName) {
263 // Instanciate new file object
264 $infoInstance = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension());
267 return $infoInstance;
271 * Creates the table info file from given dataset instance
273 * @param $dataSetInstance An instance of a database set class
276 private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
277 // Create FQFN for creating the table information file
278 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
280 // Get the data out from dataset in a local array
281 $this->tableInfo[$dataSetInstance->getTableName()] = [
282 'primary' => $dataSetInstance->getPrimaryKey(),
284 'last_updated' => time(),
287 // Write the data to the file
288 $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$dataSetInstance->getTableName()]);
292 * Updates the table info file from given dataset instance
294 * @param $dataSetInstance An instance of a database set class
297 private function updateTableInfoFile (StoreableCriteria $dataSetInstance) {
298 // Get table name from criteria
299 $tableName = $dataSetInstance->getTableName();
301 // Create FQFN for creating the table information file
302 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
304 // Get the data out from dataset in a local array
305 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
306 $this->tableInfo[$tableName]['last_updated'] = time();
308 // Write the data to the file
309 $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$tableName]);
313 * Updates the primary key information or creates the table info file if not found
315 * @param $dataSetInstance An instance of a database set class
318 private function updatePrimaryKey (StoreableCriteria $dataSetInstance) {
319 // Get table name from criteria
320 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataSetInstance=%s - CALLED!', $dataSetInstance->__toString()));
321 $tableName = $dataSetInstance->getTableName();
323 // Get the information array from lower method
324 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s', $tableName));
325 $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance);
327 // Is the primary key there?
328 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableInfo=' . print_r($this->tableInfo, true));
329 if (!isset($this->tableInfo[$tableName]['primary'])) {
330 // Then create the info file
331 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Creating info table for tableName=%s ...', $tableName));
332 $this->createTableInfoFile($dataSetInstance);
333 } elseif (FrameworkBootstrap::getConfigurationInstance()->isEnabled('db_update_primary_forced') && $dataSetInstance->getPrimaryKey() != $this->tableInfo[$tableName]['primary']) {
334 // Set the array element
335 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting primaryKey=%s for tableName=%s ...', $dataSetInstance->getPrimaryKey(), $tableName));
336 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
339 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Updating info table for tableName=%s ...', $tableName));
340 $this->updateTableInfoFile($dataSetInstance);
344 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: EXIT!');
348 * Makes sure that the database connection is alive
351 * @todo Do some checks on the database directory and files here
353 public function connectToDatabase () {
357 * Starts a SELECT query on the database by given return type, table name
358 * and search criteria
360 * @param $tableName Name of the database table
361 * @param $searchInstance Local search criteria class
362 * @return $resultData Result data of the query
363 * @throws InvalidArgumentException If a parameter is not valid
364 * @throws UnsupportedCriteriaException If the criteria is unsupported
365 * @throws SqlException If an 'SQL error' occurs
367 public function querySelect (string $tableName, LocalSearchCriteria $searchInstance) {
368 // Validate parameter
369 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s,searchInstance=%s - CALLED!', $tableName, $searchInstance->__toString()));
370 if (empty($tableName)) {
372 throw new InvalidArgumentException('Parameter "tableName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
373 } elseif (!isset($this->pathNames[$tableName])) {
374 // "Cache" is not present, so create and assign it
375 $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
378 // The result is null by any errors
382 * A 'select' query is not that easy on local files, so first try to
383 * find the 'table' which is in fact a directory on the server
386 // Get a directory pointer instance
387 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Getting directory_class for this->pathNames[%s]=%s ...', $tableName, $this->pathNames[$tableName]));
388 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
390 // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
392 BaseDatabaseResult::RESULT_NAME_STATUS => self::RESULT_OKAY,
393 BaseDatabaseResult::RESULT_NAME_ROWS => [],
394 BaseDatabaseResult::RESULT_NAME_EXCEPTION => NULL,
397 // Initialize limit/skip
402 // Read the directory with some exceptions
403 while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(['.gitkeep', 'info.' . $this->getFileExtension()])) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
404 // Does the extension match?
405 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: fileInfoInstance->extension=%s,this->fileExtension=%s', $fileInfoInstance->getExtension(), $this->getFileExtension()));
406 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
408 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Skipping fileInfoInstance->filename=%s ...', $fileInfoInstance->getFilename()));
409 $directoryInstance->getDirectoryIteratorInstance()->next();
414 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
417 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
418 if (is_array($dataArray)) {
419 // Default is nothing found
422 // Search in the criteria with FMFW (First Matches, First Wins)
423 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: data[]=%d', count($dataArray)));
424 foreach ($dataArray as $key => $value) {
426 $isFound = ($isFound && $searchInstance->isCriteriaMatching($key, $value));
427 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: key=%s,value[%s]=%s,isFound=%s', $key, gettype($value), $value, intval($isFound)));
431 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: isFound=%d,limitFound=%d,limit=%d', intval($isFound), $limitFound, $searchInstance->getLimit()));
432 if ($isFound === true) {
433 // Shall we skip this entry?
434 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: searchInstance->skip=%d,skipFound=%d', $searchInstance->getSkip(), $skipFound));
435 if ($searchInstance->getSkip() > 0 && $skipFound < $searchInstance->getSkip()) {
442 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting dataArray[%s]=%d', $this->getIndexKey(), $idx));
443 $dataArray[$this->getIndexKey()] = $idx;
446 array_push($resultData[BaseDatabaseResult::RESULT_NAME_ROWS], $dataArray);
448 // Count found entries up
449 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: resultData[%s]()=%d', BaseDatabaseResult::RESULT_NAME_ROWS, count($resultData[BaseDatabaseResult::RESULT_NAME_ROWS])));
453 // Throw an exception here
454 throw new SqlException(array($this, sprintf('File '%s' contains invalid data.', $fileInfoInstance->getPathname()), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
460 // Advance to next entry
461 $directoryInstance->getDirectoryIteratorInstance()->next();
464 // Close directory and throw the instance away
465 $directoryInstance->closeDirectory();
466 unset($directoryInstance);
468 // Reset last exception
469 $this->resetLastException();
470 } catch (PathIsNoDirectoryException $e) {
471 // Path not found means "table not found" for real databases...
472 $this->setLastException($e);
474 // So throw an SqlException here with faked error message
475 throw new SqlException (array($this, sprintf('Table '%s' not found', $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
476 } catch (FrameworkException $e) {
477 // Catch all exceptions and store them in last error
478 $this->setLastException($e);
481 // Return the gathered result
486 * "Inserts" a data set instance into a local file database folder
488 * @param $dataSetInstance A storeable data set
490 * @throws SqlException If an SQL error occurs
492 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
493 // Try to save the request away
494 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataSetInstance=%s - CALLED!', $dataSetInstance->__toString()));
496 // Create full path name
497 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
499 // Write the data away
500 $this->writeDataArrayToFqfn($infoInstance, $dataSetInstance->getCriteriaArray());
502 // Update the primary key
503 $this->updatePrimaryKey($dataSetInstance);
505 // Reset last exception
506 $this->resetLastException();
507 } catch (FrameworkException $e) {
508 // Catch all exceptions and store them in last error
509 $this->setLastException($e);
511 // Throw an SQL exception
512 throw new SqlException([
514 sprintf('Cannot write data to table '%s', is the table created? e=%s,e->message=%s',
515 $dataSetInstance->getTableName(),
519 self::DB_CODE_TABLE_UNWRITEABLE
521 self::EXCEPTION_SQL_QUERY
526 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: EXIT!');
530 * "Updates" a data set instance with a database layer
532 * @param $dataSetInstance An instance of a StorableCriteria class
534 * @throws InvalidArgumentException If $tableName is empty
535 * @throws SqlException If an SQL error occurs
537 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
539 $tableName = $dataSetInstance->getTableName();
542 if (empty($tableName)) {
543 // Should never be an empty string
544 throw new InvalidArgumentException('Class field dataSetInstance->tableName is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
545 } elseif (!isset($this->pathNames[$tableName])) {
546 // "Cache" is not present, so create and assign it
547 $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
550 // Try all the requests
552 // Get a file pointer instance
553 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
555 // Initialize limit/skip
559 // Get the criteria array from the dataset
560 $searchArray = $dataSetInstance->getCriteriaArray();
562 // Get search criteria
563 $searchInstance = $dataSetInstance->getSearchInstance();
565 // Read the directory with some exceptions
566 while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(['.gitkeep', 'info.' . $this->getFileExtension()])) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
567 // Does the extension match?
568 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
569 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
571 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
572 $directoryInstance->getDirectoryIteratorInstance()->next();
576 // Open this file for reading
577 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
578 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
581 if (is_array($dataArray)) {
582 // Default is nothing found
585 // Search in the criteria with FMFW (First Matches, First Wins)
586 foreach ($dataArray as $key => $value) {
588 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
592 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: isFound=' . intval($isFound));
593 if ($isFound === true) {
594 // Shall we skip this entry?
595 if ($searchInstance->getSkip() > 0) {
596 // We shall skip some entries
597 if ($skipFound < $searchInstance->getSkip()) {
599 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Found entry, but skipping ...');
605 // Entry found, so update it
606 foreach ($searchArray as $searchKey => $searchValue) {
607 // Make sure the value is not bool again
608 assert(!is_bool($searchValue));
609 assert($searchKey != $this->indexKey);
611 // Debug message + add/update it
612 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
613 $dataArray[$searchKey] = $searchValue;
616 // Write the data to a local file
617 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing data[]=' . count($dataArray) . ' to ' . $fileInfoInstance->getPathname() . ' ...');
618 $this->writeDataArrayToFqfn($fileInfoInstance, $dataArray);
620 // Count found entries up
625 // Advance to next entry
626 $directoryInstance->getDirectoryIteratorInstance()->next();
629 // Close the file pointer
630 $directoryInstance->closeDirectory();
632 // Update the primary key
633 $this->updatePrimaryKey($dataSetInstance);
635 // Reset last exception
636 $this->resetLastException();
637 } catch (FrameworkException $e) {
638 // Catch all exceptions and store them in last error
639 $this->setLastException($e);
641 // Throw an SQL exception
642 throw new SqlException(array($this, sprintf('Cannot write data to table '%s', is the table created? Exception: %s, message:%s', $dataSetInstance->getTableName(), $e->__toString(), $e->getMessage()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
647 * Getter for primary key of specified table or if not found null will be
648 * returned. This must be database-specific.
650 * @param $tableName Name of the table we need the primary key from
651 * @return $primaryKey Primary key column of the given table
652 * @throws InvalidArgumentException If a parameter is not valid
653 * @todo Rename method to getPrimaryKeyFromTableInfo()
655 public function getPrimaryKeyOfTable (string $tableName) {
656 // Validate parameter
657 if (empty($tableName)) {
659 throw new InvalidArgumentException('Parameter "tableName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
662 // Default key is null
665 // Does the table information exist?
666 if (isset($this->tableInfo[$tableName])) {
667 // Then return the primary key
668 $primaryKey = $this->tableInfo[$tableName]['primary'];
676 * Removes non-public data from given array.
678 * @param $data An array with possible non-public data that needs to be removed.
679 * @return $data A cleaned up array with only public data.
680 * @todo Add more generic non-public data for removal
682 public function removeNonPublicDataFromArray (array $data) {
684 unset($data[$this->indexKey]);
687 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: data[' . gettype($data) . ']='.print_r($data, true));
692 * Counts total rows of given table
694 * @param $tableName Table name
695 * @return $count Total rows of given table
696 * @throws InvalidArgumentException If a parameter is not valid
698 public function countTotalRows (string $tableName) {
699 // Validate parameter
700 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ' - CALLED!');
701 if (empty($tableName)) {
703 throw new InvalidArgumentException('Parameter "tableName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
704 } elseif (!isset($this->pathNames[$tableName])) {
705 // "Cache" is not present, so create and assign it
706 $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
709 // Try all the requests
710 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: this->pathNames[%s]=%s', $tableName, $this->pathNames[$tableName]));
712 // Get a file pointer instance
713 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
715 // Initialize counter
718 // Read the directory with some exceptions
719 while ($fileInfoInstance = $directoryInstance->readDirectoryExcept(['.gitkeep', 'info.' . $this->getFileExtension()])) {
720 // Does the extension match?
721 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
722 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
724 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
730 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
733 } catch (FrameworkException $e) {
734 // Catch all exceptions and store them in last error
735 $this->setLastException($e);
737 // Throw an SQL exception
738 throw new SqlException(array($this, sprintf('Cannot count on table '%s', is the table created?', $dataSetInstance->getTableName()), self::DB_CODE_TABLE_NOT_FOUND), self::EXCEPTION_SQL_QUERY);
742 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');