| 1 19 package com.mysql.jdbc; 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.ObjectInputStream ; 25 import java.io.StringReader ; 26 27 import java.math.BigDecimal ; 28 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 32 import java.sql.Array ; 33 import java.sql.Date ; 34 import java.sql.Ref ; 35 import java.sql.SQLException ; 36 import java.sql.SQLWarning ; 37 import java.sql.Time ; 38 import java.sql.Timestamp ; 39 import java.sql.Types ; 40 41 import java.util.Calendar ; 42 import java.util.GregorianCalendar ; 43 import java.util.HashMap ; 44 import java.util.Map ; 45 import java.util.TimeZone ; 46 47 48 106 public class ResultSet implements java.sql.ResultSet { 107 111 private TimeZone defaultTimeZone; 112 113 114 protected com.mysql.jdbc.Connection connection; 116 117 protected Map columnNameToIndex = null; 118 119 120 protected Map fullColumnNameToIndex = null; 121 122 123 protected RowData rowData; 125 126 protected java.sql.SQLWarning warningChain = null; 127 128 129 protected com.mysql.jdbc.Statement owningStatement; 130 131 132 protected String catalog = null; 133 134 138 protected String serverInfo = null; 139 140 141 protected Field[] fields; 143 144 protected byte[][] thisRow; 146 147 protected boolean doingUpdates = false; 148 149 150 protected boolean isClosed = false; 151 152 153 protected boolean onInsertRow = false; 154 155 159 protected boolean reallyResult = false; 160 161 162 protected boolean wasNullFlag = false; 163 164 169 protected char firstCharOfQuery; 170 171 172 protected int currentRow = -1; 174 175 protected int fetchDirection = FETCH_FORWARD; 176 177 178 protected int fetchSize = 0; 179 180 181 protected int resultSetConcurrency = 0; 182 183 184 protected int resultSetType = 0; 185 186 187 protected long updateCount; 188 189 197 198 protected long updateId = -1; 199 private Calendar fastDateCal = null; 200 private boolean hasBuiltIndexMapping = false; 201 private boolean useStrictFloatingPoint = false; 202 203 209 public ResultSet(long updateCount, long updateID) { 210 this.updateCount = updateCount; 211 this.updateId = updateID; 212 reallyResult = false; 213 fields = new Field[0]; 214 } 215 216 227 public ResultSet(String catalog, Field[] fields, RowData tuples, 228 com.mysql.jdbc.Connection conn) throws SQLException { 229 this(fields, tuples); 230 setConnection(conn); 231 this.catalog = catalog; 232 233 } 234 235 243 public ResultSet(Field[] fields, RowData tuples) throws SQLException { 244 this.fields = fields; 246 this.rowData = tuples; 247 this.updateCount = (long) rowData.size(); 248 249 if (Driver.DEBUG) { 250 System.out.println("Retrieved " + updateCount + " rows"); 251 } 252 253 this.reallyResult = true; 254 255 if (this.rowData.size() > 0) { 257 if (this.updateCount == 1) { 259 if (this.thisRow == null) { 260 this.rowData.close(); this.updateCount = -1; 263 } 264 } 265 } else { 266 this.thisRow = null; 267 } 268 269 this.rowData.setOwner(this); 270 } 271 272 284 public boolean isAfterLast() throws SQLException { 285 if (Driver.TRACE) { 286 Object [] args = { }; 287 Debug.methodCall(this, "isAfterLast", args); 288 } 289 290 boolean b = rowData.isAfterLast(); 291 292 if (Driver.TRACE) { 293 Debug.returnValue(this, "isAfterLast", new Boolean (b)); 294 } 295 296 return b; 297 } 298 299 309 public java.sql.Array getArray(int i) throws SQLException { 310 throw new NotImplemented(); 311 } 312 313 323 public java.sql.Array getArray(String colName) throws SQLException { 324 throw new NotImplemented(); 325 } 326 327 350 public InputStream getAsciiStream(int columnIndex) 351 throws java.sql.SQLException { 352 checkRowPos(); 353 354 return getBinaryStream(columnIndex); 355 } 356 357 366 public InputStream getAsciiStream(String columnName) 367 throws java.sql.SQLException { 368 return getAsciiStream(findColumn(columnName)); 369 } 370 371 375 387 public boolean isBeforeFirst() throws SQLException { 388 if (Driver.TRACE) { 389 Object [] args = { }; 390 Debug.methodCall(this, "isBeforeFirst", args); 391 } 392 393 boolean b = rowData.isBeforeFirst(); 394 395 if (Driver.TRACE) { 396 Debug.returnValue(this, "isBeforeFirst", new Boolean (b)); 397 } 398 399 return b; 400 } 401 402 413 public BigDecimal getBigDecimal(int columnIndex, int scale) 414 throws java.sql.SQLException { 415 String stringVal = getString(columnIndex); 416 BigDecimal val; 417 418 if (stringVal != null) { 419 if (stringVal.length() == 0) { 420 val = new BigDecimal (0); 421 422 return val.setScale(scale); 423 } 424 425 try { 426 val = new BigDecimal (stringVal); 427 } catch (NumberFormatException ex) { 428 throw new java.sql.SQLException ("Bad format for BigDecimal '" 429 + stringVal + "' in column " + columnIndex + "(" 430 + fields[columnIndex - 1] + ").", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 431 } 432 433 try { 434 return val.setScale(scale); 435 } catch (ArithmeticException ex) { 436 throw new java.sql.SQLException ("Bad format for BigDecimal '" 437 + stringVal + "' in column " + columnIndex + "(" 438 + fields[columnIndex - 1] + ").", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 439 } 440 } 441 442 return null; 443 } 444 445 455 public BigDecimal getBigDecimal(String columnName, int scale) 456 throws java.sql.SQLException { 457 return getBigDecimal(findColumn(columnName), scale); 458 } 459 460 472 public BigDecimal getBigDecimal(int columnIndex) throws SQLException { 473 String stringVal = getString(columnIndex); 474 BigDecimal val; 475 476 if (stringVal != null) { 477 if (stringVal.length() == 0) { 478 val = new BigDecimal (0); 479 480 return val; 481 } 482 483 try { 484 val = new BigDecimal (stringVal); 485 486 return val; 487 } catch (NumberFormatException ex) { 488 throw new java.sql.SQLException ("Bad format for BigDecimal '" 489 + stringVal + "' in column " + columnIndex + "(" 490 + fields[columnIndex - 1] + ").", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 491 } 492 } 493 494 return null; 495 } 496 497 507 public BigDecimal getBigDecimal(String columnName) 508 throws SQLException { 509 return getBigDecimal(findColumn(columnName)); 510 } 511 512 527 public InputStream getBinaryStream(int columnIndex) 528 throws java.sql.SQLException { 529 checkRowPos(); 530 531 byte[] b = getBytes(columnIndex); 532 533 if (b != null) { 534 return new ByteArrayInputStream (b); 535 } 536 537 return null; 538 } 539 540 549 public InputStream getBinaryStream(String columnName) 550 throws java.sql.SQLException { 551 return getBinaryStream(findColumn(columnName)); 552 } 553 554 564 public java.sql.Blob getBlob(int columnIndex) throws SQLException { 565 checkRowPos(); 566 567 if ((columnIndex < 1) || (columnIndex > fields.length)) { 568 throw new java.sql.SQLException ("Column Index out of range ( " 569 + columnIndex + " > " + fields.length + ").", SQLError.SQL_STATE_INVALID_COLUMN_NUMBER); 570 } 571 572 try { 573 if (thisRow[columnIndex - 1] == null) { 574 wasNullFlag = true; 575 } else { 576 wasNullFlag = false; 577 } 578 } catch (NullPointerException ex) { 579 wasNullFlag = true; 580 } 581 582 if (wasNullFlag) { 583 return null; 584 } 585 586 return new Blob(thisRow[columnIndex - 1]); 587 } 588 589 598 public java.sql.Blob getBlob(String colName) throws SQLException { 599 return getBlob(findColumn(colName)); 600 } 601 602 611 public boolean getBoolean(int columnIndex) throws java.sql.SQLException { 612 String stringVal = getString(columnIndex); 613 614 if ((stringVal != null) && (stringVal.length() > 0)) { 615 int c = Character.toLowerCase(stringVal.charAt(0)); 616 617 return ((c == 't') || (c == 'y') || (c == '1') 618 || stringVal.equals("-1")); 619 } 620 621 return false; 622 } 623 624 633 public boolean getBoolean(String columnName) throws java.sql.SQLException { 634 return getBoolean(findColumn(columnName)); 635 } 636 637 647 public byte getByte(int columnIndex) throws java.sql.SQLException { 648 checkRowPos(); 649 650 try { 651 if (thisRow[columnIndex - 1] == null) { 652 wasNullFlag = true; 653 } else { 654 wasNullFlag = false; 655 } 656 } catch (NullPointerException E) { 657 wasNullFlag = true; 658 } 659 660 if (wasNullFlag) { 661 return 0; 662 } 663 664 Field field = fields[columnIndex - 1]; 665 666 switch (field.getMysqlType()) { 667 case MysqlDefs.FIELD_TYPE_DECIMAL: 668 case MysqlDefs.FIELD_TYPE_TINY: 669 case MysqlDefs.FIELD_TYPE_SHORT: 670 case MysqlDefs.FIELD_TYPE_LONG: 671 case MysqlDefs.FIELD_TYPE_FLOAT: 672 case MysqlDefs.FIELD_TYPE_DOUBLE: 673 case MysqlDefs.FIELD_TYPE_LONGLONG: 674 case MysqlDefs.FIELD_TYPE_INT24: 675 676 try { 677 String stringVal = getString(columnIndex); 678 int decimalIndex = stringVal.indexOf("."); 679 680 if (decimalIndex != -1) { 682 stringVal = stringVal.substring(0, decimalIndex); 683 } 684 685 return Byte.parseByte(stringVal); 686 } catch (NumberFormatException NFE) { 687 throw new SQLException ("Value '" + getString(columnIndex) 688 + "' is out of range [-127,127]", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 689 } 690 691 default: 692 693 try { 694 String stringVal = getString(columnIndex); 695 696 int decimalIndex = stringVal.indexOf("."); 697 698 if (decimalIndex != -1) { 700 stringVal = stringVal.substring(0, decimalIndex); 701 } 702 703 return Byte.parseByte(stringVal); 704 } catch (NumberFormatException NFE) { 705 throw new SQLException ("Value '" + getString(columnIndex) 706 + "' is out of range [-127,127]", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 707 } 708 709 } 712 } 713 714 723 public byte getByte(String columnName) throws java.sql.SQLException { 724 return getByte(findColumn(columnName)); 725 } 726 727 740 public byte[] getBytes(int columnIndex) throws java.sql.SQLException { 741 checkRowPos(); 742 743 try { 744 if (thisRow[columnIndex - 1] == null) { 745 wasNullFlag = true; 746 } else { 747 wasNullFlag = false; 748 } 749 } catch (NullPointerException E) { 750 wasNullFlag = true; 751 } catch (ArrayIndexOutOfBoundsException aioobEx) { 752 throw new java.sql.SQLException ("Column Index out of range ( " 753 + columnIndex + " > " + fields.length + ").", SQLError.SQL_STATE_INVALID_COLUMN_NUMBER); 754 } 755 756 if (wasNullFlag) { 757 return null; 758 } else { 759 return thisRow[columnIndex - 1]; 760 } 761 } 762 763 772 public byte[] getBytes(String columnName) throws java.sql.SQLException { 773 return getBytes(findColumn(columnName)); 774 } 775 776 781 794 public java.io.Reader getCharacterStream(int columnIndex) 795 throws SQLException { 796 String stringVal = getString(columnIndex); 797 798 if (stringVal != null) { 799 return new StringReader (stringVal); 800 } else { 801 return null; 802 } 803 } 804 805 818 public java.io.Reader getCharacterStream(String columnName) 819 throws SQLException { 820 return getCharacterStream(findColumn(columnName)); 821 } 822 823 832 public java.sql.Clob getClob(int i) throws SQLException { 833 return new com.mysql.jdbc.Clob(getString(i)); 834 } 835 836 845 public java.sql.Clob getClob(String colName) throws SQLException { 846 return getClob(findColumn(colName)); 847 } 848 849 857 public int getConcurrency() throws SQLException { 858 return (CONCUR_READ_ONLY); 859 } 860 861 866 public void setConnection(com.mysql.jdbc.Connection conn) { 867 this.connection = conn; 868 869 if (this.connection != null) { 870 this.useStrictFloatingPoint = this.connection.useStrictFloatingPoint(); 871 this.defaultTimeZone = this.connection.getDefaultTimeZone(); 872 } else { 873 this.defaultTimeZone = TimeZone.getDefault(); 874 } 875 } 876 877 901 public String getCursorName() throws java.sql.SQLException { 902 throw new java.sql.SQLException ("Positioned Update not supported.", 903 "S1C00"); 904 } 905 906 915 public java.sql.Date getDate(int columnIndex) throws java.sql.SQLException { 916 return getDate(columnIndex, null); 917 } 918 919 |