summaryrefslogtreecommitdiff
path: root/src/interfaces/jdbc/org/postgresql/util/Serialize.java
blob: a89fba5baf387523070ba1ba41fa3f70654a2d58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package org.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 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.
   */
  public Serialize(org.postgresql.Connection c,String type) throws SQLException
  {
    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;
      rs.close();
    }
    // 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
   * @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 org.postgresql.Field

      // used getFields to get only public fields. We have no way to set values
      // for other declarations. Maybe look for setFieldName() methods?
      java.lang.reflect.Field f[] = ourClass.getFields();

      boolean hasOID=false;
      int oidFIELD=-1;
      StringBuffer sb = new StringBuffer("select");
      char sep=' ';

      // build a select for the fields. Look for the oid field to use in the where
      for(int i=0;i<f.length;i++) {
	String n = f[i].getName();
	if(n.equals("oid")) {
	  hasOID=true;
	  oidFIELD=i;
	}
	sb.append(sep);
	sb.append(n);
	sep=',';
      }
      sb.append(" from ");
      sb.append(tableName);
      sb.append(" where oid=");
      sb.append(oid);

      DriverManager.println("store: "+sb.toString());
      ResultSet rs = conn.ExecSQL(sb.toString());
      if(rs!=null) {
	if(rs.next()) {
	  for(int i=0;i<f.length;i++) {
            if ( !Modifier.isFinal(f[i].getModifiers()) ) {

              if (f[i].getType().getName().equals("short")){
                f[i].setShort(obj, rs.getShort(i+1));
              }
              else
              if (f[i].getType().getName().equals("char")){
                f[i].setChar(obj, rs.getString(i+1).toCharArray()[0]);
              }
              else
              if (f[i].getType().getName().equals("byte")){
                f[i].setByte(obj, rs.getByte(i+1));
              }
              else
              // booleans come out of pgsql as a t or an f
              if (f[i].getType().getName().equals("boolean")){
                if ( rs.getString(i+1).equals("t"))
                  f[i].setBoolean(obj, true);
                else
                  f[i].setBoolean(obj, false);
              }
              else{
	        f[i].set(obj,rs.getObject(i+1));
              }
            }
	  }
	}
	rs.close();
      } else
       throw new PSQLException("postgresql.unexpected");
      return obj;
    } catch(IllegalAccessException iae) {
      throw new SQLException(iae.toString());
    } catch(InstantiationException ie) {
      throw new SQLException(ie.toString());
    }
  }

  /**
   * This stores an object into a table, returning it's OID.<p>
   *
   * 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 org.postgresql.Field

      // don't save private fields since we would not be able to fetch them
      java.lang.reflect.Field f[] = ourClass.getFields();

      boolean hasOID=false;
      int oidFIELD=-1;
      boolean update=false;

      // Find out if we have an oid value
      for(int i=0;i<f.length;i++) {
	String n = f[i].getName();
	if(n.equals("oid")) {
	  hasOID=true;
	  oidFIELD=i;

	  // We are an update if oid != 0
	  update = f[i].getInt(o)>0;
	}
      }

      StringBuffer sb = new StringBuffer(update?"update "+tableName+" set":"insert into " + tableName);
      char sep=update?' ':'(';
      for(int i=0;i<f.length;i++) {
	String n = f[i].getName();
	sb.append(sep);
	sb.append(n);
	sep=',';
	if(update) {
	  sb.append('=');
          // handle unset values
          if (f[i].get(o) == null)
            sb.append("null");
          else
	  if(f[i].getType().getName().equals("java.lang.String") ||
             f[i].getType().getName().equals("char")) {
	    sb.append('\'');
            // don't allow single qoutes or newlines in the string
	    sb.append(fixString(f[i].get(o).toString()));
	    sb.append('\'');
	  } else
	    sb.append(f[i].get(o).toString());
	}
      }

      if(!update) {
	sb.append(") values ");
	sep='(';
	for(int i=0;i<f.length;i++) {
	  sb.append(sep);
	  sep=',';
          // handle unset values
          if (f[i].get(o) == null)
            sb.append("null");
          else
	  if(f[i].getType().getName().equals("java.lang.String") ||
             f[i].getType().getName().equals("char")) {
	    sb.append('\'');
            // don't allow single quotes or newlines in the string
	    sb.append(fixString(f[i].get(o).toString()));
	    sb.append('\'');
	  } else
	    sb.append(f[i].get(o).toString());
	}
	sb.append(')');
      }

      DriverManager.println("store: "+sb.toString());
      org.postgresql.ResultSet rs = (org.postgresql.ResultSet)conn.ExecSQL(sb.toString());

      // fetch the OID for returning
      int oid=0;
      if(hasOID) {
	// If an update use the existing oid in the object
	f[oidFIELD].setInt(o,oid);
      }
      else {
        String statStr = rs.getStatusString();
        oid = Integer.parseInt(statStr.substring(statStr.indexOf(" ") + 1, statStr.lastIndexOf(" ")));
      }

      if(rs!=null) {
	rs.close();
      }

      return oid;

    } catch(IllegalAccessException iae) {
      throw new SQLException(iae.toString());
    }
  }

  /**
   *
   */
   private String fixString(String s) {

   int idx = -1;

     // handle null
     if (s == null)
       return "";

     // if the string has single quotes in it escape them
     if ((idx = s.indexOf("'")) > -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
   * objects.
   * @param c Connection to database
   * @param o Object to base table on
   * @exception SQLException on error
   */
  public static void create(org.postgresql.Connection con,Object o) throws SQLException
  {
    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
   * objects.
   * @param c Connection to database
   * @param o Class to base table on
   * @exception SQLException on error
   */
  public static void create(org.postgresql.Connection con,Class c) throws SQLException
  {
    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();
      java.lang.reflect.Field[] fields = c.getFields();
      for(int i=0;i<fields.length;i++) {
	Class type = fields[i].getType();

	// oid is a special field
	if(!fields[i].getName().equals("oid")) {
	  sb.append(sep);
	  sb.append(fields[i].getName());
	  sb.append(' ');
	  sep=',';

	  if(type.isArray()) {
	    // array handling
	  } else {
	    // convert the java type to org.postgresql, recursing if a class
	    // is found
	    String n = type.getName();
	    int j=0;
	    for(;j<tp.length && !tp[j][0].equals(n);j++);
	    if(j<tp.length)
	      sb.append(tp[j][1]);
	    else {
	      create(con, type);
	      sb.append(toPostgreSQL(n));
	    }
	  }
	}
      }
      sb.append(")");

      // Now create the table
      DriverManager.println("Serialize.create:"+sb);
      con.ExecSQL(sb.toString());
      rs.close();
    } else {
      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
   * _<p>
   *
   * Because of this, a Class name may not have _ in the name.<p>
   * 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 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
   * .<p>
   *
   * @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('_','.');
  }

}