Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d931c8e88ffab1483f028ef93dab90333de58cd2 (plain) (blame)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package org.eclipse.linuxtools.systemtap.ui.ide.structures;

import java.util.StringTokenizer;

public final class Query {

	public String tableName;
	public String where;
	public String groupBy;
	public String orderBy;
	public String[] columnNames;
	public String[] newColumnNames;

	/**
	 * This constructor is only used internally to set the various fields
	 * to null.
	 */
	private Query() {
		this.where = null;
		this.groupBy = null;
		this.orderBy = null;
		this.newColumnNames = null;
	}
	
	public Query(final String tableName, final String[] columnNames) {
		this();
		this.tableName = tableName;
		this.columnNames = columnNames;
	}

	public Query(final Query query) {
		tableName = query.tableName;
		columnNames = query.columnNames;
		newColumnNames = query.newColumnNames;
		where = query.where;
		groupBy = query.groupBy;
		orderBy = query.orderBy;
	}

	public Query(final String tableName) {
		this();
		this.tableName = tableName;
		this.columnNames = null;
	}

	public Query(final String tableName, final String[] columnNames,
			final String[] newColumnNames, final String where,
			final String groupBy, final String orderBy) {
		this.tableName = tableName;
		this.columnNames = columnNames;
		this.newColumnNames = newColumnNames;
		this.where = where;
		this.groupBy = groupBy;
		this.orderBy = orderBy;
	}

	public String toString() {
		return buildQuery(tableName, columnNames, newColumnNames, where,
				groupBy, orderBy);
	}

	private String buildQuery(final String tableName, final String[] fields,
			final String[] columnNames, final String whereClauses,
			final String groupBy, final String orderBy) {
		String query = "SELECT ";
		if (fields == (null))
			query += "* ";
		else
			for (int i = 0; i < fields.length; i++) {
				query += fields[i];
				if ((columnNames != null)
						&& (columnNames.length == fields.length))
					query += " AS " + columnNames[i];
				if (i != (fields.length - 1))
					query += ", ";
			}
		query += " FROM " + tableName;
		if (whereClauses != null)
			query += " WHERE " + whereClauses;
		if (groupBy != null)
			query += " GROUP BY " + groupBy;
		if (orderBy != null)
			query += " ORDER BY " + orderBy;
		return query;
	}

	/**
	 * Turns the query into a string that can be written to a file and easily
	 * recreated as a Query object.
	 * 
	 * @return
	 */
	public String toSavableQueryString() {
		String query = tableName + ":: COLS ";
		if (columnNames != null)
			for (final String element : columnNames)
				query += element + " ";
		else
			query += "*";

		query += ":: NEW_COLS ";
		if (columnNames != null)
			for (final String element : newColumnNames)
				query += element + " ";
		query += ":: WHERE " + where;
		query += ":: GROUP " + groupBy;
		query += ":: ORDER " + orderBy;
		return query;
	}

	/**
	 * Creates a Query object from a string created by toSavableQueryString()
	 * 
	 * @param query
	 *            The query string to be turned into a Query object.
	 * @return a new Query object created by the given string.
	 */
	public static Query getQueryFromString(final String query) {
		if (query == null)
			return null;
		final String[] subQuery = query.split("::");
		final Query tempQuery = new Query(subQuery[0]);
		for (int i = 1; i < subQuery.length; i++) {
			subQuery[i] = subQuery[i].trim();
			if (subQuery[i].startsWith("COLS ")) {
				String temp = subQuery[i].replace("COLS ", "");
				temp = temp.trim();
				final StringTokenizer tok = new StringTokenizer(temp);
				final String next = tok.nextToken();
				if (next.equals("null") || next.equals("*"))
					tempQuery.columnNames = null;
				else {
					tempQuery.columnNames = new String[tok.countTokens() + 1];
					tempQuery.columnNames[0] = next;
					for (int j = 1; j < tempQuery.columnNames.length; j++)
						tempQuery.columnNames[j] = tok.nextToken();
				}
			} else if (subQuery[i].startsWith("NEW_COLS ")) {
				String temp = subQuery[i].replace("NEW_COLS ", "");
				temp = temp.trim();
				final StringTokenizer tok = new StringTokenizer(temp);
				final String next = tok.nextToken();
				if (next.equals("null"))
					tempQuery.newColumnNames = null;
				else {
					tempQuery.newColumnNames = new String[tok.countTokens() + 1];
					tempQuery.newColumnNames[0] = next;
					for (int j = 1; j < tempQuery.newColumnNames.length; j++)
						tempQuery.newColumnNames[j] = tok.nextToken();
				}
			} else if (subQuery[i].startsWith("WHERE ")) {
				String temp = subQuery[i].replace("WHERE ", "");
				temp = temp.trim();
				if (temp.equals("null"))
					tempQuery.where = null;
				else
					tempQuery.where = temp;
			} else if (subQuery[i].startsWith("GROUP ")) {
				String temp = subQuery[i].replace("GROUP ", "");
				temp = temp.trim();
				if (temp.equals("null"))
					tempQuery.groupBy = null;
				else
					tempQuery.groupBy = temp;
			} else if (subQuery[i].startsWith("ORDER ")) {
				String temp = subQuery[i].replace("ORDER ", "");
				temp = temp.trim();
				if (temp.equals("null"))
					tempQuery.orderBy = null;
				else
					tempQuery.orderBy = temp;
			}
		}
		return tempQuery;
	}
}

Back to the top