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