| 1 65 66 67 package org.hsqldb; 68 69 import java.io.IOException ; 70 import java.io.Serializable ; 71 import java.math.BigDecimal ; 72 import java.math.BigInteger ; 73 import java.sql.Date ; 74 import java.sql.Time ; 75 import java.sql.Timestamp ; 76 77 import org.hsqldb.HsqlNameManager.HsqlName; 78 import org.hsqldb.lib.StringConverter; 79 import org.hsqldb.store.ValuePool; 80 import org.hsqldb.types.Binary; 81 import org.hsqldb.types.JavaObject; 82 import org.hsqldb.lib.java.JavaSystem; 83 84 111 122 public class Column { 123 124 public HsqlName columnName; 129 private int colType; 130 private int colSize; 131 private int colScale; 132 private boolean isNullable; 133 private boolean isIdentity; 134 private boolean isPrimaryKey; 135 private Expression defaultExpression; 136 long identityStart; 137 long identityIncrement; 138 static final BigInteger MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE); 139 static final BigInteger MIN_LONG = BigInteger.valueOf(Long.MIN_VALUE); 140 static final BigInteger MAX_INT = BigInteger.valueOf(Integer.MAX_VALUE); 141 static final BigInteger MIN_INT = BigInteger.valueOf(Integer.MIN_VALUE); 142 static final BigDecimal BIG_DECIMAL_0 = new BigDecimal (0.0); 143 static final BigDecimal BIG_DECIMAL_1 = new BigDecimal (1.0); 144 145 159 Column(HsqlName name, boolean nullable, int type, int size, int scale, 160 boolean primarykey, 161 Expression defexpression) throws HsqlException { 162 163 columnName = name; 164 isNullable = nullable; 165 colType = type; 166 colSize = size; 167 colScale = scale; 168 isPrimaryKey = primarykey; 169 defaultExpression = defexpression; 170 } 171 172 void setIdentity(boolean identity, long startvalue, 173 long increment) throws HsqlException { 174 175 isIdentity = identity; 176 identityStart = startvalue; 177 identityIncrement = increment; 178 179 if (isIdentity) { 180 if (colType == Types.INTEGER) { 181 if (identityStart > Integer.MAX_VALUE 182 || identityIncrement > Integer.MAX_VALUE) { 183 throw Trace.error(Trace.NUMERIC_VALUE_OUT_OF_RANGE, 184 columnName.statementName); 185 } 186 } 187 } 188 } 189 190 private Column() {} 191 192 195 Column duplicate(boolean withIdentity) throws HsqlException { 196 197 Column newCol = new Column(); 198 199 newCol.columnName = columnName; 200 newCol.isNullable = isNullable; 201 newCol.colType = colType; 202 newCol.colSize = colSize; 203 newCol.colScale = colScale; 204 newCol.defaultExpression = defaultExpression; 205 206 if (withIdentity) { 207 newCol.setIdentity(isIdentity, identityStart, identityIncrement); 208 } 209 210 return newCol; 211 } 212 213 void setType(Column other) { 214 215 isNullable = other.isNullable; 216 colType = other.colType; 217 colSize = other.colSize; 218 colScale = other.colScale; 219 } 220 221 226 boolean isIdentity() { 227 return isIdentity; 228 } 229 230 235 boolean isNullable() { 236 return isNullable; 237 } 238 239 243 void setNullable(boolean value) { 244 isNullable = value; 245 } 246 247 252 public boolean isPrimaryKey() { 253 return isPrimaryKey; 254 } 255 256 260 void setPrimaryKey(boolean value) { 261 isPrimaryKey = value; 262 } 263 264 267 Object getDefaultValue(Session session) throws HsqlException { 268 269 return defaultExpression == null ? null 270 : defaultExpression.getValue(session, 271 colType); 272 } 273 274 277 String getDefaultDDL() { 278 279 String ddl = null; 280 281 try { 282 ddl = defaultExpression == null ? null 283 : defaultExpression.getDDL(); 284 } catch (HsqlException e) {} 285 286 return ddl; 287 } 288 289 292 Expression getDefaultExpression() { 293 return defaultExpression; 294 } 295 296 void setDefaultExpression(Expression expr) { 297 defaultExpression = expr; 298 } 299 300 305 int getType() { 306 return colType; 307 } 308 309 int getDIType() { 310 return colType == Types.VARCHAR_IGNORECASE ? Types.VARCHAR 311 : colType; 312 } 313 314 int getDITypeSub() { 315 316 if (colType == Types.VARCHAR_IGNORECASE) { 317 return Types.TYPE_SUB_IGNORECASE; 318 } 319 320 return Types.TYPE_SUB_DEFAULT; 321 } 322 323 328 int getSize() { 329 return colSize; 330 } 331 332 337 int getScale() { 338 return colScale; 339 } 340 341 350 static Object add(Object a, Object b, int type) throws HsqlException { 351 352 if (a == null || b == null) { 353 return null; 354 } 355 356 switch (type) { 357 358 case Types.NULL : 359 return null; 360 361 case Types.REAL : 362 case Types.FLOAT : 363 case Types.DOUBLE : { 364 double ad = ((Number ) a).doubleValue(); 365 double bd = ((Number ) b).doubleValue(); 366 367 return ValuePool.getDouble(Double.doubleToLongBits(ad + bd)); 368 369 } 371 case Types.VARCHAR : 372 case Types.CHAR : 373 case Types.LONGVARCHAR : 374 case Types.VARCHAR_IGNORECASE : 375 return (String ) a + (String ) b; 376 377 case Types.NUMERIC : 378 case Types.DECIMAL : { 379 BigDecimal abd = (BigDecimal ) a; 380 BigDecimal bbd = (BigDecimal ) b; 381 382 return abd.add(bbd); 383 } 384 case Types.TINYINT : 385 case Types.SMALLINT : 386 case Types.INTEGER : { 387 int ai = ((Number ) a).intValue(); 388 int bi = ((Number ) b).intValue(); 389 390 return ValuePool.getInt(ai + bi); 391 } 392 case Types.BIGINT : { 393 long longa = ((Number ) a).longValue(); 394 long longb = ((Number ) b).longValue(); 395 396 return ValuePool.getLong(longa + longb); 397 } 398 default : 399 throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, 400 Types.getTypeString(type)); 401 } 402 } 403 404 412 static Object concat(Object a, Object b) throws HsqlException { 413 414 if (a == null || b == null) { 415 return null; 416 } 417 418 return a.toString() + b.toString(); 419 } 420 421 429 static Object negate(Object a, int type) throws HsqlException { 430 431 if (a == null) { 432 return null; 433 } 434 435 switch (type) { 436 437 case Types.NULL : 438 return null; 439 440 case Types.REAL : 441 case Types.FLOAT : 442 case Types.DOUBLE : { 443 double ad = -((Number ) a).doubleValue(); 444 445 return ValuePool.getDouble(Double.doubleToLongBits(ad)); 446 } 447 case Types.NUMERIC : 448 case Types.DECIMAL : 449 return ((BigDecimal ) a).negate(); 450 451 case Types.TINYINT : 452 case Types.SMALLINT : 453 case Types.INTEGER : 454 return ValuePool.getInt(-((Number ) a).intValue()); 455 456 case Types.BIGINT : 457 return ValuePool.getLong(-((Number ) a).longValue()); 458 459 default : 460 throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, 461 Types.getTypeString(type)); 462 } 463 } 464 465 474 static Object multiply(Object a, Object b, 475 int type) throws HsqlException { 476 477 if (a == null || b == null) { 478 return null; 479 } 480 481 if (!(a instanceof Number && b instanceof Number )) { 483 a = Column.convertObject(a, type); 484 b = Column.convertObject(b, type); 485 } 486 487 switch (type) { 488 489 case Types.NULL : 490 return null; 491 492 case Types.REAL : 493 case Types.FLOAT : 494 case Types.DOUBLE : { 495 double ad = ((Number ) a).doubleValue(); 496 double bd = ((Number ) b).doubleValue(); 497 498 return ValuePool.getDouble(Double.doubleToLongBits(ad * bd)); 499 } 500 case Types.NUMERIC : 501 case Types.DECIMAL : { 502 BigDecimal abd = (BigDecimal ) a; 503 BigDecimal bbd = (BigDecimal ) b; 504 505 return abd.multiply(bbd); 506 } 507 case Types.TINYINT : 508 case Types.SMALLINT : 509 case Types.INTEGER : { 510 int ai = ((Number ) a).intValue(); 511 int bi = ((Number ) b).intValue(); 512 513 return ValuePool.getInt(ai * bi); 514 } 515 case Types.BIGINT : { 516 long longa = ((Number ) a).longValue(); 517 long longb = ((Number ) b).longValue(); 518 519 return ValuePool.getLong(longa * longb); 520 } 521 default : 522 throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, 523 Types.getTypeString(type)); 524 } 525 } 526 527 536 static Object divide(Object a, Object b, int type) throws HsqlException { 537 538 if (a == null || b == null) { 539 return null; 540 } 541 542 switch (type) { 543 544 case Types.NULL : 545 return null; 546 547 case Types.REAL : 548 case Types.FLOAT : 549 case Types.DOUBLE : { 550 double ad = ((Number ) a).doubleValue(); 551 double bd = ((Number ) b).doubleValue(); 552 553 return ValuePool.getDouble(Double.doubleToLongBits(ad / bd)); 554 } 555 case Types.NUMERIC : 556 case Types.DECIMAL : { 557 BigDecimal abd = (BigDecimal ) a; 558 BigDecimal bbd = (BigDecimal ) b; 559 int scale = abd.scale() > bbd.scale() ? abd.scale() 560 : bbd.scale(); 561 562 return (bbd.signum() == 0) ? null 563 : abd.divide(bbd, scale, 564 BigDecimal.ROUND_HALF_DOWN); 565 } 566 case Types.TINYINT : 567 case Types.SMALLINT : 568 case Types.INTEGER : { 569 int ai = ((Number ) a).intValue(); 570 int bi = ((Number ) b).intValue(); 571 572 if (bi == 0) { 573 throw Trace.error(Trace.DIVISION_BY_ZERO); 574 } 575 576 return ValuePool.getInt(ai / bi); 577 } 578 case Types.BIGINT : { 579 long longa = ((Number ) a).longValue(); 580 long longb = ((Number ) b).longValue(); 581 582 return (longb == 0) ? null 583 : ValuePool.getLong(longa / longb); 584 } 585 default : 586 throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, 587 Types.getTypeString(type)); 588 } 589 } 590 591 600 static Object subtract(Object a, Object b, 601 int type) throws HsqlException { 602 603 if (a == null || b == null) { 604 return null; 605 } 606 607 switch (type) { 608 609 case Types.NULL : 610 return null; 611 612 case Types.REAL : 613 case Types.FLOAT : 614 case Types.DOUBLE : { 615 double ad = ((Number ) a).doubleValue(); 616 double bd = ((Number ) b).doubleValue(); 617 618 return ValuePool.getDouble(Double.doubleToLongBits(ad - bd)); 619 } 620 case Types.NUMERIC : 621 case Types.DECIMAL : { 622 BigDecimal abd = (BigDecimal ) a; 623 BigDecimal bbd = (BigDecimal ) b; 624 625 return abd.subtract(bbd); 626 } 627 case Types.TINYINT : 628 case Types.SMALLINT : 629 case Types.INTEGER : { 630 int ai = ((Number ) a).intValue(); 631 int bi = ((Number ) b).intValue(); 632 633 return ValuePool.getInt(ai - bi); 634 } 635 case Types.BIGINT : { 636 long longa = ((Number ) a).longValue(); 637 long longb = ((Number ) b).longValue(); 638 639 return ValuePool.getLong(longa - longb); 640 } 641 default : 642 throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, 643 Types.getTypeString(type)); 644 } 645 } 646 647 660 669 static int compare(Collation collation, Object a, Object b, int type) { 670 671 int i = 0; 672 673 if (a == b) { 674 return 0; 675 } 676 677 if (a == null) { 681 return -1; 682 } 683 684 if (b == null) { 685 return 1; 686 } 687 688 switch (type) { 689 690 case Types.NULL : 691 return 0; 692 693 case Types.VARCHAR : 694 case Types.LONGVARCHAR : 695 return collation.compare((String ) a, (String ) b); 696 697 case Types.CHAR : 698 return collation.compare(Library.rtrim((String ) a), 699 Library.rtrim((String ) b)); 700 701 case Types.VARCHAR_IGNORECASE : 702 return collation.compareIgnoreCase(((String ) a), 703 ((String ) b)); 704 705 case Types.TINYINT : 706 case Types.SMALLINT : 707 case Types.INTEGER : { 708 int ai = ((Number ) a).intValue(); 709 int bi = ((Number ) b).intValue(); 710 711 return (ai > bi) ? 1 712 : (bi > ai ? -1 713 : 0); 714 } 715 case Types.BIGINT : { 716 long longa = ((Number ) a).longValue(); 717 long longb = ((Number ) b).longValue(); 718 719 return (longa > longb) ? 1 720 : (longb > longa ? -1 721 : 0); 722 } 723 case Types.REAL : 724 case Types.FLOAT : 725 case Types.DOUBLE : { 726 double ad = ((Number ) a).doubleValue(); 727 double bd = ((Number ) b).doubleValue(); 728 729 return (ad > bd) ? 1 730 : (bd > ad ? -1 731 : 0); 732 } 733 case Types.NUMERIC : 734 case Types.DECIMAL : 735 i = ((BigDecimal ) a).compareTo((BigDecimal ) b); 736 break; 737 738 case Types.DATE : 739 return HsqlDateTime.compare((Date ) a, (Date ) b); 740 741 case Types.TIME : 742 return HsqlDateTime.compare((Time ) a, (Time ) b); 743 744 case Types.TIMESTAMP : 745 return HsqlDateTime.compare((Timestamp ) a, (Timestamp ) b); 746 747 case Types.BOOLEAN : { 748 boolean boola = ((Boolean ) a).booleanValue(); 749 boolean boolb = ((Boolean ) b).booleanValue(); 750 751 return (boola == boolb) ? 0 752 : (boolb ? -1 753 : 1); 754 } 755 case Types.BINARY : 756 case Types.VARBINARY : 757 case Types.LONGVARBINARY : 758 if (a instanceof Binary && b instanceof Binary) { 759 i = compareTo(((Binary) a).getBytes(), 760 ((Binary) b).getBytes()); 761 } 762 break; 763 764 case Types.OTHER : 765 return 0; 766 } 767 768 return (i == 0) ? 0 769 : (i < 0 ? -1 770 : 1); 771 } 772 773 799 public static Object convertObject(Object o, 800 int type) throws HsqlException { 801 802 try { 803 if (o == null) { 804 return null; 805 } 806 807 switch (type) { 808 809 case Types.NULL : 810 return null; 811 812 case Types.TINYINT : 813 if (o instanceof java.lang.String ) { 814 o = Library.trim((String ) o, " ", true, true); 815 816 int val = Integer.parseInt((String ) o); 817 818 o = ValuePool.getInt(val); 819 } 820 821 if (o instanceof java.lang.Integer ) { 822 int temp = ((Number ) o).intValue(); 823 824 if (Byte.MAX_VALUE < temp || temp < Byte.MIN_VALUE) { 825 throw Trace.error( 826 Trace.NUMERIC_VALUE_OUT_OF_RANGE); 827 } 828 829 return o; 830 } 831 832 if (o instanceof java.lang.Long ) { 833 &n
|