defs/Query.java
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- Query
- replaceVars
- getStruct
- startDoc
- endDoc
1 /******************
2 Copyright (c) 2002 RIPE NCC
3
4 All Rights Reserved
5
6 Permission to use, copy, modify, and distribute this software and its
7 documentation for any purpose and without fee is hereby granted,
8 provided that the above copyright notice appear in all copies and that
9 both that copyright notice and this permission notice appear in
10 supporting documentation, and that the name of the author not be
11 used in advertising or publicity pertaining to distribution of the
12 software without specific, written prior permission.
13
14 THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
16 AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
17 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
18 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 ***************************************/
21
22 /**
23 * RIPE attribute.
24 *
25 * @author ottrey@ripe.net
26 * @version $Version$
27 *
28 */
29 public class Query {
/* [<][>][^][v][top][bottom][index][help] */
30
31 private String qrytype;
32 private boolean lookup;
33 private String keytype;
34 private String code;
35 private String clars;
36 private String sqlQuery;
37 private String Querydesc;
38
39 // -----------------oOo-----------------
40 // Constructors
41 // -----------------oOo-----------------
42 /**
43 * Creates a Query structure.
44 *
45 * @author ottrey@ripe.net
46 *
47 */
48 public Query(String qrytype, boolean lookup, String keytype, String code, String clars, String sqlQuery) {
49
50 this.qrytype = qrytype;
51 this.lookup = lookup;
52 this.keytype = keytype.toUpperCase();
53 this.code = code;
54 this.clars = clars;
55 this.sqlQuery = sqlQuery;
56
57 // now prepare a query description
58 if( qrytype.equals("SQL") ) {
59 Querydesc = qrytype + ":" + clars + "/" + code + "/" + this.keytype;
60 } else {
61 Querydesc = qrytype + ":" + clars + "/" + this.keytype;
62 }
63 }// Query()
64
65 private String replaceVars(Defs defs) {
/* [<][>][^][v][top][bottom][index][help] */
66 String finalQuery = "";
67 int lastindex = 0;
68
69
70 // paste into finalQuery, replacing the symbols
71 do {
72 int begin, end;
73
74 begin = sqlQuery.indexOf("$(", lastindex);
75
76 if( begin != -1 ) {
77 end = sqlQuery.indexOf(")", begin);
78 String variable = sqlQuery.substring(begin+2, end);
79
80 //System.out.println( "var: " + variable );
81
82 finalQuery = finalQuery.concat(sqlQuery.substring(lastindex, begin));
83 finalQuery = finalQuery.concat(defs.getValueByEnum(variable) );
84 lastindex = end + 1;
85 } else {
86 //System.out.println( "finishing: " + sqlQuery.substring(lastindex));
87 finalQuery = finalQuery.concat(sqlQuery.substring(lastindex));
88 lastindex = sqlQuery.length();
89 }
90
91 //System.out.println( "final: " + finalQuery);
92
93 } while( lastindex < sqlQuery.length() );
94
95 return finalQuery;
96
97 } // replaceVars
98
99 /**
100 * @return String C - structure in the form:
101 * {
102 * R_SQL,
103 * Q_LOOKUP,
104 * WK_NAME,
105 * A_PN,
106 * "SELECT N01.pe_ro_id FROM %s WHERE %s",
107 * 0,
108 * 0
109 * },
110 * @param String the level of indenting.
111 *
112 */
113 public String getStruct(String indent, Defs defs) {
/* [<][>][^][v][top][bottom][index][help] */
114 String result = new String();
115
116 String FinalQuery = replaceVars(defs);
117
118 result += indent + "{" + "\n ";
119 result += indent + "R_" + qrytype.toUpperCase() + ",\n ";
120 if (lookup) {
121 result += indent + "Q_LOOKUP";
122 }
123 else {
124 result += indent + "Q_INVERSE";
125 }
126 result += ",\n ";
127 result += indent + keytype.toUpperCase() + ",\n ";
128 result += indent + "A_" + code.toUpperCase() + ",\n ";
129 if (clars.equals("ANY")) {
130 result += indent + "C_ANY" + ",\n ";
131 }
132 else {
133 result += indent + "C_" + clars.toUpperCase() + ",\n ";
134 }
135 result += indent + "\"" + FinalQuery.replace('\n', ' ') + "\"" + ",\n ";
136
137 result += indent + "\"" + Querydesc + "\"" + "\n ";
138
139 result += indent + "},";
140
141 return result;
142 } // getStruct()
143
144 public static String startDoc() {
/* [<][>][^][v][top][bottom][index][help] */
145 String result = new String();
146
147 // depend on external structure definition in defs.h
148 result += "Query_t Query[] = {\n";
149
150 return result;
151 } // startDoc()
152
153 public static String endDoc() {
/* [<][>][^][v][top][bottom][index][help] */
154 String result = new String();
155
156 result += " {\n";
157 result += " 0,\n";
158 result += " 0,\n";
159 result += " 0,\n";
160 result += " 0,\n";
161 result += " 0,\n";
162 result += " NULL\n";
163 result += " }\n";
164 result += "}; /* Query[] */\n";
165
166 return result;
167 } // endDoc()
168
169 } // Query
170