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