Mercurial > hg > monetdb-java
comparison src/main/java/org/monetdb/jdbc/MonetResultSetMetaData.java @ 886:4f50019b2cf8
Improve MonetResultSetMetaData contructor by checking on valid arrays once, such that this does not need to be checked in methods for each result column.
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Wed, 03 Apr 2024 16:34:56 +0200 (13 months ago) |
parents | e1f00e0e3598 |
children | 674f9ed21308 |
comparison
equal
deleted
inserted
replaced
885:e1f00e0e3598 | 886:4f50019b2cf8 |
---|---|
93 } | 93 } |
94 this.conn = connection; | 94 this.conn = connection; |
95 schemas = header.getSchemaNames(); | 95 schemas = header.getSchemaNames(); |
96 tables = header.getTableNames(); | 96 tables = header.getTableNames(); |
97 columns = header.getNames(); | 97 columns = header.getNames(); |
98 types = header.getTypes(); | |
98 lengths = header.getColumnLengths(); | 99 lengths = header.getColumnLengths(); |
99 types = header.getTypes(); | |
100 precisions = header.getColumnPrecisions(); | 100 precisions = header.getColumnPrecisions(); |
101 scales = header.getColumnScales(); | 101 scales = header.getColumnScales(); |
102 if (schemas == null) { | |
103 throw new IllegalArgumentException("Schemas may not be null!"); | |
104 } | |
105 if (tables == null) { | |
106 throw new IllegalArgumentException("Tables may not be null!"); | |
107 } | |
108 if (columns == null) { | |
109 throw new IllegalArgumentException("Columns may not be null!"); | |
110 } | |
111 if (types == null) { | |
112 throw new IllegalArgumentException("MonetDB Types may not be null!"); | |
113 } | |
114 if (lengths == null) { | |
115 throw new IllegalArgumentException("Lengths may not be null!"); | |
116 } | |
117 // Note: the precisions and scales arrays are null when the statement is a PLAN, EXPLAIN or TRACE statement !! | |
102 | 118 |
103 colCount = columns.length; | 119 colCount = columns.length; |
104 if (columns.length != tables.length || columns.length != types.length ) { | 120 if (columns.length != tables.length || columns.length != types.length ) { |
105 throw new IllegalArgumentException("Inconsistent Header metadata"); | 121 throw new IllegalArgumentException("Inconsistent Header metadata"); |
106 } | 122 } |
392 * @throws SQLException if there is no such column | 408 * @throws SQLException if there is no such column |
393 */ | 409 */ |
394 @Override | 410 @Override |
395 public int getColumnDisplaySize(final int column) throws SQLException { | 411 public int getColumnDisplaySize(final int column) throws SQLException { |
396 checkColumnIndexValidity(column); | 412 checkColumnIndexValidity(column); |
397 if (lengths != null) { | 413 try { |
398 try { | 414 int len = lengths[column - 1]; |
399 int len = lengths[column - 1]; | 415 if (len == 0) { |
400 if (len == 0) { | 416 final String monettype = types[column - 1]; |
401 final String monettype = types[column - 1]; | 417 // in case of inet it always has 0 as length. we need to correct it. |
402 // in case of inet it always has 0 as length. we need to correct it. | 418 if ("inet".equals(monettype)) { |
403 if ("inet".equals(monettype)) { | 419 len = 18; // 128.127.126.125/24 |
404 len = 18; // 128.127.126.125/24 | 420 } |
405 } | 421 } |
406 } | 422 return len; |
407 return len; | 423 } catch (IndexOutOfBoundsException e) { |
408 } catch (IndexOutOfBoundsException e) { | 424 throw MonetResultSet.newSQLInvalidColumnIndexException(column); |
409 throw MonetResultSet.newSQLInvalidColumnIndexException(column); | 425 } |
410 } | |
411 } | |
412 return 1; | |
413 } | 426 } |
414 | 427 |
415 /** | 428 /** |
416 * Gets the designated column's suggested title for use in | 429 * Gets the designated column's suggested title for use in |
417 * printouts and displays. The suggested title is usually | 430 * printouts and displays. The suggested title is usually |
469 * @throws SQLException if there is no such column | 482 * @throws SQLException if there is no such column |
470 */ | 483 */ |
471 @Override | 484 @Override |
472 public String getSchemaName(final int column) throws SQLException { | 485 public String getSchemaName(final int column) throws SQLException { |
473 checkColumnIndexValidity(column); | 486 checkColumnIndexValidity(column); |
474 if (schemas != null) { | 487 try { |
475 try { | 488 return schemas[column - 1]; |
476 return schemas[column - 1]; | 489 } catch (IndexOutOfBoundsException e) { |
477 } catch (IndexOutOfBoundsException e) { | 490 throw MonetResultSet.newSQLInvalidColumnIndexException(column); |
478 throw MonetResultSet.newSQLInvalidColumnIndexException(column); | 491 } |
479 } | |
480 } | |
481 return ""; | |
482 } | 492 } |
483 | 493 |
484 /** | 494 /** |
485 * Gets the designated column's table name. | 495 * Gets the designated column's table name. |
486 * | 496 * |
489 * @throws SQLException if there is no such column | 499 * @throws SQLException if there is no such column |
490 */ | 500 */ |
491 @Override | 501 @Override |
492 public String getTableName(final int column) throws SQLException { | 502 public String getTableName(final int column) throws SQLException { |
493 checkColumnIndexValidity(column); | 503 checkColumnIndexValidity(column); |
494 if (tables != null) { | 504 try { |
495 try { | 505 return tables[column - 1]; |
496 return tables[column - 1]; | 506 } catch (IndexOutOfBoundsException e) { |
497 } catch (IndexOutOfBoundsException e) { | 507 throw MonetResultSet.newSQLInvalidColumnIndexException(column); |
498 throw MonetResultSet.newSQLInvalidColumnIndexException(column); | 508 } |
499 } | |
500 } | |
501 return ""; | |
502 } | 509 } |
503 | 510 |
504 /** | 511 /** |
505 * Retrieves the designated column's SQL type. | 512 * Retrieves the designated column's SQL type. |
506 * | 513 * |
667 @Override | 674 @Override |
668 public int getScale(final int column) throws SQLException { | 675 public int getScale(final int column) throws SQLException { |
669 switch (getColumnType(column)) { | 676 switch (getColumnType(column)) { |
670 case Types.DECIMAL: | 677 case Types.DECIMAL: |
671 case Types.NUMERIC: | 678 case Types.NUMERIC: |
672 { | |
673 // these data types may have a variable scale, max scale is 38 | 679 // these data types may have a variable scale, max scale is 38 |
674 try { | 680 try { |
675 // Special handling for: day_interval and sec_interval as they are | 681 // Special handling for: day_interval and sec_interval as they are |
676 // mapped to Types.NUMERIC and Types.DECIMAL types (see MonetDriver typeMap) | 682 // mapped to Types.NUMERIC and Types.DECIMAL types (see MonetDriver typeMap) |
677 // They appear to have a fixed scale (tested against Oct2020) | 683 // They appear to have a fixed scale (tested against Oct2020) |
682 return 3; | 688 return 3; |
683 | 689 |
684 if (scales != null) { | 690 if (scales != null) { |
685 return scales[column - 1]; | 691 return scales[column - 1]; |
686 } | 692 } |
693 return 0; | |
687 } catch (IndexOutOfBoundsException e) { | 694 } catch (IndexOutOfBoundsException e) { |
688 throw MonetResultSet.newSQLInvalidColumnIndexException(column); | 695 throw MonetResultSet.newSQLInvalidColumnIndexException(column); |
689 } | 696 } |
690 return 0; | |
691 } | |
692 case Types.TIME: | 697 case Types.TIME: |
693 case Types.TIME_WITH_TIMEZONE: | 698 case Types.TIME_WITH_TIMEZONE: |
694 case Types.TIMESTAMP: | 699 case Types.TIMESTAMP: |
695 case Types.TIMESTAMP_WITH_TIMEZONE: | 700 case Types.TIMESTAMP_WITH_TIMEZONE: |
696 if (scales != null) { | 701 try { |
697 try { | 702 if (scales != null) { |
698 return scales[column - 1]; | 703 return scales[column - 1]; |
699 } catch (IndexOutOfBoundsException e) { | |
700 throw MonetResultSet.newSQLInvalidColumnIndexException(column); | |
701 } | 704 } |
702 } | 705 // support microseconds, so scale 6 |
703 // support microseconds, so scale 6 | 706 return 6; // 21:51:34.399753 |
704 return 6; // 21:51:34.399753 | 707 } catch (IndexOutOfBoundsException e) { |
708 throw MonetResultSet.newSQLInvalidColumnIndexException(column); | |
709 } | |
705 | 710 |
706 // All other types should return 0 | 711 // All other types should return 0 |
707 // case Types.BIGINT: | 712 // case Types.BIGINT: |
708 // case Types.INTEGER: | 713 // case Types.INTEGER: |
709 // case Types.SMALLINT: | 714 // case Types.SMALLINT: |