File: provider.java

package info (click to toggle)
libgda5 5.2.10-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,168 kB
  • sloc: ansic: 495,319; xml: 10,486; yacc: 5,165; sh: 4,451; makefile: 4,095; php: 1,416; java: 1,300; javascript: 1,298; python: 896; sql: 879; perl: 116
file content (1121 lines) | stat: -rw-r--r-- 30,595 bytes parent folder | download | duplicates (7)
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
import java.sql.*;
import java.util.*;
import java.io.*;

/*
 * Note:
 * This provider invokes JNI in the following way: a C API calls some Java code through JNI, which in turn calls
 * some C code as native method. To exchange pointers, the C pointers are converted to the "long" java type, which is mapped
 * to the "jlong" C type which is always 64 bits long, refer to http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html
 *
 * In C code, when passing a C pointer to a method, it needs to be converted to a jlong, and the jni_cpointer_to_jlong()
 * function must be used.
 */

/*
 * This class will be instantiated once for each GdaJdbcProvider created.
 */
class GdaJProvider {
	private static native void initIDs();
	private String driver;

	// Constructor
	public GdaJProvider (String driver) {
		this.driver = driver;
	}

	// Connection opening
	public GdaJConnection openConnection (String cnc_string, String username, String password) throws Exception {
		Connection cnc;
		try {
			Class.forName (driver);
			cnc = DriverManager.getConnection (cnc_string, username, password);
		}
		catch (Exception e) {
			throw e;
		}
		GdaJConnection jcnc = new GdaJConnection (cnc, driver);
		return jcnc;
	}

	// Get a list of all the known drivers
	// See also http://www.devx.com/tips/Tip/28818 for a list of drivers
	public static String getDrivers () {
		try {
			Class driverClass = Class.forName ("org.hsqldb.jdbcDriver");
		}
		catch (Exception e) {
			// ignore exceptions, they mean some drivers are not available
		}
		
		java.util.Enumeration e = DriverManager.getDrivers();
		String res = null;
		while (e.hasMoreElements ()) {
			Driver driver = (Driver) e.nextElement(); 
			if (res == null)
				res = driver.getClass().getName();
			else
				res = res + ":" + driver.getClass().getName();
			if (false)
				System.out.println ("FOUND DRIVER " + driver.getClass().getName());

			// Properties list, for DSN spec creation.
			if (false) {
				System.out.println("===== DEBUG: Properties for " + driver.getClass().getName());
				try {
					Properties ep = new Properties();
					DriverPropertyInfo[] props = driver.getPropertyInfo ("", ep);
					if (props != null) {
						for (int i = 0; i < props.length; i++){
							DriverPropertyInfo prop = props[i];
							System.out.println ("  Prop name = "+prop.name);
							System.out.println ("     Prop description = "+prop.description);
							System.out.println ("     Prop value = "+prop.value);
							if (prop.choices != null) {
								for (int j = 0; j < prop.choices.length; j++) {
									System.out.println ("     prop choice "+j
											    +" = "+prop.choices[j]);
								}
							}
						}
					}
				}
				catch (Exception ex) {
					ex.printStackTrace ();
				}
			}
		}
		return res;
	}
	
	public static Connection openConnection (String driver, String cnc_string) {
		System.out.println ("JAVA openConnection() requested");
		return null;
	}

	// main(): in case someone tries to exetute the JAR
	public static void main (String args[]) {
                System.out.println ("This JAR is not intended to be executed directly, " +
				    "but used as Libgda's JDBC provider!");
        }

	// class initializer
	static {
		if (System.getProperty("os.name").indexOf ("Win") >= 0) {
			// If we are running on Windows, then we need to set the full library name
			// because our library is still called libgda-jdbc.dll
			System.loadLibrary ("libgda-jdbc");
		}
		else {
			System.loadLibrary ("gda-jdbc");
		}
		initIDs ();

		try {
			/* dummy objects creation to force class loading */
			byte[] data = {'e'};
			GdaInputStream is = new GdaInputStream (data);
		}
		catch (Exception e) {
			System.out.println ("Coult not initialize some JAVA classes");
			e.printStackTrace ();
		}
	}
}


/*
 * This class will be instantiated once for each GdaConnection object
 */
class GdaJConnection {
	private static native void initIDs();
	public Connection cnc = null;
	private HashMap<String,Savepoint> svp_map = new HashMap<String,Savepoint>();
	private GdaJMeta jmeta = null;
	String driver;

	// Constructor
	public GdaJConnection (Connection cnc, String driver) throws Exception {
		this.cnc = cnc;
		this.driver = driver;
	}

	// Connection close
	public void close () throws Exception {
		this.cnc.close ();
		jmeta = null;
	}

	// get server version
	public String getServerVersion () throws Exception {
		DatabaseMetaData meta_data;
		meta_data = cnc.getMetaData ();
		return meta_data.getDatabaseProductName() + " " + meta_data.getDatabaseProductVersion ();
	}

	// prepare statement
	public GdaJPStmt prepareStatement (String sql) throws Exception {
		GdaJPStmt ps;
		PreparedStatement pstmt;

		pstmt = cnc.prepareStatement (sql);
		ps = new GdaJPStmt (pstmt);
		return ps;
	}

	// direct execution of a SELECT with no parameters
	public GdaJResultSet executeDirectSQL (String sql) throws Exception {
		Statement st;
		st = cnc.createStatement ();
		return new GdaJResultSet (st.executeQuery (sql));
	}

	// Transaction management
	public void begin () throws Exception {
		if (cnc.getAutoCommit() == false) {
			throw new Exception ("Transaction already started");
		}
		cnc.setAutoCommit (false);
	}
	
	public void rollback () throws Exception {
		if (cnc.getAutoCommit() == true) {
			throw new Exception ("No transaction started");
		}
		cnc.rollback ();
		cnc.setAutoCommit (true);
	}

	public void commit () throws Exception {
		if (cnc.getAutoCommit() == true) {
			throw new Exception ("No transaction started");
		}
		cnc.commit ();
		cnc.setAutoCommit (true);
	}

	public void addSavepoint (String svp_name) throws Exception {
		Savepoint svp;
		if (cnc.getAutoCommit() == true) {
			throw new Exception ("No transaction started");
		}

		svp = svp_map.get (svp_name);
		if (svp != null) {
			throw new Exception ("Savepoint '" + svp_name + "' already exists");
		}
		svp = cnc.setSavepoint (svp_name);
		svp_map.put (svp_name, svp);
	}

	public void rollbackSavepoint (String svp_name) throws Exception {
		Savepoint svp = null;
		if (cnc.getAutoCommit() == true) {
			throw new Exception ("No transaction started");
		}
		svp = svp_map.get (svp_name);
		if (svp == null) {
			throw new Exception ("No savepoint '" + svp_name + "' found");
		}
		cnc.rollback (svp);
		svp_map.remove (svp_name);
	}

	public void releaseSavepoint (String svp_name) throws Exception {
		Savepoint svp = null;
		if (cnc.getAutoCommit() == true) {
			throw new Exception ("No transaction started");
		}
		svp = svp_map.get (svp_name);
		if (svp == null) {
			throw new Exception ("No savepoint '" + svp_name + "' found");
		}
		cnc.releaseSavepoint (svp);
		svp_map.remove (svp_name);
	}

	// meta data retrieval
	public GdaJMeta getJMeta () throws Exception {
		if (jmeta == null) {
			String name= driver.replace (".", "_") + "Meta";
			try {
				Class<?> r = Class.forName (name);
				java.lang.reflect.Constructor c = 
					r.getConstructor (new Class [] {Class.forName ("java.sql.Connection")});
				jmeta = (GdaJMeta) c.newInstance (new Object [] {cnc});
			}
			catch (Exception e) {
				if (driver.contains ("org.apache.derby.jdbc")) {
					try {
						name = "org_apache_derbyMeta";
						Class<?> r = Class.forName (name);
						java.lang.reflect.Constructor c = 
							r.getConstructor (new Class [] {Class.forName ("java.sql.Connection")});
						jmeta = (GdaJMeta) c.newInstance (new Object [] {cnc});
					}
					catch (Exception e1) {
						// nothing
					}
				}
				else if (driver.contains ("sqlserver") ||
					 driver.contains ("com.ashna.jturbo.driver.Driver") ||
					 driver.contains ("com.inet.tds.TdsDriver")) {
					try {
						name = "sqlserverMeta";
						Class<?> r = Class.forName (name);
						java.lang.reflect.Constructor c =
							r.getConstructor (new Class [] {Class.forName ("java.sql.Connection")});
						jmeta = (GdaJMeta) c.newInstance (new Object [] {cnc});
					}
					catch (Exception e1) {
						// nothing
					}
				}
			}
			if (jmeta == null)
				jmeta = new GdaJMeta (cnc);
		}
		return jmeta;
	}

	// class initializer
	static {
		initIDs ();
	}
}

/*
 * Represents a prepared statement
 */
class GdaJPStmt {
	private static native void initIDs();
	private PreparedStatement ps;
	private Vector<GdaJValue> param_values;

	// Constructor
	public GdaJPStmt (PreparedStatement ps) {
		this.ps = ps;
		param_values = new Vector<GdaJValue> (0);
	}

	// declares the expected type for the parameters
	public void declareParamTypes (long cnc_c_pointer, byte types[]) throws Exception {
		int nparams = ps.getParameterMetaData().getParameterCount ();
		int i;
		if (types.length != nparams) {
			throw new Exception ("Number of types differs from number of parameters: got " + types.length + 
					     " and expected " + nparams);
		}

		for (i = 0; i < nparams; i++) {
			GdaJValue cv = GdaJValue.proto_type_to_jvalue (types[i]);
			cv.setGdaConnectionPointer (cnc_c_pointer);
			param_values.add (cv);
			//System.out.println ("JAVA: param " + i + " as type " + types[i]);
		}
	}

	// sets a parameter's value
	// if c_value_pointer is 0, then the parameter should be set to NULL
	public void setParameterValue (int index, long c_value_pointer) throws Exception {
		GdaJValue cv = param_values.elementAt (index);
		cv.getCValue (ps, index, c_value_pointer);
	}

	// Clear previous set parameters
	public void clearParameters () throws Exception {
		ps.clearParameters ();
	}

	// Execute
	public boolean execute () throws Exception {
		return ps.execute ();
	}

	// Fetch result set
	public GdaJResultSet getResultSet () throws Exception {
		return new GdaJResultSet (ps.getResultSet ());
	}

	public int getImpactedRows () throws Exception {
		return ps.getUpdateCount ();
	}

	// class initializer
	static {
		initIDs ();
	}
}


/*
 * This class represents meta data information for a GdaJResultSet.
 *
 * All the fields are read-only
 */
class GdaJResultSetInfos {
	private static native void initIDs();
	public Vector<GdaJColumnInfos> col_infos;
	private int ncols;
	
	// Constructor
	public GdaJResultSetInfos (ResultSetMetaData md) throws Exception {
		int i;
		ncols = md.getColumnCount ();
		col_infos = new Vector<GdaJColumnInfos> (0);
		for (i = 0; i < ncols; i++) {
			GdaJColumnInfos ci;
			int rcol = i + 1;
			ci = new GdaJColumnInfos (md.getColumnName (rcol), md.getColumnLabel (rcol), md.getColumnType (rcol));
			col_infos.add (ci);
		}
	}

	public GdaJResultSetInfos (Vector<GdaJColumnInfos> col_infos) {
		this.col_infos = col_infos;
		ncols = col_infos.size ();
	}

	// describe a column, index starting at 0
	public GdaJColumnInfos describeColumn (int col) throws Exception {
		return (GdaJColumnInfos) col_infos.elementAt (col);
	}

	// class initializer
	static {
		initIDs ();
	}
}

class GdaJColumnInfos {
	private static native void initIDs();
	String col_name;
	String col_descr;
	int col_type;

	public GdaJColumnInfos (String col_name, String col_descr, int col_type) {
		this.col_name = col_name;
		this.col_descr = col_descr;
		this.col_type = col_type;
	}

	// class initializer
	static {
		initIDs ();
	}
}

/*
 * This class represents a result set: the result of a SELECT statement
 */
class GdaJResultSet {
	private static native void initIDs();
	private ResultSet rs;

	private int ncols;
	protected Vector<GdaJValue> col_values;

	// Constructor
	public GdaJResultSet (ResultSet rs) throws Exception {
		this.rs = rs;
		if (rs != null) {
			ncols = rs.getMetaData().getColumnCount();
			col_values = new Vector<GdaJValue> (ncols);
		}
	}

	protected GdaJResultSet (int ncols) {
		this.ncols = ncols;
		col_values = new Vector<GdaJValue> (ncols);
	}

	// get result set's meta data
	public GdaJResultSetInfos getInfos () throws Exception {
		return new GdaJResultSetInfos (rs.getMetaData ());
	}

	/*
	 * declares the expected type for the column
	 */
	public void declareColumnTypes (long cnc_c_pointer, byte types[]) throws Exception {
		int i;
		if (types.length != ncols) {
			throw new Exception ("Number of types differs from number of columns: expected " + ncols + " got " +
					     types.length);
		}
		for (i = 0; i < ncols; i++) {
			GdaJValue cv = GdaJValue.proto_type_to_jvalue (types[i]);
			cv.setGdaConnectionPointer (cnc_c_pointer);
			cv.setGdaRowColumn (i);
			col_values.add (cv);
		}
		columnTypesDeclared ();
	}

	protected void columnTypesDeclared () {
		// do nothing here, let sub classes do it.
	}

	/* 
	 * move iterator to next row and fills row
	 * @c_pointer is a pointer to the new GdaRow to fill values
	 * Returns: false if no more row available
	 */
	public boolean fillNextRow (long c_pointer) throws Exception {
		int i;
		if (! rs.next ())
			return false;
		for (i = 0; i < ncols; i++) {
			GdaJValue cv = col_values.elementAt (i);
			cv.setCValue (rs, i, c_pointer);
		}
		return true;
	}

	// class initializer
	static {
		initIDs ();
	}
}


/*
 * Classes to represent value transport between the JAVA and C worlds:
 * - JAVA -> C when reading through a resultset
 * - C -> JAVA when setting the parameter's values of a prepared statement
 */
abstract class GdaJValue {
	private static native void initIDs();
	protected long cnc_c_pointer = 0;
	protected int gda_row_column = -1;
	protected boolean convert_lc = false; // true if strings must be converted to lower case (ignored by non String)
	protected boolean no_null = false; // converts a NULL value to "" for a string (ignored by non String)

	/*
	 * @c_pointer points to a GdaRow
	 * @col starts at 0
	 */
	native void setCString (long c_pointer, int col, String string);
	native void setCInt (long c_pointer, int col, int i);
	native void setCChar (long c_pointer, int col, byte b);
	native void setCDouble (long c_pointer, int col, double d);
	native void setCFloat (long c_pointer, int col, float f);
	native void setCLong (long c_pointer, int col, long l);
	native void setCShort (long c_pointer, int col, short s);
	native void setCBoolean (long c_pointer, int col, boolean b);
	native void setCDate (long c_pointer, int col, int year, int month, int day);
	native void setCTime (long c_pointer, int col, int hour, int min, int sec);
	native void setCTimestamp (long c_pointer, int col, int year, int month, int day, int hour, int min, int sec);
	native void setCBinary (long c_pointer, int col, byte[] data);
	native void setCBlob (long c_pointer, int col, long cnc_c_pointer, GdaJBlobOp blob);
	native void setCNumeric (long c_pointer, int col, String str, int precision, int scale);
	
	/*
	 * @c_pointer points to a GValue, may NOT be 0 ( <=> NULL )
	 */
	native String getCString (long c_pointer);
	native int getCInt (long c_pointer);
	native byte getCChar (long c_pointer);
	native double getCDouble (long c_pointer);
	native float getCFloat (long c_pointer);
	native long getCLong (long c_pointer);
	native short getCShort (long c_pointer);
	native boolean getCBoolean (long c_pointer);
	native java.sql.Date getCDate (long c_pointer);
	native java.sql.Time getCTime (long c_pointer);
	native java.sql.Timestamp getCTimestamp (long c_pointer);
	native byte[] getCBinary (long c_pointer);
	native GdaInputStream getCBlob (long c_pointer);
	native java.math.BigDecimal getCNumeric (long c_pointer);

	/*
	 * Sets the C GValue's value from @rs at column @col (@c_pointer points to the GdaRow
	 * which contains the GValues)
	 */
	abstract void setCValue (ResultSet rs, int col, long c_pointer) throws Exception; /* @col starts at 0 */

	/*
	 * Set @ps's parameter at index @index from the C GValue pointed by @c_pointer (if @c_pointer is 0, then
	 * the parameter is set to NULL)
	 */
	abstract void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception; /* @index starts at 0 */

	public void setGdaConnectionPointer (long cnc_c_pointer) {
		this.cnc_c_pointer = cnc_c_pointer;
	}

	public void setGdaRowColumn (int col) {
		gda_row_column = col;
	}


	// (year, month, day) -> java.sql.Date
	public static java.sql.Date createDate (int year, int month, int day) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.set (year, month, day);
		return new java.sql.Date (cal.getTimeInMillis ());
	}

	// (hour, minute, sec) -> java.sql.Time
	public static java.sql.Time createTime (int hour, int min, int sec) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.set (0, 0, 0, hour, min, sec);
		return new java.sql.Time (cal.getTimeInMillis ());
	}

	// (year, month, day, hour, minute, sec) -> java.sql.Timestamp
	public static java.sql.Timestamp createTimestamp (int year, int month, int day, int hour, int min, int sec) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.set (year, month, day, hour, min, sec);
		return new java.sql.Timestamp (cal.getTimeInMillis ());
	}

	// types conversions
	public static GdaJValue proto_type_to_jvalue (byte type) throws Exception {
		GdaJValue cv;
		switch (type) {
		case 0: /* GDA_TYPE_NULL */
			cv = new GdaJNull ();
			break;
		case 1: /* G_TYPE_STRING */
			cv = new GdaJString ();
			break;
		case 2: /* G_TYPE_INT */
			cv = new GdaJInt ();
			break;
		case 3: /* G_TYPE_CHAR */
			cv = new GdaJChar ();
			break;
		case 4: /* G_TYPE_DOUBLE */
			cv = new GdaJDouble ();
			break;
		case 5: /* G_TYPE_FLOAT */
			cv = new GdaJFloat ();
			break;
		case 6: /* G_TYPE_BOOLEAN */
			cv = new GdaJBoolean ();
			break;
		case 7: /* G_TYPE_DATE */
			cv = new GdaJDate ();
			break;
		case 8: /* GDA_TYPE_TIME */
			cv = new GdaJTime ();
			break;
		case 9: /* GDA_TYPE_TIMESTAMP */
			cv = new GdaJTimestamp ();
			break;
		case 10: /* GDA_TYPE_BINARY */
			cv = new GdaJBinary ();
			break;
		case 11: /* GDA_TYPE_BLOB */
			cv = new GdaJBlob ();
			break;
		case 12: /* G_TYPE_INT64 */
			cv = new GdaJInt64 ();
			break;
		case 13: /* GDA_TYPE_SHORT */
			cv = new GdaJShort ();
			break;
		case 14: /* GDA_TYPE_NUMERIC */
			cv = new GdaJNumeric ();
			break;
			
		default:
			throw new Exception ("Unhandled protocol type " + type);
		}
		return cv;
	}

	// Same as gda-jdbc-recordset.c::jdbc_type_to_g_type
	// see http://docs.oracle.com/javase/6/docs/api/constant-values.html#java.sql.Types.ARRAY for reference
	public static String jdbc_type_to_g_type (int type) {
		switch (type) {
		case java.sql.Types.VARCHAR:
			return "gchararray";
		case java.sql.Types.ARRAY:
			return "GdaBinary";
		case java.sql.Types.BIGINT:
			return "gint64";
		case java.sql.Types.BINARY:
			return "GdaBinary";
		case java.sql.Types.BIT:
			return "gboolean";
		case java.sql.Types.BLOB:
			return "GdaBlob";
		case java.sql.Types.BOOLEAN:
			return "gboolean";
		case java.sql.Types.CHAR:
			return "gchararray";
		case java.sql.Types.CLOB:
		case java.sql.Types.DATALINK:
			return "GdaBinary";
		case java.sql.Types.DATE:
			return "GDate";
		case java.sql.Types.DECIMAL:
			return "GdaNumeric";
		case java.sql.Types.DISTINCT:
			return "GdaBinary";
		case java.sql.Types.DOUBLE:
			return "gdouble";
		case java.sql.Types.FLOAT:
			return "gfloat";
		case java.sql.Types.INTEGER:
			return "gint";
		case java.sql.Types.JAVA_OBJECT:
			return "GdaBinary";
		case java.sql.Types.LONGNVARCHAR:
			return "gchararray";
		case java.sql.Types.LONGVARBINARY:
			return "GdaBinary";
		case java.sql.Types.LONGVARCHAR:
			return "gchararray";
		case java.sql.Types.NCHAR:
			return "gchararray";
		case java.sql.Types.NCLOB:
			return "GdaBinary";
		case java.sql.Types.NULL:
			return "GdaNull";
		case java.sql.Types.NUMERIC:
			return "GdaNumeric";
		case java.sql.Types.NVARCHAR:
			return "gchararray";
		case java.sql.Types.OTHER:
			return "GdaBinary";
		case java.sql.Types.REAL:
			return "gfloat";
		case java.sql.Types.REF:
			return "GdaBinary";	
		case java.sql.Types.ROWID:
			return "gchararray";
		case java.sql.Types.SMALLINT:
			return "GdaShort";
		case java.sql.Types.SQLXML:
			return "gchararray";
		case java.sql.Types.STRUCT:
			return "GdaBinary";
		case java.sql.Types.TIME:
			return "GdaTime";
		case java.sql.Types.TIMESTAMP:
			return "GdaTimestamp";
		case java.sql.Types.TINYINT:
			return "gchar";
		case java.sql.Types.VARBINARY:
		default:
			return "GdaBinary";
		}
	}

	public static String toLower (String string) {
		String s2 = string.toUpperCase();
		if (s2 == string)
			return string.toLowerCase();
		else
			return string;
	}

	// class initializer
	static {
		initIDs ();
	}
}

class GdaJString extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		String string;
		string = rs.getString (col + 1);
		if (string == null) {
			if (no_null)
				setCString (c_pointer, gda_row_column, "");
		}
		else {
			if (convert_lc) {
				String s2 = string.toUpperCase();
				if (s2 == string)
					string = string.toLowerCase();
			}
			setCString (c_pointer, gda_row_column, string);
		}
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.VARCHAR);
		else {
			String string;
			string = getCString (c_pointer);
			ps.setString (index + 1, string);
		}
	}
}

class GdaJInt extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		int i;
		i = rs.getInt (col + 1);
		if ((i != 0) || !rs.wasNull ())
			setCInt (c_pointer, gda_row_column, i);
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.INTEGER);
		else {
			int i;
			i = getCInt (c_pointer);
			ps.setInt (index + 1, i);
		}
	}
}

class GdaJDouble extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		double d;
		d = rs.getDouble (col + 1);
		if ((d != 0) || !rs.wasNull ())
			setCDouble (c_pointer, gda_row_column, d);
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.DOUBLE);
		else {
			double d;
			d = getCDouble (c_pointer);
			ps.setDouble (index + 1, d);
		}
	}
}

class GdaJFloat extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		float f;
		f = rs.getFloat (col + 1);
		if ((f != 0) || !rs.wasNull ())
			setCFloat (c_pointer, gda_row_column, f);
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.FLOAT);
		else {
			float f;
			f = getCFloat (c_pointer);
			ps.setFloat (index + 1, f);
		}
	}
}

class GdaJBoolean extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		boolean b;
		b = rs.getBoolean (col + 1);
		if (b || !rs.wasNull ())
			setCBoolean (c_pointer, gda_row_column, b);
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.BOOLEAN);
		else {
			boolean b;
			b = getCBoolean (c_pointer);
			ps.setBoolean (index + 1, b);
		}
	}
}

class GdaJDate extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		java.sql.Date date;
		date = rs.getDate (col + 1);
		if (date != null) {
			Calendar cal = GregorianCalendar.getInstance();
			cal.setTime (date);
			setCDate (c_pointer, gda_row_column, cal.get (Calendar.YEAR), 
				  cal.get (Calendar.MONTH) + 1, cal.get (Calendar.DAY_OF_MONTH));
		}
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.DATE);
		else {
			java.sql.Date d;
			d = getCDate (c_pointer);
			ps.setDate (index + 1, d);
		}
	}
}

class GdaJTime extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		java.sql.Time time;
		time = rs.getTime (col + 1);
		if (time != null) {
			Calendar cal = GregorianCalendar.getInstance();
			cal.setTime (time);
			setCTime (c_pointer, gda_row_column, cal.get (Calendar.HOUR_OF_DAY), 
				  cal.get (Calendar.MINUTE) + 1, cal.get (Calendar.SECOND));
		}
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.TIME);
		else {
			java.sql.Time t;
			t = getCTime (c_pointer);
			ps.setTime (index + 1, t);
		}
	}
}

class GdaJTimestamp extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		java.sql.Timestamp ts;
		ts = rs.getTimestamp (col + 1);
		if (ts != null) {
			Calendar cal = GregorianCalendar.getInstance();
			cal.setTime (ts);
			setCTimestamp (c_pointer, gda_row_column, cal.get (Calendar.YEAR), 
				       cal.get (Calendar.MONTH) + 1, cal.get (Calendar.DAY_OF_MONTH),
				       cal.get (Calendar.HOUR_OF_DAY), 
				       cal.get (Calendar.MINUTE) + 1, cal.get (Calendar.SECOND));
		}
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.TIMESTAMP);
		else {
			java.sql.Timestamp ts;
			ts = getCTimestamp (c_pointer);
			ps.setTimestamp (index + 1, ts);
		}
	}
}

class GdaJBinary extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		byte[] bin;
		bin = rs.getBytes (col + 1);
		if (bin != null) {
			setCBinary (c_pointer, gda_row_column, bin);
		}
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.BINARY);
		else {
			byte[] bin;
			bin = getCBinary (c_pointer);
			ps.setBytes (index + 1, bin);
		}
	}
}

class GdaJBlob extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		java.sql.Blob blob;
		blob = rs.getBlob (col + 1);
		if (blob != null) {
			setCBlob (c_pointer, gda_row_column, cnc_c_pointer, new GdaJBlobOp (blob));
		}
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.BLOB);
		else {
			GdaInputStream is;
			is = getCBlob (c_pointer);
			ps.setBinaryStream (index + 1, is, (int) is.size);
		}
	}
}

class GdaJChar extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		Byte b;
		b = rs.getByte (col + 1);
		if ((b != 0) || !rs.wasNull ())
			setCChar (c_pointer, gda_row_column, b);
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.CHAR);
		else {
			byte b;
			b = getCChar (c_pointer);
			ps.setByte (index + 1, b);
		}
	}
}

class GdaJInt64 extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		long l;
		l = rs.getLong (col + 1);
		if ((l != 0) || !rs.wasNull ())
			setCLong (c_pointer, gda_row_column, l);
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.BIGINT);
		else {
			long l;
			l = getCLong (c_pointer);
			ps.setLong (index + 1, l);
		}
	}
}

class GdaJShort extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		short s;
		s = rs.getShort (col + 1);
		if ((s != 0) || !rs.wasNull ())
			setCShort (c_pointer, gda_row_column, s);
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.SMALLINT);
		else {
			short s;
			s = getCShort (c_pointer);
			ps.setShort (index + 1, s);
		}
	}
}

class GdaJNumeric extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		java.math.BigDecimal bd;

		bd = rs.getBigDecimal (col + 1);
		if (bd != null) {
			setCNumeric (c_pointer, gda_row_column, bd.toString(), bd.precision(), bd.scale());
		}
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		if (c_pointer == 0)
			ps.setNull (index + 1, java.sql.Types.NUMERIC);
		else {
			java.math.BigDecimal bd;
			bd = getCNumeric (c_pointer);
			ps.setBigDecimal (index + 1, bd);
		}
	}
}

class GdaJNull extends GdaJValue {
	void setCValue (ResultSet rs, int col, long c_pointer) throws Exception {
		// nothing to do
	}

	void getCValue (PreparedStatement ps, int index, long c_pointer) throws Exception {
		ParameterMetaData md;
		int i = index + 1;
		md = ps.getParameterMetaData ();
		ps.setNull (i, md.getParameterType (i));
	}
}


/*
 * This class represents a Blob, used by the GdaBlobOp object
 * to transfer data from JDBC -> C
 */
class GdaJBlobOp {
	private static native void initIDs();
	private Blob blob;

	// Constructor
	public GdaJBlobOp (Blob blob) {
		this.blob = blob;
	}

	// Get Blob's length
	public long length () throws Exception {
		return blob.length ();
	}

	// Read data
	public byte[] read (long offset, int len) throws Exception {
		return blob.getBytes (offset + 1, len);
	}

	// Write data
	public int write (long offset, byte[] data) throws Exception {
		int i;
		i = blob.setBytes (offset, data);
		if (i != data.length) {
			throw new Exception ("Could not write the complete BLOB");
		}
		return i;
	}

	// class initializer
	static {
		initIDs ();
	}
}

/*
 * an InputStream which reads from a GdaBlob, for
 * data transfer C -> JDBC
 *
 * Note: the @size attribute is not used here since the PreparedStatement.setBinaryStream() already
 * contains the blob's size and won't try to get more. If this InputStream were to be used
 * somewhere else, then @size would have to be handled correctly.
 */
class GdaInputStream extends InputStream {
	static int chunk_size = 65536; // 64kb chunks

	private static native void initIDs();
	private native byte[] readByteData (long gda_blob_pointer, long offset, long size);
	public long size;
	private long current_pos;
	private long gda_blob_pointer;

	private InputStream ist = null; // delegate

	// Constructor
	public GdaInputStream (long gda_blob_pointer, long size) {
		current_pos = 0;
		this.size = size;
		this.gda_blob_pointer = gda_blob_pointer;
	}

	// Constructor
	public GdaInputStream (byte[] data) {
		this.gda_blob_pointer = 0;
		ist = new ByteArrayInputStream (data);
	}

	// abstract class implementation
	public int read() throws IOException {
		int i = -1;
		if (ist != null)
			i = ist.read ();

		if ((i == -1) && (gda_blob_pointer != 0)) {
			byte[] data;
			data = readByteData (gda_blob_pointer, current_pos, chunk_size);
			current_pos += data.length;
			ist = new ByteArrayInputStream (data);
			i = ist.read ();
		}
		return i;
	}

	// class initializer
	static {
		initIDs ();
	}
}