DatabaseBuilder.class.php 4.5 KB

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