*** ./Serialize.java.orig Sat May 19 20:39:52 2001 --- ./Serialize.java Sun May 20 21:24:20 2001 *************** *** 23,38 **** { // This is the connection that the instance refers to protected org.postgresql.Connection conn; ! // This is the table name protected String tableName; ! // This is the class name protected String className; ! // This is the Class for this serialzed object protected Class ourClass; ! /** * This creates an instance that can be used to serialize or deserialize * a Java object from a PostgreSQL table. --- 23,38 ---- { // This is the connection that the instance refers to protected org.postgresql.Connection conn; ! // This is the table name protected String tableName; ! // This is the class name protected String className; ! // This is the Class for this serialzed object protected Class ourClass; ! /** * This creates an instance that can be used to serialize or deserialize * a Java object from a PostgreSQL table. *************** *** 41,56 **** { try { conn = c; ! tableName = type.toLowerCase(); ! className = toClassName(type); ourClass = Class.forName(className); } catch(ClassNotFoundException cnfe) { throw new PSQLException("postgresql.serial.noclass",type); } ! // Second check, the type must be a table boolean status = false; ! ResultSet rs = conn.ExecSQL("select typname from pg_type,pg_class where typname=relname and typname='"+type+"'"); if(rs!=null) { if(rs.next()) status=true; --- 41,56 ---- { try { conn = c; ! tableName = toPostgreSQL(type); ! className = type; ourClass = Class.forName(className); } catch(ClassNotFoundException cnfe) { throw new PSQLException("postgresql.serial.noclass",type); } ! // Second check, the type must be a table boolean status = false; ! ResultSet rs = conn.ExecSQL("select typname from pg_type,pg_class where typname=relname and typname='" + tableName + "'"); if(rs!=null) { if(rs.next()) status=true; *************** *** 59,68 **** // This should never occur, as org.postgresql has it's own internal checks if(!status) throw new PSQLException("postgresql.serial.table",type); ! // Finally cache the fields within the table } ! /** * This fetches an object from a table, given it's OID * @param oid The oid of the object --- 59,84 ---- // This should never occur, as org.postgresql has it's own internal checks if(!status) throw new PSQLException("postgresql.serial.table",type); ! // Finally cache the fields within the table } ! ! /** ! * Constructor when Object is passed in ! */ ! public Serialize(org.postgresql.Connection c,Object o) throws SQLException ! { ! this(c, o.getClass().getName()); ! } ! ! /** ! * Constructor when Class is passed in ! */ ! public Serialize(org.postgresql.Connection c, Class cls) throws SQLException ! { ! this(c, cls.getName()); ! } ! /** * This fetches an object from a table, given it's OID * @param oid The oid of the object *************** *** 73,86 **** { try { Object obj = ourClass.newInstance(); ! // NB: we use java.lang.reflect here to prevent confusion with // the org.postgresql.Field ! java.lang.reflect.Field f[] = ourClass.getDeclaredFields(); boolean hasOID=false; int oidFIELD=-1; StringBuffer sb = new StringBuffer("select"); char sep=' '; for(int i=0;i * --- 160,166 ---- throw new SQLException(ie.toString()); } } ! /** * This stores an object into a table, returning it's OID.

* *************** *** 138,161 **** try { // NB: we use java.lang.reflect here to prevent confusion with // the org.postgresql.Field ! java.lang.reflect.Field f[] = ourClass.getDeclaredFields(); boolean hasOID=false; int oidFIELD=-1; boolean update=false; ! // Find out if we have an oid value for(int i=0;i0; } } ! ! StringBuffer sb = new StringBuffer(update?"update "+tableName+" set":"insert into "+tableName+" values "); char sep=update?' ':'('; for(int i=0;i0; } } ! ! StringBuffer sb = new StringBuffer(update?"update "+tableName+" set":"insert into " + tableName); char sep=update?' ':'('; for(int i=0;i -1) { ! StringBuffer buf = new StringBuffer(); ! StringTokenizer tok = new StringTokenizer(s, "'"); ! // handle quote as 1St charater ! if (idx > 0) buf.append(tok.nextToken()); ! ! while(tok.hasMoreTokens()) ! buf.append("\\'").append(tok.nextToken()); ! ! s = buf.toString(); ! } ! ! // if the string has newlines in it convert them to \n ! if ((idx = s.indexOf("\n")) > -1) { ! StringBuffer buf = new StringBuffer(); ! StringTokenizer tok = new StringTokenizer(s, "\n"); ! if (idx > 0) buf.append(tok.nextToken()); ! ! while(tok.hasMoreTokens()) ! buf.append("\\n").append(tok.nextToken()); ! ! s = buf.toString(); ! } ! ! return s; ! ! } ! /** * This method is not used by the driver, but it creates a table, given * a Serializable Java Object. It should be used before serializing any *************** *** 219,225 **** { create(con,o.getClass()); } ! /** * This method is not used by the driver, but it creates a table, given * a Serializable Java Object. It should be used before serializing any --- 327,333 ---- { create(con,o.getClass()); } ! /** * This method is not used by the driver, but it creates a table, given * a Serializable Java Object. It should be used before serializing any *************** *** 232,280 **** { if(c.isInterface()) throw new PSQLException("postgresql.serial.interface"); ! // See if the table exists String tableName = toPostgreSQL(c.getName()); ! ResultSet rs = con.ExecSQL("select relname from pg_class where relname = '"+tableName+"'"); if(!rs.next()) { ! DriverManager.println("found "+rs.getString(1)); // No entries returned, so the table doesn't exist ! StringBuffer sb = new StringBuffer("create table "); sb.append(tableName); char sep='('; ! ! java.lang.reflect.Field[] fields = c.getDeclaredFields(); for(int i=0;i --- 392,417 ---- DriverManager.println("Serialize.create: table "+tableName+" exists, skipping"); } } ! // This is used to translate between Java primitives and PostgreSQL types. private static final String tp[][] = { ! // {"boolean", "int1"}, ! {"boolean", "bool"}, {"double", "float8"}, {"float", "float4"}, {"int", "int4"}, ! // {"long", "int4"}, ! {"long", "int8"}, {"short", "int2"}, {"java.lang.String", "text"}, {"java.lang.Integer", "int4"}, {"java.lang.Float", "float4"}, {"java.lang.Double", "float8"}, ! {"java.lang.Short", "int2"}, ! {"char", "char"}, ! {"byte", "int2"} }; ! /** * This converts a Java Class name to a org.postgresql table, by replacing . with * _

*************** *** 314,330 **** public static String toPostgreSQL(String name) throws SQLException { name = name.toLowerCase(); ! if(name.indexOf("_")>-1) throw new PSQLException("postgresql.serial.underscore"); ! ! if(name.length()>32) ! throw new PSQLException("postgresql.serial.namelength",name,new Integer(name.length())); ! return name.replace('.','_'); } ! ! /** * This converts a org.postgresql table to a Java Class name, by replacing _ with * .

--- 427,451 ---- public static String toPostgreSQL(String name) throws SQLException { name = name.toLowerCase(); ! if(name.indexOf("_")>-1) throw new PSQLException("postgresql.serial.underscore"); ! ! // Postgres table names can only be 32 character long ! // If the full class name with package is too long ! // then just use the class name. If the class name is ! // too long throw an exception. ! if(name.length() > 32) { ! name = name.substring(name.lastIndexOf(".") + 1); ! ! if(name.length()>32) ! throw new PSQLException("postgresql.serial.namelength",name,new Integer(name.length())); ! } ! return name.replace('.','_'); } ! ! /** * This converts a org.postgresql table to a Java Class name, by replacing _ with * .

*************** *** 338,342 **** name = name.toLowerCase(); return name.replace('_','.'); } ! } --- 459,463 ---- name = name.toLowerCase(); return name.replace('_','.'); } ! }