01: /*
02: * ParameterDefinition.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.sql.preparedstatement;
13:
14: import java.sql.PreparedStatement;
15: import java.sql.SQLException;
16: import workbench.log.LogMgr;
17: import workbench.util.SqlUtil;
18: import workbench.util.ValueConverter;
19:
20: /**
21: * The defintion for a single parameter in StatementParameters
22: *
23: * @see StatementParameters
24: * @author support@sql-workbench.net
25: */
26: public class ParameterDefinition {
27: private final ValueConverter converter = new ValueConverter();
28: private int type;
29: private int index;
30:
31: private boolean valueValid = false;
32: private Object value = null;
33:
34: public ParameterDefinition(int index, int type) {
35: this .index = index;
36: this .type = type;
37: }
38:
39: public int getIndex() {
40: return this .index;
41: }
42:
43: public int getType() {
44: return this .type;
45: }
46:
47: public boolean isValueValid(String v) {
48: try {
49: converter.convertValue(v, this .type);
50: return true;
51: } catch (Exception e) {
52: return false;
53: }
54: }
55:
56: public boolean setValue(String v) {
57: try {
58: this .value = converter.convertValue(v, this .type);
59: this .valueValid = true;
60: } catch (Exception e) {
61: this .valueValid = false;
62: LogMgr.logError("ParameterDefinition.setValue()",
63: "Error applying value " + v + " for type "
64: + SqlUtil.getTypeName(this .type), e);
65: }
66: return this .valueValid;
67: }
68:
69: public Object getValue() {
70: return this .value;
71: }
72:
73: public void setStatementValue(PreparedStatement stmt)
74: throws IllegalStateException, SQLException {
75: if (!this .valueValid)
76: throw new IllegalStateException(
77: "No valid value defined for parameter "
78: + this.index);
79: stmt.setObject(this.index, this.value);
80: }
81:
82: }
|