Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright 2011 James Moger.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.iciql;
  17. import com.iciql.util.StringUtils;
  18. import java.text.MessageFormat;
  19. /**
  20. * Represents a traditional PreparedStatment fragment like "id=?, name=?".
  21. */
  22. public class RuntimeToken implements Token {
  23. final String fragment;
  24. final Object[] args;
  25. public RuntimeToken(String fragment, Object... args) {
  26. this.fragment = fragment;
  27. this.args = args == null ? new Object[0] : args;
  28. }
  29. /**
  30. * Append the SQL to the given statement using the given query.
  31. *
  32. * @param stat the statement to append the SQL to
  33. * @param query the query to use
  34. */
  35. @Override
  36. public <T> void appendSQL(SQLStatement stat, Query<T> query) {
  37. int tokenCount = StringUtils.count('?', fragment);
  38. if (tokenCount != args.length) {
  39. throw new IciqlException(MessageFormat.format(
  40. "Fragment \"{0}\" specifies {1} tokens but you supplied {2} args", fragment, tokenCount,
  41. args.length));
  42. }
  43. stat.appendSQL(fragment);
  44. for (Object arg : args) {
  45. stat.addParameter(arg);
  46. }
  47. }
  48. }