001package io.ebean.docker.commands; 002 003import java.sql.Connection; 004import java.sql.DriverManager; 005import java.sql.SQLException; 006import java.util.Properties; 007 008/** 009 * Configuration for an DBMS like Postgres, MySql, Oracle, SQLServer 010 */ 011public abstract class DbConfig extends BaseConfig { 012 013 /** 014 * Set for in-memory tmpfs use. 015 */ 016 private String tmpfs; 017 018 /** 019 * Database admin password. 020 */ 021 String adminUsername = "admin"; 022 023 /** 024 * Database admin password. 025 */ 026 String adminPassword = "admin"; 027 028 /** 029 * An additional database. 030 */ 031 private String extraDb; 032 private String extraDbUser; 033 private String extraDbPassword; 034 private String extraDbInitSqlFile; 035 private String extraDbSeedSqlFile; 036 037 /** 038 * Database name to use. 039 */ 040 String dbName = "test_db"; 041 042 /** 043 * Database user to use. 044 */ 045 String username = "test_user"; 046 047 /** 048 * Database password for the user. 049 */ 050 String password = "test"; 051 052 /** 053 * The default database schema to use if specified. 054 */ 055 String schema; 056 057 /** 058 * Comma delimited list of database extensions required (hstore, pgcrypto etc). 059 */ 060 private String extensions; 061 062 /** 063 * SQL file executed against the database after it has been created. 064 */ 065 private String initSqlFile; 066 067 /** 068 * SQL file executed against the database after initSqlFile. 069 */ 070 private String seedSqlFile; 071 072 /** 073 * Set to true to run in-memory mode. 074 */ 075 private boolean inMemory; 076 077 /** 078 * If true we ONLY check the existence of the DB and if present we skip the 079 * other usual checks (does user exist, create extensions if not exists etc). 080 */ 081 boolean fastStartMode = true; 082 083 DbConfig(String platform, int port, int internalPort, String version) { 084 super(platform, port, internalPort, version); 085 } 086 087 /** 088 * Return a description of the configuration. 089 */ 090 @Override 091 public String startDescription() { 092 return "starting " + platform + " container:" + containerName + " port:" + port + " db:" + dbName + " user:" + username + " extensions:" + extensions + " startMode:" + startMode; 093 } 094 095 /** 096 * Return summary of the port db name and other details. 097 */ 098 public String summary() { 099 return "port:" + port + " db:" + dbName + " user:" + username + "/" + password; 100 } 101 102 /** 103 * Set the schema if it hasn't already set. Some databases (NuoDB) effectively require a 104 * default schema and it is reasonable for this to default to the username. 105 */ 106 public void initDefaultSchema() { 107 if (schema == null) { 108 schema = username; 109 } 110 } 111 112 /** 113 * Return a Connection to the database (make sure you close it). 114 */ 115 @Override 116 public Connection createConnection() throws SQLException { 117 Properties props = new java.util.Properties(); 118 props.put("user", username); 119 props.put("password", password); 120 if (schema != null) { 121 props.put("schema", schema); 122 } 123 return DriverManager.getConnection(jdbcUrl(), props); 124 } 125 126 @Override 127 public Connection createConnectionNoSchema() throws SQLException { 128 Properties props = new java.util.Properties(); 129 props.put("user", username); 130 props.put("password", password); 131 return DriverManager.getConnection(jdbcUrl(), props); 132 } 133 134 /** 135 * Return a Connection to the database using the admin user. 136 */ 137 public Connection createAdminConnection(String url) throws SQLException { 138 Properties props = new java.util.Properties(); 139 props.put("user", adminUsername); 140 props.put("password", adminPassword); 141 return DriverManager.getConnection(url, props); 142 } 143 144 public Connection createAdminConnection() throws SQLException { 145 return createAdminConnection(jdbcAdminUrl()); 146 } 147 148 /** 149 * Load configuration from properties. 150 */ 151 public DbConfig setProperties(Properties properties) { 152 if (properties == null) { 153 return this; 154 } 155 super.setProperties(properties); 156 157 inMemory = Boolean.parseBoolean(prop(properties, "inMemory", Boolean.toString(inMemory))); 158 fastStartMode = Boolean.parseBoolean(prop(properties, "fastStartMode", Boolean.toString(fastStartMode))); 159 160 tmpfs = prop(properties, "tmpfs", tmpfs); 161 dbName = prop(properties, "dbName", dbName); 162 username = prop(properties, "username", username); 163 password = prop(properties, "password", password); 164 schema = prop(properties, "schema", schema); 165 extensions = prop(properties, "extensions", extensions); 166 adminUsername = prop(properties, "adminUsername", adminUsername); 167 adminPassword = prop(properties, "adminPassword", adminPassword); 168 initSqlFile = prop(properties, "initSqlFile", initSqlFile); 169 seedSqlFile = prop(properties, "seedSqlFile", seedSqlFile); 170 171 extraDb = prop(properties, "extraDb.dbName", prop(properties, "extraDb", extraDb)); 172 extraDbUser = prop(properties, "extraDb.username", extraDbUser); 173 extraDbPassword = prop(properties, "extraDb.password", extraDbPassword); 174 extraDbInitSqlFile = prop(properties, "extraDb.initSqlFile", extraDbInitSqlFile); 175 extraDbSeedSqlFile = prop(properties, "extraDb.seedSqlFile", extraDbSeedSqlFile); 176 return this; 177 } 178 179 /** 180 * Set the password for the DB admin user. 181 */ 182 public DbConfig setAdminUser(String dbAdminUser) { 183 this.adminUsername = dbAdminUser; 184 return this; 185 } 186 187 /** 188 * Set the password for the DB admin user. 189 */ 190 public DbConfig setAdminPassword(String adminPassword) { 191 this.adminPassword = adminPassword; 192 return this; 193 } 194 195 /** 196 * Set the temp fs for in-memory use. 197 */ 198 public DbConfig setTmpfs(String tmpfs) { 199 this.tmpfs = tmpfs; 200 return this; 201 } 202 203 /** 204 * Set to true to use fast start mode. 205 */ 206 public DbConfig setFastStartMode(boolean fastStartMode) { 207 this.fastStartMode = fastStartMode; 208 return this; 209 } 210 211 /** 212 * Set the DB name. 213 */ 214 public DbConfig setDbName(String dbName) { 215 this.dbName = dbName; 216 return this; 217 } 218 219 /** 220 * Set the DB user. 221 */ 222 public DbConfig setUser(String user) { 223 this.username = user; 224 return this; 225 } 226 227 /** 228 * Set the DB password. 229 */ 230 public DbConfig setPassword(String password) { 231 this.password = password; 232 return this; 233 } 234 235 /** 236 * Set the DB schema. 237 */ 238 public DbConfig setSchema(String schema) { 239 this.schema = schema; 240 return this; 241 } 242 243 /** 244 * Set the DB extensions to install (Postgres hstore, pgcrypto etc) 245 */ 246 public DbConfig setExtensions(String extensions) { 247 this.extensions = extensions; 248 return this; 249 } 250 251 /** 252 * Set the SQL file to execute after creating the database. 253 */ 254 public DbConfig setInitSqlFile(String initSqlFile) { 255 this.initSqlFile = initSqlFile; 256 return this; 257 } 258 259 /** 260 * Set the SQL file to execute after creating the database and initSqlFile. 261 */ 262 public DbConfig setSeedSqlFile(String seedSqlFile) { 263 this.seedSqlFile = seedSqlFile; 264 return this; 265 } 266 267 /** 268 * Set the name of an extra database to create. 269 */ 270 public DbConfig setExtraDb(String extraDb) { 271 this.extraDb = extraDb; 272 return this; 273 } 274 275 /** 276 * Set the name of an extra user to create. If an extra database is also created this would be the 277 * owner of that extra database. 278 */ 279 public DbConfig setExtraDbUser(String extraDbUser) { 280 this.extraDbUser = extraDbUser; 281 return this; 282 } 283 284 /** 285 * Set the password for an extra user. If nothing is set this would default to be the same as 286 * the main users password. 287 */ 288 public DbConfig setExtraDbPassword(String extraDbPassword) { 289 this.extraDbPassword = extraDbPassword; 290 return this; 291 } 292 293 /** 294 * Set a file to execute after creating the extra database. 295 */ 296 public DbConfig setExtraDbInitSqlFile(String extraDbInitSqlFile) { 297 this.extraDbInitSqlFile = extraDbInitSqlFile; 298 return this; 299 } 300 301 /** 302 * Set a file to execute after creating the extra database. 303 */ 304 public DbConfig setExtraDbSeedSqlFile(String extraDbSeedSqlFile) { 305 this.extraDbSeedSqlFile = extraDbSeedSqlFile; 306 return this; 307 } 308 309 /** 310 * Set to true to run using in memory storage for data via tmpfs. 311 */ 312 public DbConfig setInMemory(boolean inMemory) { 313 this.inMemory = inMemory; 314 return this; 315 } 316 317 public boolean isInMemory() { 318 return inMemory; 319 } 320 321 public String getTmpfs() { 322 return tmpfs; 323 } 324 325 public String getAdminUsername() { 326 return adminUsername; 327 } 328 329 public String getAdminPassword() { 330 return adminPassword; 331 } 332 333 public String getDbName() { 334 return dbName; 335 } 336 337 public String getUsername() { 338 return username; 339 } 340 341 public String getPassword() { 342 return password; 343 } 344 345 public String getSchema() { 346 return schema; 347 } 348 349 public String getExtensions() { 350 return extensions; 351 } 352 353 public String getInitSqlFile() { 354 return initSqlFile; 355 } 356 357 public String getSeedSqlFile() { 358 return seedSqlFile; 359 } 360 361 public String getExtraDb() { 362 return extraDb; 363 } 364 365 public String getExtraDbUser() { 366 return extraDbUser; 367 } 368 369 public String getExtraDbPassword() { 370 return extraDbPassword; 371 } 372 373 public String getExtraDbInitSqlFile() { 374 return extraDbInitSqlFile; 375 } 376 377 public String getExtraDbSeedSqlFile() { 378 return extraDbSeedSqlFile; 379 } 380 381 public boolean isFastStartMode() { 382 return fastStartMode; 383 } 384}