Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add morf-redshift. Do not merge. #258

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions morf-redshift/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-parent</artifactId>
<version>2.5.1-SNAPSHOT</version>
</parent>

<name>Morf - Redshift</name>
<description>Morf is a library for cross-platform evolutionary relational database mechanics, database access and database imaging/cloning.</description>
<url>https://github.com/alfasoftware/morf</url>

<artifactId>morf-redshift</artifactId>

<properties>
<sonar.coverage.jacoco.xmlReportPaths>${basedir}/../${aggregate.report.dir}</sonar.coverage.jacoco.xmlReportPaths>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-core</artifactId>
</dependency>
<dependency>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-testsupport</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.alfasoftware.morf.jdbc.redshift;

import java.sql.Connection;
import java.util.Optional;
import java.util.Stack;

import javax.sql.XADataSource;

import org.alfasoftware.morf.jdbc.AbstractDatabaseType;
import org.alfasoftware.morf.jdbc.JdbcUrlElements;
import org.alfasoftware.morf.jdbc.SqlDialect;
import org.alfasoftware.morf.metadata.Schema;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Support for Redshift database hosts.
*
* @author Copyright (c) Alfa Financial Software 2019
*/
public final class Redshift extends AbstractDatabaseType {

private static final Log log = LogFactory.getLog(Redshift.class);

public static final String IDENTIFIER = "REDSHIFT";

public Redshift() {
super("com.amazon.redshift.Driver", IDENTIFIER);
}


@Override
public String formatJdbcUrl(JdbcUrlElements jdbcUrlElements) {
return "jdbc:redshift://"
+ jdbcUrlElements.getHostName()
+ (jdbcUrlElements.getPort() == 0 ? "" : ":" + jdbcUrlElements.getPort())
+ "/" + jdbcUrlElements.getDatabaseName()
;
}


@Override
public Schema openSchema(Connection connection, String databaseName, String schemaName) {
return new RedshiftMetaDataProvider(connection, schemaName);
}


/**
* Returns a Redshift XA data source.
*
* @throws IllegalStateException If the data source cannot be created.
*
* @see org.alfasoftware.morf.jdbc.DatabaseType#getXADataSource(String, String, String)
*/
@Override
public XADataSource getXADataSource(String jdbcUrl, String username, String password) {
throw new UnsupportedOperationException("Morf-Redshift does not implement XA connections. ");
}


@Override
public SqlDialect sqlDialect(String schemaName) {
return new RedshiftDialect(schemaName);
}


@Override
public boolean matchesProduct(String product) {
return product.equalsIgnoreCase("Redshift");
}


@Override
public Optional<JdbcUrlElements> extractJdbcUrl(String url) {
Stack<String> splitURL = splitJdbcUrl(url);

String scheme = splitURL.pop();

if (!scheme.equalsIgnoreCase("redshift")) {
return Optional.empty();
}

if (!splitURL.pop().equals("://")) {
// If the next characters are not "://" then die
throw new IllegalArgumentException("Expected '//' to follow the scheme name in [" + url + "]");
}

JdbcUrlElements.Builder connectionDetails = extractHostAndPort(splitURL);

// Now get the path
String path = extractPath(splitURL);

connectionDetails.withDatabaseName(path);

return Optional.of(connectionDetails.build());
}
}
Loading