Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 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.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import com.google.common.collect.Lists;
/**
* @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());
return Unchecked.get(() -> schema.select(query, key.array()));
}
@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) {
checkNotNull(row);
List columns = row.columns();
columns.forEach(this::checkValidColumn);
List