~pbms-core/pbms/async_read

« back to all changes in this revision

Viewing changes to mybs/java/src/com/mysql/jdbc/MysqlParameterMetadata.java

  • Committer: paul-mccullagh
  • Date: 2008-03-26 11:35:17 UTC
  • Revision ID: paul-mccullagh-afb1610c21464a577ae428d72fc725eb986c05a5
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2005 MySQL AB
 
3
 
 
4
   This program is free software; you can redistribute it and/or modify
 
5
   it under the terms of version 2 of the GNU General Public License as 
 
6
   published by the Free Software Foundation.
 
7
 
 
8
   There are special exceptions to the terms and conditions of the GPL 
 
9
   as it is applied to this software. View the full text of the 
 
10
   exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
 
11
   software distribution.
 
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
 
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
   GNU General Public License for more details.
 
17
 
 
18
   You should have received a copy of the GNU General Public License
 
19
   along with this program; if not, write to the Free Software
 
20
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
 
 
22
*/
 
23
 
 
24
package com.mysql.jdbc;
 
25
 
 
26
import java.sql.ParameterMetaData;
 
27
import java.sql.SQLException;
 
28
import java.sql.Types;
 
29
 
 
30
public class MysqlParameterMetadata implements ParameterMetaData {
 
31
        boolean returnSimpleMetadata = false;
 
32
        ResultSetMetaData metadata = null;
 
33
        int parameterCount = 0;
 
34
        
 
35
        
 
36
        MysqlParameterMetadata(Field[] fieldInfo, int parameterCount) {
 
37
                this.metadata = new ResultSetMetaData(fieldInfo, false);
 
38
                
 
39
                this.parameterCount = parameterCount;
 
40
        }
 
41
        
 
42
        /**
 
43
         * Used for "fake" basic metadata for client-side prepared statements
 
44
         * when we don't know the parameter types.
 
45
         * 
 
46
         * @param parameterCount
 
47
         */
 
48
        MysqlParameterMetadata(int count) {
 
49
                this.parameterCount = count;
 
50
                this.returnSimpleMetadata = true;
 
51
        }
 
52
        
 
53
        public int getParameterCount() throws SQLException {
 
54
                return this.parameterCount;
 
55
        }
 
56
 
 
57
        public int isNullable(int arg0) throws SQLException {
 
58
                checkAvailable();
 
59
                
 
60
                return this.metadata.isNullable(arg0);
 
61
        }
 
62
 
 
63
        private void checkAvailable() throws SQLException {
 
64
                if (this.metadata == null || this.metadata.fields == null) {
 
65
                        throw SQLError.createSQLException(
 
66
                                "Parameter metadata not available for the given statement",
 
67
                                SQLError.SQL_STATE_DRIVER_NOT_CAPABLE);
 
68
                }
 
69
        }
 
70
 
 
71
        public boolean isSigned(int arg0) throws SQLException {
 
72
                if (this.returnSimpleMetadata) {
 
73
                        checkBounds(arg0);
 
74
                        
 
75
                        return false;
 
76
                }
 
77
                
 
78
                checkAvailable();
 
79
                
 
80
                return (this.metadata.isSigned(arg0));
 
81
        }
 
82
 
 
83
        public int getPrecision(int arg0) throws SQLException {
 
84
                if (this.returnSimpleMetadata) {
 
85
                        checkBounds(arg0);
 
86
                        
 
87
                        return 0;
 
88
                }
 
89
                
 
90
                checkAvailable();
 
91
                
 
92
                return (this.metadata.getPrecision(arg0));
 
93
        }
 
94
 
 
95
        public int getScale(int arg0) throws SQLException {
 
96
                if (this.returnSimpleMetadata) {
 
97
                        checkBounds(arg0);
 
98
                        
 
99
                        return 0;
 
100
                }
 
101
                
 
102
                checkAvailable();
 
103
                
 
104
                return (this.metadata.getScale(arg0));
 
105
        }
 
106
 
 
107
        public int getParameterType(int arg0) throws SQLException {
 
108
                if (this.returnSimpleMetadata) {
 
109
                        checkBounds(arg0);
 
110
                        
 
111
                        return Types.VARCHAR;
 
112
                }
 
113
                
 
114
                checkAvailable();
 
115
                
 
116
                return (this.metadata.getColumnType(arg0));
 
117
        }
 
118
 
 
119
        public String getParameterTypeName(int arg0) throws SQLException {
 
120
                if (this.returnSimpleMetadata) {
 
121
                        checkBounds(arg0);
 
122
                        
 
123
                        return "VARCHAR";
 
124
                }
 
125
                
 
126
                checkAvailable();
 
127
                
 
128
                return (this.metadata.getColumnTypeName(arg0));
 
129
        }
 
130
 
 
131
        public String getParameterClassName(int arg0) throws SQLException {
 
132
                if (this.returnSimpleMetadata) {
 
133
                        checkBounds(arg0);
 
134
                        
 
135
                        return "java.lang.String";
 
136
                }
 
137
                
 
138
                checkAvailable();
 
139
                
 
140
                return (this.metadata.getColumnClassName(arg0));
 
141
        }
 
142
 
 
143
        public int getParameterMode(int arg0) throws SQLException {
 
144
                return parameterModeIn;
 
145
        }
 
146
        
 
147
        private void checkBounds(int paramNumber) throws SQLException {
 
148
                if (paramNumber < 1) {
 
149
                        throw SQLError.createSQLException("Parameter index of '" + paramNumber + 
 
150
                                        "' is invalid.",
 
151
                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
 
152
                }
 
153
                
 
154
                if (paramNumber > this.parameterCount) {
 
155
                        throw SQLError.createSQLException("Parameter index of '" + paramNumber + 
 
156
                                        "' is greater than number of parameters, which is '" + 
 
157
                                        this.parameterCount + "'.",
 
158
                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
 
159
                                        
 
160
                }
 
161
        }
 
162
}