2e47f6fb718d98bdb2c38652cb57242b6690efcf
[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(
494                                         $this,
495                                         sprintf('Cannot write data to table &#39;%s&#39;, is the table created?', $dataSetInstance->getTableName()),
496                                         self::DB_CODE_TABLE_UNWRITEABLE
497                                 ),
498                                 self::EXCEPTION_SQL_QUERY
499                         );
500                 }
501         }
502
503         /**
504          * "Updates" a data set instance with a database layer
505          *
506          * @param       $dataSetInstance        A storeable data set
507          * @return      void
508          * @throws      SqlException    If an SQL error occurs
509          */
510         public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
511                 // Create full path name
512                 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR;
513
514                 // Try all the requests
515                 try {
516                         // Get a file pointer instance
517                         $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
518
519                         // Initialize limit/skip
520                         $limitFound = 0;
521                         $skipFound = 0;
522
523                         // Get the criteria array from the dataset
524                         $searchArray = $dataSetInstance->getCriteriaArray();
525
526                         // Get search criteria
527                         $searchInstance = $dataSetInstance->getSearchInstance();
528
529                         // Read the directory with some exceptions
530                         while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
531                                 // Debug message
532                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInstance.extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
533
534                                 // Does the extension match?
535                                 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
536                                         // Debug message
537                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
538                                         // Skip this file!
539                                         continue;
540                                 } // END - if
541
542                                 // Open this file for reading
543                                 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
544                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
545
546                                 // Is this an array?
547                                 if (is_array($dataArray)) {
548                                         // Default is nothing found
549                                         $isFound = true;
550
551                                         // Search in the criteria with FMFW (First Matches, First Wins)
552                                         foreach ($dataArray as $key => $value) {
553                                                 // Make sure value is not bool
554                                                 assert(!is_bool($value));
555
556                                                 // Found one entry?
557                                                 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
558                                         } // END - foreach
559
560                                         // Is all found?
561                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: isFound=' . intval($isFound));
562                                         if ($isFound === true) {
563                                                 // Shall we skip this entry?
564                                                 if ($searchInstance->getSkip() > 0) {
565                                                         // We shall skip some entries
566                                                         if ($skipFound < $searchInstance->getSkip()) {
567                                                                 // Skip this entry
568                                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Found entry, but skipping ...');
569                                                                 $skipFound++;
570                                                                 break;
571                                                         } // END - if
572                                                 } // END - if
573
574                                                 // Entry found, so update it
575                                                 foreach ($searchArray as $searchKey => $searchValue) {
576                                                         // Make sure the value is not bool again
577                                                         assert(!is_bool($searchValue));
578                                                         assert($searchKey != $this->indexKey);
579
580                                                         // Debug message + add/update it
581                                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
582                                                         $dataArray[$searchKey] = $searchValue;
583                                                 } // END - foreach
584
585                                                 // Write the data to a local file
586                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing dataArray()=' . count($dataArray) . ' to ' . $fileInfoInstance->getPathname() . ' ...');
587                                                 $this->writeDataArrayToFqfn($fileInfoInstance, $dataArray);
588
589                                                 // Count found entries up
590                                                 $limitFound++;
591                                         } // END - if
592                                 } // END - if
593                         } // END - while
594
595                         // Close the file pointer
596                         $directoryInstance->closeDirectory();
597
598                         // Update the primary key
599                         $this->updatePrimaryKey($dataSetInstance);
600
601                         // Reset last exception
602                         $this->resetLastException();
603                 } catch (FrameworkException $e) {
604                         // Catch all exceptions and store them in last error
605                         $this->setLastException($e);
606
607                         // Throw an SQL exception
608                         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);
609                 }
610         }
611
612         /**
613          * Getter for primary key of specified table or if not found null will be
614          * returned. This must be database-specific.
615          *
616          * @param       $tableName              Name of the table we need the primary key from
617          * @return      $primaryKey             Primary key column of the given table
618          * @todo        Rename method to getPrimaryKeyFromTableInfo()
619          */
620         public function getPrimaryKeyOfTable ($tableName) {
621                 // Default key is null
622                 $primaryKey = NULL;
623
624                 // Does the table information exist?
625                 if (isset($this->tableInfo[$tableName])) {
626                         // Then return the primary key
627                         $primaryKey = $this->tableInfo[$tableName]['primary'];
628                 } // END - if
629
630                 // Return the column
631                 return $primaryKey;
632         }
633
634         /**
635          * Removes non-public data from given array.
636          *
637          * @param       $data   An array with possible non-public data that needs to be removed.
638          * @return      $data   A cleaned up array with only public data.
639          * @todo        Add more generic non-public data for removal
640          */
641         public function removeNonPublicDataFromArray (array $data) {
642                 // Remove '__idx'
643                 unset($data[$this->indexKey]);
644
645                 // Return it
646                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: data[' . gettype($data) . ']='.print_r($data, true));
647                 return $data;
648         }
649
650         /**
651          * Counts total rows of given table
652          *
653          * @param       $tableName      Table name
654          * @return      $count          Total rows of given table
655          */
656         public function countTotalRows($tableName) {
657                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
658
659                 // Create full path name
660                 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
661
662                 // Try all the requests
663                 try {
664                         // Get a file pointer instance
665                         $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
666
667                         // Initialize counter
668                         $count = 0;
669
670                         // Read the directory with some exceptions
671                         while ($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) {
672                                 // Debug message
673                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInstance.extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
674
675                                 // Does the extension match?
676                                 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
677                                         // Debug message
678                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
679                                         // Skip this file!
680                                         continue;
681                                 } // END - if
682
683                                 // Count this row up
684                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
685                                 $count++;
686                         } // END - while
687                 } catch (FrameworkException $e) {
688                         // Catch all exceptions and store them in last error
689                         $this->setLastException($e);
690
691                         // Throw an SQL exception
692                         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);
693                 }
694
695                 // Return count
696                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');
697                 return $count;
698         }
699
700 }