Debian Codegraph @

lang/sql/jdbc/SQLite/JDBC2/JDBCResultSet.java

raw
1 package SQLite.JDBC2;
2
3 import java.sql.*;
4 import java.math.BigDecimal;
5 import java.sql.ResultSet;
6 import java.sql.ResultSetMetaData;
7 import java.sql.SQLException;
8 import java.sql.SQLWarning;
9 import java.sql.Statement;
10 import java.sql.Types;
11
12 public class JDBCResultSet implements java.sql.ResultSet {
13
14 /**
15 * Current row to be retrieved.
16 */
17 private int row;
18
19 /**
20 * Table returned by Database.get_table()
21 */
22 protected SQLite.TableResult tr;
23
24 /**
25 * Statement from which result set was produced.
26 */
27 private JDBCStatement s;
28
29 /**
30 * Meta data for result set or null.
31 */
32 private JDBCResultSetMetaData md;
33
34 /**
35 * Last result cell retrieved or null.
36 */
37 private String lastg;
38
39 /**
40 * Updatability of this result set.
41 */
42 private int updatable;
43
44 /**
45 * When updatable this is the table name.
46 */
47 private String uptable;
48
49 /**
50 * When updatable these are the PK column names of uptable.
51 */
52 private String pkcols[];
53
54 /**
55 * When updatable these are the PK column indices (0-based) of uptable.
56 */
57 private int pkcoli[];
58
59 /*
60 * Constants to reflect updateability.
61 */
62 private final static int UPD_UNKNOWN = -1;
63 private final static int UPD_NO = 0;
64 private final static int UPD_INS = 1;
65 private final static int UPD_INSUPDDEL = 2;
66
67 /**
68 * Flag for cursor being (not) on insert row.
69 */
70 private boolean oninsrow;
71
72 /**
73 * Row buffer for insert/update row.
74 */
75 private String rowbuf[];
76
77 private static final boolean nullrepl =
78 SQLite.Database.version().compareTo("2.5.0") < 0;
79
80 public JDBCResultSet(SQLite.TableResult tr, JDBCStatement s) {
81 this.tr = tr;
82 this.s = s;
83 this.md = null;
84 this.lastg = null;
85 this.row = -1;
86 this.updatable = UPD_UNKNOWN;
87 this.oninsrow = false;
88 this.rowbuf = null;
89 }
90
91 public boolean isUpdatable() throws SQLException {
92 if (updatable == UPD_UNKNOWN) {
93 try {
94 JDBCResultSetMetaData m =
95 (JDBCResultSetMetaData) getMetaData();
96 java.util.HashSet h = new java.util.HashSet();
97 String lastt = null;
98 for (int i = 1; i <= tr.ncolumns; i++) {
99 lastt = m.getTableName(i);
100 h.add(lastt);
101 }
102 if (h.size() > 1 || lastt == null) {
103 updatable = UPD_NO;
104 throw new SQLException("view or join");
105 }
106 updatable = UPD_INS;
107 uptable = lastt;
108 JDBCResultSet pk = (JDBCResultSet)
109 s.conn.getMetaData().getPrimaryKeys(null, null, uptable);
110 if (pk.tr.nrows > 0) {
111 boolean colnotfound = false;
112 pkcols = new String[pk.tr.nrows];
113 pkcoli = new int[pk.tr.nrows];
114 for (int i = 0; i < pk.tr.nrows; i++) {
115 String rd[] = (String []) pk.tr.rows.elementAt(i);
116 pkcols[i] = rd[3];
117 try {
118 pkcoli[i] = findColumn(pkcols[i]) - 1;
119 } catch (SQLException ee) {
120 colnotfound = true;
121 }
122 }
123 if (!colnotfound) {
124 updatable = UPD_INSUPDDEL;
125 }
126 }
127 pk.close();
128 } catch (SQLException e) {
129 updatable = UPD_NO;
130 }
131 }
132 if (updatable < UPD_INS) {
133 throw new SQLException("result set not updatable");
134 }
135 return true;
136 }
137
138 public void fillRowbuf() throws SQLException {
139 if (rowbuf == null) {
140 if (row < 0) {
141 throw new SQLException("cursor outside of result set");
142 }
143 rowbuf = new String[tr.ncolumns];
144 System.arraycopy(tr.rows.elementAt(row), 0,
145 rowbuf, 0, tr.ncolumns);
146 }
147 }
148
149 public boolean next() throws SQLException {
150 if (tr == null) {
151 return false;
152 }
153 row++;
154 return row < tr.nrows;
155 }
156
157 public int findColumn(String columnName) throws SQLException {
158 JDBCResultSetMetaData m = (JDBCResultSetMetaData) getMetaData();
159 return m.findColByName(columnName);
160 }
161
162 public int getRow() throws SQLException {
163 if (tr == null) {
164 throw new SQLException("no rows");
165 }
166 return row + 1;
167 }
168
169 public boolean previous() throws SQLException {
170 if (tr == null) {
171 throw new SQLException("result set already closed");
172 }
173 if (row >= 0) {
174 row--;
175 }
176 return row >= 0;
177 }
178
179 public boolean absolute(int row) throws SQLException {
180 if (tr == null) {
181 return false;
182 }
183 if (row < 0) {
184 row = tr.nrows + 1 + row;
185 }
186 row--;
187 if (row < 0 || row > tr.nrows) {
188 return false;
189 }
190 this.row = row;
191 return true;
192 }
193
194 public boolean relative(int row) throws SQLException {
195 if (tr == null) {
196 return false;
197 }
198 if (this.row + row < 0 || this.row + row >= tr.nrows) {
199 return false;
200 }
201 this.row += row;
202 return true;
203 }
204
205 public void setFetchDirection(int dir) throws SQLException {
206 if (dir != ResultSet.FETCH_FORWARD) {
207 throw new SQLException("not supported");
208 }
209 }
210
211 public int getFetchDirection() throws SQLException {
212 return ResultSet.FETCH_FORWARD;
213 }
214
215 public void setFetchSize(int fsize) throws SQLException {
216 if (fsize != 1) {
217 throw new SQLException("not supported");
218 }
219 }
220
221 public int getFetchSize() throws SQLException {
222 return 1;
223 }
224
225 public String getString(int columnIndex) throws SQLException {
226 if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) {
227 throw new SQLException("column " + columnIndex + " not found");
228 }
229 String rd[] = (String []) tr.rows.elementAt(row);
230 lastg = rd[columnIndex - 1];
231 return lastg;
232 }
233
234 public String getString(String columnName) throws SQLException {
235 int col = findColumn(columnName);
236 return getString(col);
237 }
238
239 public int getInt(int columnIndex) throws SQLException {
240 Integer i = internalGetInt(columnIndex);
241 if (i != null) {
242 return i.intValue();
243 }
244 return 0;
245 }
246
247 private Integer internalGetInt(int columnIndex) throws SQLException {
248 if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) {
249 throw new SQLException("column " + columnIndex + " not found");
250 }
251 String rd[] = (String []) tr.rows.elementAt(row);
252 lastg = rd[columnIndex - 1];
253 try {
254 return Integer.valueOf(lastg);
255 } catch (java.lang.Exception e) {
256 lastg = null;
257 }
258 return null;
259 }
260
261 public int getInt(String columnName) throws SQLException {
262 int col = findColumn(columnName);
263 return getInt(col);
264 }
265
266 public boolean getBoolean(int columnIndex) throws SQLException {
267 return getInt(columnIndex) == 1 ||
268 Boolean.valueOf(getString(columnIndex)).booleanValue();
269 }
270
271 public boolean getBoolean(String columnName) throws SQLException {
272 int col = findColumn(columnName);
273 return getBoolean(col);
274 }
275
276 public ResultSetMetaData getMetaData() throws SQLException {
277 if (md == null) {
278 md = new JDBCResultSetMetaData(this);
279 }
280 return md;
281 }
282
283 public short getShort(int columnIndex) throws SQLException {
284 Short sh = internalGetShort(columnIndex);
285 if (sh != null) {
286 return sh.shortValue();
287 }
288 return 0;
289 }
290
291 private Short internalGetShort(int columnIndex) throws SQLException {
292 if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) {
293 throw new SQLException("column " + columnIndex + " not found");
294 }
295 String rd[] = (String []) tr.rows.elementAt(row);
296 lastg = rd[columnIndex - 1];
297 try {
298 return Short.valueOf(lastg);
299 } catch (java.lang.Exception e) {
300 lastg = null;
301 }
302 return null;
303 }
304
305 public short getShort(String columnName) throws SQLException {
306 int col = findColumn(columnName);
307 return getShort(col);
308 }
309
310 public java.sql.Time getTime(int columnIndex) throws SQLException {
311 return internalGetTime(columnIndex, null);
312 }
313
314 private java.sql.Time internalGetTime(int columnIndex,
315 java.util.Calendar cal)
316 throws SQLException {
317 if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) {
318 throw new SQLException("column " + columnIndex + " not found");
319 }
320 String rd[] = (String []) tr.rows.elementAt(row);
321 lastg = rd[columnIndex - 1];
322 try {
323 if (s.conn.useJulian) {
324 try {
325 return new java.sql.Time(SQLite.Database.long_from_julian(lastg));
326 } catch (java.lang.Exception ee) {
327 return java.sql.Time.valueOf(lastg);
328 }
329 } else {
330 try {
331 return java.sql.Time.valueOf(lastg);
332 } catch (java.lang.Exception ee) {
333 return new java.sql.Time(SQLite.Database.long_from_julian(lastg));
334 }
335 }
336 } catch (java.lang.Exception e) {
337 lastg = null;
338 }
339 return null;
340 }
341
342 public java.sql.Time getTime(String columnName) throws SQLException {
343 int col = findColumn(columnName);
344 return getTime(col);
345 }
346
347 public java.sql.Time getTime(int columnIndex, java.util.Calendar cal)
348 throws SQLException {
349 return internalGetTime(columnIndex, cal);
350 }
351
352 public java.sql.Time getTime(String columnName, java.util.Calendar cal)
353 throws SQLException{
354 int col = findColumn(columnName);
355 return getTime(col, cal);
356 }
357
358 public java.sql.Timestamp getTimestamp(int columnIndex)
359 throws SQLException{
360 return internalGetTimestamp(columnIndex, null);
361 }
362
363 private java.sql.Timestamp internalGetTimestamp(int columnIndex,
364 java.util.Calendar cal)
365 throws SQLException {
366 if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) {
367 throw new SQLException("column " + columnIndex + " not found");
368 }
369 String rd[] = (String []) tr.rows.elementAt(row);
370 lastg = rd[columnIndex - 1];
371 try {
372 if (s.conn.useJulian) {
373 try {
374 return new
375 java.sql.Timestamp(SQLite.Database.long_from_julian(lastg));
376 } catch (java.lang.Exception ee) {
377 return java.sql.Timestamp.valueOf(lastg);
378 }
379 } else {
380 try {
381 return java.sql.Timestamp.valueOf(lastg);
382 } catch (java.lang.Exception ee) {
383 return new
384 java.sql.Timestamp(SQLite.Database.long_from_julian(lastg));
385 }
386 }
387 } catch (java.lang.Exception e) {
388 lastg = null;
389 }
390 return null;
391 }
392
393 public java.sql.Timestamp getTimestamp(String columnName)
394 throws SQLException{
395 int col = findColumn(columnName);
396 return getTimestamp(col);
397 }
398
399 public java.sql.Timestamp getTimestamp(int columnIndex,
400 java.util.Calendar cal)
401 throws SQLException {
402 return internalGetTimestamp(columnIndex, cal);
403 }
404
405 public java.sql.Timestamp getTimestamp(String columnName,
406 java.util.Calendar cal)
407 throws SQLException {
408 int col = findColumn(columnName);
409 return getTimestamp(col, cal);
410 }
411
412 public java.sql.Date getDate(int columnIndex) throws SQLException {
413 return internalGetDate(columnIndex, null);
414 }
415
416 private java.sql.Date internalGetDate(int columnIndex,
417 java.util.Calendar cal)
418 throws SQLException {
419 if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) {
420 throw new SQLException("column " + columnIndex + " not found");
421 }
422 String rd[] = (String []) tr.rows.elementAt(row);
423 lastg = rd[columnIndex - 1];
424 try {
425 if (s.conn.useJulian) {
426 try {
427 return new java.sql.Date(SQLite.Database.long_from_julian(lastg));
428 } catch (java.lang.Exception ee) {
429 return java.sql.Date.valueOf(lastg);
430 }
431 } else {
432 try {
433 return java.sql.Date.valueOf(lastg);
434 } catch (java.lang.Exception ee) {
435 return new java.sql.Date(SQLite.Database.long_from_julian(lastg));
436 }
437 }
438 } catch (java.lang.Exception e) {
439 lastg = null;
440 }
441 return null;
442 }
443
444 public java.sql.Date getDate(String columnName) throws SQLException {
445 int col = findColumn(columnName);
446 return getDate(col);
447 }
448
449 public java.sql.Date getDate(int columnIndex, java.util.Calendar cal)
450 throws SQLException{
451 return internalGetDate(columnIndex, cal);
452 }
453
454 public java.sql.Date getDate(String columnName, java.util.Calendar cal)
455 throws SQLException{
456 int col = findColumn(columnName);
457 return getDate(col, cal);
458 }
459
460 public double getDouble(int columnIndex) throws SQLException {
461 Double d = internalGetDouble(columnIndex);
462 if (d != null) {
463 return d.doubleValue();
464 }
465 return 0;
466 }
467
468 private Double internalGetDouble(int columnIndex) throws SQLException {
469 if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) {
470 throw new SQLException("column " + columnIndex + " not found");
471 }
472 String rd[] = (String []) tr.rows.elementAt(row);
473 lastg = rd[columnIndex - 1];
474 try {
475 return Double.valueOf(lastg);
476 } catch (java.lang.Exception e) {
477 lastg = null;
478 }
479 return null;
480 }
481
482 public double getDouble(String columnName) throws SQLException {
483 int col = findColumn(columnName);
484 return getDouble(col);
485 }
486
487 public float getFloat(int columnIndex) throws SQLException {
488 Float f = internalGetFloat(columnIndex);
489 if (f != null) {
490 return f.floatValue();
491 }
492 return 0;
493 }
494
495 private Float internalGetFloat(int columnIndex) throws SQLException {
496 if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) {
497 throw new SQLException("column " + columnIndex + " not found");
498 }
499 String rd[] = (String []) tr.rows.elementAt(row);
500 lastg = rd[columnIndex - 1];
501 try {
502 return Float.valueOf(lastg);
503 } catch (java.lang.Exception e) {
504 lastg = null;
505 }
506 return null;
507 }
508
509 public float getFloat(String columnName) throws SQLException {
510 int col = findColumn(columnName);
511 return getFloat(col);
512 }
513
514 public long getLong(int columnIndex) throws SQLException {
515 Long l = internalGetLong(columnIndex);
516 if (l != null) {
517 return l.longValue();
518 }
519 return 0;
520 }
521
522 private Long internalGetLong(int columnIndex) throws SQLException {
523 if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) {
524 throw new SQLException("column " + columnIndex + " not found");
525 }
526 String rd[] = (String []) tr.rows.elementAt(row);
527 lastg = rd[columnIndex - 1];
528 try {
529 return Long.valueOf(lastg);
530 } catch (java.lang.Exception e) {
531 lastg = null;
532 }
533 return null;
534 }
535
536 public long getLong(String columnName) throws SQLException {
537 int col = findColumn(columnName);
538 return getLong(col);
539 }
540
541 public java.io.InputStream getUnicodeStream(int columnIndex)
542 throws SQLException {
543 throw new SQLException("not supported");
544 }
545
546 public java.io.InputStream getUnicodeStream(String columnName)
547 throws SQLException {
548 throw new SQLException("not supported");
549 }
550
551 public java.io.InputStream getAsciiStream(String columnName)
552 throws SQLException {
553 throw new SQLException("not supported");
554 }
555
556 public java.io.InputStream getAsciiStream(int columnIndex)
557 throws SQLException {
558 throw new SQLException("not supported");
559 }
560
561 public BigDecimal getBigDecimal(String columnName)
562 throws SQLException {
563 throw new SQLException("not supported");
564 }
565
566 public BigDecimal getBigDecimal(String columnName, int scale)
567 throws SQLException {
568 throw new SQLException("not supported");
569 }
570
571 public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
572 throw new SQLException("not supported");
573 }
574
575 public BigDecimal getBigDecimal(int columnIndex, int scale)
576 throws SQLException {
577 throw new SQLException("not supported");
578 }
579
580 public java.io.InputStream getBinaryStream(int columnIndex)
581 throws SQLException {
582 byte data[] = getBytes(columnIndex);
583 if (data != null) {
584 return new java.io.ByteArrayInputStream(data);
585 }
586 return null;
587 }
588
589 public java.io.InputStream getBinaryStream(String columnName)
590 throws SQLException {
591 byte data[] = getBytes(columnName);
592 if (data != null) {
593 return new java.io.ByteArrayInputStream(data);
594 }
595 return null;
596 }
597
598 public byte getByte(int columnIndex) throws SQLException {
599 throw new SQLException("not supported");
600 }
601
602 public byte getByte(String columnName) throws SQLException {
603 throw new SQLException("not supported");
604 }
605
606 public byte[] getBytes(int columnIndex) throws SQLException {
607 if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) {
608 throw new SQLException("column " + columnIndex + " not found");
609 }
610 byte ret[] = null;
611 String rd[] = (String []) tr.rows.elementAt(row);
612 lastg = rd[columnIndex - 1];
613 if (lastg != null) {
614 ret = SQLite.StringEncoder.decode(lastg);
615 }
616 return ret;
617 }
618
619 public byte[] getBytes(String columnName) throws SQLException {
620 int col = findColumn(columnName);
621 return getBytes(col);
622 }
623
624 public String getCursorName() throws SQLException {
625 return null;
626 }
627
628 public Object getObject(int columnIndex) throws SQLException {
629 if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) {
630 throw new SQLException("column " + columnIndex + " not found");
631 }
632 String rd[] = (String []) tr.rows.elementAt(row);
633 lastg = rd[columnIndex - 1];
634 Object ret = lastg;
635 if (tr instanceof TableResultX) {
636 switch (((TableResultX) tr).sql_type[columnIndex - 1]) {
637 case Types.SMALLINT:
638 ret = internalGetShort(columnIndex);
639 break;
640 case Types.INTEGER:
641 ret = internalGetInt(columnIndex);
642 break;
643 case Types.DOUBLE:
644 ret = internalGetDouble(columnIndex);
645 break;
646 case Types.FLOAT:
647 ret = internalGetFloat(columnIndex);
648 break;
649 case Types.BIGINT:
650 ret = internalGetLong(columnIndex);
651 break;
652 case Types.BINARY:
653 case Types.VARBINARY:
654 case Types.LONGVARBINARY:
655 ret = getBytes(columnIndex);
656 break;
657 case Types.NULL:
658 ret = null;
659 break;
660 /* defaults to String below */
661 }
662 }
663 return ret;
664 }
665
666 public Object getObject(String columnName) throws SQLException {
667 int col = findColumn(columnName);
668 return getObject(col);
669 }
670
671 public Object getObject(int columnIndex, java.util.Map map)
672 throws SQLException {
673 throw new SQLException("not supported");
674 }
675
676 public Object getObject(String columnIndex, java.util.Map map)
677 throws SQLException {
678 throw new SQLException("not supported");
679 }
680
681 public java.sql.Ref getRef(int columnIndex) throws SQLException {
682 throw new SQLException("not supported");
683 }
684
685 public java.sql.Ref getRef(String columnIndex) throws SQLException {
686 throw new SQLException("not supported");
687 }
688
689 public java.sql.Blob getBlob(int columnIndex) throws SQLException {
690 throw new SQLException("not supported");
691 }
692
693 public java.sql.Blob getBlob(String columnIndex) throws SQLException {
694 throw new SQLException("not supported");
695 }
696
697 public java.sql.Clob getClob(int columnIndex) throws SQLException {
698 throw new SQLException("not supported");
699 }
700
701 public java.sql.Clob getClob(String columnIndex) throws SQLException {
702 throw new SQLException("not supported");
703 }
704
705 public java.sql.Array getArray(int columnIndex) throws SQLException {
706 throw new SQLException("not supported");
707 }
708
709 public java.sql.Array getArray(String columnIndex) throws SQLException {
710 throw new SQLException("not supported");
711 }
712
713 public java.io.Reader getCharacterStream(int columnIndex)
714 throws SQLException {
715 String data = getString(columnIndex);
716 if (data != null) {
717 char[] cdata = data.toCharArray();
718 return new java.io.CharArrayReader(cdata);
719 }
720 return null;
721 }
722
723 public java.io.Reader getCharacterStream(String columnName)
724 throws SQLException {
725 String data = getString(columnName);
726 if (data != null) {
727 char[] cdata = data.toCharArray();
728 return new java.io.CharArrayReader(cdata);
729 }
730 return null;
731 }
732
733 public SQLWarning getWarnings() throws SQLException {
734 throw new SQLException("not supported");
735 }
736
737 public boolean wasNull() throws SQLException {
738 return lastg == null;
739 }
740
741 public void clearWarnings() throws SQLException {
742 throw new SQLException("not supported");
743 }
744
745 public boolean isFirst() throws SQLException {
746 if (tr == null) {
747 return true;
748 }
749 return row == 0;
750 }
751
752 public boolean isBeforeFirst() throws SQLException {
753 if (tr == null || tr.nrows <= 0) {
754 return false;
755 }
756 return row < 0;
757 }
758
759 public void beforeFirst() throws SQLException {
760 if (tr == null) {
761 return;
762 }
763 row = -1;
764 }
765
766 public boolean first() throws SQLException {
767 if (tr == null || tr.nrows <= 0) {
768 return false;
769 }
770 row = 0;
771 return true;
772 }
773
774 public boolean isAfterLast() throws SQLException {
775 if (tr == null || tr.nrows <= 0) {
776 return false;
777 }
778 return row >= tr.nrows;
779 }
780
781 public void afterLast() throws SQLException {
782 if (tr == null) {
783 return;
784 }
785 row = tr.nrows;
786 }
787
788 public boolean isLast() throws SQLException {
789 if (tr == null) {
790 return true;
791 }
792 return row == tr.nrows - 1;
793 }
794
795 public boolean last() throws SQLException {
796 if (tr == null || tr.nrows <= 0) {
797 return false;
798 }
799 row = tr.nrows -1;
800 return true;
801 }
802
803 public int getType() throws SQLException {
804 return TYPE_SCROLL_SENSITIVE;
805 }
806
807 public int getConcurrency() throws SQLException {
808 return CONCUR_UPDATABLE;
809 }
810
811 public boolean rowUpdated() throws SQLException {
812 return false;
813 }
814
815 public boolean rowInserted() throws SQLException {
816 return false;
817 }
818
819 public boolean rowDeleted() throws SQLException {
820 return false;
821 }
822
823 public void insertRow() throws SQLException {
824 isUpdatable();
825 if (!oninsrow || rowbuf == null) {
826 throw new SQLException("no insert data provided");
827 }
828 JDBCResultSetMetaData m = (JDBCResultSetMetaData) getMetaData();
829 StringBuffer sb = new StringBuffer();
830 sb.append("INSERT INTO ");
831 sb.append(SQLite.Shell.sql_quote_dbl(uptable));
832 sb.append("(");
833 for (int i = 0; i < tr.ncolumns; i++) {
834 sb.append(SQLite.Shell.sql_quote_dbl(m.getColumnName(i + 1)));
835 if (i < tr.ncolumns - 1) {
836 sb.append(",");
837 }
838 }
839 sb.append(") VALUES(");
840 for (int i = 0; i < tr.ncolumns; i++) {
841 sb.append(nullrepl ? "'%q'" : "%Q");
842 if (i < tr.ncolumns - 1) {
843 sb.append(",");
844 }
845 }
846 sb.append(")");
847 try {
848 this.s.conn.db.exec(sb.toString(), null, rowbuf);
849 } catch (SQLite.Exception e) {
850 throw new SQLException(e.getMessage());
851 }
852 tr.newrow(rowbuf);
853 rowbuf = null;
854 oninsrow = false;
855 last();
856 }
857
858 public void updateRow() throws SQLException {
859 isUpdatable();
860 if (rowbuf == null) {
861 throw new SQLException("no update data provided");
862 }
863 if (oninsrow) {
864 throw new SQLException("cursor on insert row");
865 }
866 if (updatable < UPD_INSUPDDEL) {
867 throw new SQLException("no primary key on table defined");
868 }
869 String rd[] = (String []) tr.rows.elementAt(row);
870 JDBCResultSetMetaData m = (JDBCResultSetMetaData) getMetaData();
871 String args[] = new String[tr.ncolumns + pkcols.length];
872 StringBuffer sb = new StringBuffer();
873 sb.append("UPDATE ");
874 sb.append(SQLite.Shell.sql_quote_dbl(uptable));
875 sb.append(" SET ");
876 int i;
877 for (i = 0; i < tr.ncolumns; i++) {
878 sb.append(SQLite.Shell.sql_quote_dbl(m.getColumnName(i + 1)));
879 sb.append(" = " + (nullrepl ? "'%q'" : "%Q"));
880 if (i < tr.ncolumns - 1) {
881 sb.append(",");
882 }
883 args[i] = rowbuf[i];
884 }
885 sb. append(" WHERE ");
886 for (int k = 0; k < pkcols.length; k++, i++) {
887 sb.append(SQLite.Shell.sql_quote_dbl(pkcols[k]));
888 sb.append(" = " + (nullrepl ? "'%q'" : "%Q"));
889 if (k < pkcols.length - 1) {
890 sb.append(" AND ");
891 }
892 args[i] = rd[pkcoli[k]];
893 }
894 try {
895 this.s.conn.db.exec(sb.toString(), null, args);
896 } catch (SQLite.Exception e) {
897 throw new SQLException(e.getMessage());
898 }
899 System.arraycopy(rowbuf, 0, rd, 0, rowbuf.length);
900 rowbuf = null;
901 }
902
903 public void deleteRow() throws SQLException {
904 isUpdatable();
905 if (oninsrow) {
906 throw new SQLException("cursor on insert row");
907 }
908 if (updatable < UPD_INSUPDDEL) {
909 throw new SQLException("no primary key on table defined");
910 }
911 fillRowbuf();
912 StringBuffer sb = new StringBuffer();
913 sb.append("DELETE FROM ");
914 sb.append(SQLite.Shell.sql_quote_dbl(uptable));
915 sb.append(" WHERE ");
916 String args[] = new String[pkcols.length];
917 for (int i = 0; i < pkcols.length; i++) {
918 sb.append(SQLite.Shell.sql_quote_dbl(pkcols[i]));
919 sb.append(" = " + (nullrepl ? "'%q'" : "%Q"));
920 if (i < pkcols.length - 1) {
921 sb.append(" AND ");
922 }
923 args[i] = rowbuf[pkcoli[i]];
924 }
925 try {
926 this.s.conn.db.exec(sb.toString(), null, args);
927 } catch (SQLite.Exception e) {
928 throw new SQLException(e.getMessage());
929 }
930 rowbuf = null;
931 }
932
933 public void refreshRow() throws SQLException {
934 isUpdatable();
935 if (oninsrow) {
936 throw new SQLException("cursor on insert row");
937 }
938 if (updatable < UPD_INSUPDDEL) {
939 throw new SQLException("no primary key on table defined");
940 }
941 JDBCResultSetMetaData m = (JDBCResultSetMetaData) getMetaData();
942 String rd[] = (String []) tr.rows.elementAt(row);
943 StringBuffer sb = new StringBuffer();
944 sb.append("SELECT ");
945 for (int i = 0; i < tr.ncolumns; i++) {
946 sb.append(SQLite.Shell.sql_quote_dbl(m.getColumnName(i + 1)));
947 if (i < tr.ncolumns - 1) {
948 sb.append(",");
949 }
950 }
951 sb.append(" FROM ");
952 sb.append(SQLite.Shell.sql_quote_dbl(uptable));
953 sb.append(" WHERE ");
954 String args[] = new String[pkcols.length];
955 for (int i = 0; i < pkcols.length; i++) {
956 sb.append(SQLite.Shell.sql_quote_dbl(pkcols[i]));
957 sb.append(" = " + (nullrepl ? "'%q'" : "%Q"));
958 if (i < pkcols.length - 1) {
959 sb.append(" AND ");
960 }
961 args[i] = rd[pkcoli[i]];
962 }
963 SQLite.TableResult trnew = null;
964 try {
965 trnew = this.s.conn.db.get_table(sb.toString(), args);
966 } catch (SQLite.Exception e) {
967 throw new SQLException(e.getMessage());
968 }
969 if (trnew.nrows != 1) {
970 throw new SQLException("wrong size of result set; expected 1, got " + trnew.nrows);
971 }
972 rowbuf = null;
973 tr.rows.setElementAt(trnew.rows.elementAt(0), row);
974 }
975
976 public void cancelRowUpdates() throws SQLException {
977 rowbuf = null;
978 }
979
980 public void moveToInsertRow() throws SQLException {
981 isUpdatable();
982 if (!oninsrow) {
983 oninsrow = true;
984 rowbuf = new String[tr.nrows];
985 }
986 }
987
988 public void moveToCurrentRow() throws SQLException {
989 if (oninsrow) {
990 oninsrow = false;
991 rowbuf = null;
992 }
993 }
994
995 public void updateNull(int colIndex) throws SQLException {
996 isUpdatable();
997 if (tr == null || colIndex < 1 || colIndex > tr.ncolumns) {
998 throw new SQLException("column " + colIndex + " not found");
999 }
1000 fillRowbuf();
1001 rowbuf[colIndex - 1] = null;
1002 }
1003
1004 public void updateBoolean(int colIndex, boolean b) throws SQLException {
1005 updateString(colIndex, b ? "1" : "0");
1006 }
1007
1008 public void updateByte(int colIndex, byte b) throws SQLException {
1009 throw new SQLException("not supported");
1010 }
1011
1012 public void updateShort(int colIndex, short b) throws SQLException {
1013 isUpdatable();
1014 if (tr == null || colIndex < 1 || colIndex > tr.ncolumns) {
1015 throw new SQLException("column " + colIndex + " not found");
1016 }
1017 fillRowbuf();
1018 rowbuf[colIndex - 1] = Short.toString(b);
1019 }
1020
1021 public void updateInt(int colIndex, int b) throws SQLException {
1022 isUpdatable();
1023 if (tr == null || colIndex < 1 || colIndex > tr.ncolumns) {
1024 throw new SQLException("column " + colIndex + " not found");
1025 }
1026 fillRowbuf();
1027 rowbuf[colIndex - 1] = Integer.toString(b);
1028 }
1029
1030 public void updateLong(int colIndex, long b) throws SQLException {
1031 isUpdatable();
1032 if (tr == null || colIndex < 1 || colIndex > tr.ncolumns) {
1033 throw new SQLException("column " + colIndex + " not found");
1034 }
1035 fillRowbuf();
1036 rowbuf[colIndex - 1] = Long.toString(b);
1037 }
1038
1039 public void updateFloat(int colIndex, float f) throws SQLException {
1040 isUpdatable();
1041 if (tr == null || colIndex < 1 || colIndex > tr.ncolumns) {
1042 throw new SQLException("column " + colIndex + " not found");
1043 }
1044 fillRowbuf();
1045 rowbuf[colIndex - 1] = Float.toString(f);
1046 }
1047
1048 public void updateDouble(int colIndex, double f) throws SQLException {
1049 isUpdatable();
1050 if (tr == null || colIndex < 1 || colIndex > tr.ncolumns) {
1051 throw new SQLException("column " + colIndex + " not found");
1052 }
1053 fillRowbuf();
1054 rowbuf[colIndex - 1] = Double.toString(f);
1055 }
1056
1057 public void updateBigDecimal(int colIndex, BigDecimal f)
1058 throws SQLException {
1059 throw new SQLException("not supported");
1060 }
1061
1062 public void updateString(int colIndex, String s) throws SQLException {
1063 isUpdatable();
1064 if (tr == null || colIndex < 1 || colIndex > tr.ncolumns) {
1065 throw new SQLException("column " + colIndex + " not found");
1066 }
1067 fillRowbuf();
1068 rowbuf[colIndex - 1] = s;
1069 }
1070
1071 public void updateBytes(int colIndex, byte[] s) throws SQLException {
1072 isUpdatable();
1073 if (tr == null || colIndex < 1 || colIndex > tr.ncolumns) {
1074 throw new SQLException("column " + colIndex + " not found");
1075 }
1076 fillRowbuf();
1077 if (this.s.conn.db.is3()) {
1078 rowbuf[colIndex - 1] = SQLite.StringEncoder.encodeX(s);
1079 } else {
1080 rowbuf[colIndex - 1] = SQLite.StringEncoder.encode(s);
1081 }
1082 }
1083
1084 public void updateDate(int colIndex, java.sql.Date d) throws SQLException {
1085 isUpdatable();
1086 if (tr == null || colIndex < 1 || colIndex > tr.ncolumns) {
1087 throw new SQLException("column " + colIndex + " not found");
1088 }
1089 fillRowbuf();
1090 rowbuf[colIndex - 1] = d.toString();
1091 }
1092
1093 public void updateTime(int colIndex, java.sql.Time t) throws SQLException {
1094 isUpdatable();
1095 if (tr == null || colIndex < 1 || colIndex > tr.ncolumns) {
1096 throw new SQLException("column " + colIndex + " not found");
1097 }
1098 fillRowbuf();
1099 rowbuf[colIndex - 1] = t.toString();
1100 }
1101
1102 public void updateTimestamp(int colIndex, java.sql.Timestamp t)
1103 throws SQLException {
1104 isUpdatable();
1105 if (tr == null || colIndex < 1 || colIndex > tr.ncolumns) {
1106 throw new SQLException("column " + colIndex + " not found");
1107 }
1108 fillRowbuf();
1109 rowbuf[colIndex - 1] = t.toString();
1110 }
1111
1112 public void updateAsciiStream(int colIndex, java.io.InputStream in, int s)
1113 throws SQLException {
1114 throw new SQLException("not supported");
1115 }
1116
1117 public void updateBinaryStream(int colIndex, java.io.InputStream in, int s)
1118 throws SQLException {
1119 throw new SQLException("not supported");
1120 }
1121
1122 public void updateCharacterStream(int colIndex, java.io.Reader in, int s)
1123 throws SQLException {
1124 throw new SQLException("not supported");
1125 }
1126
1127 public void updateObject(int colIndex, Object obj) throws SQLException {
1128 updateString(colIndex, obj.toString());
1129 }
1130
1131 public void updateObject(int colIndex, Object obj, int s)
1132 throws SQLException {
1133 updateString(colIndex, obj.toString());
1134 }
1135
1136 public void updateNull(String colName) throws SQLException {
1137 int col = findColumn(colName);
1138 updateNull(col);
1139 }
1140
1141 public void updateBoolean(String colName, boolean b) throws SQLException {
1142 int col = findColumn(colName);
1143 updateBoolean(col, b);
1144 }
1145
1146 public void updateByte(String colName, byte b) throws SQLException {
1147 throw new SQLException("not supported");
1148 }
1149
1150 public void updateShort(String colName, short b) throws SQLException {
1151 int col = findColumn(colName);
1152 updateShort(col, b);
1153 }
1154
1155 public void updateInt(String colName, int b) throws SQLException {
1156 int col = findColumn(colName);
1157 updateInt(col, b);
1158 }
1159
1160 public void updateLong(String colName, long b) throws SQLException {
1161 int col = findColumn(colName);
1162 updateLong(col, b);
1163 }
1164
1165 public void updateFloat(String colName, float f) throws SQLException {
1166 int col = findColumn(colName);
1167 updateFloat(col, f);
1168 }
1169
1170 public void updateDouble(String colName, double f) throws SQLException {
1171 int col = findColumn(colName);
1172 updateDouble(col, f);
1173 }
1174
1175 public void updateBigDecimal(String colName, BigDecimal f)
1176 throws SQLException {
1177 throw new SQLException("not supported");
1178 }
1179
1180 public void updateString(String colName, String s) throws SQLException {
1181 int col = findColumn(colName);
1182 updateString(col, s);
1183 }
1184
1185 public void updateBytes(String colName, byte[] s) throws SQLException {
1186 int col = findColumn(colName);
1187 updateBytes(col, s);
1188 }
1189
1190 public void updateDate(String colName, java.sql.Date d)
1191 throws SQLException {
1192 int col = findColumn(colName);
1193 updateDate(col, d);
1194 }
1195
1196 public void updateTime(String colName, java.sql.Time t)
1197 throws SQLException {
1198 int col = findColumn(colName);
1199 updateTime(col, t);
1200 }
1201
1202 public void updateTimestamp(String colName, java.sql.Timestamp t)
1203 throws SQLException {
1204 int col = findColumn(colName);
1205 updateTimestamp(col, t);
1206 }
1207
1208 public void updateAsciiStream(String colName, java.io.InputStream in,
1209 int s)
1210 throws SQLException {
1211 throw new SQLException("not supported");
1212 }
1213
1214 public void updateBinaryStream(String colName, java.io.InputStream in,
1215 int s)
1216 throws SQLException {
1217 throw new SQLException("not supported");
1218 }
1219
1220 public void updateCharacterStream(String colName, java.io.Reader in,
1221 int s)
1222 throws SQLException {
1223 throw new SQLException("not supported");
1224 }
1225
1226 public void updateObject(String colName, Object obj)
1227 throws SQLException {
1228 int col = findColumn(colName);
1229 updateObject(col, obj);
1230 }
1231
1232 public void updateObject(String colName, Object obj, int s)
1233 throws SQLException {
1234 int col = findColumn(colName);
1235 updateObject(col, obj, s);
1236 }
1237
1238 public Statement getStatement() throws SQLException {
1239 if (s == null) {
1240 throw new SQLException("stale result set");
1241 }
1242 return s;
1243 }
1244
1245 public void close() throws SQLException {
1246 s = null;
1247 tr = null;
1248 lastg = null;
1249 oninsrow = false;
1250 rowbuf = null;
1251 row = -1;
1252 }
1253 }