summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/java-stable-20060304/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/QueryString.java
blob: 8304457a727c4608f43fb95b83d338a45f0d0965 (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
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.das.rdb.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.tuscany.das.rdb.util.DebugUtil;


public class QueryString {

	private final String unmodifiedQueryString;
	private final String preparedString;
	private HashMap parameters = new HashMap();
	private static final boolean debug = false;
	
	public QueryString(String originalString) {
		this.unmodifiedQueryString = originalString;
		this.preparedString = replaceNamesAndSetIndexes(originalString);	
	}

	public String getPreparedString() {
		return this.preparedString;
	}
	
	public String getUnmodifiedString() {
		return this.unmodifiedQueryString;
	}
	
	public int getParameterIndex(String name) {
		DebugUtil.debugln(getClass(), debug, "Looking for parameter index for: " + name);
		//System.out.println(toString());
		return ((Integer)parameters.get(name)).intValue();
	}
	
	private String replaceNamesAndSetIndexes(String query) {
		DebugUtil.debugln(getClass(), debug, "Parameterizing query: " + query);
		Pattern p = Pattern.compile(":[\\S&&[^,()]]*");
		Matcher m = p.matcher(query);

		int index = 1;
		while (m.find()) {
			parameters.put(m.group().substring(1), new Integer(index));
			query = m.replaceFirst("?");
			m = p.matcher(query);
			index++;
		}
		
		DebugUtil.debugln(getClass(), debug, "Parameterized query: " + query);
		return query;
	}

	
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("\nOriginal SQL: ");
		buffer.append(unmodifiedQueryString);
		buffer.append("\nPrepared SQL: ");
		buffer.append(preparedString);
		buffer.append("\nParameters: ");
		Iterator i = parameters.keySet().iterator();
		while ( i.hasNext() ) {
			String key = (String)i.next();
			buffer.append("\n");
			buffer.append(key);
		}
		return buffer.toString();
	}

}