1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.bnf.context; |
7 | |
8 | import org.h2.util.New; |
9 | |
10 | import java.sql.DatabaseMetaData; |
11 | import java.sql.ResultSet; |
12 | import java.sql.SQLException; |
13 | import java.util.ArrayList; |
14 | |
15 | /** |
16 | * Contains meta data information about a procedure. |
17 | * This class is used by the H2 Console. |
18 | */ |
19 | public class DbProcedure { |
20 | |
21 | private final DbSchema schema; |
22 | private final String name; |
23 | private final String quotedName; |
24 | private boolean returnsResult; |
25 | private DbColumn[] parameters; |
26 | |
27 | public DbProcedure(DbSchema schema, ResultSet rs) throws SQLException { |
28 | this.schema = schema; |
29 | name = rs.getString("PROCEDURE_NAME"); |
30 | returnsResult = rs.getShort("PROCEDURE_TYPE") == |
31 | DatabaseMetaData.procedureReturnsResult; |
32 | quotedName = schema.getContents().quoteIdentifier(name); |
33 | } |
34 | |
35 | /** |
36 | * @return The schema this table belongs to. |
37 | */ |
38 | public DbSchema getSchema() { |
39 | return schema; |
40 | } |
41 | |
42 | /** |
43 | * @return The column list. |
44 | */ |
45 | public DbColumn[] getParameters() { |
46 | return parameters; |
47 | } |
48 | |
49 | /** |
50 | * @return The table name. |
51 | */ |
52 | public String getName() { |
53 | return name; |
54 | } |
55 | |
56 | /** |
57 | * @return The quoted table name. |
58 | */ |
59 | public String getQuotedName() { |
60 | return quotedName; |
61 | } |
62 | |
63 | /** |
64 | * @return True if this function return a value |
65 | */ |
66 | public boolean isReturnsResult() { |
67 | return returnsResult; |
68 | } |
69 | |
70 | /** |
71 | * Read the column for this table from the database meta data. |
72 | * |
73 | * @param meta the database meta data |
74 | */ |
75 | void readParameters(DatabaseMetaData meta) throws SQLException { |
76 | ResultSet rs = meta.getProcedureColumns(null, schema.name, name, null); |
77 | ArrayList<DbColumn> list = New.arrayList(); |
78 | while (rs.next()) { |
79 | DbColumn column = DbColumn.getProcedureColumn(schema.getContents(), rs); |
80 | if (column.getPosition() > 0) { |
81 | // Not the return type |
82 | list.add(column); |
83 | } |
84 | } |
85 | rs.close(); |
86 | parameters = new DbColumn[list.size()]; |
87 | // Store the parameter in the good position [1-n] |
88 | for (int i = 0; i < parameters.length; i++) { |
89 | DbColumn column = list.get(i); |
90 | if (column.getPosition() > 0 |
91 | && column.getPosition() <= parameters.length) { |
92 | parameters[column.getPosition() - 1] = column; |
93 | } |
94 | } |
95 | } |
96 | |
97 | } |