ImmutableDbms(ImmutableProject parent, Map<String, Object> dbms) { super(parent, requireKeys(dbms, Dbms.TYPE_NAME)); final Dbms prototype = new DbmsImpl(parent, dbms); this.enabled = prototype.isEnabled(); this.id = prototype.getId(); this.name = prototype.getName(); this.alias = prototype.getAlias(); this.typeName = prototype.getTypeName(); this.ipAddress = prototype.getIpAddress(); this.port = prototype.getPort(); this.username = prototype.getUsername(); this.schemas = unmodifiableList(super.children(SCHEMAS, ImmutableSchema::new).collect(toList())); }
@Override public BUILDER withUsername(String dbmsName, String username) { requireNonNull(dbmsName); // username nullable with(Dbms.class, dbmsName, d -> d.mutator().setUsername(username)); return self(); }
/** * Debug method used to track type mappers of a project. May be of future use if one perhaps would venture to * investigate why we get several copies of the dbms from the database */ private void printTypeMappers(String heading, Project p) { System.out.println(heading); p.dbmses().map(d -> (Dbms) d).forEach(dbms -> { dbms.schemas().map(s -> (Schema) s).forEach(schema -> { schema.tables().map(t -> (Table) t).forEach(table -> { table.columns().map(c -> (Column) c).filter(c -> c.getTypeMapper().isPresent()).forEach(column -> { String mapperName = column.getTypeMapper().get(); if (mapperName.endsWith("PrimitiveTypeMapper")) { mapperName = "Primitive"; } System.out.println(" - " + dbms.getName() + ":" + schema.getName() + "/" + table.getName() + "." + column.getName() + " mapped by " + mapperName); }); }); }); }); }
/** * Returns {@code true} if the two specified documents represents the same * element in the database. Two documents are considered same if they have * the same name and type and their parents are considered same. * * @param first the first document * @param second the second document * @return {@code true} if same, else {@code false} */ public static boolean isSame(Dbms first, Dbms second) { if (first.getId().equals(second.getId())) { final Project firstParent = first.getParentOrThrow(); final Project secondParent = second.getParentOrThrow(); return isSame(firstParent, secondParent); } else { return false; } }
@Override public String from(Dbms dbms) { final StringBuilder result = new StringBuilder() .append("jdbc:postgresql://") .append(dbms.getIpAddress().orElse("")); dbms.getPort().ifPresent(p -> result.append(":").append(p)); result.append("/").append(dbms.getName()); result.append("?stringtype=unspecified"); // to allow database JSON types to be written return result.toString(); } }
/** * Returns the password associated with the specified dbms. * This is equivalent to calling {@code get(dbms.getName())}. * * @param dbms the dbms used as a key * @return the associated password or empty */ default Optional<char[]> get(Dbms dbms) { requireNonNull(dbms); return get(dbms.getId()); } }
@Override public String from(Dbms dbms) { final StringBuilder result = new StringBuilder() .append("jdbc:mariadb://") .append(dbms.getIpAddress().orElse("")); dbms.getPort().ifPresent(p -> result.append(":").append(p)); result/*.append("/").append(dbms.getName()) */ // MariaDB treats this as default schema name .append("?useUnicode=true&characterEncoding=UTF-8") .append("&useServerPrepStmts=true&useCursorFetch=true") .append("&zeroDateTimeBehavior=convertToNull"); return result.toString(); } }
/** * Locates the {@link DbmsType} corresponding to the specified {@link Dbms}, * or throws a {@code SpeedmentException} if it can not be found. * * @param dbmsHandlerComponent the handler to look in * @param dbms the dbms to look for * @return the dbms type of that dbms */ public static DbmsType findDbmsType(DbmsHandlerComponent dbmsHandlerComponent, Dbms dbms) { return dbmsHandlerComponent.findByName(dbms.getTypeName()) .orElseThrow(() -> new SpeedmentException( "Could not find any installed DbmsType named '" + dbms.getTypeName() + "'." )); }
if (!naming.getSchemaExcludeSet().contains(name)) { if (filterCriteria.test(name)) { final Schema schema = dbms.mutator().addNewSchema(); schema.mutator().setId(name); schema.mutator().setName(name); if (filterCriteria.test(schemaName)) { if (!naming.getSchemaExcludeSet().contains(schemaName)) { final Schema schema = dbms.mutator().addNewSchema(); schema.mutator().setId(schemaName); schema.mutator().setName(schemaName); @SuppressWarnings({"unchecked", "rawtypes"}) final CompletableFuture<Schema>[] tablesTask = dbms.schemas() .map(schema -> tables(sqlTypeMappingTask, dbms, schema, progress)) .toArray(s -> (CompletableFuture<Schema>[]) new CompletableFuture[s]);
@Override public Connection newConnection(final Dbms dbms) { final String uri = DatabaseUtil.findConnectionUrl(dbmsHandlerComponent, dbms); final String username = unwrap(dbms.getUsername()); final char[] password = unwrap(passwordComponent.get(dbms)); return newConnection(uri, username, password); }
protected void checkDatabaseConnectivity(Injector injector) { LOGGER.debug("Checking Database Connectivity"); final Project project = injector.getOrThrow(ProjectComponent.class) .getProject(); project.dbmses().forEachOrdered(dbms -> { final DbmsHandlerComponent dbmsHandlerComponent = injector .getOrThrow(DbmsHandlerComponent.class); final DbmsType dbmsType = DatabaseUtil .dbmsTypeOf(dbmsHandlerComponent, dbms); final DbmsMetadataHandler handler = dbmsType.getMetadataHandler(); try { LOGGER.info(handler.getDbmsInfoString(dbms)); } catch (final SQLException sqle) { throw new SpeedmentException("Unable to establish initial " + "connection with the database named " + dbms.getName() + ".", sqle); } }); }
public StringProperty ipAddressProperty() { return stringPropertyOf(IP_ADDRESS, () -> Dbms.super.getIpAddress().orElse(null)); }
public IntegerProperty portProperty() { return integerPropertyOf(PORT, () -> Dbms.super.getPort().orElse(0)); }
@Override public String from(Dbms dbms) { return "jdbc:sqlite:" + dbms.getLocalPath().orElse(":memory:"); } }
/** * Determines the connection URL to use for the specified {@code Dbms} by * first: * <ol> * <li>checking if the {@code CONNECTION_URL} property is set; * <li>otherwise, calculate it using the {@link DbmsType}. * </ol> * If the {@link DbmsType} can not be found by calling * {@link DatabaseUtil#dbmsTypeOf(DbmsHandlerComponent, Dbms)}, a * {@code SpeedmentException} will be thrown. * * @param dbmsHandlerComponent the dbms handler component instance * @param dbms the database manager * @return the connection URL to use * @throws SpeedmentException if the {@link DbmsType} couldn't be found */ public static String findConnectionUrl(DbmsHandlerComponent dbmsHandlerComponent, Dbms dbms) throws SpeedmentException { final DbmsType type = findDbmsType(dbmsHandlerComponent, dbms); return dbms.getConnectionUrl().orElseGet(() -> type.getConnectionUrlGenerator().from(dbms)); }
public static Optional<? extends Dbms> referencedDbmsIfPresent(Project project, String dbmsId) { return project.dbmses().filter(dbms -> dbmsId.equals(dbms.getId())).findAny(); }
@Override public String from(Dbms dbms) { final StringBuilder result = new StringBuilder() .append("jdbc:mysql://") .append(dbms.getIpAddress().orElse("")); dbms.getPort().ifPresent(p -> result.append(":").append(p)); result/*.append("/").append(dbms.getName())*/ // MySQL treats this as default schema name .append("?useUnicode=true&characterEncoding=UTF-8") .append("&useServerPrepStmts=true") .append("&zeroDateTimeBehavior=") .append(driverVersion() >= 8 ? "CONVERT_TO_NULL" : "convertToNull") .append("&nullNamePatternMatchesAll=true") // Fix #190 .append("&useLegacyDatetimeCode=true"); // Fix #190 if (driverVersion() <= 5) { result.append("&useSSL=false"); } else { result.append("&serverTimezone=UTC"); } return result.toString(); } }
/** * Returns the {@link DbmsType} for the specified {@link Dbms}. * * @param handler the dbms handler to use * @param dbms the dbms * @return the type of that dbms */ public static DbmsType dbmsTypeOf(DbmsHandlerComponent handler, Dbms dbms) { final String typeName = dbms.getTypeName(); return handler.findByName(typeName).orElseThrow(() -> new SpeedmentException( "Unable to find the database type " + typeName + ". The installed types are: " + handler.supportedDbmsTypes() .map(DbmsType::getName) .collect(joining(", ")) ) ); }