
br.com.objectos.db.PreparedStatementBinder Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.db;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
/**
* @author [email protected] (Marcio Endo)
*/
public class PreparedStatementBinder implements ParameterBinder {
private final SqlStatement owner;
private final PreparedStatement statement;
private int index = 1;
PreparedStatementBinder(SqlStatement owner, PreparedStatement statement) {
this.owner = owner;
this.statement = statement;
}
PreparedStatementBinder(SqlStatement owner, PreparedStatement statement, int index) {
this.owner = owner;
this.statement = statement;
this.index = index;
}
public SqlStatement addBatch() {
return owner.addBatch();
}
@Override
public PreparedStatementBinder booleanValue(boolean value) {
try {
statement.setBoolean(index++, value);
return this;
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
@Override
public PreparedStatementBinder doubleValue(double value) {
try {
statement.setDouble(index++, value);
return this;
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
public int executeUpdate() {
return owner.executeUpdate();
}
@Override
public PreparedStatementBinder floatValue(float value) {
try {
statement.setFloat(index++, value);
return this;
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
@Override
public PreparedStatementBinder intValue(int value) {
try {
statement.setInt(index++, value);
return this;
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
@Override
public PreparedStatementBinder localDate(LocalDate value) {
Date date = Date.from(value.atStartOfDay(ZoneId.systemDefault()).toInstant());
setDate(date);
return this;
}
@Override
public PreparedStatementBinder localDateTime(LocalDateTime value) {
Date date = Date.from(value.atZone(ZoneId.systemDefault()).toInstant());
setTimestamp(date);
return this;
}
@Override
public PreparedStatementBinder longValue(long value) {
try {
statement.setLong(index++, value);
return this;
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
@Override
public PreparedStatementBinder nullValue(ColumnType columnType) {
try {
columnType.setNull(statement, index++);
return this;
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
@Override
public PreparedStatementBinder string(String value) {
try {
statement.setString(index++, value);
return this;
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
@Override
public void variable(Variable> variable) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
private void setDate(java.util.Date date) {
try {
java.sql.Date sql = new java.sql.Date(date.getTime());
statement.setDate(index++, sql);
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
private void setTimestamp(java.util.Date date) {
try {
Timestamp sql = new Timestamp(date.getTime());
statement.setTimestamp(index++, sql);
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy