|
| 1 | +package com.annimon.ownlang.modules.jdbc; |
| 2 | + |
| 3 | +import com.annimon.ownlang.exceptions.ArgumentsMismatchException; |
| 4 | +import com.annimon.ownlang.exceptions.OwnLangRuntimeException; |
| 5 | +import com.annimon.ownlang.lib.*; |
| 6 | +import java.sql.Connection; |
| 7 | +import java.sql.PreparedStatement; |
| 8 | +import java.sql.SQLException; |
| 9 | +import static com.annimon.ownlang.modules.jdbc.JdbcConverters.*; |
| 10 | + |
| 11 | +class ConnectionValue extends MapValue { |
| 12 | + |
| 13 | + private final Connection connection; |
| 14 | + |
| 15 | + public ConnectionValue(Connection connection) { |
| 16 | + super(20); |
| 17 | + this.connection = connection; |
| 18 | + init(); |
| 19 | + } |
| 20 | + |
| 21 | + private void init() { |
| 22 | + set("createStatement", new FunctionValue(this::createStatement)); |
| 23 | + set("prepareStatement", new FunctionValue(this::prepareStatement)); |
| 24 | + set("close", new FunctionValue(this::close)); |
| 25 | + |
| 26 | + set("clearWarnings", voidFunction(connection::clearWarnings)); |
| 27 | + set("commit", voidFunction(connection::commit)); |
| 28 | + set("rollback", voidFunction(connection::rollback)); |
| 29 | + |
| 30 | + set("setHoldability", voidIntFunction(connection::setHoldability)); |
| 31 | + set("setTransactionIsolation", voidIntFunction(connection::setTransactionIsolation)); |
| 32 | + |
| 33 | + set("getAutoCommit", booleanFunction(connection::getAutoCommit)); |
| 34 | + set("isClosed", booleanFunction(connection::isClosed)); |
| 35 | + set("isReadOnly", booleanFunction(connection::isReadOnly)); |
| 36 | + |
| 37 | + set("getHoldability", intFunction(connection::getHoldability)); |
| 38 | + set("getNetworkTimeout", intFunction(connection::getNetworkTimeout)); |
| 39 | + set("getTransactionIsolation", intFunction(connection::getTransactionIsolation)); |
| 40 | + set("getUpdateCount", intFunction(connection::getHoldability)); |
| 41 | + |
| 42 | + set("getCatalog", stringFunction(connection::getCatalog)); |
| 43 | + set("getSchema", stringFunction(connection::getSchema)); |
| 44 | + } |
| 45 | + |
| 46 | + private Value createStatement(Value[] args) { |
| 47 | + try { |
| 48 | + return switch (args.length) { |
| 49 | + case 0 -> new StatementValue(connection.createStatement()); |
| 50 | + case 2 -> new StatementValue(connection.createStatement(args[0].asInt(), args[1].asInt())); |
| 51 | + case 3 -> new StatementValue(connection.createStatement(args[0].asInt(), args[1].asInt(), args[2].asInt())); |
| 52 | + default -> throw new ArgumentsMismatchException("Wrong number of arguments"); |
| 53 | + }; |
| 54 | + } catch (SQLException sqlex) { |
| 55 | + throw new OwnLangRuntimeException(sqlex); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private Value prepareStatement(Value[] args) { |
| 60 | + Arguments.checkRange(1, 4, args.length); |
| 61 | + try { |
| 62 | + final String sql = args[0].asString(); |
| 63 | + return switch (args.length) { |
| 64 | + case 1 -> new StatementValue(connection.prepareStatement(sql)); |
| 65 | + case 2 -> { |
| 66 | + final PreparedStatement ps = columnData(args[1], |
| 67 | + (int autogeneratedKeys) -> connection.prepareStatement(sql, autogeneratedKeys), |
| 68 | + (int[] columnIndices) -> connection.prepareStatement(sql, columnIndices), |
| 69 | + (String[] columnNames) -> connection.prepareStatement(sql, columnNames)); |
| 70 | + yield new StatementValue(ps); |
| 71 | + } |
| 72 | + case 3 -> new StatementValue(connection.prepareStatement(sql, args[1].asInt(), args[2].asInt())); |
| 73 | + case 4 -> new StatementValue(connection.prepareStatement(sql, args[1].asInt(), args[2].asInt(), args[3].asInt())); |
| 74 | + default -> throw new ArgumentsMismatchException("Wrong number of arguments"); |
| 75 | + }; |
| 76 | + } catch (SQLException sqlex) { |
| 77 | + throw new OwnLangRuntimeException(sqlex); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private Value close(Value[] args) { |
| 82 | + try { |
| 83 | + if (connection != null) { |
| 84 | + connection.close(); |
| 85 | + } |
| 86 | + } catch (SQLException sqlex) { |
| 87 | + // skip |
| 88 | + } |
| 89 | + return NumberValue.ZERO; |
| 90 | + } |
| 91 | +} |
0 commit comments