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