From d6e0ee6bcbcbe447cb1e22fefbe378939193b3ae Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Tue, 16 Jun 1998 06:57:27 +0000 Subject: [PATCH] Added missing file. --- .../jdbc/postgresql/util/Serialize.java | 342 ++++++++++++++++++ src/interfaces/libpq/fe-print.c | 3 +- 2 files changed, 344 insertions(+), 1 deletion(-) create mode 100644 src/interfaces/jdbc/postgresql/util/Serialize.java diff --git a/src/interfaces/jdbc/postgresql/util/Serialize.java b/src/interfaces/jdbc/postgresql/util/Serialize.java new file mode 100644 index 00000000000..56018490688 --- /dev/null +++ b/src/interfaces/jdbc/postgresql/util/Serialize.java @@ -0,0 +1,342 @@ +package postgresql.util; + +import java.io.*; +import java.lang.*; +import java.lang.reflect.*; +import java.net.*; +import java.util.*; +import java.sql.*; + +/** + * This class uses PostgreSQL's object oriented features to store Java Objects. + * + * It does this by mapping a Java Class name to a table in the database. Each + * entry in this new table then represents a Serialized instance of this + * class. As each entry has an OID (Object IDentifier), this OID can be + * included in another table. + * + * This is too complex to show here, and will be documented in the main + * documents in more detail. + * + */ +public class Serialize +{ + // This is the connection that the instance refers to + protected 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. + */ + public Serialize(postgresql.Connection c,String type) throws SQLException + { + try { + conn = c; + tableName = type.toLowerCase(); + className = toClassName(type); + ourClass = Class.forName(className); + } catch(ClassNotFoundException cnfe) { + throw new SQLException("No class found for '"+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; + rs.close(); + } + // This should never occur, as postgresql has it's own internal checks + if(!status) + throw new SQLException("The table for "+type+" is not in the database. Contact the DBA, as the database is in an inconsistent state."); + + // 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 + * @return Object relating to oid + * @exception SQLException on error + */ + public Object fetch(int oid) throws SQLException + { + try { + Object obj = ourClass.newInstance(); + + // NB: we use java.lang.reflect here to prevent confusion with + // the 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 + * + * If the object has an int called OID, and it is > 0, then + * that value is used for the OID, and the table will be updated. + * If the value of OID is 0, then a new row will be created, and the + * value of OID will be set in the object. This enables an object's + * value in the database to be updateable. + * + * If the object has no int called OID, then the object is stored. However + * if the object is later retrieved, amended and stored again, it's new + * state will be appended to the table, and will not overwrite the old + * entries. + * + * @param o Object to store (must implement Serializable) + * @return oid of stored object + * @exception SQLException on error + */ + public int store(Object o) throws SQLException + { + try { + // NB: we use java.lang.reflect here to prevent confusion with + // the 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;i + * + * Because of this, a Class name may not have _ in the name.

+ * Another limitation, is that the entire class name (including packages) + * cannot be longer than 32 characters (a limit forced by PostgreSQL). + * + * @param name Class name + * @return PostgreSQL table name + * @exception SQLException on error + */ + public static String toPostgreSQL(String name) throws SQLException + { + name = name.toLowerCase(); + + if(name.indexOf("_")>-1) + throw new SQLException("Class names may not have _ in them: "+name); + + if(name.length()>32) + throw new SQLException("Class & Package name length cannot be longer than 32 characters. "+name+" is "+name.length()+" characters."); + + return name.replace('.','_'); + } + + + /** + * This converts a postgresql table to a Java Class name, by replacing _ with + * .

+ * + * @param name PostgreSQL table name + * @return Class name + * @exception SQLException on error + */ + public static String toClassName(String name) throws SQLException + { + name = name.toLowerCase(); + return name.replace('_','.'); + } + +} diff --git a/src/interfaces/libpq/fe-print.c b/src/interfaces/libpq/fe-print.c index 493b37d0a3e..140658b9422 100644 --- a/src/interfaces/libpq/fe-print.c +++ b/src/interfaces/libpq/fe-print.c @@ -9,10 +9,11 @@ * didn't really belong there. * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.3 1998/06/15 19:30:26 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.4 1998/06/16 06:57:27 momjian Exp $ * *------------------------------------------------------------------------- */ +#include #include #include #include