Skip to content

Commit

Permalink
JDBC bindings (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlin authored Sep 18, 2020
1 parent 7298095 commit 050940f
Show file tree
Hide file tree
Showing 13 changed files with 754 additions and 30 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
sudo pip3 install --system pre-commit $PIP_DEPS black flake8 pylint
- name: pre-commit
run: pre-commit run --all-files
- name: mvn spotless:check
run: mvn spotless:check -B -f bindings/jdbc/genomicsqlite-jdbc/pom.xml

test:
runs-on: ubuntu-20.04
Expand Down Expand Up @@ -97,7 +99,7 @@ jobs:
name: libgenomicsqlite.dylib
path: libgenomicsqlite.dylib

combined-zip:
artifacts:
runs-on: ubuntu-20.04
needs: [linux-so, macOS-dylib]
steps:
Expand All @@ -123,3 +125,11 @@ jobs:
genomicsqlite.h
libgenomicsqlite.so
libgenomicsqlite.dylib
- name: build jar
run: |
cp libgenomicsqlite.so libgenomicsqlite.dylib bindings/jdbc/genomicsqlite-jdbc/src/main/resources/
mvn deploy -Drevision=${{ env.GIT_REVISION }} -B -f bindings/jdbc/genomicsqlite-jdbc/pom.xml
- uses: actions/upload-artifact@v2
with:
name: genomicsqlite-jdbc-${{ env.GIT_REVISION }}
path: bindings/jdbc/genomicsqlite-jdbc/target/mvn-repo
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
add_subdirectory(test)
include(CTest)
enable_testing()
set(GENOMICSQLITE_TEST_ENV env PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}/bindings/python:${PYTHONPATH} LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}:${LD_LIBRARY_PATH} GENOMICSQLITE_SYSTEM_LIBRARY=1)
set(GENOMICSQLITE_TEST_ENV env PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}/bindings/python:${PYTHONPATH} LIBGENOMICSQLITE=${CMAKE_BINARY_DIR}/libgenomicsqlite)
add_test(NAME smoke_test COMMAND ${GENOMICSQLITE_TEST_ENV} python3 ${CMAKE_CURRENT_SOURCE_DIR}/test/genomicsqlite_smoke_test.py)
add_test(NAME capi_smoke_test COMMAND ${GENOMICSQLITE_TEST_ENV} ${CMAKE_BINARY_DIR}/test/capi_smoke_test)
add_test(NAME JdbcTests COMMAND ${GENOMICSQLITE_TEST_ENV} mvn test -B -f ${CMAKE_CURRENT_SOURCE_DIR}/bindings/jdbc/genomicsqlite-jdbc/pom.xml)
add_test(NAME pytest COMMAND ${GENOMICSQLITE_TEST_ENV} python3 -m pytest -xv -n 4 --tb=short ${CMAKE_CURRENT_SOURCE_DIR}/test)
endif()
12 changes: 12 additions & 0 deletions bindings/jdbc/genomicsqlite-jdbc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
src/resources/libgenomicsqlite*
128 changes: 128 additions & 0 deletions bindings/jdbc/genomicsqlite-jdbc/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?xml version="1.0" encoding="UTF-8"?>

<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>

<groupId>net.mlin</groupId>
<artifactId>genomicsqlite-jdbc</artifactId>
<version>${revision}</version>
<packaging>jar</packaging>

<name>genomicsqlite-jdbc</name>
<url>https://github.com/mlin/GenomicSQLite</url>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>

<developers>
<developer>
<name>Mike Lin</name>
<email>dna@mlin.net</email>
<organization>mlin.net LLC</organization>
<organizationUrl>https://www.mlin.net</organizationUrl>
</developer>
</developers>

<scm>
<connection>scm:git:git://github.com/mlin/GenomicSQLite.git</connection>
<developerConnection>scm:git:ssh://github.com:mlin/GenomicSQLite.git</developerConnection>
<url>https://github.com/mlin/GenomicSQLite/tree/main</url>
</scm>

<distributionManagement>
<repository>
<id>internal.repo</id>
<name>Temporary Staging Repository</name>
<url>file://${project.build.directory}/mvn-repo</url>
</repository>
</distributionManagement>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>[3.31.1,)</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<java>
<includes>
<include>src/**/*.java</include>
</includes>
<importOrder/>
<removeUnusedImports/>
<googleJavaFormat/>
</java>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package net.mlin.genomicsqlite;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

public class GenomicSQLite {
public static String version(Connection conn) throws SQLException {
ResultSet row = conn.createStatement().executeQuery("SELECT genomicsqlite_version()");
row.next();
return row.getString(1);
}

public static String attachSQL(
Connection conn, String dbFileName, String schemaName, String configJson)
throws SQLException {
PreparedStatement stmt = conn.prepareStatement("SELECT genomicsqlite_attach_sql(?,?,?)");
stmt.setString(1, dbFileName);
stmt.setString(2, schemaName);
stmt.setString(3, configJson);
ResultSet row = stmt.executeQuery();
row.next();
return row.getString(1);
}

public static String attachSQL(Connection conn, String dbFileName, String schemaName)
throws SQLException {
return attachSQL(conn, dbFileName, schemaName, "{}");
}

public static String vacuumIntoSQL(Connection conn, String destFileName, String configJson)
throws SQLException {
PreparedStatement stmt = conn.prepareStatement("SELECT genomicsqlite_vacuum_into_sql(?,?)");
stmt.setString(1, destFileName);
stmt.setString(2, configJson);
ResultSet row = stmt.executeQuery();
row.next();
return row.getString(1);
}

public static String vacuumIntoSQL(Connection conn, String destFileName) throws SQLException {
return vacuumIntoSQL(conn, destFileName, "{}");
}

public static String createGenomicRangeIndexSQL(
Connection conn,
String tableName,
String rid,
String beginPosition,
String endPosition,
int floor)
throws SQLException {
PreparedStatement stmt =
conn.prepareStatement("SELECT create_genomic_range_index_sql(?,?,?,?,?)");
stmt.setString(1, tableName);
stmt.setString(2, rid);
stmt.setString(3, beginPosition);
stmt.setString(4, endPosition);
stmt.setInt(5, floor);
ResultSet row = stmt.executeQuery();
row.next();
return row.getString(1);
}

public static String createGenomicRangeIndexSQL(
Connection conn, String tableName, String rid, String beginPosition, String endPosition)
throws SQLException {
return createGenomicRangeIndexSQL(conn, tableName, rid, beginPosition, endPosition, -1);
}

public static String putReferenceAssemblySQL(Connection conn, String assembly)
throws SQLException {
PreparedStatement stmt = conn.prepareStatement("SELECT put_genomic_reference_assembly_sql(?)");
stmt.setString(1, assembly);
ResultSet row = stmt.executeQuery();
row.next();
return row.getString(1);
}

public static String putReferenceSequenceSQL(
Connection conn,
String name,
long length,
String assembly,
String refget_id,
String meta_json,
long rid)
throws SQLException {
PreparedStatement stmt =
conn.prepareStatement("SELECT put_genomic_reference_sequence_sql(?,?,?,?,?,?)");
stmt.setString(1, name);
stmt.setLong(2, length);
stmt.setString(3, assembly);
stmt.setString(4, refget_id);
stmt.setString(5, meta_json);
stmt.setLong(6, rid);
ResultSet row = stmt.executeQuery();
row.next();
return row.getString(1);
}

public static String putReferenceSequenceSQL(
Connection conn,
String name,
long length,
String assembly,
String refget_id,
String meta_json)
throws SQLException {
PreparedStatement stmt =
conn.prepareStatement("SELECT put_genomic_reference_sequence_sql(?,?,?,?,?)");
stmt.setString(1, name);
stmt.setLong(2, length);
if (assembly != null) stmt.setString(3, assembly);
if (refget_id != null) stmt.setString(4, refget_id);
if (meta_json != null) stmt.setString(5, meta_json);
ResultSet row = stmt.executeQuery();
row.next();
return row.getString(1);
}

public static String putReferenceSequenceSQL(
Connection conn, String name, long length, String assembly, String refget_id)
throws SQLException {
return putReferenceSequenceSQL(conn, name, length, assembly, refget_id, null);
}

public static String putReferenceSequenceSQL(
Connection conn, String name, long length, String assembly) throws SQLException {
return putReferenceSequenceSQL(conn, name, length, assembly, null, null);
}

public static String putReferenceSequenceSQL(Connection conn, String name, long length)
throws SQLException {
return putReferenceSequenceSQL(conn, name, length, null, null, null);
}

public static HashMap<Long, ReferenceSequence> getReferenceSequencesByRid(
Connection conn, String assembly) throws SQLException {
String sql =
"SELECT _gri_rid, gri_refseq_name, gri_refseq_length, gri_assembly, gri_refget_id, gri_refseq_meta_json FROM _gri_refseq";
PreparedStatement stmt;
if (assembly != null) {
stmt = conn.prepareStatement(sql + " WHERE gri_assembly = ?");
stmt.setString(1, assembly);
} else {
stmt = conn.prepareStatement(sql);
}
ResultSet row = stmt.executeQuery();
HashMap<Long, ReferenceSequence> ans = new HashMap<Long, ReferenceSequence>();
while (row.next()) {
ReferenceSequence grs =
new ReferenceSequence(
row.getLong(1),
row.getString(2),
row.getLong(3),
row.getString(4),
row.getString(5),
row.getString(6));
ans.put(grs.rid, grs);
}
return ans;
}

public static HashMap<Long, ReferenceSequence> getReferenceSequencesByRid(Connection conn)
throws SQLException {
return getReferenceSequencesByRid(conn, null);
}

public static HashMap<String, ReferenceSequence> getReferenceSequencesByName(
Connection conn, String assembly) throws SQLException {
HashMap<String, ReferenceSequence> ans = new HashMap<String, ReferenceSequence>();
for (HashMap.Entry<Long, ReferenceSequence> entry :
getReferenceSequencesByRid(conn, assembly).entrySet()) {
ReferenceSequence grs = entry.getValue();
if (ans.containsKey(grs.name)) {
throw new RuntimeException(
"GenomicSQLite.getReferenceSequencesByName: duplicate name; try filtering by assembly");
}
ans.put(grs.name, grs);
}
return ans;
}

public static HashMap<String, ReferenceSequence> getReferenceSequencesByName(Connection conn)
throws SQLException {
return getReferenceSequencesByName(conn, null);
}
}
Loading

0 comments on commit 050940f

Please sign in to comment.