3 namespace Org\Mxchange\CoreFramework\Database\Backend\Lfdb;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Criteria\Criteria;
7 use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
8 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
9 use Org\Mxchange\CoreFramework\Database\Backend\BaseDatabaseBackend;
10 use Org\Mxchange\CoreFramework\Database\Backend\DatabaseBackend;
11 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
12 use Org\Mxchange\CoreFramework\Filesystem\FileNotFoundException;
13 use Org\Mxchange\CoreFramework\Generic\FrameworkException;
14 use Org\Mxchange\CoreFramework\Handler\Stream\IoHandler;
20 * Database backend class for storing objects in locally created files.
22 * This class serializes arrays stored in the dataset instance and saves them
23 * to local files. Every file (except 'info') represents a single line. Every
24 * directory within the 'db' (base) directory represents a table.
26 * A configurable 'file_io_class' is being used as "storage backend".
28 * @author Roland Haeder <webmaster@shipsimu.org>
30 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
31 * @license GNU GPL 3.0 or any newer version
32 * @link http://www.shipsimu.org
34 * This program is free software: you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation, either version 3 of the License, or
37 * (at your option) any later version.
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
44 * You should have received a copy of the GNU General Public License
45 * along with this program. If not, see <http://www.gnu.org/licenses/>.
47 class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
49 * The file's extension
51 private $fileExtension = 'serialized';
54 * The last read file's name
56 private $lastFile = '';
59 * The last read file's content including header information
61 private $lastContents = array();
64 * Whether the "connection is already up
66 private $alreadyConnected = false;
69 * Table information array
71 private $tableInfo = array();
76 private $indexKey = '__idx';
79 * The file I/O instance for the template loader
81 private $fileIoInstance = NULL;
84 * The protected constructor. Do never instance from outside! You need to
85 * set a local file path. The class will then validate it.
89 protected function __construct () {
90 // Call parent constructor
91 parent::__construct(__CLASS__);
95 * Create an object of CachedLocalFileDatabase and set the save path from
96 * configuration for local files.
98 * @return $databaseInstance An instance of CachedLocalFileDatabase
100 public static final function createCachedLocalFileDatabase () {
102 $databaseInstance = new CachedLocalFileDatabase();
104 // Get a new compressor channel instance
105 $compressorInstance = ObjectFactory::createObjectByConfiguredName('compressor_channel_class');
107 // Set the compressor channel
108 $databaseInstance->setCompressorChannel($compressorInstance);
110 // Get a file IO handler
111 $fileIoInstance = ObjectFactory::createObjectByConfiguredName('file_io_class');
114 $databaseInstance->setFileIoInstance($fileIoInstance);
116 // "Connect" to the database
117 $databaseInstance->connectToDatabase();
119 // Return database instance
120 return $databaseInstance;
124 * Setter for the last read file
126 * @param $infoInstance The FQFN of the last read file
129 private final function setLastFile (SplFileInfo $infoInstance) {
130 // Cast string and set it
131 $this->lastFile = $infoInstance;
135 * Getter for last read file
137 * @return $lastFile The last read file's name with full path
139 public final function getLastFile () {
140 return $this->lastFile;
144 * Setter for contents of the last read file
146 * @param $contents An array with header and data elements
149 private final function setLastFileContents (array $contents) {
151 $this->lastContents = $contents;
155 * Getter for last read file's content as an array
157 * @return $lastContent The array with elements 'header' and 'data'.
159 public final function getLastContents () {
160 return $this->lastContents;
164 * Getter for file extension
166 * @return $fileExtension The array with elements 'header' and 'data'.
168 public final function getFileExtension () {
169 return $this->fileExtension;
173 * Getter for index key
175 * @return $indexKey Index key
177 public final function getIndexKey () {
178 return $this->indexKey;
181 * Private getter for file IO instance
183 * @return $fileIoInstance An instance to the file I/O sub-system
185 protected final function getFileIoInstance () {
186 return $this->fileIoInstance;
190 * Setter for file I/O instance
192 * @param $fileIoInstance An instance to the file I/O sub-system
195 public final function setFileIoInstance (IoHandler $fileIoInstance) {
196 $this->fileIoInstance = $fileIoInstance;
200 * Reads a local data file and returns it's contents in an array
202 * @param $infoInstance An instance of a SplFileInfo class
205 private function getDataArrayFromFile (SplFileInfo $infoInstance) {
206 // Init compressed data
207 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Reading elements from database file ' . $infoInstance . ' ...');
208 $compressedData = $this->getFileIoInstance()->loadFileContents($infoInstance);
209 $compressedData = $compressedData['data'];
212 $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
215 $dataArray = json_decode($serializedData, true);
218 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Read ' . count($dataArray) . ' elements from database file ' . $infoInstance . '.');
219 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: dataArray=' . print_r($dataArray, true));
224 * Writes data array to local file
226 * @param $infoInstance An instance of a SplFileInfo class
227 * @param $dataArray An array with all the data we shall write
230 private function writeDataArrayToFqfn (SplFileInfo $infoInstance, array $dataArray) {
231 // Serialize and compress it
232 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Flushing ' . count($dataArray) . ' elements to database file ' . $infoInstance . ' ...');
233 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: dataArray=' . print_r($dataArray, true));
234 $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(json_encode($dataArray));
236 // Write this data BASE64 encoded to the file
237 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing ' . strlen($compressedData) . ' bytes ...');
238 $this->getFileIoInstance()->saveStreamToFile($infoInstance, $compressedData, $this);
241 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Flushing ' . count($dataArray) . ' elements to database file completed.');
245 * Getter for table information file contents or an empty if info file was not created
247 * @param $dataSetInstance An instance of a database set class
248 * @return $infoArray An array with all table informations
250 private function getContentsFromTableInfoFile (StoreableCriteria $dataSetInstance) {
251 // Default content is no data
252 $infoArray = array();
254 // Create FQFN for getting the table information file
255 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
257 // Get the file contents
259 $infoArray = $this->getDataArrayFromFile($infoInstance);
260 } catch (FileNotFoundException $e) {
261 // Not found, so ignore it here
269 * Generates a file info class from given dataset instance and string
271 * @param $dataSetInstance An instance of a database set class
272 * @param $rowName Name of the row
273 * @return $infoInstance An instance of a SplFileInfo class
275 private function generateFileFromDataSet (Criteria $dataSetInstance, string $rowName) {
276 // Instanciate new file object
277 $infoInstance = new SplFileInfo($this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension());
280 return $infoInstance;
284 * Creates the table info file from given dataset instance
286 * @param $dataSetInstance An instance of a database set class
289 private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
290 // Create FQFN for creating the table information file
291 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
293 // Get the data out from dataset in a local array
294 $this->tableInfo[$dataSetInstance->getTableName()] = array(
295 'primary' => $dataSetInstance->getPrimaryKey(),
297 'last_updated' => time()
300 // Write the data to the file
301 $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$dataSetInstance->getTableName()]);
305 * Updates the table info file from given dataset instance
307 * @param $dataSetInstance An instance of a database set class
310 private function updateTableInfoFile (StoreableCriteria $dataSetInstance) {
311 // Get table name from criteria
312 $tableName = $dataSetInstance->getTableName();
314 // Create FQFN for creating the table information file
315 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
317 // Get the data out from dataset in a local array
318 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
319 $this->tableInfo[$tableName]['last_updated'] = time();
321 // Write the data to the file
322 $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$tableName]);
326 * Updates the primary key information or creates the table info file if not found
328 * @param $dataSetInstance An instance of a database set class
331 private function updatePrimaryKey (StoreableCriteria $dataSetInstance) {
332 // Get table name from criteria
333 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataSetInstance=%s - CALLED!', $dataSetInstance->__toString()));
334 $tableName = $dataSetInstance->getTableName();
336 // Get the information array from lower method
337 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s', $tableName));
338 $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance);
340 // Is the primary key there?
341 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableInfo=' . print_r($this->tableInfo, true));
342 if (!isset($this->tableInfo[$tableName]['primary'])) {
343 // Then create the info file
344 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Creating info table for tableName=%s ...', $tableName));
345 $this->createTableInfoFile($dataSetInstance);
346 } elseif (($this->getConfigInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo[$tableName]['primary'])) {
347 // Set the array element
348 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting primaryKey=%s for tableName=%s ...', $dataSetInstance->getPrimaryKey(), $tableName));
349 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
352 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Updating info table for tableName=%s ...', $tableName));
353 $this->updateTableInfoFile($dataSetInstance);
357 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: EXIT!');
361 * Makes sure that the database connection is alive
364 * @todo Do some checks on the database directory and files here
366 public function connectToDatabase () {
370 * Starts a SELECT query on the database by given return type, table name
371 * and search criteria
373 * @param $tableName Name of the database table
374 * @param $searchInstance Local search criteria class
375 * @return $resultData Result data of the query
376 * @throws UnsupportedCriteriaException If the criteria is unsupported
377 * @throws SqlException If an 'SQL error' occurs
379 public function querySelect (string $tableName, LocalSearchCriteria $searchInstance) {
380 // The result is null by any errors
381 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s,searchInstance=%s - CALLED!', $tableName, $searchInstance->__toString()));
384 // Create full path name
385 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
388 * A 'select' query is not that easy on local files, so first try to
389 * find the 'table' which is in fact a directory on the server
392 // Get a directory pointer instance
393 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Getting directory_class for pathName=%s ...', $pathName));
394 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
396 // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
398 BaseDatabaseBackend::RESULT_INDEX_STATUS => self::RESULT_OKAY,
399 BaseDatabaseBackend::RESULT_INDEX_ROWS => array()
402 // Initialize limit/skip
407 // Read the directory with some exceptions
408 while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
409 // Does the extension match?
410 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: fileInfoInstance->extension=%s,this->fileExtension=%s', $fileInfoInstance->getExtension(), $this->getFileExtension()));
411 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
413 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Skipping fileInfoInstance->filename=%s ...', $fileInfoInstance->getFilename()));
414 $directoryInstance->getDirectoryIteratorInstance()->next();
419 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
422 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
423 if (is_array($dataArray)) {
424 // Default is nothing found
427 // Search in the criteria with FMFW (First Matches, First Wins)
428 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataArray()=%d', count($dataArray)));
429 foreach ($dataArray as $key => $value) {
430 // Make sure value is not bool
431 assert(!is_bool($value));
434 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
435 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: key=%s,value[%s]=%s,isFound=%s', $key, gettype($value), $value, intval($isFound)));
439 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: isFound=%d,limitFound=%d,limit=%d', intval($isFound), $limitFound, $searchInstance->getLimit()));
440 if ($isFound === true) {
441 // Shall we skip this entry?
442 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: searchInstance->skip=%d', $searchInstance->getSkip()));
443 if ($searchInstance->getSkip() > 0) {
444 // We shall skip some entries
445 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: skipFound=%s', $skipFound));
446 if ($skipFound < $searchInstance->getSkip()) {
454 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting dataArray[%s]=%d', $this->getIndexKey(), $idx));
455 $dataArray[$this->getIndexKey()] = $idx;
458 array_push($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS], $dataArray);
460 // Count found entries up
461 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: resultData[%s]()=%d', BaseDatabaseBackend::RESULT_INDEX_ROWS, count($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS])));
465 // Throw an exception here
466 throw new SqlException(array($this, sprintf('File '%s' contains invalid data.', $fileInfoInstance->getPathname()), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
472 // Advance to next entry
473 $directoryInstance->getDirectoryIteratorInstance()->next();
476 // Close directory and throw the instance away
477 $directoryInstance->closeDirectory();
478 unset($directoryInstance);
480 // Reset last exception
481 $this->resetLastException();
482 } catch (PathIsNoDirectoryException $e) {
483 // Path not found means "table not found" for real databases...
484 $this->setLastException($e);
486 // So throw an SqlException here with faked error message
487 throw new SqlException (array($this, sprintf('Table '%s' not found', $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
488 } catch (FrameworkException $e) {
489 // Catch all exceptions and store them in last error
490 $this->setLastException($e);
493 // Return the gathered result
498 * "Inserts" a data set instance into a local file database folder
500 * @param $dataSetInstance A storeable data set
502 * @throws SqlException If an SQL error occurs
504 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
505 // Try to save the request away
506 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataSetInstance=%s - CALLED!', $dataSetInstance->__toString()));
508 // Create full path name
509 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
511 // Write the data away
512 $this->writeDataArrayToFqfn($infoInstance, $dataSetInstance->getCriteriaArray());
514 // Update the primary key
515 $this->updatePrimaryKey($dataSetInstance);
517 // Reset last exception
518 $this->resetLastException();
519 } catch (FrameworkException $e) {
520 // Catch all exceptions and store them in last error
521 $this->setLastException($e);
523 // Throw an SQL exception
524 throw new SqlException(array(
526 sprintf('Cannot write data to table '%s', is the table created?', $dataSetInstance->getTableName()),
527 self::DB_CODE_TABLE_UNWRITEABLE
529 self::EXCEPTION_SQL_QUERY
534 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: EXIT!');
538 * "Updates" a data set instance with a database layer
540 * @param $dataSetInstance A storeable data set
542 * @throws SqlException If an SQL error occurs
544 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
545 // Create full path name
546 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR;
548 // Try all the requests
550 // Get a file pointer instance
551 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
553 // Initialize limit/skip
557 // Get the criteria array from the dataset
558 $searchArray = $dataSetInstance->getCriteriaArray();
560 // Get search criteria
561 $searchInstance = $dataSetInstance->getSearchInstance();
563 // Read the directory with some exceptions
564 while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
565 // Does the extension match?
566 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
567 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
569 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
570 $directoryInstance->getDirectoryIteratorInstance()->next();
574 // Open this file for reading
575 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
576 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
579 if (is_array($dataArray)) {
580 // Default is nothing found
583 // Search in the criteria with FMFW (First Matches, First Wins)
584 foreach ($dataArray as $key => $value) {
585 // Make sure value is not bool
586 assert(!is_bool($value));
589 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
593 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: isFound=' . intval($isFound));
594 if ($isFound === true) {
595 // Shall we skip this entry?
596 if ($searchInstance->getSkip() > 0) {
597 // We shall skip some entries
598 if ($skipFound < $searchInstance->getSkip()) {
600 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Found entry, but skipping ...');
606 // Entry found, so update it
607 foreach ($searchArray as $searchKey => $searchValue) {
608 // Make sure the value is not bool again
609 assert(!is_bool($searchValue));
610 assert($searchKey != $this->indexKey);
612 // Debug message + add/update it
613 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
614 $dataArray[$searchKey] = $searchValue;
617 // Write the data to a local file
618 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing dataArray()=' . count($dataArray) . ' to ' . $fileInfoInstance->getPathname() . ' ...');
619 $this->writeDataArrayToFqfn($fileInfoInstance, $dataArray);
621 // Count found entries up
626 // Advance to next entry
627 $directoryInstance->getDirectoryIteratorInstance()->next();
630 // Close the file pointer
631 $directoryInstance->closeDirectory();
633 // Update the primary key
634 $this->updatePrimaryKey($dataSetInstance);
636 // Reset last exception
637 $this->resetLastException();
638 } catch (FrameworkException $e) {
639 // Catch all exceptions and store them in last error
640 $this->setLastException($e);
642 // Throw an SQL exception
643 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);
648 * Getter for primary key of specified table or if not found null will be
649 * returned. This must be database-specific.
651 * @param $tableName Name of the table we need the primary key from
652 * @return $primaryKey Primary key column of the given table
653 * @todo Rename method to getPrimaryKeyFromTableInfo()
655 public function getPrimaryKeyOfTable (string $tableName) {
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
691 public function countTotalRows (string $tableName) {
692 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ' - CALLED!');
694 // Create full path name
695 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
697 // Try all the requests
699 // Get a file pointer instance
700 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
702 // Initialize counter
705 // Read the directory with some exceptions
706 while ($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) {
707 // Does the extension match?
708 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
709 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
711 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
717 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
720 } catch (FrameworkException $e) {
721 // Catch all exceptions and store them in last error
722 $this->setLastException($e);
724 // Throw an SQL exception
725 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);
729 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');