DatabaseLoader.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. class DatabaseLoader
  3. {
  4. private $db;
  5. static $instances = array();
  6. public function __construct($db)
  7. {
  8. $this->db = $db;
  9. if (!isset(static::$instances[$db->protocol]))
  10. static::$instances[$db->protocol] = 0;
  11. if (static::$instances[$db->protocol]++ == 0)
  12. {
  13. // drop and re-create the tables one time only
  14. $this->drop_tables();
  15. $this->exec_sql_script($db->protocol);
  16. }
  17. }
  18. public function reset_table_data()
  19. {
  20. foreach ($this->get_fixture_tables() as $table)
  21. {
  22. if ($this->db->protocol == 'oci' && $table == 'rm-bldg')
  23. continue;
  24. $this->db->query('DELETE FROM ' . $this->quote_name($table));
  25. $this->load_fixture_data($table);
  26. }
  27. $after_fixtures = $this->db->protocol.'-after-fixtures';
  28. try {
  29. $this->exec_sql_script($after_fixtures);
  30. } catch (Exception $e) {
  31. // pass
  32. }
  33. }
  34. public function drop_tables()
  35. {
  36. $tables = $this->db->tables();
  37. foreach ($this->get_fixture_tables() as $table)
  38. {
  39. if ($this->db->protocol == 'oci')
  40. {
  41. $table = strtoupper($table);
  42. if ($table == 'RM-BLDG')
  43. continue;
  44. }
  45. if (in_array($table,$tables))
  46. $this->db->query('DROP TABLE ' . $this->quote_name($table));
  47. if ($this->db->protocol == 'oci')
  48. {
  49. try {
  50. $this->db->query("DROP SEQUENCE {$table}_seq");
  51. } catch (ActiveRecord\DatabaseException $e) {
  52. // ignore
  53. }
  54. }
  55. }
  56. }
  57. public function exec_sql_script($file)
  58. {
  59. foreach (explode(';',$this->get_sql($file)) as $sql)
  60. {
  61. if (trim($sql) != '')
  62. $this->db->query($sql);
  63. }
  64. }
  65. public function get_fixture_tables()
  66. {
  67. $tables = array();
  68. foreach (glob(__DIR__ . '/../fixtures/*.csv') as $file)
  69. {
  70. $info = pathinfo($file);
  71. $tables[] = $info['filename'];
  72. }
  73. return $tables;
  74. }
  75. public function get_sql($file)
  76. {
  77. $file = __DIR__ . "/../sql/$file.sql";
  78. if (!file_exists($file))
  79. throw new Exception("File not found: $file");
  80. return file_get_contents($file);
  81. }
  82. public function load_fixture_data($table)
  83. {
  84. $fp = fopen(__DIR__ . "/../fixtures/$table.csv",'r');
  85. $fields = fgetcsv($fp);
  86. if (!empty($fields))
  87. {
  88. $markers = join(',',array_fill(0,count($fields),'?'));
  89. $table = $this->quote_name($table);
  90. foreach ($fields as &$name)
  91. $name = $this->quote_name(trim($name));
  92. $fields = join(',',$fields);
  93. while (($values = fgetcsv($fp)))
  94. $this->db->query("INSERT INTO $table($fields) VALUES($markers)",$values);
  95. }
  96. fclose($fp);
  97. }
  98. public function quote_name($name)
  99. {
  100. if ($this->db->protocol == 'oci')
  101. $name = strtoupper($name);
  102. return $this->db->quote_name($name);
  103. }
  104. }
  105. ?>