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