|
| 1 | +package org.tarantool; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.LinkedHashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.ListIterator; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.tarantool.jdbc.SQLResultSet; |
| 11 | + |
| 12 | +public class JDBCBridge { |
| 13 | + public static final JDBCBridge EMPTY = new JDBCBridge(Collections.<TarantoolBase.SQLMetaData>emptyList(), Collections.<List<Object>>emptyList()); |
| 14 | + |
| 15 | + final List<TarantoolBase.SQLMetaData> sqlMetadata; |
| 16 | + final Map<String,Integer> columnsByName; |
| 17 | + final List<List<Object>> rows; |
| 18 | + |
| 19 | + protected JDBCBridge(TarantoolConnection connection) { |
| 20 | + this(connection.getSQLMetadata(),connection.getSQLData()); |
| 21 | + } |
| 22 | + |
| 23 | + protected JDBCBridge(List<TarantoolBase.SQLMetaData> sqlMetadata, List<List<Object>> rows) { |
| 24 | + this.sqlMetadata = sqlMetadata; |
| 25 | + this.rows = rows; |
| 26 | + columnsByName = new LinkedHashMap<String, Integer>((int) Math.ceil(sqlMetadata.size() / 0.75), 0.75f); |
| 27 | + for (int i = 0; i < sqlMetadata.size(); i++) { |
| 28 | + columnsByName.put(sqlMetadata.get(i).getName(), i + 1); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public static JDBCBridge query(TarantoolConnection connection, String sql, Object ... params) { |
| 33 | + connection.sql(sql, params); |
| 34 | + return new JDBCBridge(connection); |
| 35 | + } |
| 36 | + |
| 37 | + public static int update(TarantoolConnection connection, String sql, Object ... params) { |
| 38 | + return connection.update(sql, params).intValue(); |
| 39 | + } |
| 40 | + |
| 41 | + public static JDBCBridge mock(List<String> fields, List<List<Object>> values) { |
| 42 | + List<TarantoolBase.SQLMetaData> meta = new ArrayList<TarantoolBase.SQLMetaData>(fields.size()); |
| 43 | + for(String field:fields) { |
| 44 | + meta.add(new TarantoolBase.SQLMetaData(field)); |
| 45 | + } |
| 46 | + return new JDBCBridge(meta, values); |
| 47 | + } |
| 48 | + |
| 49 | + public static Object execute(TarantoolConnection connection, String sql, Object ... params) { |
| 50 | + connection.sql(sql, params); |
| 51 | + Long rowCount = connection.getSqlRowCount(); |
| 52 | + if(rowCount == null) { |
| 53 | + return new SQLResultSet(new JDBCBridge(connection)); |
| 54 | + } |
| 55 | + return rowCount.intValue(); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + public String getColumnName(int columnIndex) { |
| 60 | + return columnIndex > sqlMetadata.size() ? null : sqlMetadata.get(columnIndex - 1).getName(); |
| 61 | + } |
| 62 | + |
| 63 | + public Integer getColumnIndex(String columnName) { |
| 64 | + return columnsByName.get(columnName); |
| 65 | + } |
| 66 | + |
| 67 | + public int getColumnCount() { |
| 68 | + return columnsByName.size(); |
| 69 | + } |
| 70 | + |
| 71 | + public ListIterator<List<Object>> iterator() { |
| 72 | + return rows.listIterator(); |
| 73 | + } |
| 74 | + |
| 75 | + public int size() { |
| 76 | + return rows.size(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String toString() { |
| 81 | + return "JDBCBridge{" + |
| 82 | + "sqlMetadata=" + sqlMetadata + |
| 83 | + ", columnsByName=" + columnsByName + |
| 84 | + ", rows=" + rows + |
| 85 | + '}'; |
| 86 | + } |
| 87 | +} |
0 commit comments