001: /*-------------------------------------------------------------------------
002: *
003: * Copyright (c) 2004-2005, PostgreSQL Global Development Group
004: * Copyright (c) 2004, Open Cloud Limited.
005: *
006: * IDENTIFICATION
007: * $PostgreSQL: pgjdbc/org/postgresql/core/v3/SimpleQuery.java,v 1.11 2006/08/06 18:02:45 jurka Exp $
008: *
009: *-------------------------------------------------------------------------
010: */
011: package org.postgresql.core.v3;
012:
013: import org.postgresql.core.*;
014: import java.lang.ref.PhantomReference;
015:
016: /**
017: * V3 Query implementation for a single-statement query.
018: * This also holds the state of any associated server-side
019: * named statement. We use a PhantomReference managed by
020: * the QueryExecutor to handle statement cleanup.
021: *
022: * @author Oliver Jowett (oliver@opencloud.com)
023: */
024: class SimpleQuery implements V3Query {
025: SimpleQuery(String[] fragments) {
026: this .fragments = fragments;
027: }
028:
029: public ParameterList createParameterList() {
030: if (fragments.length == 1)
031: return NO_PARAMETERS;
032:
033: return new SimpleParameterList(fragments.length - 1);
034: }
035:
036: public String toString(ParameterList parameters) {
037: StringBuffer sbuf = new StringBuffer(fragments[0]);
038: for (int i = 1; i < fragments.length; ++i) {
039: if (parameters == null)
040: sbuf.append('?');
041: else
042: sbuf.append(parameters.toString(i));
043: sbuf.append(fragments[i]);
044: }
045: return sbuf.toString();
046: }
047:
048: public String toString() {
049: return toString(null);
050: }
051:
052: public void close() {
053: unprepare();
054: }
055:
056: //
057: // V3Query
058: //
059:
060: public SimpleQuery[] getSubqueries() {
061: return null;
062: }
063:
064: //
065: // Implementation guts
066: //
067:
068: String[] getFragments() {
069: return fragments;
070: }
071:
072: void setStatementName(String statementName) {
073: this .statementName = statementName;
074: this .encodedStatementName = Utils.encodeUTF8(statementName);
075: }
076:
077: void setStatementTypes(int[] paramTypes) {
078: this .preparedTypes = paramTypes;
079: }
080:
081: String getStatementName() {
082: return statementName;
083: }
084:
085: boolean isPreparedFor(int[] paramTypes) {
086: if (statementName == null)
087: return false; // Not prepared.
088:
089: // Check for compatible types.
090: for (int i = 0; i < paramTypes.length; ++i)
091: if (paramTypes[i] != 0 && paramTypes[i] != preparedTypes[i])
092: return false;
093:
094: return true;
095: }
096:
097: byte[] getEncodedStatementName() {
098: return encodedStatementName;
099: }
100:
101: void setCleanupRef(PhantomReference cleanupRef) {
102: if (this .cleanupRef != null) {
103: this .cleanupRef.clear();
104: this .cleanupRef.enqueue();
105: }
106: this .cleanupRef = cleanupRef;
107: }
108:
109: void unprepare() {
110: if (cleanupRef != null) {
111: cleanupRef.clear();
112: cleanupRef.enqueue();
113: cleanupRef = null;
114: }
115:
116: statementName = null;
117: encodedStatementName = null;
118: }
119:
120: private final String[] fragments;
121: private String statementName;
122: private byte[] encodedStatementName;
123: private PhantomReference cleanupRef;
124: private int[] preparedTypes;
125:
126: final static SimpleParameterList NO_PARAMETERS = new SimpleParameterList(
127: 0);
128: }
|