summaryrefslogtreecommitdiffstats
path: root/java/rasj/test/TestQuery.java
blob: 362b99f4ce439fe65af87280d656c887ea56df69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*************************************************************
 *
 *
 * PURPOSE:
 * test rasj: simple open/read query/close cycle.
 * rasj test program for executing a query against some rasdaman database.
 * Note: database is opened readonly, so send only SELECT queries.
 * 
 * @param --server s   - use server s (default: localhost)
 * @param --port p     - use server port p (default: 7001)
 * @param --database d - use database d (default: RASBASE)
 * @param --user u     - log in as user u (default: rasguest)
 * @param --passwd p   - log in with password p (default: rasguest)
 * @param --query q    - send SELECT query string q to server (default: "select r from RAS_COLLECTIONNAMES as r"")
 * @param --count c    - execute query c times (default: 1)
 *
 *
 * COMMENTS:
 * - no parameter line error handling
 ************************************************************/

import rasj.*;
import rasj.odmg.*;
import org.odmg.*;
import java.util.*;
import java.io.*;

public class TestQuery
  {

    public static void main( String[] args )
      {
        String server = "localhost";
        String port = "7001";
        String base = "RASBASE";
        String user = "rasguest";
        String passwd = "rasguest";
        String query = "select r from RAS_COLLECTIONNAMES as r";
        int count = 1;

        if (args.length == 0)
          {
            System.out.println( "usage: TestQuery [options]" );
            System.out.println( "options:" );
            System.out.println( "  --server s   - use server s (default: localhost)" );
            System.out.println( "  --port p     - use server port p (default: 7001)" );
            System.out.println( "  --database d - use database d (default: RASBASE)" );
            System.out.println( "  --user u     - log in as user u (default: rasguest)" );
            System.out.println( "  --passwd p   - log in with password p (default: rasguest)" );
            System.out.println( "  --query q    - send SELECT query string q to server (default: select r from RAS_COLLECTIONNAMES as r)" );
            System.out.println( "  --count c    - execute query c times (default: 1)" );
            return;
          }

        for (int i=args.length-1; i>=0; i--)
          {
            if (args[i].equals("--server"))
                server = args[i+1];
            if (args[i].equals("--port"))
                port = args[i+1];
            if (args[i].equals("--database"))
                base = args[i+1];
            if (args[i].equals("--user"))
                user = args[i+1];
            if (args[i].equals("--passwd"))
                passwd = args[i+1];
            if (args[i].equals("--query"))
                query = args[i+1];
            if (args[i].equals("--count"))
                count = Integer.parseInt(args[i+1]);
          }

        System.out.println( "Query test started with server=" + server + ", port=" + port + ", database=" + base + ", user=" + user + ", count=" + count + ", query=" + query );

        try
          {
            RasImplementation myApp = new RasImplementation("http://"+server+":"+port);
            myApp.setUserIdentification(user, passwd);

            System.out.println( "opening database..." );
            Database myDb = myApp.newDatabase();
            myDb.open( base, Database.OPEN_READ_WRITE );

            System.out.println( "starting transaction..." );
            Transaction myTa = myApp.newTransaction();
            myTa.begin();

            for (int i = 1; i <= count; i++)
              {

                System.out.print( "sending query #" + i + "..." );
                OQLQuery myQu = myApp.newOQLQuery();
                myQu.create(query);
                DBag result = (DBag) myQu.execute();

                System.out.println( "result is: " + result );
        
              }

            System.out.println( "closing transaction..." );
            myTa.commit();

            // System.out.println( "sending query out of TA, must produce an error..." );
            // OQLQuery myQu2 = myApp.newOQLQuery();
            // myQu2.create(query);
            // DBag result2 = (DBag) myQu2.execute();

            System.out.println( "closing database..." );
            myDb.close();

          }
        catch(Exception e)
          {
            System.err.println( e.getMessage() );
          }

        System.out.println( "Query test done." );

      } // main()

} // TestQuery