defs/Query.java
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- Query
- replaceVars
- getStruct
- startDoc
- endDoc
1 /**
2 * RIPE attribute.
3 *
4 * @author ottrey@ripe.net
5 * @version $Version$
6 *
7 */
8 public class Query {
/* [<][>][^][v][top][bottom][index][help] */
9
10 private String qrytype;
11 private boolean lookup;
12 private String keytype;
13 private String code;
14 private String clars;
15 private String sqlQuery;
16 private String Querydesc;
17
18 // -----------------oOo-----------------
19 // Constructors
20 // -----------------oOo-----------------
21 /**
22 * Creates a Query structure.
23 *
24 * @author ottrey@ripe.net
25 *
26 */
27 public Query(String qrytype, boolean lookup, String keytype, String code, String clars, String sqlQuery) {
28
29 this.qrytype = qrytype;
30 this.lookup = lookup;
31 this.keytype = keytype.toUpperCase();
32 this.code = code;
33 this.clars = clars;
34 this.sqlQuery = sqlQuery;
35
36 // now prepare a query description
37 if( qrytype.equals("SQL") ) {
38 Querydesc = qrytype + ":" + clars + "/" + code + "/" + this.keytype;
39 } else {
40 Querydesc = qrytype + ":" + clars + "/" + this.keytype;
41 }
42 }// Query()
43
44 private String replaceVars(Defs defs) {
/* [<][>][^][v][top][bottom][index][help] */
45 String finalQuery = "";
46 int lastindex = 0;
47
48
49 // paste into finalQuery, replacing the symbols
50 do {
51 int begin, end;
52
53 begin = sqlQuery.indexOf("$(", lastindex);
54
55 if( begin != -1 ) {
56 end = sqlQuery.indexOf(")", begin);
57 String variable = sqlQuery.substring(begin+2, end);
58
59 //System.out.println( "var: " + variable );
60
61 finalQuery = finalQuery.concat(sqlQuery.substring(lastindex, begin));
62 finalQuery = finalQuery.concat(defs.getValueByEnum(variable) );
63 lastindex = end + 1;
64 } else {
65 //System.out.println( "finishing: " + sqlQuery.substring(lastindex));
66 finalQuery = finalQuery.concat(sqlQuery.substring(lastindex));
67 lastindex = sqlQuery.length();
68 }
69
70 //System.out.println( "final: " + finalQuery);
71
72 } while( lastindex < sqlQuery.length() );
73
74 return finalQuery;
75
76 } // replaceVars
77
78 /**
79 * @return String C - structure in the form:
80 * {
81 * R_SQL,
82 * Q_LOOKUP,
83 * WK_NAME,
84 * A_PN,
85 * "SELECT N01.pe_ro_id FROM %s WHERE %s",
86 * 0,
87 * 0
88 * },
89 * @param String the level of indenting.
90 *
91 */
92 public String getStruct(String indent, Defs defs) {
/* [<][>][^][v][top][bottom][index][help] */
93 String result = new String();
94
95 String FinalQuery = replaceVars(defs);
96
97 result += indent + "{" + "\n ";
98 result += indent + "R_" + qrytype.toUpperCase() + ",\n ";
99 if (lookup) {
100 result += indent + "Q_LOOKUP";
101 }
102 else {
103 result += indent + "Q_INVERSE";
104 }
105 result += ",\n ";
106 result += indent + keytype.toUpperCase() + ",\n ";
107 result += indent + "A_" + code.toUpperCase() + ",\n ";
108 if (clars.equals("ANY")) {
109 result += indent + "C_ANY" + ",\n ";
110 }
111 else {
112 result += indent + "C_" + clars.toUpperCase() + ",\n ";
113 }
114 result += indent + "\"" + FinalQuery.replace('\n', ' ') + "\"" + ",\n ";
115
116 result += indent + "\"" + Querydesc + "\"" + "\n ";
117
118 result += indent + "},";
119
120 return result;
121 } // getStruct()
122
123 public static String startDoc() {
/* [<][>][^][v][top][bottom][index][help] */
124 String result = new String();
125
126 // depend on external structure definition in defs.h
127 result += "Query_t Query[] = {\n";
128
129 return result;
130 } // startDoc()
131
132 public static String endDoc() {
/* [<][>][^][v][top][bottom][index][help] */
133 String result = new String();
134
135 result += " {\n";
136 result += " 0,\n";
137 result += " 0,\n";
138 result += " 0,\n";
139 result += " 0,\n";
140 result += " 0,\n";
141 result += " NULL\n";
142 result += " }\n";
143 result += "}; /* Query[] */\n";
144
145 return result;
146 } // endDoc()
147
148 } // Query
149