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