1 <?php
2 3 4 5
6
7 namespace Fluxsauce\Switchboard;
8
9 10 11 12
13 class Sqlite {
14 15 16 17 18 19
20 public static function getLocation() {
21 $brain_path = drush_cache_get('brain_path', 'switchboard');
22 if (isset($brain_path->data)) {
23 return $brain_path->data;
24 }
25 $default_location = drush_directory_cache('switchboard') . '/switchboard.sqlite';
26 Sqlite::setLocation($default_location);
27 return $default_location;
28 }
29
30 31 32 33 34 35 36 37 38
39 public static function setLocation($brain_path) {
40 drush_cache_set('brain_path', $brain_path, 'switchboard');
41 }
42
43 44 45
46 public static function destroy() {
47 $location = Sqlite::getLocation();
48 drush_log(dt('Destroying SQLite database at @location', array(
49 '@location' => $location,
50 )));
51 if (file_exists($location)) {
52 unlink($location);
53 drush_cache_clear_all('brain_path', 'switchboard', TRUE);
54 drush_log(dt('SQLite database removed.'));
55 }
56 else {
57 drush_log(dt('SQLite database does not exist, nothing to remove.'));
58 }
59 }
60
61 62 63 64 65 66
67 public static function destroyTable($table) {
68 $pdo = Sqlite::get();
69
70 try {
71 $sql_query = 'DROP TABLE IF EXISTS ' . $table;
72 $pdo->exec($sql_query);
73 }
74 catch (\PDOException $e) {
75 switchboard_pdo_exception_debug($e);
76 }
77 }
78
79 80 81 82 83 84
85 public static function get() {
86 static $pdo;
87 if (!isset($pdo)) {
88 try {
89 $pdo = new \PDO('sqlite:' . Sqlite::getLocation());
90 $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
91 $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, FALSE);
92 }
93 catch (\PDOException $e) {
94 switchboard_pdo_exception_debug($e);
95 }
96
97 try {
98 $sql_query = 'CREATE TABLE IF NOT EXISTS sites ( ';
99 $sql_query .= 'id INTEGER PRIMARY KEY ';
100 $sql_query .= ', provider TEXT ';
101 $sql_query .= ', uuid TEXT ';
102 $sql_query .= ', realm TEXT ';
103 $sql_query .= ', name TEXT ';
104 $sql_query .= ', title TEXT ';
105 $sql_query .= ', unixUsername TEXT ';
106 $sql_query .= ', vcsUrl TEXT ';
107 $sql_query .= ', vcsType TEXT ';
108 $sql_query .= ', vcsProtocol TEXT ';
109 $sql_query .= ', sshPort INTEGER ';
110 $sql_query .= ', updated INTEGER ';
111 $sql_query .= ') ';
112
113 $pdo->exec($sql_query);
114 }
115 catch (\PDOException $e) {
116 switchboard_pdo_exception_debug($e);
117 }
118
119 try {
120 $sql_query = 'CREATE TABLE IF NOT EXISTS environments ( ';
121 $sql_query .= 'id INTEGER PRIMARY KEY ';
122 $sql_query .= ', siteId INTEGER ';
123 $sql_query .= ', name TEXT ';
124 $sql_query .= ', host TEXT ';
125 $sql_query .= ', username TEXT ';
126 $sql_query .= ', branch TEXT ';
127 $sql_query .= ', updated INTEGER ';
128 $sql_query .= ') ';
129
130 $pdo->exec($sql_query);
131 }
132 catch (\PDOException $e) {
133 switchboard_pdo_exception_debug($e);
134 }
135
136 try {
137 $sql_query = 'CREATE TABLE IF NOT EXISTS envdbs ( ';
138 $sql_query .= 'id INTEGER PRIMARY KEY ';
139 $sql_query .= ', environmentId INTEGER ';
140 $sql_query .= ', name TEXT ';
141 $sql_query .= ', updated INTEGER ';
142 $sql_query .= ') ';
143
144 $pdo->exec($sql_query);
145 }
146 catch (\PDOException $e) {
147 switchboard_pdo_exception_debug($e);
148 }
149
150 try {
151 $sql_query = 'CREATE TABLE IF NOT EXISTS projects ( ';
152 $sql_query .= 'id INTEGER PRIMARY KEY ';
153 $sql_query .= ', name TEXT ';
154 $sql_query .= ', uuid TEXT ';
155 $sql_query .= ', siteId INTEGER ';
156 $sql_query .= ', hostname TEXT ';
157 $sql_query .= ', username TEXT ';
158 $sql_query .= ', sshPort INTEGER ';
159 $sql_query .= ', codePath TEXT ';
160 $sql_query .= ', filesPath TEXT ';
161 $sql_query .= ', databaseHost TEXT ';
162 $sql_query .= ', databaseUsername TEXT ';
163 $sql_query .= ', databasePassword TEXT ';
164 $sql_query .= ', databaseName TEXT ';
165 $sql_query .= ', databasePort INTEGER ';
166 $sql_query .= ', updated INTEGER ';
167 $sql_query .= ') ';
168
169 $pdo->exec($sql_query);
170 }
171 catch (\PDOException $e) {
172 switchboard_pdo_exception_debug($e);
173 }
174 }
175 return $pdo;
176 }
177 }
178