You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SQLStatement.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.util.ArrayList;
  22. import com.iciql.util.JdbcUtils;
  23. /**
  24. * This class represents a parameterized SQL statement.
  25. */
  26. public class SQLStatement {
  27. private Db db;
  28. private StringBuilder buff = new StringBuilder();
  29. private String sql;
  30. private ArrayList<Object> params = new ArrayList<Object>();
  31. SQLStatement(Db db) {
  32. this.db = db;
  33. }
  34. void setSQL(String sql) {
  35. this.sql = sql;
  36. buff = new StringBuilder(sql);
  37. }
  38. public SQLStatement appendSQL(String s) {
  39. buff.append(s);
  40. sql = null;
  41. return this;
  42. }
  43. public SQLStatement appendTable(String schema, String table) {
  44. return appendSQL(db.getDialect().prepareTableName(schema, table));
  45. }
  46. public SQLStatement appendColumn(String column) {
  47. return appendSQL(db.getDialect().prepareColumnName(column));
  48. }
  49. String getSQL() {
  50. if (sql == null) {
  51. sql = buff.toString();
  52. }
  53. return sql;
  54. }
  55. SQLStatement addParameter(Object o) {
  56. params.add(o);
  57. return this;
  58. }
  59. ResultSet executeQuery() {
  60. try {
  61. return prepare(false).executeQuery();
  62. } catch (SQLException e) {
  63. throw new IciqlException(e);
  64. }
  65. }
  66. int executeUpdate() {
  67. PreparedStatement ps = null;
  68. try {
  69. ps = prepare(false);
  70. return ps.executeUpdate();
  71. } catch (SQLException e) {
  72. throw new IciqlException(e);
  73. } finally {
  74. JdbcUtils.closeSilently(ps);
  75. }
  76. }
  77. long executeInsert() {
  78. PreparedStatement ps = null;
  79. try {
  80. ps = prepare(true);
  81. ps.executeUpdate();
  82. long identity = -1;
  83. ResultSet rs = ps.getGeneratedKeys();
  84. if (rs != null && rs.next()) {
  85. identity = rs.getLong(1);
  86. }
  87. JdbcUtils.closeSilently(rs);
  88. return identity;
  89. } catch (SQLException e) {
  90. throw new IciqlException(e);
  91. } finally {
  92. JdbcUtils.closeSilently(ps);
  93. }
  94. }
  95. private static void setValue(PreparedStatement prep, int parameterIndex, Object x) {
  96. try {
  97. prep.setObject(parameterIndex, x);
  98. } catch (SQLException e) {
  99. throw new IciqlException(e);
  100. }
  101. }
  102. private PreparedStatement prepare(boolean returnGeneratedKeys) {
  103. PreparedStatement prep = db.prepare(getSQL(), returnGeneratedKeys);
  104. for (int i = 0; i < params.size(); i++) {
  105. Object o = params.get(i);
  106. setValue(prep, i + 1, o);
  107. }
  108. return prep;
  109. }
  110. }