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\Factory\Object\ObjectFactory;
13 use Org\Mxchange\CoreFramework\Filesystem\FileNotFoundException;
14 use Org\Mxchange\CoreFramework\Generic\FrameworkException;
15 use Org\Mxchange\CoreFramework\Result\Database\BaseDatabaseResult;
16 use Org\Mxchange\CoreFramework\Traits\Compressor\Channel\CompressorChannelTrait;
17 use Org\Mxchange\CoreFramework\Traits\Handler\Io\IoHandlerTrait;
20 use \InvalidArgumentException;
22 use \UnexpectedValueException;
25 * Database backend class for storing objects in locally created files.
27 * This class serializes arrays stored in the dataset instance and saves them
28 * to local files. Every file (except 'info') represents a single line. Every
29 * directory within the 'db' (base) directory represents a table.
31 * A configurable 'file_io_class' is being used as "storage backend".
33 * @author Roland Haeder <webmaster@shipsimu.org>
35 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
36 * @license GNU GPL 3.0 or any newer version
37 * @link http://www.shipsimu.org
39 * This program is free software: you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation, either version 3 of the License, or
42 * (at your option) any later version.
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
49 * You should have received a copy of the GNU General Public License
50 * along with this program. If not, see <http://www.gnu.org/licenses/>.
52 class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
54 use CompressorChannelTrait;
58 * The file's extension
60 private $fileExtension = 'serialized';
63 * The last read file's name
65 private $lastFile = '';
68 * The last read file's content including header information
70 private $lastContents = [];
73 * Whether the "connection is already up
75 private $alreadyConnected = false;
78 * Table information array
80 private $tableInfo = [];
85 private $indexKey = '__idx';
88 * Cached file names based on table name to avoid "expensive" invocations
89 * of FrameworkConfiguration->getConfigEntry().
91 private $pathNames = [];
94 * The protected constructor. Do never instance from outside! You need to
95 * set a local file path. The class will then validate it.
99 private function __construct () {
100 // Call parent constructor
101 parent::__construct(__CLASS__);
105 * Create an object of CachedLocalFileDatabase and set the save path from
106 * configuration for local files.
108 * @return $databaseInstance An instance of CachedLocalFileDatabase
110 public static final function createCachedLocalFileDatabase () {
112 $databaseInstance = new CachedLocalFileDatabase();
114 // Set the compressor channel
115 $databaseInstance->setCompressorChannelInstance(ObjectFactory::createObjectByConfiguredName('compressor_channel_class'));
117 // Get a file IO handler and set it
118 $databaseInstance->setFileIoInstance(ObjectFactory::createObjectByConfiguredName('file_io_class'));
120 // "Connect" to the database
121 $databaseInstance->connectToDatabase();
123 // Return database instance
124 return $databaseInstance;
128 * Setter for the last read file
130 * @param $infoInstance The FQFN of the last read file
133 private final function setLastFile (SplFileInfo $infoInstance) {
134 // Cast string and set it
135 $this->lastFile = $infoInstance;
139 * Getter for last read file
141 * @return $lastFile The last read file's name with full path
143 public final function getLastFile () {
144 return $this->lastFile;
148 * Setter for contents of the last read file
150 * @param $contents An array with header and data elements
153 private final function setLastFileContents (array $contents) {
155 $this->lastContents = $contents;
159 * Getter for last read file's content as an array
161 * @return $lastContent The array with elements 'header' and 'data'.
163 public final function getLastContents () {
164 return $this->lastContents;
168 * Getter for file extension
170 * @return $fileExtension The array with elements 'header' and 'data'.
172 public final function getFileExtension () {
173 return $this->fileExtension;
177 * Getter for index key
179 * @return $indexKey Index key
181 public final function getIndexKey () {
182 return $this->indexKey;
186 * Reads a local data file and returns it's contents in an array
188 * @param $infoInstance An instance of a SplFileInfo class
191 private function getDataArrayFromFile (SplFileInfo $infoInstance) {
192 // Init compressed data
193 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Reading elements from database file ' . $infoInstance . ' ...');
194 $compressedData = $this->getFileIoInstance()->loadFileContents($infoInstance);
195 $compressedData = $compressedData['data'];
198 $serializedData = $this->getCompressorChannelInstance()->getCompressor()->decompressStream($compressedData);
201 $dataArray = json_decode($serializedData, true);
204 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Read ' . count($dataArray) . ' elements from database file ' . $infoInstance . '.');
205 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: dataArray=' . print_r($dataArray, true));
210 * Writes data array to local file
212 * @param $infoInstance An instance of a SplFileInfo class
213 * @param $dataArray An array with all the data we shall write
216 private function writeDataArrayToFqfn (SplFileInfo $infoInstance, array $dataArray) {
217 // Serialize and compress it
218 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Flushing ' . count($dataArray) . ' elements to database file ' . $infoInstance . ' ...');
219 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: dataArray=' . print_r($dataArray, true));
220 $compressedData = $this->getCompressorChannelInstance()->getCompressor()->compressStream(json_encode($dataArray));
222 // Write this data BASE64 encoded to the file
223 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing ' . strlen($compressedData) . ' bytes ...');
224 $this->getFileIoInstance()->saveStreamToFile($infoInstance, $compressedData, $this);
227 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Flushing ' . count($dataArray) . ' elements to database file completed.');
231 * Getter for table information file contents or an empty if info file was not created
233 * @param $dataSetInstance An instance of a database set class
234 * @return $infoArray An array with all table informations
236 private function getContentsFromTableInfoFile (StoreableCriteria $dataSetInstance) {
237 // Default content is no data
240 // Create FQFN for getting the table information file
241 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
243 // Get the file contents
245 $infoArray = $this->getDataArrayFromFile($infoInstance);
246 } catch (FileNotFoundException $e) {
247 // Not found, so ignore it here
255 * Generates a file info class from given dataset instance and string
257 * @param $dataSetInstance An instance of a database set class
258 * @param $rowName Name of the row
259 * @return $infoInstance An instance of a SplFileInfo class
261 private function generateFileFromDataSet (Criteria $dataSetInstance, string $rowName) {
262 // Instanciate new file object
263 $infoInstance = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension());
266 return $infoInstance;
270 * Creates the table info file from given dataset instance
272 * @param $dataSetInstance An instance of a database set class
275 private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
276 // Create FQFN for creating the table information file
277 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
279 // Get the data out from dataset in a local array
280 $this->tableInfo[$dataSetInstance->getTableName()] = array(
281 'primary' => $dataSetInstance->getPrimaryKey(),
283 'last_updated' => time()
286 // Write the data to the file
287 $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$dataSetInstance->getTableName()]);
291 * Updates the table info file from given dataset instance
293 * @param $dataSetInstance An instance of a database set class
296 private function updateTableInfoFile (StoreableCriteria $dataSetInstance) {
297 // Get table name from criteria
298 $tableName = $dataSetInstance->getTableName();
300 // Create FQFN for creating the table information file
301 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
303 // Get the data out from dataset in a local array
304 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
305 $this->tableInfo[$tableName]['last_updated'] = time();
307 // Write the data to the file
308 $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$tableName]);
312 * Updates the primary key information or creates the table info file if not found
314 * @param $dataSetInstance An instance of a database set class
317 private function updatePrimaryKey (StoreableCriteria $dataSetInstance) {
318 // Get table name from criteria
319 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataSetInstance=%s - CALLED!', $dataSetInstance->__toString()));
320 $tableName = $dataSetInstance->getTableName();
322 // Get the information array from lower method
323 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s', $tableName));
324 $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance);
326 // Is the primary key there?
327 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableInfo=' . print_r($this->tableInfo, true));
328 if (!isset($this->tableInfo[$tableName]['primary'])) {
329 // Then create the info file
330 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Creating info table for tableName=%s ...', $tableName));
331 $this->createTableInfoFile($dataSetInstance);
332 } elseif ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo[$tableName]['primary'])) {
333 // Set the array element
334 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting primaryKey=%s for tableName=%s ...', $dataSetInstance->getPrimaryKey(), $tableName));
335 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
338 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Updating info table for tableName=%s ...', $tableName));
339 $this->updateTableInfoFile($dataSetInstance);
343 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: EXIT!');
347 * Makes sure that the database connection is alive
350 * @todo Do some checks on the database directory and files here
352 public function connectToDatabase () {
356 * Starts a SELECT query on the database by given return type, table name
357 * and search criteria
359 * @param $tableName Name of the database table
360 * @param $searchInstance Local search criteria class
361 * @return $resultData Result data of the query
362 * @throws InvalidArgumentException If a parameter is not valid
363 * @throws UnsupportedCriteriaException If the criteria is unsupported
364 * @throws SqlException If an 'SQL error' occurs
366 public function querySelect (string $tableName, LocalSearchCriteria $searchInstance) {
367 // Validate parameter
368 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s,searchInstance=%s - CALLED!', $tableName, $searchInstance->__toString()));
369 if (empty($tableName)) {
371 throw new InvalidArgumentException('Parameter "tableName" is empty');
372 } elseif (!isset($this->pathNames[$tableName])) {
373 // "Cache" is not present, so create and assign it
374 $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
377 // The result is null by any errors
381 * A 'select' query is not that easy on local files, so first try to
382 * find the 'table' which is in fact a directory on the server
385 // Get a directory pointer instance
386 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Getting directory_class for this->pathNames[%s]=%s ...', $tableName, $this->pathNames[$tableName]));
387 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
389 // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
391 BaseDatabaseResult::RESULT_NAME_STATUS => self::RESULT_OKAY,
392 BaseDatabaseResult::RESULT_NAME_ROWS => []
395 // Initialize limit/skip
400 // Read the directory with some exceptions
401 while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(['.gitkeep', 'info.' . $this->getFileExtension()])) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
402 // Does the extension match?
403 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: fileInfoInstance->extension=%s,this->fileExtension=%s', $fileInfoInstance->getExtension(), $this->getFileExtension()));
404 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
406 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Skipping fileInfoInstance->filename=%s ...', $fileInfoInstance->getFilename()));
407 $directoryInstance->getDirectoryIteratorInstance()->next();
412 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
415 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
416 if (is_array($dataArray)) {
417 // Default is nothing found
420 // Search in the criteria with FMFW (First Matches, First Wins)
421 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: data[]=%d', count($dataArray)));
422 foreach ($dataArray as $key => $value) {
424 $isFound = ($isFound && $searchInstance->isCriteriaMatching($key, $value));
425 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: key=%s,value[%s]=%s,isFound=%s', $key, gettype($value), $value, intval($isFound)));
429 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: isFound=%d,limitFound=%d,limit=%d', intval($isFound), $limitFound, $searchInstance->getLimit()));
430 if ($isFound === true) {
431 // Shall we skip this entry?
432 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: searchInstance->skip=%d,skipFound=%d', $searchInstance->getSkip(), $skipFound));
433 if ($searchInstance->getSkip() > 0 && $skipFound < $searchInstance->getSkip()) {
440 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting dataArray[%s]=%d', $this->getIndexKey(), $idx));
441 $dataArray[$this->getIndexKey()] = $idx;
444 array_push($resultData[BaseDatabaseResult::RESULT_NAME_ROWS], $dataArray);
446 // Count found entries up
447 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: resultData[%s]()=%d', BaseDatabaseResult::RESULT_NAME_ROWS, count($resultData[BaseDatabaseResult::RESULT_NAME_ROWS])));
451 // Throw an exception here
452 throw new SqlException(array($this, sprintf('File '%s' contains invalid data.', $fileInfoInstance->getPathname()), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
458 // Advance to next entry
459 $directoryInstance->getDirectoryIteratorInstance()->next();
462 // Close directory and throw the instance away
463 $directoryInstance->closeDirectory();
464 unset($directoryInstance);
466 // Reset last exception
467 $this->resetLastException();
468 } catch (PathIsNoDirectoryException $e) {
469 // Path not found means "table not found" for real databases...
470 $this->setLastException($e);
472 // So throw an SqlException here with faked error message
473 throw new SqlException (array($this, sprintf('Table '%s' not found', $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
474 } catch (FrameworkException $e) {
475 // Catch all exceptions and store them in last error
476 $this->setLastException($e);
479 // Return the gathered result
484 * "Inserts" a data set instance into a local file database folder
486 * @param $dataSetInstance A storeable data set
488 * @throws SqlException If an SQL error occurs
490 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
491 // Try to save the request away
492 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataSetInstance=%s - CALLED!', $dataSetInstance->__toString()));
494 // Create full path name
495 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
497 // Write the data away
498 $this->writeDataArrayToFqfn($infoInstance, $dataSetInstance->getCriteriaArray());
500 // Update the primary key
501 $this->updatePrimaryKey($dataSetInstance);
503 // Reset last exception
504 $this->resetLastException();
505 } catch (FrameworkException $e) {
506 // Catch all exceptions and store them in last error
507 $this->setLastException($e);
509 // Throw an SQL exception
510 throw new SqlException(array(
512 sprintf('Cannot write data to table '%s', is the table created?', $dataSetInstance->getTableName()),
513 self::DB_CODE_TABLE_UNWRITEABLE
515 self::EXCEPTION_SQL_QUERY
520 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: EXIT!');
524 * "Updates" a data set instance with a database layer
526 * @param $dataSetInstance An instance of a StorableCriteria class
528 * @throws UnexpectedValueException If $tableName is empty
529 * @throws SqlException If an SQL error occurs
531 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
533 $tableName = $dataSetInstance->getTableName();
536 if (empty($tableName)) {
537 // Should never be an empty string
538 throw new UnexpectedValueException('Class field dataSetInstance->tableName is empty');
539 } elseif (!isset($this->pathNames[$tableName])) {
540 // "Cache" is not present, so create and assign it
541 $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
544 // Try all the requests
546 // Get a file pointer instance
547 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
549 // Initialize limit/skip
553 // Get the criteria array from the dataset
554 $searchArray = $dataSetInstance->getCriteriaArray();
556 // Get search criteria
557 $searchInstance = $dataSetInstance->getSearchInstance();
559 // Read the directory with some exceptions
560 while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(['.gitkeep', 'info.' . $this->getFileExtension()])) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
561 // Does the extension match?
562 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
563 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
565 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
566 $directoryInstance->getDirectoryIteratorInstance()->next();
570 // Open this file for reading
571 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
572 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
575 if (is_array($dataArray)) {
576 // Default is nothing found
579 // Search in the criteria with FMFW (First Matches, First Wins)
580 foreach ($dataArray as $key => $value) {
582 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
586 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: isFound=' . intval($isFound));
587 if ($isFound === true) {
588 // Shall we skip this entry?
589 if ($searchInstance->getSkip() > 0) {
590 // We shall skip some entries
591 if ($skipFound < $searchInstance->getSkip()) {
593 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Found entry, but skipping ...');
599 // Entry found, so update it
600 foreach ($searchArray as $searchKey => $searchValue) {
601 // Make sure the value is not bool again
602 assert(!is_bool($searchValue));
603 assert($searchKey != $this->indexKey);
605 // Debug message + add/update it
606 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
607 $dataArray[$searchKey] = $searchValue;
610 // Write the data to a local file
611 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing data[]=' . count($dataArray) . ' to ' . $fileInfoInstance->getPathname() . ' ...');
612 $this->writeDataArrayToFqfn($fileInfoInstance, $dataArray);
614 // Count found entries up
619 // Advance to next entry
620 $directoryInstance->getDirectoryIteratorInstance()->next();
623 // Close the file pointer
624 $directoryInstance->closeDirectory();
626 // Update the primary key
627 $this->updatePrimaryKey($dataSetInstance);
629 // Reset last exception
630 $this->resetLastException();
631 } catch (FrameworkException $e) {
632 // Catch all exceptions and store them in last error
633 $this->setLastException($e);
635 // Throw an SQL exception
636 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);
641 * Getter for primary key of specified table or if not found null will be
642 * returned. This must be database-specific.
644 * @param $tableName Name of the table we need the primary key from
645 * @return $primaryKey Primary key column of the given table
646 * @throws InvalidArgumentException If a parameter is not valid
647 * @todo Rename method to getPrimaryKeyFromTableInfo()
649 public function getPrimaryKeyOfTable (string $tableName) {
650 // Validate parameter
651 if (empty($tableName)) {
653 throw new InvalidArgumentException('Parameter "tableName" is empty');
656 // Default key is null
659 // Does the table information exist?
660 if (isset($this->tableInfo[$tableName])) {
661 // Then return the primary key
662 $primaryKey = $this->tableInfo[$tableName]['primary'];
670 * Removes non-public data from given array.
672 * @param $data An array with possible non-public data that needs to be removed.
673 * @return $data A cleaned up array with only public data.
674 * @todo Add more generic non-public data for removal
676 public function removeNonPublicDataFromArray (array $data) {
678 unset($data[$this->indexKey]);
681 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: data[' . gettype($data) . ']='.print_r($data, true));
686 * Counts total rows of given table
688 * @param $tableName Table name
689 * @return $count Total rows of given table
690 * @throws InvalidArgumentException If a parameter is not valid
692 public function countTotalRows (string $tableName) {
693 // Validate parameter
694 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ' - CALLED!');
695 if (empty($tableName)) {
697 throw new InvalidArgumentException('Parameter "tableName" is empty');
698 } elseif (!isset($this->pathNames[$tableName])) {
699 // "Cache" is not present, so create and assign it
700 $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
703 // Try all the requests
704 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: this->pathNames[%s]=%s', $tableName, $this->pathNames[$tableName]));
706 // Get a file pointer instance
707 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
709 // Initialize counter
712 // Read the directory with some exceptions
713 while ($fileInfoInstance = $directoryInstance->readDirectoryExcept(['.gitkeep', 'info.' . $this->getFileExtension()])) {
714 // Does the extension match?
715 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
716 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
718 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
724 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
727 } catch (FrameworkException $e) {
728 // Catch all exceptions and store them in last error
729 $this->setLastException($e);
731 // Throw an SQL exception
732 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);
736 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');