
io.kazuki.v0.internal.helper.SqlParamBindings Maven / Gradle / Ivy
The newest version!
/**
* Copyright 2014 Sunny Gleason and original author or authors
*
* 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 io.kazuki.v0.internal.helper;
import io.kazuki.v0.store.schema.model.Attribute;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.skife.jdbi.v2.SQLStatement;
public class SqlParamBindings {
private final AtomicInteger next = new AtomicInteger();
private final Map> params =
new LinkedHashMap>();
private final boolean positional;
public SqlParamBindings(boolean positional) {
this.positional = positional;
}
public String bind(String key, Object value, Attribute.Type type) {
Pair pair = params.get(key);
if (value != null && value instanceof String && type.equals(Attribute.Type.BOOLEAN)) {
value = Boolean.valueOf((String) value) ? 1 : 0;
}
if (pair == null) {
pair = new Pair(next.getAndIncrement(), value);
} else {
pair = new Pair(pair.first, value);
}
params.put(key, pair);
return positional ? "?" : ":" + key;
}
public String bind(String key, Attribute.Type type) {
return bind(key, null, type);
}
public void bindToStatement(SQLStatement> stmt) {
for (Map.Entry> e : params.entrySet()) {
Pair pair = e.getValue();
if (positional) {
stmt.bind(pair.first, pair.second);
} else {
stmt.bind(e.getKey(), pair.second);
}
}
}
public Map asMap() {
Map toReturn = new LinkedHashMap();
for (Map.Entry> e : params.entrySet()) {
Pair pair = e.getValue();
toReturn.put(e.getKey(), pair.second);
}
return Collections.unmodifiableMap(toReturn);
}
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy