svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / impl / expressionevaluator / ExecuteSQLFunction.java @ 45606
History | View | Annotate | Download (3.62 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.fmap.dal.store.jdbc2.impl.expressionevaluator; |
25 |
|
26 |
import org.apache.commons.lang3.Range; |
27 |
import org.gvsig.expressionevaluator.Interpreter; |
28 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
29 |
import org.gvsig.fmap.dal.DALLocator; |
30 |
import org.gvsig.fmap.dal.DataManager; |
31 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
32 |
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
35 |
import static org.gvsig.fmap.dal.store.jdbc2.impl.expressionevaluator.JDBCSymbolTableFactory.GROUP_JDBC; |
36 |
|
37 |
/**
|
38 |
*
|
39 |
* @author jjdelcerro
|
40 |
*/
|
41 |
public class ExecuteSQLFunction extends AbstractFunction { |
42 |
|
43 |
public static final String FUNCTION_EXECUTESQL = "EXECUTESQL"; |
44 |
|
45 |
public ExecuteSQLFunction() {
|
46 |
super( GROUP_JDBC,
|
47 |
FUNCTION_EXECUTESQL, |
48 |
Range.between(1,2), |
49 |
"Receive a connection and SQL string as argument.",
|
50 |
FUNCTION_EXECUTESQL + "({{connection}}, sql)",
|
51 |
new String[]{ |
52 |
"connection - conextion string (optional)",
|
53 |
"sql - sql string"
|
54 |
}, |
55 |
"Object",
|
56 |
false
|
57 |
); |
58 |
} |
59 |
|
60 |
@Override
|
61 |
public boolean allowConstantFolding() { |
62 |
return false; |
63 |
} |
64 |
|
65 |
@Override
|
66 |
public boolean useArgumentsInsteadObjects() { |
67 |
return false; |
68 |
} |
69 |
|
70 |
@Override
|
71 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
72 |
String connectionName;
|
73 |
String sql;
|
74 |
JDBCServerExplorer explorer; |
75 |
if (args.length==1) { |
76 |
sql = this.getStr(args, 0); |
77 |
FeatureStore store = (FeatureStore) interpreter.getSymbolTable().function(DataManager.FUNCTION_CURRENT_STORE).call(interpreter,(Object[]) null); |
78 |
explorer = (JDBCServerExplorer) store.getExplorer(); |
79 |
} else {
|
80 |
connectionName = this.getStr(args, 0); |
81 |
sql = this.getStr(args, 1); |
82 |
if (this.getObject(args, 0) instanceof JDBCServerExplorer) { |
83 |
explorer = (JDBCServerExplorer) this.getObject(args, 0); |
84 |
} else if (this.getObject(args, 0) instanceof FeatureStore) { |
85 |
explorer = (JDBCServerExplorer) ((FeatureStore) this.getObject(args, 0)).getExplorer(); |
86 |
} else {
|
87 |
DataManager dalManager = DALLocator.getManager(); |
88 |
DataServerExplorerPoolEntry poolEntry = dalManager.getDataServerExplorerPool().get(connectionName); |
89 |
DataServerExplorerParameters explorerParameters = poolEntry.getExplorerParameters(); |
90 |
explorer = (JDBCServerExplorer) dalManager.openServerExplorer(explorerParameters.getProviderName(), explorerParameters); |
91 |
} |
92 |
} |
93 |
Object result = explorer.execute(sql);
|
94 |
return result;
|
95 |
|
96 |
} |
97 |
|
98 |
} |