1 package org.opendaylight.controller.md.sal.dom.xsql.jdbc;
3 import org.opendaylight.controller.md.sal.dom.xsql.XSQLAdapter;
4 import org.opendaylight.controller.md.sal.dom.xsql.XSQLBluePrint;
5 import org.opendaylight.controller.md.sal.dom.xsql.XSQLBluePrintNode;
6 import org.opendaylight.controller.md.sal.dom.xsql.XSQLColumn;
7 import org.opendaylight.controller.md.sal.dom.xsql.XSQLCriteria;
9 import java.net.ServerSocket;
10 import java.net.Socket;
11 import java.sql.SQLException;
12 import java.util.ArrayList;
13 import java.util.List;
15 import java.util.StringTokenizer;
16 import java.util.concurrent.ConcurrentHashMap;
18 public class JDBCServer extends Thread {
19 private ServerSocket socket = null;
20 private XSQLAdapter adapter = null;
22 public JDBCServer(XSQLAdapter a) {
30 socket = new ServerSocket(40004);
31 while (!adapter.stopped) {
32 Socket s = socket.accept();
33 new JDBCConnection(s, adapter);
35 } catch (Exception err) {
36 err.printStackTrace();
40 public void connectToClient(String addr) {
42 Socket s = new Socket(addr, 50003);
43 new JDBCConnection(s, adapter);
44 } catch (Exception err) {
45 err.printStackTrace();
49 public static void execute(JDBCResultSet rs, XSQLAdapter adapter)
51 parseTables(rs, adapter.getBluePrint());
52 parseFields(rs, adapter.getBluePrint());
53 parseCriteria(rs, adapter.getBluePrint());
56 } catch (Exception err) {
57 throw new SQLException("Error", err);
61 public static void parseTables(JDBCResultSet rs, XSQLBluePrint bp)
63 String lowSQL = rs.getSQL().toLowerCase();
64 int from = lowSQL.indexOf("from");
65 int where = lowSQL.indexOf("where");
66 int subQuery = lowSQL.indexOf("select", 2);
67 int fromTo = lowSQL.indexOf(";");
69 if (where != -1 && subQuery != -1 && where < subQuery) {
71 } else if (where != -1 && subQuery != -1 && where > subQuery) {
73 } else if (where != -1) {
75 } else if (subQuery != -1) {
80 throw new SQLException("Missing \"from\" statement.");
84 throw new SQLException("Missing terminating \";\".");
87 String tableNames = rs.getSQL().substring(from + 4, fromTo).trim();
88 StringTokenizer tokens = new StringTokenizer(tableNames, ",");
89 while (tokens.hasMoreTokens()) {
90 String tableName = tokens.nextToken().trim();
91 XSQLBluePrintNode table = bp.getBluePrintNodeByTableName(tableName);
93 throw new SQLException(
94 "Unknown table name \"" + tableName + "\"");
96 rs.addTableToQuery(table);
100 public static void addCriteria(XSQLColumn col, XSQLCriteria c,
102 Map<XSQLColumn, List<XSQLCriteria>> tblCriteria =
103 rs.getCriteria().get(col.getTableName());
104 if (tblCriteria == null) {
106 new ConcurrentHashMap<XSQLColumn, List<XSQLCriteria>>();
107 rs.getCriteria().put(col.getTableName(), tblCriteria);
109 List<XSQLCriteria> lstCriteria = tblCriteria.get(col);
110 if (lstCriteria == null) {
111 lstCriteria = new ArrayList<XSQLCriteria>();
112 tblCriteria.put(col, lstCriteria);
117 public static void parseFields(JDBCResultSet rs, XSQLBluePrint bp)
118 throws SQLException {
119 String lowSQL = rs.getSQL().toLowerCase();
120 if (!lowSQL.startsWith("select")) {
121 throw new SQLException("Missing 'select' statement.");
123 int from = lowSQL.indexOf("from");
125 throw new SQLException("Missing 'from' statement.");
127 String fields = rs.getSQL().substring(6, from).trim();
128 StringTokenizer tokens = new StringTokenizer(fields, ",");
129 while (tokens.hasMoreTokens()) {
130 String token = tokens.nextToken().trim();
131 if (token.equals("*")) {
132 for (XSQLBluePrintNode table : rs.getTables()) {
133 rs.getFields().addAll(table.getColumns());
137 if (token.indexOf(".") != -1) {
138 XSQLBluePrintNode tbl = bp.getBluePrintNodeByTableName(
139 token.substring(0, token.indexOf(".")).trim());
140 String p = token.substring(token.indexOf(".") + 1);
142 for (XSQLColumn c : tbl.getColumns()) {
143 rs.getFields().add(c);
146 XSQLColumn col = tbl.findColumnByName(p);
147 rs.getFields().add(col);
150 XSQLColumn col = null;
151 for (XSQLBluePrintNode table : rs.getTables()) {
153 col = table.findColumnByName(token);
154 } catch (Exception err) {
161 throw new SQLException(
162 "Unknown field name '" + token + "'.");
165 rs.getFields().add(col);
170 public static void parseCriteria(JDBCResultSet rs, XSQLBluePrint bp) {
171 String lowSQL = rs.getSQL().toLowerCase();
172 int where = lowSQL.indexOf("where");
173 int order = lowSQL.indexOf("order");
174 int subQuery = lowSQL.indexOf("select", 2);
175 int whereTo = lowSQL.indexOf(";");
181 if (where != -1 && subQuery != -1 && subQuery < where) {
185 if (order != -1 && subQuery != -1 && order < subQuery) {
187 } else if (order != -1 && subQuery != -1 && order > subQuery) {
189 } else if (order != -1) {
191 } else if (subQuery != -1) {
194 String whereStatement =
195 rs.getSQL().substring(where + 5, whereTo).trim();
196 XSQLCriteria cr = new XSQLCriteria(whereStatement, -1);
197 for (XSQLBluePrintNode tbl : rs.getTables()) {
198 for (XSQLColumn col : tbl.getColumns()) {
199 String colCriteria = cr.getCriteriaForProperty(col);
200 if (colCriteria != null && !colCriteria.trim().equals("")) {
201 addCriteria(col, new XSQLCriteria(colCriteria, -1), rs);