/** * (non-PHPdoc). * * @see Alpha\Model\ActiveRecordProviderInterface::getVersion() */ public function getVersion() { self::$logger->debug('>>getVersion()'); $sqlQuery = 'SELECT version_num FROM ' . $this->BO->getTableName() . ' WHERE OID = ?;'; $this->BO->setLastQuery($sqlQuery); $stmt = self::getConnection()->stmt_init(); if ($stmt->prepare($sqlQuery)) { $stmt->bind_param('i', $this->BO->getOID()); $stmt->execute(); $result = $this->bindResult($stmt); if (isset($result[0])) { $row = $result[0]; } $stmt->close(); } else { self::$logger->warn('The following query caused an unexpected result [' . $sqlQuery . ']'); if (!$this->BO->checkTableExists()) { $this->BO->makeTable(); throw new RecordNotFoundException('Failed to get the version number, table did not exist so had to create!'); } return; } if (!isset($row['version_num']) || $row['version_num'] < 1) { self::$logger->debug('<<getVersion [0]'); return 0; } else { $version_num = $row['version_num']; self::$logger->debug('<<getVersion [' . $version_num . ']'); return $version_num; } }
/** * (non-PHPdoc). * * @see Alpha\Model\ActiveRecordProviderInterface::checkTableNeedsUpdate() */ public function checkTableNeedsUpdate() { self::$logger->debug('>>checkTableNeedsUpdate()'); if (!$this->BO->checkTableExists()) { return false; } $updateRequired = false; $matchCount = 0; $query = 'PRAGMA table_info(' . $this->BO->getTableName() . ')'; $result = self::getConnection()->query($query); $this->BO->setLastQuery($query); // get the class attributes $reflection = new ReflectionClass(get_class($this->BO)); $properties = $reflection->getProperties(); foreach ($properties as $propObj) { $propName = $propObj->name; if (!in_array($propName, $this->BO->getTransientAttributes())) { $foundMatch = false; while ($row = $result->fetchArray(SQLITE3_ASSOC)) { if ($propName == $row['name']) { $foundMatch = true; break; } } if (!$foundMatch) { --$matchCount; } $result->reset(); } } // check for the "classname" field in overloaded tables if ($this->BO->isTableOverloaded()) { $foundMatch = false; while ($row = $result->fetchArray(SQLITE3_ASSOC)) { if ('classname' == $row['name']) { $foundMatch = true; break; } } if (!$foundMatch) { --$matchCount; } } if ($matchCount != 0) { $updateRequired = true; } if (!$result) { throw new AlphaException('Failed to access the system database correctly, error is [' . self::getLastDatabaseError() . ']'); self::$logger->debug('<<checkTableNeedsUpdate [false]'); return false; } else { // check the table indexes try { $this->checkIndexes(); } catch (AlphaException $ae) { self::$logger->warn("Error while checking database indexes:\n\n" . $ae->getMessage()); } self::$logger->debug('<<checkTableNeedsUpdate [' . $updateRequired . ']'); return $updateRequired; } }