1 | /*
|
---|
2 | * To change this template, choose Tools | Templates
|
---|
3 | * and open the template in the editor.
|
---|
4 | */
|
---|
5 |
|
---|
6 | package wcs.tools;
|
---|
7 |
|
---|
8 | import java.util.Iterator;
|
---|
9 | import org.odmg.DBag;
|
---|
10 | import org.odmg.Database;
|
---|
11 | import org.odmg.Implementation;
|
---|
12 | import org.odmg.ODMGException;
|
---|
13 | import org.odmg.OQLQuery;
|
---|
14 | import org.odmg.Transaction;
|
---|
15 | import rasj.RasImplementation;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | *
|
---|
19 | * @author andrei
|
---|
20 | */
|
---|
21 | public class QueryExample
|
---|
22 | {
|
---|
23 | public final String query = "select max_cells(c.0) from rgb as c";
|
---|
24 |
|
---|
25 | private String server;
|
---|
26 | private String base;
|
---|
27 | Implementation myApp = null;
|
---|
28 | Database myDb = null;
|
---|
29 | Transaction myTa = null;
|
---|
30 |
|
---|
31 | public QueryExample(String s, String b)
|
---|
32 | {
|
---|
33 | this.server = s;
|
---|
34 | this.base = b;
|
---|
35 |
|
---|
36 | myApp = new RasImplementation(server);
|
---|
37 | myDb = myApp.newDatabase();
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void log(String msg)
|
---|
41 | {
|
---|
42 | System.err.flush();
|
---|
43 | System.out.println("log> " + msg);
|
---|
44 | System.out.flush();
|
---|
45 | }
|
---|
46 |
|
---|
47 | private void init() throws ODMGException
|
---|
48 | {
|
---|
49 | if (myApp == null)
|
---|
50 | {
|
---|
51 | myApp = new RasImplementation(server);
|
---|
52 | }
|
---|
53 | if (myDb == null)
|
---|
54 | {
|
---|
55 | myDb = myApp.newDatabase();
|
---|
56 | }
|
---|
57 | log("Opening database...");
|
---|
58 | myDb.open(base, Database.OPEN_READ_ONLY);
|
---|
59 | log("Beginning new transaction ...");
|
---|
60 | myTa = myApp.newTransaction();
|
---|
61 | myTa.begin();
|
---|
62 | }
|
---|
63 |
|
---|
64 | private void finish() throws ODMGException
|
---|
65 | {
|
---|
66 | log("Commiting transaction ...");
|
---|
67 | myTa.commit();
|
---|
68 | log("Closing database...");
|
---|
69 | myDb.close();
|
---|
70 | log("Finished !");
|
---|
71 | }
|
---|
72 |
|
---|
73 | private void abort() throws ODMGException
|
---|
74 | {
|
---|
75 | log("Aborting transaction...");
|
---|
76 | myTa.abort();
|
---|
77 | log("Closing database...");
|
---|
78 | myDb.close();
|
---|
79 | log("Finished !");
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void sendQuery() throws ODMGException
|
---|
83 | {
|
---|
84 |
|
---|
85 |
|
---|
86 | init();
|
---|
87 |
|
---|
88 | log("Querying for all available collections ...");
|
---|
89 | OQLQuery myQu = myApp.newOQLQuery();
|
---|
90 | myQu.create(query);
|
---|
91 |
|
---|
92 | DBag resultSet = (DBag) myQu.execute();
|
---|
93 | if (resultSet != null)
|
---|
94 | {
|
---|
95 | Iterator iter = resultSet.iterator();
|
---|
96 | while (iter.hasNext())
|
---|
97 | {
|
---|
98 | Byte myByte = (Byte) iter.next();
|
---|
99 | System.out.println("*** Result is: " + myByte);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | finish();
|
---|
104 | }
|
---|
105 |
|
---|
106 | public static void main(String[] args) throws ODMGException
|
---|
107 | {
|
---|
108 | QueryExample ex = new QueryExample(
|
---|
109 | "http://kahlua.eecs.jacobs-university.de:9001", "RASSERVICE");
|
---|
110 | ex.sendQuery();
|
---|
111 | }
|
---|
112 | }
|
---|