SetDate.java

  1. /***************************************************************************
  2.    Copyright 2012 Emily Estes

  3.    Licensed under the Apache License, Version 2.0 (the "License");
  4.    you may not use this file except in compliance with the License.
  5.    You may obtain a copy of the License at

  6.        http://www.apache.org/licenses/LICENSE-2.0

  7.    Unless required by applicable law or agreed to in writing, software
  8.    distributed under the License is distributed on an "AS IS" BASIS,
  9.    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10.    See the License for the specific language governing permissions and
  11.    limitations under the License.
  12. ***************************************************************************/
  13. package net.metanotion.sqlc.setters;


  14. import java.sql.Date;
  15. import java.sql.PreparedStatement;
  16. import java.sql.SQLException;
  17. import java.util.Map;

  18. public final class SetDate extends SQLSetter {
  19.     public SetDate(int count, String name) { super(count, name); }

  20.     @Override public void set(PreparedStatement stmt, Map<String,Object> env) throws SQLException {
  21.         Object o = env.get(name);
  22.         Date dt = null;
  23.         if(o instanceof Date) {
  24.             dt = (Date) o;
  25.         } else if(o instanceof java.util.Date) {
  26.             dt = new Date(((java.util.Date) o).getTime());
  27.         } else { throw new RuntimeException("Invalid Date Object."); }
  28.         stmt.setDate(count, dt);
  29.     }

  30.     @Override public String setStatic(String stmtVar, String paramVar) {
  31.         return stmtVar + ".setDate(" + count + ", new java.sql.Date(" + paramVar + ".getTime()))";
  32.     }
  33. }