4
0

DatabaseDAO.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * This class is used to test database is well-constructed.
  4. */
  5. class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
  6. public function tablesAreCorrect() {
  7. $sql = 'SHOW TABLES';
  8. $stm = $this->bd->prepare($sql);
  9. $stm->execute();
  10. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  11. $tables = array(
  12. $this->prefix . 'category' => false,
  13. $this->prefix . 'feed' => false,
  14. $this->prefix . 'entry' => false,
  15. );
  16. foreach ($res as $value) {
  17. $tables[array_pop($value)] = true;
  18. }
  19. return count(array_keys($tables, true, true)) == count($tables);
  20. }
  21. public function getSchema($table) {
  22. $sql = 'DESC ' . $this->prefix . $table;
  23. $stm = $this->bd->prepare($sql);
  24. $stm->execute();
  25. return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
  26. }
  27. public function checkTable($table, $schema) {
  28. $columns = $this->getSchema($table);
  29. $ok = (count($columns) == count($schema));
  30. foreach ($columns as $c) {
  31. $ok &= in_array($c['name'], $schema);
  32. }
  33. return $ok;
  34. }
  35. public function categoryIsCorrect() {
  36. return $this->checkTable('category', array(
  37. 'id', 'name'
  38. ));
  39. }
  40. public function feedIsCorrect() {
  41. return $this->checkTable('feed', array(
  42. 'id', 'url', 'category', 'name', 'website', 'description', 'lastUpdate',
  43. 'priority', 'pathEntries', 'httpAuth', 'error', 'keep_history', 'ttl',
  44. 'cache_nbEntries', 'cache_nbUnreads'
  45. ));
  46. }
  47. public function entryIsCorrect() {
  48. return $this->checkTable('entry', array(
  49. 'id', 'guid', 'title', 'author', 'content_bin', 'link', 'date', 'is_read',
  50. 'is_favorite', 'id_feed', 'tags'
  51. ));
  52. }
  53. public function daoToSchema($dao) {
  54. return array(
  55. 'name' => $dao['Field'],
  56. 'type' => strtolower($dao['Type']),
  57. 'notnull' => (bool)$dao['Null'],
  58. 'default' => $dao['Default'],
  59. );
  60. }
  61. public function listDaoToSchema($listDAO) {
  62. $list = array();
  63. foreach ($listDAO as $dao) {
  64. $list[] = $this->daoToSchema($dao);
  65. }
  66. return $list;
  67. }
  68. }