00001
package com.quadcap.jdbc;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.IOException;
00042
import java.io.InputStream;
00043
import java.io.Reader;
00044
00045
import java.util.Calendar;
00046
import java.util.Vector;
00047
00048
import java.math.BigDecimal;
00049
00050
00051
import java.sql.Array;
00052
import java.sql.Blob;
00053
import java.sql.Clob;
00054
import java.sql.Ref;
00055
00056
00057
import java.sql.Date;
00058
import java.sql.SQLException;
00059
import java.sql.SQLWarning;
00060
import java.sql.Time;
00061
import java.sql.Timestamp;
00062
import java.sql.Types;
00063
00064
import antlr.RecognitionException;
00065
00066
import com.quadcap.sql.InsertBlob;
00067
import com.quadcap.sql.ParameterExpression;
00068
import com.quadcap.sql.SQLParser;
00069
import com.quadcap.sql.Stmt;
00070
00071
import com.quadcap.io.AsciiReader;
00072
import com.quadcap.io.ReaderInputStream;
00073
00074
import com.quadcap.sql.file.BlockFile;
00075
00076
import com.quadcap.sql.types.*;
00077
00078
import com.quadcap.util.Debug;
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 public class PreparedStatement extends Statement
00091 implements java.sql.
PreparedStatement
00092 {
00093 Stmt
stmt;
00094 Vector
params;
00095
00096 String
sql;
00097
00098
00099
00100
00101
00102 public PreparedStatement(
Connection conn, String sql)
00103
throws SQLException, IOException
00104 {
00105 super(conn);
00106
00107
this.sql =
sql;
00108
if (
Statement.trace.bit(0)) {
00109
Debug.println(
"prepare(" +
sql +
")");
00110 }
00111
00112 session.
makeTransaction();
00113
SQLParser p =
new SQLParser(session,
sql, escapeProcessing);
00114 String auth = qConn.getAuth();
00115
try {
00116
stmt = p.
statement();
00117
params = p.
getParameters();
00118 }
catch (RecognitionException e) {
00119
throw new SQLException(e.toString(),
"42000");
00120 }
catch (antlr.TokenStreamException e) {
00121
throw new SQLException(e.toString(),
"Q0010");
00122 } finally {
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 qConn.setAuth(auth, null);
00135 }
00136 }
00137
00138
00139
00140
00141
00142
00143 public void addBatch() throws SQLException {
00144
throw new SQLException(
"not implemented",
"0A000");
00145 }
00146
00147 ParameterExpression getParam(
int i) {
00148
return (
ParameterExpression)
params.elementAt(i-1);
00149 }
00150
00151
00152
00153
00154
00155
00156
00157 public void clearParameters() throws SQLException {
00158
for (
int i = 1; i <=
params.size(); i++) {
00159
ParameterExpression pe = getParam(i);
00160 pe.
setValue(
ValueNull.valueNull);
00161 }
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 public boolean execute() throws SQLException {
00174
00175
if (
Statement.trace.bit(1)) {
00176
Debug.println(
"PreparedStatement.execute(" +
sql +
")");
00177 }
00178
00179
this.rs = null;
00180
this.updateCount = -1;
00181
00182
if (
this.qConn.isClosed()) {
00183
throw new SQLException(
"Connection closed");
00184 }
00185
try {
00186 session.
doStatement(
stmt);
00187
this.rs = (
ResultSet)session.
getResultSet(
this);
00188
if (rs != null) {
00189
return true;
00190 }
else {
00191
this.updateCount = session.
getUpdateCount();
00192
return false;
00193 }
00194 }
catch (IOException e) {
00195
try {
00196 session.
endStatement(
true);
00197 }
catch (IOException e1) {}
00198
throw new SQLException(e.toString(),
"Q0011");
00199 }
catch (SQLException e) {
00200
try {
00201 session.
endStatement(
true);
00202 }
catch (IOException e1) {}
00203
throw e;
00204 }
catch (Throwable e) {
00205
Debug.print(e);
00206
try {
00207 session.
endStatement(
true);
00208 }
catch (IOException e1) {}
00209
throw new SQLException(e.toString(),
"Q0013");
00210 }
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 public java.sql.ResultSet
executeQuery() throws SQLException {
00223
if (
execute()) {
00224
return this.rs;
00225 }
else {
00226
return null;
00227 }
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 public int executeUpdate() throws SQLException {
00239
if (!
execute()) {
00240
return updateCount;
00241 }
else {
00242
return 0;
00243 }
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 public java.sql.ResultSetMetaData
getMetaData() {
00258
return null;
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 public void setAsciiStream(
int col, InputStream in,
int length)
00274
throws SQLException
00275 {
00276
try {
00277
if (in == null) {
00278
setParamValue(col,
ValueNull.valueNull);
00279 }
else {
00280
ValueClob clob =
new ValueClob(session.
getDatabase(),
00281 session.
makeTransaction(),
00282
new AsciiReader(in), length);
00283
setParamValue(col, clob);
00284 session.
doStep(
new InsertBlob(session, clob));
00285 clob.
close();
00286 }
00287
00288 }
catch (IOException e) {
00289
Debug.print(e);
00290
throw new SQLException(e.toString(),
"Q001H");
00291 }
00292 }
00293
00294 final void setParamValue(
int col,
Value v)
throws SQLException {
00295
00296
if (
Statement.trace.bit(4)) {
00297
Debug.println(
"setValue(" + col +
", " + v +
")");
00298 }
00299
00300 getParam(col).
setValue(v);
00301 }
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 public void setBigDecimal(
int col, BigDecimal val)
throws SQLException {
00312
if (val == null) {
00313 setParamValue(col,
ValueNull.valueNull);
00314 }
else {
00315 setParamValue(col,
new ValueScaledInteger(val));
00316 }
00317 }
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 public void setBinaryStream(
int col, InputStream in,
int length)
00336
throws SQLException
00337 {
00338
try {
00339
if (in == null) {
00340 setParamValue(col,
ValueNull.valueNull);
00341 }
else {
00342
ValueBlob blob =
new ValueBlob(session.
getDatabase(),
00343 session.
makeTransaction(),
00344 in, length);
00345 setParamValue(col, blob);
00346 session.
doStep(
new InsertBlob(session, blob));
00347 blob.
close();
00348 }
00349
00350 }
catch (IOException e) {
00351
Debug.print(e);
00352
throw new SQLException(e.toString(),
"Q001H");
00353 }
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 public void setBoolean(
int col,
boolean val)
throws SQLException {
00365 setParamValue(col,
new ValueBoolean(val));
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 public void setByte(
int col, byte val)
throws SQLException {
00377 setParamValue(col,
new ValueShort(val));
00378 }
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388 public void setBytes(
int col, byte[] val)
throws SQLException {
00389
if (val == null) {
00390 setParamValue(col,
ValueNull.valueNull);
00391 }
else {
00392 setParamValue(col,
new ValueOctets(val));
00393 }
00394 }
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406 public void setCharacterStream(
int col, Reader r,
int length)
00407
throws SQLException
00408 {
00409
try {
00410
if (r == null) {
00411 setParamValue(col,
ValueNull.valueNull);
00412 }
else {
00413
ValueClob clob =
new ValueClob(session.
getDatabase(),
00414 session.
makeTransaction(),
00415 r, length);
00416 setParamValue(col, clob);
00417 session.
doStep(
new InsertBlob(session, clob));
00418 clob.
close();
00419 }
00420
00421 }
catch (IOException e) {
00422
Debug.print(e);
00423
throw new SQLException(e.toString(),
"Q001H");
00424 }
00425 }
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435 public void setDate(
int col, Date val)
throws SQLException {
00436
if (val == null) {
00437 setParamValue(col,
ValueNull.valueNull);
00438 }
else {
00439 setParamValue(col,
new ValueDate(val.getTime()));
00440 }
00441 }
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 public void setDate(
int col, Date val, Calendar cal)
throws SQLException {
00455
if (val == null) {
00456 setParamValue(col,
ValueNull.valueNull);
00457 }
else {
00458 cal.setTime(val);
00459 setParamValue(col,
new ValueDate(cal.getTime().getTime()));
00460 }
00461 }
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 public void setDouble(
int col,
double val)
throws SQLException {
00472 setParamValue(col,
new ValueDouble(val));
00473 }
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 public void setFloat(
int col,
float val)
throws SQLException {
00484 setParamValue(col,
new ValueDouble(val));
00485 }
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495 public void setInt(
int col,
int val)
throws SQLException {
00496 setParamValue(col,
new ValueInteger(val));
00497 }
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507 public void setLong(
int col,
long val)
throws SQLException {
00508 setParamValue(col,
new ValueLong(val));
00509 }
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520 public void setNull(
int col,
int type)
throws SQLException {
00521 setParamValue(col,
ValueNull.valueNull);
00522 }
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536 public void setNull(
int col,
int type, String
typename)
00537
throws SQLException
00538 {
00539 setParamValue(col,
ValueNull.valueNull);
00540 }
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550 public void setObject(
int col, Object val)
throws SQLException {
00551
if (val instanceof Blob) {
00552 Blob blob = (Blob)val;
00553 setBinaryStream(col, blob.getBinaryStream(), (
int)blob.length());
00554 }
else if (val instanceof Clob) {
00555 Clob clob = (Clob)val;
00556 setCharacterStream(col, clob.getCharacterStream(),
00557 (
int)clob.length());
00558 }
else {
00559 setParamValue(col,
Value.fromObject(val));
00560 }
00561 }
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572 public void setObject(
int col, Object val,
int type)
throws SQLException {
00573
Type t =
Value.typeForJdbcType(type);
00574
Value v =
Value.fromObject(val);
00575 setParamValue(col, t.
convert(v));
00576 }
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588 public void setObject(
int col, Object val,
int type,
int scale)
00589
throws SQLException
00590 {
00591
Value v =
Value.fromObject(val);
00592
Type t = null;
00593
switch (type) {
00594
case Types.NUMERIC:
00595
case Types.DECIMAL:
00596 t =
new TypeDecimal(32, scale);
00597
break;
00598
default:
00599 t =
Value.typeForJdbcType(type);
00600 }
00601 setParamValue(col, t.
convert(v));
00602 }
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612 public void setShort(
int col,
short val)
throws SQLException {
00613 setParamValue(col,
new ValueShort(val));
00614 }
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624 public void setString(
int col, String val)
throws SQLException {
00625
if (val == null) {
00626 setParamValue(col,
ValueNull.valueNull);
00627 }
else {
00628 setParamValue(col,
new ValueString(val));
00629 }
00630 }
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640 public void setTime(
int col, Time val)
throws SQLException {
00641
if (val == null) {
00642 setParamValue(col,
ValueNull.valueNull);
00643 }
else {
00644 setParamValue(col,
new ValueTime(val.getTime()));
00645 }
00646 }
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659 public void setTime(
int col, Time val, Calendar cal)
throws SQLException {
00660
if (val == null) {
00661 setParamValue(col,
ValueNull.valueNull);
00662 }
else {
00663
long t = val.getTime();
00664 cal.setTime(val);
00665 t -= (cal.get(Calendar.ZONE_OFFSET) +
00666 cal.get(Calendar.DST_OFFSET));
00667 setParamValue(col,
new ValueTime(t));
00668 }
00669 }
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679 public void setTimestamp(
int col, Timestamp val)
throws SQLException {
00680
if (val == null) {
00681 setParamValue(col,
ValueNull.valueNull);
00682 }
else {
00683 setParamValue(col,
new ValueTimestamp(val));
00684 }
00685 }
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698 public void setTimestamp(
int col, Timestamp val, Calendar cal)
00699
throws SQLException
00700 {
00701
if (val == null) {
00702 setParamValue(col,
ValueNull.valueNull);
00703 }
else {
00704
long t = val.getTime();
00705 cal.setTime(val);
00706 t -= (cal.get(Calendar.ZONE_OFFSET) +
00707 cal.get(Calendar.DST_OFFSET));
00708 Timestamp r =
new Timestamp(t);
00709 r.setNanos(val.getNanos());
00710 setParamValue(col,
new ValueTimestamp(r));
00711 }
00712 }
00713
00714
00715
00716
00717 public void setUnicodeStream(
int col, InputStream in,
int length)
00718
throws SQLException
00719 {
00720 setBinaryStream(col, in, length);
00721 }
00722
00723
00724
00725
00726
00727 public PreparedStatement(
Connection conn, String sql,
00728
int resultSetType,
00729
int resultSetConcurrency)
00730
throws SQLException, IOException
00731 {
00732
this(conn,
sql);
00733
this.resultSetType = resultSetType;
00734
this.resultSetConcurrency = resultSetConcurrency;
00735 }
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748 public void setArray(
int col, Array val)
throws SQLException {
00749
throw new SQLException(
"not implemented",
"0A000");
00750 }
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760 public void setBlob(
int col, Blob val)
throws SQLException {
00761
if (val == null) {
00762 setParamValue(col,
ValueNull.valueNull);
00763 }
else {
00764
if (val instanceof
ValueBlob) {
00765 setParamValue(col, (
ValueBlob)val);
00766 }
else {
00767 setBinaryStream(col, val.getBinaryStream(),
00768 (
int)(val.length()));
00769 }
00770 }
00771 }
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781 public void setClob(
int col, Clob val)
throws SQLException {
00782
if (val == null) {
00783 setParamValue(col,
ValueNull.valueNull);
00784 }
else {
00785 setAsciiStream(col, val.getAsciiStream(), (
int)(val.length()));
00786 }
00787 }
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799 public void setRef(
int col, Ref val)
throws SQLException {
00800
throw new SQLException(
"not implemented",
"0A000");
00801 }
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821 public void setURL(
int parameterIndex, java.net.URL x)
00822 throws SQLException
00823 {
00824 setString(parameterIndex, x.toString());
00825 }
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841 public java.sql.ParameterMetaData
getParameterMetaData()
00842 throws SQLException
00843 {
00844
throw new SQLException(
"Not implemented");
00845 }
00846
00847
00848 public String
toString() {
00849 StringBuffer sb =
00850
new StringBuffer(
"com.quadcap.jdbc.PreparedStatement(");
00851
if (
params != null)
for (
int i = 1; i <=
params.size(); i++) {
00852
if (i > 1) sb.append(
",");
00853
ParameterExpression e = getParam(i);
00854
Value v = e.
getValue(null, null);
00855 sb.append(String.valueOf(v));
00856 }
00857 sb.append(
")");
00858
return sb.toString();
00859 }
00860
00861 }