001 /** 002 * Copyright (c) 2008 Regiscope Digital Imaging Co, LLC, All rights reserved. 003 * This program is free software; you can redistribute it and/or modify 004 * it under the terms of version 2 of the GNU General Public License as 005 * published by the Free Software Foundation. 006 * There are special exceptions to the terms and conditions of the GPL 007 * as it is applied to this software. See the GNU General Public License for more details. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program; if not, write to the Free Software 016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 017 */ 018 019 package org.dbreplicator.replication.column; 020 021 import java.io.*; 022 import java.sql.*; 023 import java.util.*; 024 025 import org.dbreplicator.replication.xml.*; 026 import org.dbreplicator.replication.EncoderDecoder; 027 import org.dbreplicator.replication.DBHandler.AbstractDataBaseHandler; 028 029 /** 030 * Handles floating-point data type. 031 */ 032 public class FloatObject extends AbstractColumnObject 033 { 034 035 int sqlType; 036 AbstractDataBaseHandler abstractDBHandler; 037 038 /** 039 * sets the SQL type fro float datatype 040 * @param sqlType0 041 */ 042 043 public FloatObject(int sqlType0,AbstractDataBaseHandler abstractDBHandler0) 044 { 045 sqlType = sqlType0; 046 abstractDBHandler=abstractDBHandler0; 047 } 048 049 /** 050 * set the value for the corresponding datatype i.e float 051 * @param pst 052 * @param element 053 * @param index 054 * @throws SQLException 055 */ 056 public void setColumnObject(PreparedStatement pst, XMLElement element, 057 int index) throws SQLException 058 { 059 String value = element.elementValue; 060 setColumnObject(pst, value, index); 061 062 } 063 064 public void setColumnObject(PreparedStatement pst, String value, 065 int index) throws SQLException 066 { 067 if (value.equalsIgnoreCase("NULL")) 068 { 069 pst.setNull(index, sqlType); 070 } 071 else 072 { 073 pst.setFloat(index, Float.parseFloat(value)); 074 } 075 } 076 077 /** 078 * writes a float values in XML file 079 * @param os 080 * @param rs 081 * @param index 082 * @throws IOException 083 * @throws SQLException 084 */ 085 public void write(Writer os, ResultSet rs, int index,ArrayList encodedCols,String col) throws 086 SQLException, IOException 087 { 088 try 089 { 090 if(!encodedCols.contains(col.toUpperCase())) { 091 os.write(getObject(rs, index).toString()); 092 // os.write("<![CDATA[" + getObject(rs, index).toString() + "]]>"); 093 } else { 094 os.write(EncoderDecoder.escapeUnicodeString1(getObject(rs, index). 095 toString(), true)); 096 097 // os.write("<![CDATA[" + 098 // EncoderDecoder.escapeUnicodeString1(getObject(rs, index). 099 // toString(), true) + 100 // "]]>"); 101 } 102 103 } 104 catch (NullPointerException ex) 105 { 106 os.write("NULL"); 107 } 108 } 109 110 private void write(Writer os, Object rowValue,ArrayList encodedCols,String col) throws 111 SQLException, IOException 112 { 113 try 114 { 115 if(!encodedCols.contains(col.toUpperCase())) { 116 os.write("<![CDATA[" + rowValue.toString() + "]]>"); 117 } else { 118 os.write("<![CDATA[" + 119 EncoderDecoder.escapeUnicodeString1(rowValue.toString(), true) + 120 "]]>"); 121 } 122 } 123 catch (NullPointerException ex) 124 { 125 os.write("NULL"); 126 } 127 } 128 129 /** 130 * puts the value for the column aginst the column Name 131 * @param os 132 * @param rows 133 * @param oldResultSet 134 * @param index 135 * @param modifiedColumns 136 * @param columnName 137 * @throws SQLException 138 * @throws IOException 139 */ 140 public void writeUpdate(Writer os, ResultSet rows, 141 ResultSet oldResultSet 142 , int index, HashMap modifiedColumns, 143 String columnName,ArrayList encodedCols) throws SQLException, IOException 144 { 145 Object newObject = rows.getObject(index); 146 Object oldObject = oldResultSet.getObject(index); 147 148 if (newObject == null) 149 { 150 write(os, "NULL",encodedCols,columnName); 151 if (oldObject != null) 152 { 153 modifiedColumns.put(columnName, "NULL"); 154 } 155 } 156 else 157 { 158 write(os, newObject,encodedCols,columnName); 159 if (oldObject != null) 160 { 161 if (! (newObject.equals(oldObject))) 162 { 163 modifiedColumns.put(columnName, newObject); 164 } 165 } 166 else 167 { 168 modifiedColumns.put(columnName, newObject); 169 } 170 171 } 172 } 173 174 /** 175 * returns the parsed value of String in float 176 * @param value String 177 * @return Object 178 */ 179 public Object getObject(String value) throws SQLException 180 { 181 if (value.equalsIgnoreCase("NULL")) 182 { 183 return null; 184 } 185 return Float.valueOf(value); 186 } 187 188 /** 189 * returns the Object corresponding to the index passed from the resultSet 190 * @param row 191 * @param index 192 * @return 193 * @throws SQLException 194 */ 195 private Object getObject(ResultSet row, int index) throws SQLException 196 { 197 return row.getObject(index); 198 } 199 200 201 }

