~starbuggers/sakila-server/mysql-5.1-wl820

« back to all changes in this revision

Viewing changes to plugin/java_udf/com/mysql/udf/NativeUDFCallback.java

  • Committer: Antony T Curtis
  • Date: 2008-04-10 06:09:05 UTC
  • mto: (2542.76.4 mysql-5.1-wl820)
  • mto: This revision was merged to the branch mainline in revision 2772.
  • Revision ID: antony@anubis.xiphis.org-20080410060905-itpom5iyz8ae4dhh
Initial import into Bazaar repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.mysql.udf;
 
2
 
 
3
import java.math.BigDecimal;
 
4
import java.sql.ResultSetMetaData;
 
5
import java.sql.SQLException;
 
6
 
 
7
public class NativeUDFCallback implements UDFCallback {
 
8
 
 
9
    private static final int MYSQL_TYPE_STRING = 254;
 
10
 
 
11
    private static final int RESULT_INDEX = -1;
 
12
 
 
13
    private final long that;
 
14
 
 
15
    public NativeUDFCallback(long ptr) {
 
16
        this.that = ptr;
 
17
    }
 
18
 
 
19
    public long getPointer() {
 
20
        return that;
 
21
    }
 
22
 
 
23
    public int getArgumentCount() {
 
24
        int rv = get_argument_count(that);
 
25
        // System.err
 
26
        // .println("JAVA: NativeUDFCallback.getArgumentCount() = " + rv);
 
27
        return rv;
 
28
    }
 
29
 
 
30
    public int getArgumentType(int argIndex) {
 
31
        // System.err.println("JAVA: NativeUDFCallback.getArgumentType");
 
32
        return get_argument_type(that, argIndex);
 
33
    }
 
34
 
 
35
    public boolean isArgumentNull(int argIndex) {
 
36
        boolean rv = is_argument_null(that, argIndex);
 
37
        // System.err.println("JAVA: NativeUDFCallback.isArgumentNull(" +
 
38
        // argIndex + ") = " + rv);
 
39
        return rv;
 
40
    }
 
41
 
 
42
    public void setNullArgument(int idx) {
 
43
        // System.err.println("JAVA: NativeUDFCallback.setNullArgument");
 
44
        set_null_argument(that, idx);
 
45
    }
 
46
 
 
47
    public long getLongArgument(int argIndex) {
 
48
        // System.err.println("JAVA: NativeUDFCallback.getLongArgument");
 
49
        return get_long_argument(that, argIndex);
 
50
    }
 
51
 
 
52
    public void setLongArgument(int idx, long value) {
 
53
        // System.err.println("JAVA: NativeUDFCallback.setLongArgument");
 
54
        set_long_argument(that, idx, value);
 
55
    }
 
56
 
 
57
    public String getStringArgument(int argIndex) {
 
58
        // System.err.println("JAVA: NativeUDFCallback.getStringArgument");
 
59
        return get_string_argument(that, argIndex);
 
60
    }
 
61
 
 
62
    public void setStringArgument(int idx, String value) {
 
63
        // System.err.println("JAVA: NativeUDFCallback.setStringArgument");
 
64
        set_string_argument(that, idx, value);
 
65
    }
 
66
 
 
67
    public double getDoubleArgument(int argIndex) {
 
68
        // System.err.println("JAVA: NativeUDFCallback.getDoubleArgument");
 
69
        return get_double_argument(that, argIndex);
 
70
    }
 
71
 
 
72
    public void setDoubleArgument(int idx, double value) {
 
73
        // System.err.println("JAVA: NativeUDFCallback.setDoubleArgument");
 
74
        set_double_argument(that, idx, value);
 
75
    }
 
76
 
 
77
    public BigDecimal getBigDecimalArgument(int argIndex) {
 
78
        // System.err.println("JAVA: NativeUDFCallback.getBigDecimalArgument");
 
79
        return new BigDecimal(get_big_decimal_argument(that, argIndex));
 
80
    }
 
81
 
 
82
    public void setBigDecimalArgument(int idx, BigDecimal value) {
 
83
        // System.err.println("JAVA: NativeUDFCallback.setBigDecimalArgument");
 
84
        set_big_decimal_argument(that, idx, value.toString());
 
85
    }
 
86
 
 
87
    public void storeResultNull() {
 
88
        // System.err.println("JAVA: NativeUDFCallback.storeResultNull");
 
89
        set_null_argument(that, RESULT_INDEX);
 
90
    }
 
91
 
 
92
    public void storeResultLong(long value) {
 
93
        // System.err.println("JAVA: NativeUDFCallback.storeResultLong");
 
94
        set_long_argument(that, RESULT_INDEX, value);
 
95
    }
 
96
 
 
97
    public void storeResultString(String value) {
 
98
        // System.err.println("JAVA: NativeUDFCallback.storeResultString");
 
99
        set_string_argument(that, RESULT_INDEX, value);
 
100
    }
 
101
 
 
102
    public void storeResultDouble(double value) {
 
103
        // System.err.println("JAVA: NativeUDFCallback.storeResultDouble");
 
104
        set_double_argument(that, RESULT_INDEX, value);
 
105
    }
 
106
 
 
107
    public void storeResultBigDecimal(BigDecimal value) {
 
108
        // System.err.println("JAVA: NativeUDFCallback.storeResultBigDecimal");
 
109
        set_big_decimal_argument(that, RESULT_INDEX, value.toString());
 
110
    }
 
111
 
 
112
    public void storeResultResultSet(java.sql.ResultSet rs) {
 
113
        // System.err.println("JAVA: NativeUDFCallback.storeResultResultSet");
 
114
        try {
 
115
            int colcount;
 
116
            final ResultSetMetaData metaData = rs.getMetaData();
 
117
            colcount = metaData.getColumnCount();
 
118
            for (int i = 1; i <= colcount; i++) {
 
119
                String name = metaData.getColumnLabel(i);
 
120
                int width = metaData.getColumnDisplaySize(i);
 
121
                int mysql_type = MYSQL_TYPE_STRING;
 
122
                int precision = 0;
 
123
 
 
124
                row_field(that, name, mysql_type, width, precision);
 
125
            }
 
126
            while (rs.next()) {
 
127
                row_prepare(that);
 
128
                for (int i = 1; i <= colcount; i++) {
 
129
                    storeResultString(rs.getString(i));
 
130
                }
 
131
                row_send(that);
 
132
            }
 
133
            row_send_eof(that);
 
134
        } catch (SQLException e) {
 
135
            throw new UDFException(e);
 
136
        } finally {
 
137
            if (rs != null) {
 
138
                try {
 
139
                    rs.close();
 
140
                } catch (Exception e) {
 
141
                    // just log this:
 
142
                    e.printStackTrace();
 
143
                }
 
144
            }
 
145
        }
 
146
    }
 
147
 
 
148
    private static native int get_argument_count(long that);
 
149
 
 
150
    private static native int get_argument_type(long that, int arg_index);
 
151
 
 
152
    private static native boolean is_argument_null(long that, int arg_index);
 
153
 
 
154
    private static native void set_null_argument(long that, int idx);
 
155
 
 
156
    private static native long get_long_argument(long that, int arg_index);
 
157
 
 
158
    private static native void set_long_argument(long that, int idx, long value);
 
159
 
 
160
    private static native String get_string_argument(long that, int arg_index);
 
161
 
 
162
    private static native void set_string_argument(long that, int idx,
 
163
            String value);
 
164
 
 
165
    private static native double get_double_argument(long that, int arg_index);
 
166
 
 
167
    private static native void set_double_argument(long that, int idx,
 
168
            double value);
 
169
 
 
170
    private static native String get_big_decimal_argument(long that,
 
171
            int argIndex);
 
172
 
 
173
    private static native void set_big_decimal_argument(long that, int idx,
 
174
            String value);
 
175
 
 
176
    /* The following are for result sets */
 
177
 
 
178
    /* this method is called for each column in the result set to be constructed */
 
179
    private static native int row_field(long that, String column_name,
 
180
            int field_type, int column_width, int precision);
 
181
 
 
182
    /* call row_prepare() before filling in the row */
 
183
    private static native int row_prepare(long that);
 
184
 
 
185
    /* call store for each column value */
 
186
 
 
187
    /* call row_send for each row */
 
188
    private static native int row_send(long that);
 
189
 
 
190
    /* call row_send_eof after the last row */
 
191
    private static native int row_send_eof(long that);
 
192
}