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