| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.bnf.context; |
| 7 |
|
| 8 |
import java.sql.DatabaseMetaData; |
| 9 |
import java.sql.ResultSet; |
| 10 |
import java.sql.SQLException; |
| 11 |
import java.util.ArrayList; |
| 12 |
|
| 13 |
import org.h2.util.Utils; |
| 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 final 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 |
* @throws SQLException on failure |
| 75 |
*/ |
| 76 |
void readParameters(DatabaseMetaData meta) throws SQLException { |
| 77 |
ResultSet rs = meta.getProcedureColumns(null, schema.name, name, null); |
| 78 |
ArrayList<DbColumn> list = Utils.newSmallArrayList(); |
| 79 |
while (rs.next()) { |
| 80 |
DbColumn column = DbColumn.getProcedureColumn(schema.getContents(), rs); |
| 81 |
if (column.getPosition() > 0) { |
| 82 |
// Not the return type |
| 83 |
list.add(column); |
| 84 |
} |
| 85 |
} |
| 86 |
rs.close(); |
| 87 |
parameters = new DbColumn[list.size()]; |
| 88 |
// Store the parameter in the good position [1-n] |
| 89 |
for (int i = 0; i < parameters.length; i++) { |
| 90 |
DbColumn column = list.get(i); |
| 91 |
if (column.getPosition() > 0 |
| 92 |
&& column.getPosition() <= parameters.length) { |
| 93 |
parameters[column.getPosition() - 1] = column; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
} |