e8a8555a03220a65b9db947efb7cd99938f86146
[core.git] / framework / main / classes / database / backend / lfdb_legacy / class_CachedLocalFileDatabase.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Database\Backend\Lfdb;
4
5 // Import framework stuff
6 use CoreFramework\Criteria\Criteria;
7 use CoreFramework\Criteria\Local\LocalSearchCriteria;
8 use CoreFramework\Criteria\Storing\StoreableCriteria;
9 use CoreFramework\Database\Backend\BaseDatabaseBackend;
10 use CoreFramework\Database\Backend\DatabaseBackend;
11 use CoreFramework\Factory\ObjectFactory;
12 use CoreFramework\Filesystem\FileNotFoundException;
13 use CoreFramework\Generic\FrameworkException;
14
15 /**
16  * Database backend class for storing objects in locally created files.
17  *
18  * This class serializes arrays stored in the dataset instance and saves them
19  * to local files. Every file (except 'info') represents a single line. Every
20  * directory within the 'db' (base) directory represents a table.
21  *
22  * A configurable 'file_io_class' is being used as "storage backend".
23  *
24  * @author              Roland Haeder <webmaster@shipsimu.org>
25  * @version             0.0.0
26  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
27  * @license             GNU GPL 3.0 or any newer version
28  * @link                http://www.shipsimu.org
29  *
30  * This program is free software: you can redistribute it and/or modify
31  * it under the terms of the GNU General Public License as published by
32  * the Free Software Foundation, either version 3 of the License, or
33  * (at your option) any later version.
34  *
35  * This program is distributed in the hope that it will be useful,
36  * but WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  * GNU General Public License for more details.
39  *
40  * You should have received a copy of the GNU General Public License
41  * along with this program. If not, see <http://www.gnu.org/licenses/>.
42  */
43 class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
44         /**
45          * The file's extension
46          */
47         private $fileExtension = 'serialized';
48
49         /**
50          * The last read file's name
51          */
52         private $lastFile = '';
53
54         /**
55          * The last read file's content including header information
56          */
57         private $lastContents = array();
58
59         /**
60          * Whether the "connection is already up
61          */
62         private $alreadyConnected = false;
63
64         /**
65          * Table information array
66          */
67         private $tableInfo = array();
68
69         /**
70          * Element for index
71          */
72         private $indexKey = '__idx';
73
74         /**
75          * The protected constructor. Do never instance from outside! You need to
76          * set a local file path. The class will then validate it.
77          *
78          * @return      void
79          */
80         protected function __construct () {
81                 // Call parent constructor
82                 parent::__construct(__CLASS__);
83         }
84
85         /**
86          * Create an object of CachedLocalFileDatabase and set the save path from
87          * configuration for local files.
88          *
89          * @return      $databaseInstance       An instance of CachedLocalFileDatabase
90          */
91         public static final function createCachedLocalFileDatabase () {
92                 // Get an instance
93                 $databaseInstance = new CachedLocalFileDatabase();
94
95                 // Get a new compressor channel instance
96                 $compressorInstance = ObjectFactory::createObjectByConfiguredName('compressor_channel_class');
97
98                 // Set the compressor channel
99                 $databaseInstance->setCompressorChannel($compressorInstance);
100
101                 // Get a file IO handler
102                 $fileIoInstance = ObjectFactory::createObjectByConfiguredName('file_io_class');
103
104                 // ... and set it
105                 $databaseInstance->setFileIoInstance($fileIoInstance);
106
107                 // "Connect" to the database
108                 $databaseInstance->connectToDatabase();
109
110                 // Return database instance
111                 return $databaseInstance;
112         }
113
114         /**
115          * Setter for the last read file
116          *
117          * @param       $fqfn   The FQFN of the last read file
118          * @return      void
119          */
120         private final function setLastFile ($fqfn) {
121                 // Cast string and set it
122                 $this->lastFile = (string) $fqfn;
123         }
124
125         /**
126          * Getter for last read file
127          *
128          * @return      $lastFile       The last read file's name with full path
129          */
130         public final function getLastFile () {
131                 return $this->lastFile;
132         }
133
134         /**
135          * Setter for contents of the last read file
136          *
137          * @param               $contents       An array with header and data elements
138          * @return      void
139          */
140         private final function setLastFileContents (array $contents) {
141                 // Set array
142                 $this->lastContents = $contents;
143         }
144
145         /**
146          * Getter for last read file's content as an array
147          *
148          * @return      $lastContent    The array with elements 'header' and 'data'.
149          */
150         public final function getLastContents () {
151                 return $this->lastContents;
152         }
153
154         /**
155          * Getter for file extension
156          *
157          * @return      $fileExtension  The array with elements 'header' and 'data'.
158          */
159         public final function getFileExtension () {
160                 return $this->fileExtension;
161         }
162
163         /**
164          * Getter for index key
165          *
166          * @return      $indexKey       Index key
167          */
168         public final function getIndexKey () {
169                 return $this->indexKey;
170         }
171
172         /**
173          * Reads a local data file  and returns it's contents in an array
174          *
175          * @param       $fqfn   The FQFN for the requested file
176          * @return      $dataArray
177          */
178         private function getDataArrayFromFile ($fqfn) {
179                 // Debug message
180                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Reading elements from database file ' . $fqfn . ' ...');
181
182                 // Init compressed data
183                 $compressedData = $this->getFileIoInstance()->loadFileContents($fqfn);
184                 $compressedData = $compressedData['data'];
185
186                 // Close the file and throw the instance away
187                 unset($fileInstance);
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 ' . $fqfn . '.');
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       $fqfn           The FQFN of the local file
207          * @param       $dataArray      An array with all the data we shall write
208          * @return      void
209          */
210         private function writeDataArrayToFqfn ($fqfn, array $dataArray) {
211                 // Debug message
212                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file ' . $fqfn . ' ...');
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($fqfn, $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                 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
240
241                 // Get the file contents
242                 try {
243                         $infoArray = $this->getDataArrayFromFile($fqfn);
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 an FQFN 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      $fqfn                           The FQFN for this row
258          */
259         private function generateFqfnFromDataSet (Criteria $dataSetInstance, $rowName) {
260                 // This is the FQFN
261                 $fqfn = $this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension();
262
263                 // Return it
264                 return $fqfn;
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                 $fqfn = $this->generateFqfnFromDataSet($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($fqfn, $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                 $fqfn = $this->generateFqfnFromDataSet($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($fqfn, $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 (($dataFile = $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: dataFile=' . $dataFile . ',this->getFileExtension()=' . $this->getFileExtension());
388
389                                 // Does the extension match?
390                                 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
391                                         // Skip this file!
392                                         continue;
393                                 } // END - if
394
395                                 // Read the file
396                                 $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
397                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',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.', $dataFile), 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                         $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
479
480                         // Write the data away
481                         $this->writeDataArrayToFqfn($fqfn, $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 (($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
525                                 // Does the extension match?
526                                 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
527                                         // Debug message
528                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
529                                         // Skip this file!
530                                         continue;
531                                 } // END - if
532
533                                 // Open this file for reading
534                                 $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
535                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, true));
536
537                                 // Is this an array?
538                                 if (is_array($dataArray)) {
539                                         // Default is nothing found
540                                         $isFound = true;
541
542                                         // Search in the criteria with FMFW (First Matches, First Wins)
543                                         foreach ($dataArray as $key => $value) {
544                                                 // Make sure value is not bool
545                                                 assert(!is_bool($value));
546
547                                                 // Found one entry?
548                                                 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
549                                         } // END - foreach
550
551                                         // Is all found?
552                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: isFound=' . intval($isFound));
553                                         if ($isFound === true) {
554                                                 // Shall we skip this entry?
555                                                 if ($searchInstance->getSkip() > 0) {
556                                                         // We shall skip some entries
557                                                         if ($skipFound < $searchInstance->getSkip()) {
558                                                                 // Skip this entry
559                                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Found entry, but skipping ...');
560                                                                 $skipFound++;
561                                                                 break;
562                                                         } // END - if
563                                                 } // END - if
564
565                                                 // Entry found, so update it
566                                                 foreach ($searchArray as $searchKey => $searchValue) {
567                                                         // Make sure the value is not bool again
568                                                         assert(!is_bool($searchValue));
569                                                         assert($searchKey != $this->indexKey);
570
571                                                         // Debug message + add/update it
572                                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
573                                                         $dataArray[$searchKey] = $searchValue;
574                                                 } // END - foreach
575
576                                                 // Write the data to a local file
577                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing dataArray()=' . count($dataArray) . ' to ' . $dataFile . ' ...');
578                                                 $this->writeDataArrayToFqfn($pathName . $dataFile, $dataArray);
579
580                                                 // Count found entries up
581                                                 $limitFound++;
582                                         } // END - if
583                                 } // END - if
584                         } // END - while
585
586                         // Close the file pointer
587                         $directoryInstance->closeDirectory();
588
589                         // Update the primary key
590                         $this->updatePrimaryKey($dataSetInstance);
591
592                         // Reset last exception
593                         $this->resetLastException();
594                 } catch (FrameworkException $e) {
595                         // Catch all exceptions and store them in last error
596                         $this->setLastException($e);
597
598                         // Throw an SQL exception
599                         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);
600                 }
601         }
602
603         /**
604          * Getter for primary key of specified table or if not found null will be
605          * returned. This must be database-specific.
606          *
607          * @param       $tableName              Name of the table we need the primary key from
608          * @return      $primaryKey             Primary key column of the given table
609          */
610         public function getPrimaryKeyOfTable ($tableName) {
611                 // Default key is null
612                 $primaryKey = NULL;
613
614                 // Does the table information exist?
615                 if (isset($this->tableInfo[$tableName])) {
616                         // Then return the primary key
617                         $primaryKey = $this->tableInfo[$tableName]['primary'];
618                 } // END - if
619
620                 // Return the column
621                 return $primaryKey;
622         }
623
624         /**
625          * Removes non-public data from given array.
626          *
627          * @param       $data   An array with possible non-public data that needs to be removed.
628          * @return      $data   A cleaned up array with only public data.
629          * @todo        Add more generic non-public data for removal
630          */
631         public function removeNonPublicDataFromArray (array $data) {
632                 // Remove '__idx'
633                 unset($data[$this->indexKey]);
634
635                 // Return it
636                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: data[' . gettype($data) . ']='.print_r($data, true));
637                 return $data;
638         }
639
640         /**
641          * Counts total rows of given table
642          *
643          * @param       $tableName      Table name
644          * @return      $count          Total rows of given table
645          */
646         public function countTotalRows($tableName) {
647                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
648
649                 // Create full path name
650                 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
651
652                 // Try all the requests
653                 try {
654                         // Get a file pointer instance
655                         $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
656
657                         // Initialize counter
658                         $count = 0;
659
660                         // Read the directory with some exceptions
661                         while ($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) {
662                                 // Does the extension match?
663                                 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
664                                         // Debug message
665                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
666                                         // Skip this file!
667                                         continue;
668                                 } // END - if
669
670                                 // Count this row up
671                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
672                                 $count++;
673                         } // END - while
674                 } catch (FrameworkException $e) {
675                         // Catch all exceptions and store them in last error
676                         $this->setLastException($e);
677
678                         // Throw an SQL exception
679                         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);
680                 }
681
682                 // Return count
683                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');
684                 return $count;
685         }
686
687 }