1 /*
2 * Hibernate, Relational Persistence for Idiomatic Java
3 *
4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Middleware LLC.
8 *
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
23 *
24 */
25 package org.hibernate.engine.query;
26
27 import java.io.Serializable;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Set;
31
32 import org.hibernate.QueryParameterException;
33 import org.hibernate.type.Type;
34
35 /**
36 * Encapsulates metadata about parameters encountered within a query.
37 *
38 * @author Steve Ebersole
39 */
40 public class ParameterMetadata implements Serializable {
41
42 private static final OrdinalParameterDescriptor[] EMPTY_ORDINALS = new OrdinalParameterDescriptor[0];
43
44 private final OrdinalParameterDescriptor[] ordinalDescriptors;
45 private final Map namedDescriptorMap;
46
47 /**
48 * Instantiates a ParameterMetadata container.
49 *
50 * @param ordinalDescriptors
51 * @param namedDescriptorMap
52 */
53 public ParameterMetadata(OrdinalParameterDescriptor[] ordinalDescriptors, Map namedDescriptorMap) {
54 if ( ordinalDescriptors == null ) {
55 this.ordinalDescriptors = EMPTY_ORDINALS;
56 }
57 else {
58 OrdinalParameterDescriptor[] copy = new OrdinalParameterDescriptor[ ordinalDescriptors.length ];
59 System.arraycopy( ordinalDescriptors, 0, copy, 0, ordinalDescriptors.length );
60 this.ordinalDescriptors = copy;
61 }
62 if ( namedDescriptorMap == null ) {
63 this.namedDescriptorMap = java.util.Collections.EMPTY_MAP;
64 }
65 else {
66 int size = ( int ) ( ( namedDescriptorMap.size() / .75 ) + 1 );
67 Map copy = new HashMap( size );
68 copy.putAll( namedDescriptorMap );
69 this.namedDescriptorMap = java.util.Collections.unmodifiableMap( copy );
70 }
71 }
72
73 public int getOrdinalParameterCount() {
74 return ordinalDescriptors.length;
75 }
76
77 public OrdinalParameterDescriptor getOrdinalParameterDescriptor(int position) {
78 if ( position < 1 || position > ordinalDescriptors.length ) {
79 throw new IndexOutOfBoundsException( "Remember that ordinal parameters are 1-based!" );
80 }
81 return ordinalDescriptors[position - 1];
82 }
83
84 public Type getOrdinalParameterExpectedType(int position) {
85 return getOrdinalParameterDescriptor( position ).getExpectedType();
86 }
87
88 public int getOrdinalParameterSourceLocation(int position) {
89 return getOrdinalParameterDescriptor( position ).getSourceLocation();
90 }
91
92 public Set getNamedParameterNames() {
93 return namedDescriptorMap.keySet();
94 }
95
96 public NamedParameterDescriptor getNamedParameterDescriptor(String name) {
97 NamedParameterDescriptor meta = ( NamedParameterDescriptor ) namedDescriptorMap.get( name );
98 if ( meta == null ) {
99 throw new QueryParameterException( "could not locate named parameter [" + name + "]" );
100 }
101 return meta;
102 }
103
104 public Type getNamedParameterExpectedType(String name) {
105 return getNamedParameterDescriptor( name ).getExpectedType();
106 }
107
108 public int[] getNamedParameterSourceLocations(String name) {
109 return getNamedParameterDescriptor( name ).getSourceLocations();
110 }
111
112 }