DatabaseBuilder.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. class DatabaseBuilder {
  3. private $database;
  4. public function init() {
  5. $this->database = new SqliteDatabase();
  6. $this->dropTables();
  7. $this->createTables();
  8. $this->insertDefaultData();
  9. $this->outputData();
  10. }
  11. private function createTables() {
  12. $sql = "CREATE TABLE users (
  13. username CHAR(50) NOT NULL,
  14. password CHAR(64) NOT NULL,
  15. display_name TEXT);";
  16. $this->database->exec($sql);
  17. $sql = "CREATE TABLE accounts (
  18. subdomain CHAR(64) NOT NULL,
  19. display_name TEXT);";
  20. $this->database->exec($sql);
  21. $sql = "CREATE TABLE projects (
  22. bucket CHAR(64) NOT NULL,
  23. folder CHAR(64) NOT NULL,
  24. display_name TEXT);";
  25. $this->database->exec($sql);
  26. $sql = "CREATE TABLE user_accounts (
  27. user_id INT NOT NULL,
  28. account_id INT NOT NULL,
  29. PRIMARY KEY(user_id, account_id) ON CONFLICT REPLACE);";
  30. $this->database->exec($sql);
  31. $sql = "CREATE TABLE account_projects (
  32. account_id INT NOT NULL,
  33. project_id INT NOT NULL,
  34. PRIMARY KEY (account_id, project_id) ON CONFLICT REPLACE);";
  35. $this->database->exec($sql);
  36. $sql = "CREATE TABLE security_logs (
  37. message TEXT,
  38. action CHAR(32),
  39. recordtime DATETIME);";
  40. $this->database->exec($sql);
  41. }
  42. private function dropTables() {
  43. $sql = "DROP TABLE IF EXISTS users;";
  44. $this->database->exec($sql);
  45. $sql = "DROP TABLE IF EXISTS accounts";
  46. $this->database->exec($sql);
  47. $sql = "DROP TABLE IF EXISTS projects";
  48. $this->database->exec($sql);
  49. $sql = "DROP TABLE IF EXISTS security_logs";
  50. $this->database->exec($sql);
  51. $sql = "DROP TABLE IF EXISTS user_accounts";
  52. $this->database->exec($sql);
  53. $sql = "DROP TABLE IF EXISTS account_projects";
  54. $this->database->exec($sql);
  55. }
  56. private function insertDefaultData() {
  57. $sql = "INSERT INTO users (username, password, display_name)
  58. VALUES
  59. ('jgilman', '55546635451d24c0ccdbe97ba94cd080fecf4d45123524af4503c9a8993e213c', 'Justin Gilman'),
  60. ('jparra', 'ebb128706f3c5c05502c7f684035b6dbd139e8230cacb11f8fa9bf0e4911dd00', 'Jesus Parra'),
  61. ('clienttest', 'fc7cb737c3a41323c556fb948fd59ed9cddf7833ecae0a5356d33c48ea996691', 'Client Test');";
  62. $this->database->exec($sql);
  63. $sql = "INSERT INTO accounts (subdomain, display_name)
  64. VALUES
  65. ('admin', 'CCLD Admin'),
  66. ('verizon', 'Verizon'),
  67. ('att', 'AT&T');";
  68. $this->database->exec($sql);
  69. $sql = "INSERT INTO projects (bucket, folder, display_name)
  70. VALUES
  71. ('".BUCKET_PREFIX."ccld-test', 'ccld-test-folder', 'New_Route');";
  72. $this->database->exec($sql);
  73. $sql = "INSERT INTO user_accounts (user_id, account_id)
  74. VALUES
  75. (1, 1),
  76. (1, 2),
  77. (2, 1),
  78. (3, 2);";
  79. $this->database->exec($sql);
  80. $sql = "INSERT INTO account_projects (account_id, project_id)
  81. VALUES
  82. (2, 1),
  83. (3, 1);";
  84. $this->database->exec($sql);
  85. }
  86. private function outputData() {
  87. $sql = "SELECT rowid AS user_id, * FROM users;";
  88. echo $sql . "<br />\n";
  89. $return = $this->database->query($sql);
  90. while($row = $return->fetchArray(SQLITE3_ASSOC)) {
  91. var_dump($row);
  92. }
  93. $sql = "SELECT rowid as client_id, * FROM accounts;";
  94. echo $sql . "<br />\n";
  95. $return = $this->database->query($sql);
  96. while($row = $return->fetchArray(SQLITE3_ASSOC)) {
  97. var_dump($row);
  98. }
  99. $sql = "SELECT rowid as project_id, * FROM projects;";
  100. echo $sql . "<br />\n";
  101. $return = $this->database->query($sql);
  102. while($row = $return->fetchArray(SQLITE3_ASSOC)) {
  103. var_dump($row);
  104. }
  105. $sql = "SELECT * FROM security_logs;";
  106. echo $sql . "<br />\n";
  107. $return = $this->database->query($sql);
  108. while($row = $return->fetchArray(SQLITE3_ASSOC)) {
  109. var_dump($row);
  110. }
  111. $sql = "SELECT * FROM user_accounts;";
  112. echo $sql . "<br />\n";
  113. $return = $this->database->query($sql);
  114. while($row = $return->fetchArray(SQLITE3_ASSOC)) {
  115. var_dump($row);
  116. }
  117. $sql = "SELECT * FROM account_projects;";
  118. echo $sql . "<br />\n";
  119. $return = $this->database->query($sql);
  120. while($row = $return->fetchArray(SQLITE3_ASSOC)) {
  121. var_dump($row);
  122. }
  123. }
  124. }