svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / dataaccess / ExistsFunction.java @ 44750
History | View | Annotate | Download (3.9 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.expressionevaluator.impl.function.dataaccess; |
25 |
|
26 |
import java.util.List; |
27 |
import org.apache.commons.lang3.Range; |
28 |
import org.gvsig.expressionevaluator.Codes; |
29 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
30 |
import org.gvsig.expressionevaluator.Interpreter; |
31 |
import org.gvsig.expressionevaluator.impl.DALFunctions; |
32 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
33 |
import static org.gvsig.fmap.dal.DataManager.FUNCTION_EXISTS; |
34 |
import org.gvsig.tools.util.UnmodifiableBasicCollection; |
35 |
import org.gvsig.tools.util.UnmodifiableBasicCollection64; |
36 |
|
37 |
/**
|
38 |
*
|
39 |
* @author jjdelcerro
|
40 |
*/
|
41 |
public class ExistsFunction extends AbstractFunction { |
42 |
|
43 |
public static final String NAME = "EXISTS"; |
44 |
|
45 |
public ExistsFunction() {
|
46 |
//
|
47 |
// https://www.postgresql.org/docs/current/functions-subquery.html
|
48 |
//
|
49 |
// Nota:
|
50 |
// En H2 no existe la funcion EXISTS, pero muy probablemente se pueda
|
51 |
// trasladar a:
|
52 |
// NVL2(COALESCE(lista-subselect),FALSE,TRUE)
|
53 |
//
|
54 |
super(DALFunctions.SYMBOLTABLE_NAME,
|
55 |
FUNCTION_EXISTS, |
56 |
Range.between(1, 2), |
57 |
"Receive a list as an argument. Returns true if the list has some value and false if the list is empty.",
|
58 |
FUNCTION_EXISTS + "({{list}})",
|
59 |
new String[]{ |
60 |
"list - a list of values or null"
|
61 |
}, |
62 |
"Boolean",
|
63 |
true
|
64 |
); |
65 |
} |
66 |
|
67 |
@Override
|
68 |
public boolean allowConstantFolding() { |
69 |
return false; |
70 |
} |
71 |
|
72 |
@Override
|
73 |
public boolean useArgumentsInsteadObjects() { |
74 |
return true; |
75 |
} |
76 |
|
77 |
@Override
|
78 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
79 |
throw new UnsupportedOperationException("Not supported yet."); |
80 |
} |
81 |
|
82 |
@Override
|
83 |
public Object call(Interpreter interpreter, Codes args) throws Exception { |
84 |
if (args.size() == 2) { |
85 |
Object id = this.getObject(interpreter, args, 1); |
86 |
if (id instanceof CharSequence) { |
87 |
if (interpreter.getSymbolTable().exists(id.toString())) {
|
88 |
Object value = interpreter.getSymbolTable().value(id.toString());
|
89 |
if (value == null) { |
90 |
return false; |
91 |
} |
92 |
if (value instanceof Boolean) { |
93 |
return value;
|
94 |
} |
95 |
} |
96 |
} |
97 |
} |
98 |
|
99 |
Object list = this.getObject(interpreter, args, 0); |
100 |
if (list == null) { |
101 |
return false; |
102 |
} |
103 |
boolean isEmpty = true; |
104 |
if (list instanceof Object[]) { |
105 |
isEmpty = (((Object[]) list).length==0); |
106 |
} else if (list instanceof List) { |
107 |
isEmpty = ((List) list).isEmpty();
|
108 |
} else if (list instanceof UnmodifiableBasicCollection) { |
109 |
isEmpty = ((UnmodifiableBasicCollection) list).isEmpty(); |
110 |
} else if (list instanceof UnmodifiableBasicCollection64) { |
111 |
isEmpty = ((UnmodifiableBasicCollection64) list).isEmpty(); |
112 |
} else {
|
113 |
throw new ExpressionRuntimeException("Invalid argument. List or null expected."); |
114 |
} |
115 |
// System.out.println("EXISTS: "+ interpreter.getSymbolTable().value("NAME")+", "+isEmpty);
|
116 |
return !isEmpty;
|
117 |
} |
118 |
|
119 |
} |