1. /*
  2. * Copyright 2002-2004 the original author or authors.
  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 org.springframework.jdbc.core;
  17. import java.util.LinkedList;
  18. import java.util.List;
  19. /**
  20. * Object to represent a SQL parameter definition.
  21. * Parameters may be anonymous, in which case name is null.
  22. * However all parameters must define a SQL type constant
  23. * from java.sql.Types.
  24. * @author Rod Johnson
  25. */
  26. public class SqlParameter {
  27. private String name;
  28. /** SQL type constant from java.sql.Types */
  29. private int type;
  30. /** used for types that are user-named like: STRUCT, DISTINCT, JAVA_OBJECT, and named array types. */
  31. private String typeName;
  32. /**
  33. * Add a new anonymous parameter
  34. */
  35. public SqlParameter(int type) {
  36. this(null, type, null);
  37. }
  38. public SqlParameter(int type, String typeName) {
  39. this(null, type, typeName);
  40. }
  41. public SqlParameter(String name, int type) {
  42. this(name, type, null);
  43. }
  44. public SqlParameter(String name, int type, String typeName) {
  45. this.name = name;
  46. this.type = type;
  47. this.typeName = typeName;
  48. }
  49. public String getName() {
  50. return name;
  51. }
  52. public int getSqlType() {
  53. return type;
  54. }
  55. public String getTypeName() {
  56. return typeName;
  57. }
  58. /**
  59. * Convert a list of JDBC types, as defined in the java.sql.Types class,
  60. * to a List of SqlParameter objects as used in this package
  61. */
  62. public static List sqlTypesToAnonymousParameterList(int[] types) {
  63. List l = new LinkedList();
  64. if (types != null) {
  65. for (int i = 0; i < types.length; i++) {
  66. l.add(new SqlParameter(types[i]));
  67. }
  68. }
  69. return l;
  70. }
  71. }