File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\expression\TableFunction.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.expression;
8
9 import java.util.ArrayList;
10 import org.h2.constant.ErrorCode;
11 import org.h2.engine.Database;
12 import org.h2.engine.Session;
13 import org.h2.message.DbException;
14 import org.h2.result.LocalResult;
15 import org.h2.result.ResultInterface;
16 import org.h2.table.Column;
17 import org.h2.tools.SimpleResultSet;
18 import org.h2.util.MathUtils;
19 import org.h2.util.StatementBuilder;
20 import org.h2.value.DataType;
21 import org.h2.value.Value;
22 import org.h2.value.ValueArray;
23 import org.h2.value.ValueNull;
24 import org.h2.value.ValueResultSet;
25
26 /**
27 * Implementation of the functions TABLE(..) and TABLE_DISTINCT(..).
28 */
29 public class TableFunction extends Function {
30 private final boolean distinct;
31 private final long rowCount;
32 private Column[] columnList;
33
34 TableFunction(Database database, FunctionInfo info, long rowCount) {
35 super(database, info);
36 distinct = info.type == Function.TABLE_DISTINCT;
37 this.rowCount = rowCount;
38 }
39
40 public Value getValue(Session session) {
41 return getTable(session, args, false, distinct);
42 }
43
44 protected void checkParameterCount(int len) {
45 if (len < 1) {
46 throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, getName(), ">0");
47 }
48 }
49
50 public String getSQL() {
51 StatementBuilder buff = new StatementBuilder(getName());
52 buff.append('(');
53 int i = 0;
54 for (Expression e : args) {
55 buff.appendExceptFirst(", ");
56 buff.append(columnList[i++].getCreateSQL()).append('=').append(e.getSQL());
57 }
58 return buff.append(')').toString();
59 }
60
61
62 public String getName() {
63 return distinct ? "TABLE_DISTINCT" : "TABLE";
64 }
65
66 public ValueResultSet getValueForColumnList(Session session, Expression[] nullArgs) {
67 return getTable(session, args, true, false);
68 }
69
70 public void setColumns(ArrayList<Column> columns) {
71 this.columnList = new Column[columns.size()];
72 columns.toArray(columnList);
73 }
74
75 private ValueResultSet getTable(Session session, Expression[] argList, boolean onlyColumnList, boolean distinctRows) {
76 int len = columnList.length;
77 Expression[] header = new Expression[len];
78 Database db = session.getDatabase();
79 for (int i = 0; i < len; i++) {
80 Column c = columnList[i];
81 ExpressionColumn col = new ExpressionColumn(db, c);
82 header[i] = col;
83 }
84 LocalResult result = new LocalResult(session, header, len);
85 if (distinctRows) {
86 result.setDistinct();
87 }
88 if (!onlyColumnList) {
89 Value[][] list = new Value[len][];
90 int rows = 0;
91 for (int i = 0; i < len; i++) {
92 Value v = argList[i].getValue(session);
93 if (v == ValueNull.INSTANCE) {
94 list[i] = new Value[0];
95 } else {
96 ValueArray array = (ValueArray) v.convertTo(Value.ARRAY);
97 Value[] l = array.getList();
98 list[i] = l;
99 rows = Math.max(rows, l.length);
100 }
101 }
102 for (int row = 0; row < rows; row++) {
103 Value[] r = new Value[len];
104 for (int j = 0; j < len; j++) {
105 Value[] l = list[j];
106 Value v;
107 if (l.length <= row) {
108 v = ValueNull.INSTANCE;
109 } else {
110 Column c = columnList[j];
111 v = l[row];
112 v = c.convert(v);
113 v = v.convertPrecision(c.getPrecision(), false);
114 v = v.convertScale(true, c.getScale());
115 }
116 r[j] = v;
117 }
118 result.addRow(r);
119 }
120 }
121 result.done();
122 ValueResultSet vr = ValueResultSet.get(getSimpleResultSet(result, Integer.MAX_VALUE));
123 return vr;
124 }
125
126 private static SimpleResultSet getSimpleResultSet(ResultInterface rs, int maxrows) {
127 int columnCount = rs.getVisibleColumnCount();
128 SimpleResultSet simple = new SimpleResultSet();
129 for (int i = 0; i < columnCount; i++) {
130 String name = rs.getColumnName(i);
131 int sqlType = DataType.convertTypeToSQLType(rs.getColumnType(i));
132 int precision = MathUtils.convertLongToInt(rs.getColumnPrecision(i));
133 int scale = rs.getColumnScale(i);
134 simple.addColumn(name, sqlType, precision, scale);
135 }
136 rs.reset();
137 for (int i = 0; i < maxrows && rs.next(); i++) {
138 Object[] list = new Object[columnCount];
139 for (int j = 0; j < columnCount; j++) {
140 list[j] = rs.currentRow()[j].getObject();
141 }
142 simple.addRow(list);
143 }
144 return simple;
145 }
146
147 public long getRowCount() {
148 return rowCount;
149 }
150
151 public Expression[] getExpressionColumns(Session session) {
152 return getExpressionColumns(session, getTable(session, getArgs(), true, false).getResultSet());
153 }
154
155 }
156