
com.lithium.flow.table.SqlTable Maven / Gradle / Ivy
/*
* Copyright 2015 Lithium Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lithium.flow.table;
import static com.google.common.base.Preconditions.checkNotNull;
import com.lithium.flow.db.Schema;
import com.lithium.flow.util.Logs;
import com.lithium.flow.util.Unchecked;
import com.lithium.flow.util.UncheckedException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.slf4j.Logger;
/**
* @author Matt Ayres
*/
public class SqlTable implements Table {
private static final Logger log = Logs.getLogger();
private final Schema schema;
private final String table;
private final List keyColumns;
private final List validColumns;
public SqlTable(@Nonnull Schema schema, @Nonnull String table, List keyColumns) throws SQLException {
this.schema = checkNotNull(schema);
this.table = checkNotNull(table);
this.keyColumns = checkNotNull(keyColumns);
this.validColumns = buildColumns(schema, table);
}
@Override
@Nullable
public T getCell(@Nonnull Key key, @Nonnull String column, @Nonnull Class clazz) {
checkNotNull(key);
checkValidColumn(column);
checkNotNull(clazz);
String query = buildSelectQuery(column);
log.debug("select: {} {}", query, key.list());
try {
return schema.select(query, key.array());
} catch (SQLException e) {
throw new UncheckedException(e);
}
}
@Override
@Nonnull
public Row getRow(@Nonnull Key key) {
String query = buildSelectQuery("*");
log.debug("select: {} {}", query, key.list());
return Unchecked.get(() -> {
Row row = new Row(key);
schema.queryRows(query, rs -> {
ResultSetMetaData meta = rs.getMetaData();
for (int i = 1; i <= meta.getColumnCount(); i++) {
String column = meta.getColumnName(i);
row.putCell(column, rs.getObject(i));
}
}, key.array());
return row;
});
}
@Override
public void putRow(@Nonnull Row row) {
writeRow(row, this::buildInsertQuery);
}
@Override
public void putRows(@Nonnull List rows) {
checkNotNull(rows);
List columns = rows.get(0).columns();
columns.forEach(this::checkValidColumn);
String query = buildInsertQuery(columns);
Unchecked.run(() -> {
try (Connection con = schema.getConnection()) {
con.setAutoCommit(false);
try (PreparedStatement ps = con.prepareStatement(query)) {
for (Row row : rows) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy