| @@ 1829-1901 (lines=73) @@ | ||
| 1826 | * |
|
| 1827 | * @see Alpha\Model\ActiveRecordProviderInterface::checkTableNeedsUpdate() |
|
| 1828 | */ |
|
| 1829 | public function checkTableNeedsUpdate() |
|
| 1830 | { |
|
| 1831 | self::$logger->debug('>>checkTableNeedsUpdate()'); |
|
| 1832 | ||
| 1833 | $updateRequired = false; |
|
| 1834 | ||
| 1835 | $matchCount = 0; |
|
| 1836 | ||
| 1837 | $query = 'SHOW COLUMNS FROM '.$this->BO->getTableName(); |
|
| 1838 | $result = self::getConnection()->query($query); |
|
| 1839 | $this->BO->setLastQuery($query); |
|
| 1840 | ||
| 1841 | // get the class attributes |
|
| 1842 | $reflection = new ReflectionClass(get_class($this->BO)); |
|
| 1843 | $properties = $reflection->getProperties(); |
|
| 1844 | ||
| 1845 | foreach ($properties as $propObj) { |
|
| 1846 | $propName = $propObj->name; |
|
| 1847 | if (!in_array($propName, $this->BO->getTransientAttributes())) { |
|
| 1848 | $foundMatch = false; |
|
| 1849 | ||
| 1850 | while ($row = $result->fetch_array(MYSQLI_ASSOC)) { |
|
| 1851 | if ($propName == $row['Field']) { |
|
| 1852 | $foundMatch = true; |
|
| 1853 | break; |
|
| 1854 | } |
|
| 1855 | } |
|
| 1856 | ||
| 1857 | if (!$foundMatch) { |
|
| 1858 | --$matchCount; |
|
| 1859 | } |
|
| 1860 | ||
| 1861 | $result->data_seek(0); |
|
| 1862 | } |
|
| 1863 | } |
|
| 1864 | ||
| 1865 | // check for the "classname" field in overloaded tables |
|
| 1866 | if ($this->BO->isTableOverloaded()) { |
|
| 1867 | $foundMatch = false; |
|
| 1868 | ||
| 1869 | while ($row = $result->fetch_array(MYSQLI_ASSOC)) { |
|
| 1870 | if ('classname' == $row['Field']) { |
|
| 1871 | $foundMatch = true; |
|
| 1872 | break; |
|
| 1873 | } |
|
| 1874 | } |
|
| 1875 | if (!$foundMatch) { |
|
| 1876 | --$matchCount; |
|
| 1877 | } |
|
| 1878 | } |
|
| 1879 | ||
| 1880 | if ($matchCount != 0) { |
|
| 1881 | $updateRequired = true; |
|
| 1882 | } |
|
| 1883 | ||
| 1884 | if ($result) { |
|
| 1885 | // check the table indexes |
|
| 1886 | try { |
|
| 1887 | $this->checkIndexes(); |
|
| 1888 | } catch (AlphaException $ae) { |
|
| 1889 | self::$logger->warn("Error while checking database indexes:\n\n".$ae->getMessage()); |
|
| 1890 | } |
|
| 1891 | ||
| 1892 | self::$logger->debug('<<checkTableNeedsUpdate ['.$updateRequired.']'); |
|
| 1893 | ||
| 1894 | return $updateRequired; |
|
| 1895 | } else { |
|
| 1896 | throw new AlphaException('Failed to access the system database correctly, error is ['.self::getConnection()->error.']'); |
|
| 1897 | self::$logger->debug('<<checkTableNeedsUpdate [false]'); |
|
| 1898 | ||
| 1899 | return false; |
|
| 1900 | } |
|
| 1901 | } |
|
| 1902 | ||
| 1903 | /** |
|
| 1904 | * (non-PHPdoc). |
|
| @@ 1797-1871 (lines=75) @@ | ||
| 1794 | * |
|
| 1795 | * @see Alpha\Model\ActiveRecordProviderInterface::checkTableNeedsUpdate() |
|
| 1796 | */ |
|
| 1797 | public function checkTableNeedsUpdate() |
|
| 1798 | { |
|
| 1799 | self::$logger->debug('>>checkTableNeedsUpdate()'); |
|
| 1800 | ||
| 1801 | if (!$this->BO->checkTableExists()) { |
|
| 1802 | return false; |
|
| 1803 | } |
|
| 1804 | ||
| 1805 | $updateRequired = false; |
|
| 1806 | ||
| 1807 | $matchCount = 0; |
|
| 1808 | ||
| 1809 | $query = 'PRAGMA table_info('.$this->BO->getTableName().')'; |
|
| 1810 | $result = self::getConnection()->query($query); |
|
| 1811 | $this->BO->setLastQuery($query); |
|
| 1812 | ||
| 1813 | // get the class attributes |
|
| 1814 | $reflection = new ReflectionClass(get_class($this->BO)); |
|
| 1815 | $properties = $reflection->getProperties(); |
|
| 1816 | ||
| 1817 | foreach ($properties as $propObj) { |
|
| 1818 | $propName = $propObj->name; |
|
| 1819 | if (!in_array($propName, $this->BO->getTransientAttributes())) { |
|
| 1820 | $foundMatch = false; |
|
| 1821 | ||
| 1822 | while ($row = $result->fetchArray(SQLITE3_ASSOC)) { |
|
| 1823 | if ($propName == $row['name']) { |
|
| 1824 | $foundMatch = true; |
|
| 1825 | break; |
|
| 1826 | } |
|
| 1827 | } |
|
| 1828 | ||
| 1829 | if (!$foundMatch) { |
|
| 1830 | --$matchCount; |
|
| 1831 | } |
|
| 1832 | ||
| 1833 | $result->reset(); |
|
| 1834 | } |
|
| 1835 | } |
|
| 1836 | ||
| 1837 | // check for the "classname" field in overloaded tables |
|
| 1838 | if ($this->BO->isTableOverloaded()) { |
|
| 1839 | $foundMatch = false; |
|
| 1840 | ||
| 1841 | while ($row = $result->fetchArray(SQLITE3_ASSOC)) { |
|
| 1842 | if ('classname' == $row['name']) { |
|
| 1843 | $foundMatch = true; |
|
| 1844 | break; |
|
| 1845 | } |
|
| 1846 | } |
|
| 1847 | if (!$foundMatch) { |
|
| 1848 | --$matchCount; |
|
| 1849 | } |
|
| 1850 | } |
|
| 1851 | ||
| 1852 | if ($matchCount != 0) { |
|
| 1853 | $updateRequired = true; |
|
| 1854 | } |
|
| 1855 | ||
| 1856 | if (!$result) { |
|
| 1857 | self::$logger->debug('<<checkTableNeedsUpdate'); |
|
| 1858 | throw new AlphaException('Failed to access the system database correctly, error is ['.self::getLastDatabaseError().']'); |
|
| 1859 | } else { |
|
| 1860 | // check the table indexes |
|
| 1861 | try { |
|
| 1862 | $this->checkIndexes(); |
|
| 1863 | } catch (AlphaException $ae) { |
|
| 1864 | self::$logger->warn("Error while checking database indexes:\n\n".$ae->getMessage()); |
|
| 1865 | } |
|
| 1866 | ||
| 1867 | self::$logger->debug('<<checkTableNeedsUpdate ['.$updateRequired.']'); |
|
| 1868 | ||
| 1869 | return $updateRequired; |
|
| 1870 | } |
|
| 1871 | } |
|
| 1872 | ||
| 1873 | /** |
|
| 1874 | * (non-PHPdoc). |
|