001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.rowio;
032:
033: import java.math.BigDecimal;
034: import java.sql.Date;
035: import java.sql.Time;
036: import java.sql.Timestamp;
037:
038: import org.hsqldb.CachedRow;
039: import org.hsqldb.Column;
040: import org.hsqldb.HsqlDateTime;
041: import org.hsqldb.lib.StringConverter;
042: import org.hsqldb.types.Binary;
043: import org.hsqldb.types.JavaObject;
044:
045: /**
046: * @author fredt@users
047: * @since 1.7.2
048: * @version 1.7.2
049: */
050: public class RowOutputTextLog extends RowOutputBase {
051:
052: static final byte[] BYTES_NULL = "NULL".getBytes();
053: static final byte[] BYTES_TRUE = "TRUE".getBytes();
054: static final byte[] BYTES_FALSE = "FALSE".getBytes();
055: static final byte[] BYTES_AND = " AND ".getBytes();
056: static final byte[] BYTES_IS = " IS ".getBytes();
057: public static final int MODE_DELETE = 1;
058: public static final int MODE_INSERT = 0;
059: private boolean isWritten;
060: private int logMode;
061:
062: public void setMode(int mode) {
063: logMode = mode;
064: }
065:
066: protected void writeFieldPrefix() {
067:
068: if (logMode == MODE_DELETE && isWritten) {
069: write(BYTES_AND);
070: }
071: }
072:
073: protected void writeChar(String s, int t) {
074:
075: write('\'');
076: StringConverter.unicodeToAscii(this , s, true);
077: write('\'');
078: }
079:
080: protected void writeReal(Double o, int type) {
081: writeBytes(Column.createSQLString(((Number) o).doubleValue()));
082: }
083:
084: protected void writeSmallint(Number o) {
085: this .writeBytes(o.toString());
086: }
087:
088: public void writeEnd() {
089: }
090:
091: protected void writeTime(Time o) {
092:
093: write('\'');
094: writeBytes(o.toString());
095: write('\'');
096: }
097:
098: protected void writeBinary(Binary o, int t) {
099:
100: ensureRoom(o.getBytesLength() * 2 + 2);
101: write('\'');
102: StringConverter.writeHex(getBuffer(), count, o.getBytes());
103:
104: count += o.getBytesLength() * 2;
105:
106: write('\'');
107: }
108:
109: public void writeType(int type) {
110: }
111:
112: public void writeSize(int size) {
113: }
114:
115: protected void writeDate(Date o) {
116:
117: write('\'');
118: this .writeBytes(o.toString());
119: write('\'');
120: }
121:
122: public int getSize(CachedRow row) {
123: return 0;
124: }
125:
126: protected void writeInteger(Number o) {
127: this .writeBytes(o.toString());
128: }
129:
130: protected void writeBigint(Number o) {
131: this .writeBytes(o.toString());
132: }
133:
134: //fredt@users - patch 1108647 by nkowalcz@users (NataliaK) fix for IS NULL
135: protected void writeNull(int type) {
136:
137: if (logMode == MODE_DELETE) {
138: write(BYTES_IS);
139: } else if (isWritten) {
140: write(',');
141: }
142:
143: isWritten = true;
144:
145: write(BYTES_NULL);
146: }
147:
148: protected void writeOther(JavaObject o) {
149:
150: ensureRoom(o.getBytesLength() * 2 + 2);
151: write('\'');
152: StringConverter.writeHex(getBuffer(), count, o.getBytes());
153:
154: count += o.getBytesLength() * 2;
155:
156: write('\'');
157: }
158:
159: public void writeString(String value) {
160: StringConverter.unicodeToAscii(this , value, false);
161: }
162:
163: protected void writeBit(Boolean o) {
164: write(o.booleanValue() ? BYTES_TRUE : BYTES_FALSE);
165: }
166:
167: protected void writeDecimal(BigDecimal o) {
168: this .writeBytes(o.toString());
169: }
170:
171: protected void writeFieldType(int type) {
172:
173: if (logMode == MODE_DELETE) {
174: write('=');
175: } else if (isWritten) {
176: write(',');
177: }
178:
179: isWritten = true;
180: }
181:
182: public void writeLongData(long value) {
183: this .writeBytes(Long.toString(value));
184: }
185:
186: public void writeIntData(int i, int position) {
187: }
188:
189: protected void writeTimestamp(Timestamp o) {
190:
191: write('\'');
192: this .writeBytes(HsqlDateTime.getTimestampString(o));
193: write('\'');
194: }
195:
196: public void writeShortData(short i) {
197: writeBytes(Integer.toString(i));
198: }
199:
200: public void writeIntData(int i) {
201: writeBytes(Integer.toString(i));
202: }
203:
204: public void reset() {
205:
206: super .reset();
207:
208: isWritten = false;
209: }
210: }
|