field.cc 179 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16 17 18 19 20 21
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/*****************************************************************************
** This file implements classes defined in field.h
*****************************************************************************/

22
#ifdef USE_PRAGMA_IMPLEMENTATION
bk@work.mysql.com's avatar
bk@work.mysql.com committed
23 24
#pragma implementation				// gcc: Class implementation
#endif
25 26

#include "mysql_priv.h"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
27 28 29 30 31 32 33
#include "sql_select.h"
#include <m_ctype.h>
#include <errno.h>
#ifdef HAVE_FCONVERT
#include <floatingpoint.h>
#endif

peter@mysql.com's avatar
peter@mysql.com committed
34 35 36
// Maximum allowed exponent value for converting string to decimal
#define MAX_EXPONENT 1024

bk@work.mysql.com's avatar
bk@work.mysql.com committed
37
/*****************************************************************************
38
  Instansiate templates and static variables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
39 40 41 42 43 44 45 46 47 48
*****************************************************************************/

#ifdef __GNUC__
template class List<create_field>;
template class List_iterator<create_field>;
#endif

uchar Field_null::null[1]={1};
const char field_separator=',';

49
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE 320
50 51
#define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
((ulong) ((LL(1) << min(arg, 4) * 8) - LL(1)))
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
/*
  Rules for merging different types of fields in UNION

  NOTE: to avoid 256*256 table, gap in table types numeration is skiped
  following #defines describe that gap and how to canculate number of fields
  and index of field in thia array.
*/
#define FIELDTYPE_TEAR_FROM (MYSQL_TYPE_NEWDATE+1)
#define FIELDTYPE_TEAR_TO   (MYSQL_TYPE_ENUM-1)
#define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255-FIELDTYPE_TEAR_TO))
inline int field_type2index (enum_field_types field_type)
{
  return (field_type < FIELDTYPE_TEAR_FROM ?
          field_type :
          ((int)FIELDTYPE_TEAR_FROM) + (field_type - FIELDTYPE_TEAR_TO) - 1);
}

static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
{
  /* MYSQL_TYPE_DECIMAL -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_DECIMAL,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_DECIMAL,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_DECIMAL,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_TINY -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_TINY,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_TINY,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_SHORT -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_SHORT,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_SHORT,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_LONG -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_LONG,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_LONG,        MYSQL_TYPE_LONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_LONG,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONG,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_LONG,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_FLOAT -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_FLOAT,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_FLOAT,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_FLOAT,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_DOUBLE -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_NULL -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_TINY,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_NULL,        MYSQL_TYPE_TIMESTAMP,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_TIME,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_YEAR,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_NEWDATE,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_ENUM,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_SET,         MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_GEOMETRY
  },
  /* MYSQL_TYPE_TIMESTAMP -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_TIMESTAMP,   MYSQL_TYPE_TIMESTAMP,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_DATETIME,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_LONGLONG -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_LONGLONG,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONGLONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONG,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_LONGLONG,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_INT24 -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_INT24,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_INT24,       MYSQL_TYPE_LONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_INT24,       MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_INT24,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_DATE -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_NEWDATE,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_TIME -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_TIME,        MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_TIME,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_DATETIME,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_DATETIME -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_DATETIME,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_YEAR -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_TINY,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_YEAR,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_YEAR,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_NEWDATE -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_NEWDATE,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_ENUM -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_ENUM,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_SET -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_SET,         MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_TINY_BLOB -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_TINY_BLOB,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB
  },
  /* MYSQL_TYPE_MEDIUM_BLOB -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_MEDIUM_BLOB,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB
  },
  /* MYSQL_TYPE_LONG_BLOB -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_LONG_BLOB,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB
  },
  /* MYSQL_TYPE_BLOB -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_BLOB,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB
  },
  /* MYSQL_TYPE_VAR_STRING -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING
  },
  /* MYSQL_TYPE_STRING -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_STRING,      MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING
  },
  /* MYSQL_TYPE_GEOMETRY -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_GEOMETRY,    MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_NEWDATE      <14>
    MYSQL_TYPE_VAR_STRING,
  //<246>                   MYSQL_TYPE_ENUM
                            MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VAR_STRING,  MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VAR_STRING,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_GEOMETRY
  }
};

/*
  Return type of which can carry value of both given types in UNION result

  SYNOPSIS
    Field::field_type_merge()
    a, b  types for merging

  RETURN
    type of field
*/

enum_field_types Field::field_type_merge(enum_field_types a,
                                         enum_field_types b)
{
  DBUG_ASSERT(a < FIELDTYPE_TEAR_FROM || a > FIELDTYPE_TEAR_TO);
  DBUG_ASSERT(b < FIELDTYPE_TEAR_FROM || b > FIELDTYPE_TEAR_TO);
  return field_types_merge_rules[field_type2index(a)]
                                [field_type2index(b)];
}


static Item_result field_types_result_type [FIELDTYPE_NUM]=
{
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
  REAL_RESULT,              INT_RESULT,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
  INT_RESULT,               INT_RESULT,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
  REAL_RESULT,              REAL_RESULT,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
  INT_RESULT,               INT_RESULT,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
  STRING_RESULT,            INT_RESULT,
  //MYSQL_TYPE_NEWDATE      <14>
  STRING_RESULT,
  //<246>                   MYSQL_TYPE_ENUM
                            STRING_RESULT,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
  STRING_RESULT,            STRING_RESULT
};


/*
  Detect Item_result by given field type of UNION merge result

  SYNOPSIS
    Field::result_merge_type()
    field_type  given field type

  RETURN
    Item_result (type of internal MySQL expression result)
*/

Item_result Field::result_merge_type(enum_field_types field_type)
{
  DBUG_ASSERT(field_type < FIELDTYPE_TEAR_FROM || field_type
              > FIELDTYPE_TEAR_TO);
  return field_types_result_type[field_type2index(field_type)];
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
840
/*****************************************************************************
841
  Static help functions
bk@work.mysql.com's avatar
bk@work.mysql.com committed
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
*****************************************************************************/

void Field_num::prepend_zeros(String *value)
{
  int diff;
  if ((diff= (int) (field_length - value->length())) > 0)
  {
    bmove_upp((char*) value->ptr()+field_length,value->ptr()+value->length(),
	      value->length());
    bfill((char*) value->ptr(),diff,'0');
    value->length(field_length);
    (void) value->c_ptr_quick();		// Avoid warnings in purify
  }
}

/*
858
  Test if given number is a int (or a fixed format float with .000)
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875

  SYNOPSIS
    test_if_int()
    str		String to test
    end		Pointer to char after last used digit
    cs		Character set

  NOTES
    This is called after one has called my_strntol() or similar function.
    This is only used to give warnings in ALTER TABLE or LOAD DATA...

  TODO
    Make this multi-byte-character safe

  RETURN
    0	ok
    1	error
bk@work.mysql.com's avatar
bk@work.mysql.com committed
876 877
*/

878 879
bool test_if_int(const char *str, int length, const char *int_end,
		 CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
880
{
881 882
  if (str == int_end)
    return 0;					// Empty string
bk@work.mysql.com's avatar
bk@work.mysql.com committed
883
  const char *end=str+length;
884 885
  if ((str= int_end) == end)
    return 1;					// All digits was used
bk@work.mysql.com's avatar
bk@work.mysql.com committed
886

887 888
  /* Allow end .0000 */
  if (*str == '.')
bk@work.mysql.com's avatar
bk@work.mysql.com committed
889
  {
890 891 892 893 894 895 896
    for (str++ ; str != end && *str == '0'; str++) ;
  }
  /* Allow end space */
  for (str++ ; str != end ; str++)
  {
    if (!my_isspace(cs,*str))
      return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
897 898 899 900
  }
  return 1;
}

serg@serg.mylan's avatar
serg@serg.mylan committed
901
#ifdef NOT_USED
902
static bool test_if_real(const char *str,int length, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
903
{
904
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
905 906

  while (length && my_isspace(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
907 908 909 910 911 912 913 914
  {						// Allow start space
    length--; str++;
  }
  if (!length)
    return 0;
  if (*str == '+' || *str == '-')
  {
    length--; str++;
915
    if (!length || !(my_isdigit(cs,*str) || *str == '.'))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
916 917
      return 0;
  }
918
  while (length && my_isdigit(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
919 920 921 922 923 924 925 926
  {
    length--; str++;
  }
  if (!length)
    return 1;
  if (*str == '.')
  {
    length--; str++;
927
    while (length && my_isdigit(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
928 929 930 931 932 933 934 935
    {
      length--; str++;
    }
  }
  if (!length)
    return 1;
  if (*str == 'E' || *str == 'e')
  {
936
    if (length < 3 || (str[1] != '+' && str[1] != '-') || 
937
        !my_isdigit(cs,str[2]))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
938 939 940
      return 0;
    length-=3;
    str+=3;
941
    while (length && my_isdigit(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
942 943 944 945
    {
      length--; str++;
    }
  }
946
  for (; length ; length--, str++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
947
  {						// Allow end space
948
    if (!my_isspace(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
949 950 951 952
      return 0;
  }
  return 1;
}
serg@serg.mylan's avatar
serg@serg.mylan committed
953
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
954

955

bk@work.mysql.com's avatar
bk@work.mysql.com committed
956 957
/****************************************************************************
** Functions for the base classes
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
958
** This is an unpacked number.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
959 960 961
****************************************************************************/

Field::Field(char *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
962
	     uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
963 964
	     utype unireg_check_arg, const char *field_name_arg,
	     struct st_table *table_arg)
965
  :ptr(ptr_arg),null_ptr(null_ptr_arg),
966 967
   table(table_arg),orig_table(table_arg),
   table_name(table_arg ? table_arg->table_name : 0),
968
   field_name(field_name_arg),
serg@serg.mylan's avatar
serg@serg.mylan committed
969 970
   query_id(0), key_start(0), part_of_key(0), part_of_sortkey(0),
   unireg_check(unireg_check_arg),
971
   field_length(length_arg),null_bit(null_bit_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
972 973
{
  flags=null_ptr ? 0: NOT_NULL_FLAG;
974 975
  comment.str= (char*) "";
  comment.length=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
976 977 978 979 980 981 982 983 984 985 986 987 988
}

uint Field::offset()
{
  return (uint) (ptr - (char*) table->record[0]);
}


void Field::copy_from_tmp(int row_offset)
{
  memcpy(ptr,ptr+row_offset,pack_length());
  if (null_ptr)
  {
989 990
    *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) |
			null_ptr[row_offset] & (uchar) null_bit);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
991 992 993 994
  }
}


995
bool Field::send_binary(Protocol *protocol)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
996 997
{
  char buff[MAX_FIELD_WIDTH];
998
  String tmp(buff,sizeof(buff),charset());
999
  val_str(&tmp);
1000
  return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1001 1002 1003 1004 1005
}


void Field_num::add_zerofill_and_unsigned(String &res) const
{
1006 1007 1008 1009
  if (unsigned_flag)
    res.append(" unsigned");
  if (zerofill)
    res.append(" zerofill");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1010 1011 1012 1013
}

void Field_num::make_field(Send_field *field)
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1014
  /* table_cache_key is not set for temp tables */
1015 1016 1017 1018 1019 1020 1021 1022 1023
  if (orig_table->table_cache_key)
  {
    field->db_name= orig_table->table_cache_key;
    field->org_table_name= orig_table->real_name;
  }
  else
  {
    field->db_name= field->org_table_name= "";
  }
1024
  field->table_name= orig_table->table_name;
1025
  field->col_name=field->org_col_name=field_name;
1026
  field->charsetnr= charset()->number;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1027 1028 1029 1030 1031 1032 1033 1034 1035
  field->length=field_length;
  field->type=type();
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
  field->decimals=dec;
}


void Field_str::make_field(Send_field *field)
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1036
  /* table_cache_key is not set for temp tables */
1037 1038 1039 1040 1041 1042 1043 1044 1045
  if (orig_table->table_cache_key)
  {
    field->db_name= orig_table->table_cache_key;
    field->org_table_name= orig_table->real_name;
  }
  else
  {
    field->db_name= field->org_table_name= "";
  }
1046
  field->table_name= orig_table->table_name;
1047
  field->col_name=field->org_col_name=field_name;
1048
  field->charsetnr= charset()->number;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1049 1050 1051 1052 1053 1054
  field->length=field_length;
  field->type=type();
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
  field->decimals=0;
}

1055

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1056 1057
uint Field::fill_cache_field(CACHE_FIELD *copy)
{
1058
  uint store_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
  copy->str=ptr;
  copy->length=pack_length();
  copy->blob_field=0;
  if (flags & BLOB_FLAG)
  {
    copy->blob_field=(Field_blob*) this;
    copy->strip=0;
    copy->length-=table->blob_ptr_size;
    return copy->length;
  }
  else if (!zero_pack() && (type() == FIELD_TYPE_STRING && copy->length > 4 ||
			    type() == FIELD_TYPE_VAR_STRING))
1071
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1072
    copy->strip=1;				/* Remove end space */
1073 1074
    store_length= 2;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1075
  else
1076
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1077
    copy->strip=0;
1078 1079 1080
    store_length= 0;
  }
  return copy->length+ store_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1081 1082
}

1083 1084

bool Field::get_date(TIME *ltime,uint fuzzydate)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1085 1086
{
  char buff[40];
1087 1088
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
  if (!(res=val_str(&tmp)) ||
1089 1090
      str_to_datetime_with_warn(res->ptr(), res->length(),
                                ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1091 1092 1093 1094 1095 1096 1097
    return 1;
  return 0;
}

bool Field::get_time(TIME *ltime)
{
  char buff[40];
1098 1099
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
  if (!(res=val_str(&tmp)) ||
1100
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1101 1102 1103 1104
    return 1;
  return 0;
}

1105 1106 1107 1108 1109 1110
/*
  This is called when storing a date in a string

  NOTES
    Needs to be changed if/when we want to support different time formats
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1111 1112 1113

void Field::store_time(TIME *ltime,timestamp_type type)
{
1114 1115 1116
  char buff[MAX_DATE_STRING_REP_LENGTH];
  uint length= (uint) my_TIME_to_str(ltime, buff);
  store(buff, length, &my_charset_bin);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1117 1118 1119
}


1120
bool Field::optimize_range(uint idx, uint part)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1121
{
1122
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1123 1124
}

1125
/****************************************************************************
1126
  Field_null, a field that always return NULL
1127 1128 1129 1130
****************************************************************************/

void Field_null::sql_type(String &res) const
{
1131
  res.set_ascii("null", 4);
1132 1133 1134
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
1135
/****************************************************************************
1136 1137
  Functions for the Field_decimal class
  This is an number stored as a pre-space (or pre-zero) string
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1138 1139 1140 1141 1142
****************************************************************************/

void
Field_decimal::reset(void)
{
1143
  Field_decimal::store("0",1,&my_charset_bin);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1144 1145 1146 1147 1148
}

void Field_decimal::overflow(bool negative)
{
  uint len=field_length;
1149
  char *to=ptr, filler= '9';
1150

1151
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1152
  if (negative)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1153
  {
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
    if (!unsigned_flag)
    {
      /* Put - sign as a first digit so we'll have -999..999 or 999..999 */
      *to++ = '-';
      len--;
    }
    else
    {
      filler= '0';				// Fill up with 0
      if (!zerofill)
      {
	/*
	  Handle unsigned integer without zerofill, in which case
	  the number should be of format '   0' or '   0.000'
	*/
	uint whole_part=field_length- (dec ? dec+2 : 1);
	// Fill with spaces up to the first digit
	bfill(to, whole_part, ' ');
	to+=  whole_part;
	len-= whole_part;
	// The main code will also handle the 0 before the decimal point
      }
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1177
  }
1178
  bfill(to, len, filler);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1179 1180 1181 1182 1183 1184
  if (dec)
    ptr[field_length-dec-1]='.';
  return;
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1185
int Field_decimal::store(const char *from, uint len, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1186
{
1187
  char buff[80];
1188
  String tmp(buff,sizeof(buff), &my_charset_bin);
1189

1190 1191 1192
  /* Convert character set if the old one is multi byte */
  if (cs->mbmaxlen > 1)
  { 
1193 1194
    uint dummy_errors;
    tmp.copy(from, len, cs, &my_charset_bin, &dummy_errors);
1195 1196 1197
    from= tmp.ptr();
    len=  tmp.length();
  }
1198

1199 1200 1201 1202 1203 1204 1205 1206 1207
  const char *end= from+len;
  /* The pointer where the field value starts (i.e., "where to write") */
  char *to=ptr;
  uint tmp_dec, tmp_uint;
  /*
    The sign of the number : will be 0 (means positive but sign not
    specified), '+' or '-'
  */
  char sign_char=0;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1208
  /* The pointers where prezeros start and stop */
1209
  const char *pre_zeros_from, *pre_zeros_end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1210
  /* The pointers where digits at the left of '.' start and stop */
1211
  const char *int_digits_from, *int_digits_end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1212
  /* The pointers where digits at the right of '.' start and stop */
1213
  const char *frac_digits_from, *frac_digits_end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1214
  /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
1215
  char expo_sign_char=0;
peter@mysql.com's avatar
peter@mysql.com committed
1216
  uint exponent=0;                                // value of the exponent
1217 1218 1219 1220 1221 1222 1223
  /*
    Pointers used when digits move from the left of the '.' to the
    right of the '.' (explained below)
  */
  const char *int_digits_tail_from;
  /* Number of 0 that need to be added at the left of the '.' (1E3: 3 zeros) */
  uint int_digits_added_zeros;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1224 1225 1226 1227
  /*
    Pointer used when digits move from the right of the '.' to the left
    of the '.'
  */
1228
  const char *frac_digits_head_end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1229
  /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
1230 1231 1232 1233 1234
  uint frac_digits_added_zeros;
  char *pos,*tmp_left_pos,*tmp_right_pos;
  /* Pointers that are used as limits (begin and end of the field buffer) */
  char *left_wall,*right_wall;
  char tmp_char;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1235
  /*
1236
    To remember if table->in_use->cuted_fields has already been incremented,
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1237 1238
    to do that only once
  */
1239 1240 1241 1242 1243 1244 1245 1246 1247
  bool is_cuted_fields_incr=0;

  LINT_INIT(int_digits_tail_from);
  LINT_INIT(int_digits_added_zeros);
  LINT_INIT(frac_digits_head_end);
  LINT_INIT(frac_digits_added_zeros);

  /*
    There are three steps in this function :
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1248 1249
    - parse the input string
    - modify the position of digits around the decimal dot '.' 
1250
      according to the exponent value (if specified)
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1251
    - write the formatted number
1252 1253 1254 1255 1256
  */

  if ((tmp_dec=dec))
    tmp_dec++;

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1257
  /* skip pre-space */
1258
  while (from != end && my_isspace(&my_charset_bin,*from))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1259
    from++;
1260
  if (from == end)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1261
  {
1262
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
1263
    is_cuted_fields_incr=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1264
  }
1265
  else if (*from == '+' || *from == '-')	// Found some sign ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1266
  {
1267
    sign_char= *from++;
1268
    /*
1269 1270 1271
      We allow "+" for unsigned decimal unless defined different
      Both options allowed as one may wish not to have "+" for unsigned numbers
      because of data processing issues
1272 1273 1274 1275
    */ 
    if (unsigned_flag)  
    { 
      if (sign_char=='-')
1276
      {
1277
        Field_decimal::overflow(1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1278
        return 1;
1279
      }
1280
      /* 
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1281 1282
	 Defining this will not store "+" for unsigned decimal type even if
	 it is passed in numeric string. This will make some tests to fail
1283 1284
      */	 
#ifdef DONT_ALLOW_UNSIGNED_PLUS      
1285 1286
      else 
        sign_char=0;
1287
#endif 	
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1288
    }
1289
  }
1290

1291 1292 1293 1294
  pre_zeros_from= from;
  for (; from!=end && *from == '0'; from++) ;	// Read prezeros
  pre_zeros_end=int_digits_from=from;      
  /* Read non zero digits at the left of '.'*/
1295
  for (; from != end && my_isdigit(&my_charset_bin, *from) ; from++) ;
1296 1297 1298 1299 1300
  int_digits_end=from;
  if (from!=end && *from == '.')		// Some '.' ?
    from++;
  frac_digits_from= from;
  /* Read digits at the right of '.' */
1301
  for (;from!=end && my_isdigit(&my_charset_bin, *from); from++) ;
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
  frac_digits_end=from;
  // Some exponentiation symbol ?
  if (from != end && (*from == 'e' || *from == 'E'))
  {   
    from++;
    if (from != end && (*from == '+' || *from == '-'))  // Some exponent sign ?
      expo_sign_char= *from++;
    else
      expo_sign_char= '+';
    /*
1312 1313 1314 1315
      Read digits of the exponent and compute its value.  We must care about
      'exponent' overflow, because as unsigned arithmetic is "modulo", big 
      exponents will become small (e.g. 1e4294967296 will become 1e0, and the 
      field will finally contain 1 instead of its max possible value).
1316
    */
1317
    for (;from!=end && my_isdigit(&my_charset_bin, *from); from++)
1318
    {
1319
      exponent=10*exponent+(*from-'0');
peter@mysql.com's avatar
peter@mysql.com committed
1320
      if (exponent>MAX_EXPONENT)
1321 1322
        break;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1323
  }
1324
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1325
  /*
1326 1327 1328 1329
    We only have to generate warnings if count_cuted_fields is set.
    This is to avoid extra checks of the number when they are not needed.
    Even if this flag is not set, it's ok to increment warnings, if
    it makes the code easer to read.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1330
  */
1331

1332
  if (table->in_use->count_cuted_fields)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1333
  {
1334
    // Skip end spaces
1335
    for (;from != end && my_isspace(&my_charset_bin, *from); from++) ;
1336
    if (from != end)                     // If still something left, warn
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1337
    {
1338
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
1339
      is_cuted_fields_incr=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1340
    }
1341 1342 1343 1344 1345 1346
  }
  
  /*
    Now "move" digits around the decimal dot according to the exponent value,
    and add necessary zeros.
    Examples :
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1347 1348 1349
    - 1E+3 : needs 3 more zeros at the left of '.' (int_digits_added_zeros=3)
    - 1E-3 : '1' moves at the right of '.', and 2 more zeros are needed
    between '.' and '1'
1350
    - 1234.5E-3 : '234' moves at the right of '.'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1351 1352
    These moves are implemented with pointers which point at the begin
    and end of each moved segment. Examples :
1353
    - 1234.5E-3 : before the code below is executed, the int_digits part is
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1354 1355 1356
    from '1' to '4' and the frac_digits part from '5' to '5'. After the code
    below, the int_digits part is from '1' to '1', the frac_digits_head
    part is from '2' to '4', and the frac_digits part from '5' to '5'.
1357
    - 1234.5E3 : before the code below is executed, the int_digits part is
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1358 1359 1360 1361
    from '1' to '4' and the frac_digits part from '5' to '5'. After the code
    below, the int_digits part is from '1' to '4', the int_digits_tail
    part is from '5' to '5', the frac_digits part is empty, and
    int_digits_added_zeros=2 (to make 1234500).
1362 1363
  */
  
1364
  /* 
peter@mysql.com's avatar
peter@mysql.com committed
1365
     Below tmp_uint cannot overflow with small enough MAX_EXPONENT setting,
1366
     as int_digits_added_zeros<=exponent<4G and 
1367 1368
     (int_digits_end-int_digits_from)<=max_allowed_packet<=2G and
     (frac_digits_from-int_digits_tail_from)<=max_allowed_packet<=2G
1369 1370
  */

1371 1372 1373 1374 1375 1376 1377 1378
  if (!expo_sign_char)
    tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
  else if (expo_sign_char == '-') 
  {
    tmp_uint=min(exponent,(uint)(int_digits_end-int_digits_from));
    frac_digits_added_zeros=exponent-tmp_uint;
    int_digits_end -= tmp_uint;
    frac_digits_head_end=int_digits_end+tmp_uint;
peter@mysql.com's avatar
peter@mysql.com committed
1379
    tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);     
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
  }
  else // (expo_sign_char=='+') 
  {
    tmp_uint=min(exponent,(uint)(frac_digits_end-frac_digits_from));
    int_digits_added_zeros=exponent-tmp_uint;
    int_digits_tail_from=frac_digits_from;
    frac_digits_from=frac_digits_from+tmp_uint;
    /*
      We "eat" the heading zeros of the 
      int_digits.int_digits_tail.int_digits_added_zeros concatenation
      (for example 0.003e3 must become 3 and not 0003)
    */
    if (int_digits_from == int_digits_end) 
    {
      /*
	There was nothing in the int_digits part, so continue
	eating int_digits_tail zeros
      */
      for (; int_digits_tail_from != frac_digits_from &&
	     *int_digits_tail_from == '0'; int_digits_tail_from++) ;
      if (int_digits_tail_from == frac_digits_from) 
      {
	// there were only zeros in int_digits_tail too
	int_digits_added_zeros=0;
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1405
    }
1406 1407 1408
    tmp_uint= (tmp_dec+(int_digits_end-int_digits_from)+
               (uint)(frac_digits_from-int_digits_tail_from)+
               int_digits_added_zeros);
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
  }
  
  /*
    Now write the formated number
    
    First the digits of the int_% parts.
    Do we have enough room to write these digits ?
    If the sign is defined and '-', we need one position for it
  */

1419
  if (field_length < tmp_uint + (int) (sign_char == '-')) 
1420
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1421
    // too big number, change to max or min number
1422
    Field_decimal::overflow(sign_char == '-');
1423
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1424
  }
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
 
  /*
    Tmp_left_pos is the position where the leftmost digit of
    the int_% parts will be written
  */
  tmp_left_pos=pos=to+(uint)(field_length-tmp_uint);
  
  // Write all digits of the int_% parts
  while (int_digits_from != int_digits_end)
    *pos++ = *int_digits_from++ ;

  if (expo_sign_char == '+')
  {    
    while (int_digits_tail_from != frac_digits_from)
      *pos++= *int_digits_tail_from++;
    while (int_digits_added_zeros-- >0)
      *pos++= '0';  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1442
  }
1443 1444 1445 1446 1447 1448
  /*
    Note the position where the rightmost digit of the int_% parts has been
    written (this is to later check if the int_% parts contained nothing,
    meaning an extra 0 is needed).
  */
  tmp_right_pos=pos;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1449 1450

  /*
1451 1452
    Step back to the position of the leftmost digit of the int_% parts,
    to write sign and fill with zeros or blanks or prezeros.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1453
  */
1454 1455 1456 1457
  pos=tmp_left_pos-1;
  if (zerofill)
  {
    left_wall=to-1;
1458
    while (pos > left_wall)			// Fill with zeros
1459 1460 1461 1462
      *pos--='0';
  }
  else
  {
1463
    left_wall=to+(sign_char != 0)-1;
1464 1465
    if (!expo_sign_char)	// If exponent was specified, ignore prezeros
    {
1466
      for (;pos > left_wall && pre_zeros_from !=pre_zeros_end;
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
	   pre_zeros_from++)
	*pos--= '0';
    }
    if (pos == tmp_right_pos-1)
      *pos--= '0';		// no 0 has ever been written, so write one
    left_wall= to-1;
    if (sign_char && pos != left_wall)
    {
      /* Write sign if possible (it is if sign is '-') */
      *pos--= sign_char;
    }
    while (pos != left_wall)
      *pos--=' ';  //fill with blanks
  }
  
1482 1483
  /*
    Write digits of the frac_% parts ;
1484
    Depending on table->in_use->count_cutted_fields, we may also want
1485 1486 1487 1488 1489
    to know if some non-zero tail of these parts will
    be truncated (for example, 0.002->0.00 will generate a warning,
    while 0.000->0.00 will not)
    (and 0E1000000000 will not, while 1E-1000000000 will)
  */
1490
      
1491 1492 1493
  pos=to+(uint)(field_length-tmp_dec);	// Calculate post to '.'
  right_wall=to+field_length;
  if (pos != right_wall) 
1494 1495
    *pos++='.';

1496 1497 1498
  if (expo_sign_char == '-')
  {
    while (frac_digits_added_zeros-- > 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1499
    {
1500
      if (pos == right_wall) 
1501
      {
1502
        if (table->in_use->count_cuted_fields && !is_cuted_fields_incr) 
1503
          break; // Go on below to see if we lose non zero digits
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1504
        return 0;
1505
      }
1506 1507 1508 1509 1510 1511
      *pos++='0';
    }
    while (int_digits_end != frac_digits_head_end)
    {
      tmp_char= *int_digits_end++;
      if (pos == right_wall)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1512
      {
1513 1514 1515
        if (tmp_char != '0')			// Losing a non zero digit ?
        {
          if (!is_cuted_fields_incr)
1516 1517
            set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                        ER_WARN_DATA_TRUNCATED, 1);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1518
          return 0;
1519 1520
        }
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1521
      }
1522
      *pos++= tmp_char;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1523
    }
1524
  }
1525

1526 1527 1528 1529
  for (;frac_digits_from!=frac_digits_end;) 
  {
    tmp_char= *frac_digits_from++;
    if (pos == right_wall)
1530
    {
1531
      if (tmp_char != '0')			// Losing a non zero digit ?
1532
      {
1533
        if (!is_cuted_fields_incr)
1534
	  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1535
        return 0;
1536
      }
1537
      continue;
1538
    }
1539
    *pos++= tmp_char;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1540
  }
1541 1542 1543
      
  while (pos != right_wall)
   *pos++='0';			// Fill with zeros at right of '.'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1544
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1545 1546 1547
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1548
int Field_decimal::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1549 1550 1551 1552
{
  if (unsigned_flag && nr < 0)
  {
    overflow(1);
1553
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1554
  }
1555
  
1556 1557
#ifdef HAVE_FINITE
  if (!finite(nr)) // Handle infinity as special case
1558 1559
  {
    overflow(nr < 0.0);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1560
    return 1;
1561
  }
1562
#endif
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1563

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1564 1565
  reg4 uint i,length;
  char fyllchar,*to;
1566
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1567 1568

  fyllchar = zerofill ? (char) '0' : (char) ' ';
serg@serg.mylan's avatar
serg@serg.mylan committed
1569
#ifdef HAVE_SNPRINTF
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1570 1571
  buff[sizeof(buff)-1]=0;			// Safety
  snprintf(buff,sizeof(buff)-1, "%.*f",(int) dec,nr);
1572
  length=(uint) strlen(buff);
1573
#else
1574
  length=(uint) my_sprintf(buff,(buff,"%.*f",dec,nr));
1575
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1576 1577 1578 1579

  if (length > field_length)
  {
    overflow(nr < 0.0);
1580
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1581 1582 1583 1584 1585 1586 1587
  }
  else
  {
    to=ptr;
    for (i=field_length-length ; i-- > 0 ;)
      *to++ = fyllchar;
    memcpy(to,buff,length);
1588
    return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1589 1590 1591 1592
  }
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1593
int Field_decimal::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1594 1595 1596 1597
{
  if (unsigned_flag && nr < 0)
  {
    overflow(1);
1598
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1599 1600 1601 1602 1603 1604 1605 1606
  }
  char buff[22];
  uint length=(uint) (longlong10_to_str(nr,buff,-10)-buff);
  uint int_part=field_length- (dec  ? dec+1 : 0);

  if (length > int_part)
  {
    overflow(test(nr < 0L));			/* purecov: inspected */
1607
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
  }
  else
  {
    char fyllchar = zerofill ? (char) '0' : (char) ' ';
    char *to=ptr;
    for (uint i=int_part-length ; i-- > 0 ;)
      *to++ = fyllchar;
    memcpy(to,buff,length);
    if (dec)
    {
      to[length]='.';
      bfill(to+length+1,dec,'0');
    }
1621
    return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1622 1623 1624 1625 1626 1627
  }
}


double Field_decimal::val_real(void)
{
1628
  int not_used;
1629 1630 1631
  char *end_not_used;
  return my_strntod(&my_charset_bin, ptr, field_length, &end_not_used,
                    &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1632 1633 1634 1635
}

longlong Field_decimal::val_int(void)
{
1636
  int not_used;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1637
  if (unsigned_flag)
1638
    return my_strntoull(&my_charset_bin, ptr, field_length, 10, NULL,
1639
			&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1640
  else
1641
    return my_strntoll(&my_charset_bin, ptr, field_length, 10, NULL,
1642
			&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1643 1644
}

1645

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1646 1647 1648 1649 1650 1651
String *Field_decimal::val_str(String *val_buffer __attribute__((unused)),
			       String *val_ptr)
{
  char *str;
  for (str=ptr ; *str == ' ' ; str++) ;
  uint tmp_length=(uint) (str-ptr);
1652
  val_ptr->set_charset(&my_charset_bin);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1653 1654 1655
  if (field_length < tmp_length)		// Error in data
    val_ptr->length(0);
  else
1656
    val_ptr->set_ascii((const char*) str, field_length-tmp_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
  return val_ptr;
}

/*
** Should be able to handle at least the following fixed decimal formats:
** 5.00 , -1.0,  05,  -05, +5 with optional pre/end space
*/

int Field_decimal::cmp(const char *a_ptr,const char *b_ptr)
{
  const char *end;
1668
  int swap=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1669 1670 1671 1672
  /* First remove prefixes '0', ' ', and '-' */
  for (end=a_ptr+field_length;
       a_ptr != end &&
	 (*a_ptr == *b_ptr ||
1673
	  ((my_isspace(&my_charset_bin,*a_ptr)  || *a_ptr == '+' || 
1674
            *a_ptr == '0') &&
1675
	   (my_isspace(&my_charset_bin,*b_ptr) || *b_ptr == '+' || 
1676
            *b_ptr == '0')));
1677 1678 1679 1680 1681
       a_ptr++,b_ptr++)
  {
    if (*a_ptr == '-')				// If both numbers are negative
      swap= -1 ^ 1;				// Swap result      
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1682 1683 1684
  if (a_ptr == end)
    return 0;
  if (*a_ptr == '-')
1685
    return -1;
1686
  if (*b_ptr == '-')
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
    return 1;

  while (a_ptr != end)
  {
    if (*a_ptr++ != *b_ptr++)
      return swap ^ (a_ptr[-1] < b_ptr[-1] ? -1 : 1); // compare digits
  }
  return 0;
}


void Field_decimal::sort_string(char *to,uint length)
{
  char *str,*end;
  for (str=ptr,end=ptr+length;
       str != end &&
1703
	 ((my_isspace(&my_charset_bin,*str) || *str == '+' ||
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1704
	   *str == '0')) ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
       str++)
    *to++=' ';
  if (str == end)
    return;					/* purecov: inspected */

  if (*str == '-')
  {
    *to++=1;					// Smaller than any number
    str++;
    while (str != end)
1715
      if (my_isdigit(&my_charset_bin,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1716 1717 1718 1719 1720 1721 1722
	*to++= (char) ('9' - *str++);
      else
	*to++= *str++;
  }
  else memcpy(to,str,(uint) (end-str));
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1723

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1724 1725
void Field_decimal::sql_type(String &res) const
{
1726
  CHARSET_INFO *cs=res.charset();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1727 1728 1729 1730 1731
  uint tmp=field_length;
  if (!unsigned_flag)
    tmp--;
  if (dec)
    tmp--;
1732
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1733
			  "decimal(%d,%d)",tmp,dec));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1734 1735 1736 1737 1738 1739 1740 1741
  add_zerofill_and_unsigned(res);
}


/****************************************************************************
** tiny int
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1742
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1743
{
1744
  int not_used;				// We can ignore result from str2int
1745
  char *end;
1746
  long tmp= my_strntol(cs, from, len, 10, &end, &not_used);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1747
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1748 1749 1750 1751 1752 1753

  if (unsigned_flag)
  {
    if (tmp < 0)
    {
      tmp=0; /* purecov: inspected */
1754
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1755
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1756 1757 1758 1759
    }
    else if (tmp > 255)
    {
      tmp= 255;
1760
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1761
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1762
    }
1763
    else if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
1764
    {
1765
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1766
      error= 1;
1767
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1768 1769 1770 1771 1772 1773
  }
  else
  {
    if (tmp < -128)
    {
      tmp= -128;
1774
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1775
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1776 1777 1778 1779
    }
    else if (tmp >= 128)
    {
      tmp= 127;
1780
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1781
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1782
    }
1783
    else if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
1784
    {
1785
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1786
      error= 1;
1787
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1788 1789
  }
  ptr[0]= (char) tmp;
1790
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1791 1792 1793
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1794
int Field_tiny::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1795
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1796
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1797 1798 1799 1800 1801 1802
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0.0)
    {
      *ptr=0;
1803
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1804
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1805 1806 1807 1808
    }
    else if (nr > 255.0)
    {
      *ptr=(char) 255;
1809
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1810
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1811 1812 1813 1814 1815 1816 1817 1818 1819
    }
    else
      *ptr=(char) nr;
  }
  else
  {
    if (nr < -128.0)
    {
      *ptr= (char) -128;
1820
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1821
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1822 1823 1824 1825
    }
    else if (nr > 127.0)
    {
      *ptr=127;
1826
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1827
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1828 1829 1830 1831
    }
    else
      *ptr=(char) nr;
  }
1832
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1833 1834
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1835
int Field_tiny::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1836
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1837
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1838 1839 1840 1841 1842
  if (unsigned_flag)
  {
    if (nr < 0L)
    {
      *ptr=0;
1843
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1844
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1845 1846 1847 1848
    }
    else if (nr > 255L)
    {
      *ptr= (char) 255;
1849
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1850
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1851 1852 1853 1854 1855 1856 1857 1858 1859
    }
    else
      *ptr=(char) nr;
  }
  else
  {
    if (nr < -128L)
    {
      *ptr= (char) -128;
1860
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1861
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1862 1863 1864 1865
    }
    else if (nr > 127L)
    {
      *ptr=127;
1866
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1867
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1868 1869 1870 1871
    }
    else
      *ptr=(char) nr;
  }
1872
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
}


double Field_tiny::val_real(void)
{
  int tmp= unsigned_flag ? (int) ((uchar*) ptr)[0] :
    (int) ((signed char*) ptr)[0];
  return (double) tmp;
}

longlong Field_tiny::val_int(void)
{
  int tmp= unsigned_flag ? (int) ((uchar*) ptr)[0] :
    (int) ((signed char*) ptr)[0];
  return (longlong) tmp;
}

String *Field_tiny::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
1893
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1894
  uint length;
1895 1896
  uint mlength=max(field_length+1,5*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1897
  char *to=(char*) val_buffer->ptr();
1898

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1899
  if (unsigned_flag)
1900 1901
    length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
					   (long) *((uchar*) ptr));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1902
  else
1903 1904
    length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
					   (long) *((signed char*) ptr));
1905
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1906 1907 1908 1909 1910 1911
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}

1912 1913 1914 1915
bool Field_tiny::send_binary(Protocol *protocol)
{
  return protocol->store_tiny((longlong) (int8) ptr[0]);
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935

int Field_tiny::cmp(const char *a_ptr, const char *b_ptr)
{
  signed char a,b;
  a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
  if (unsigned_flag)
    return ((uchar) a < (uchar) b) ? -1 : ((uchar) a > (uchar) b) ? 1 : 0;
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_tiny::sort_string(char *to,uint length __attribute__((unused)))
{
  if (unsigned_flag)
    *to= *ptr;
  else
    to[0] = (char) ((uchar) ptr[0] ^ (uchar) 128);	/* Revers signbit */
}

void Field_tiny::sql_type(String &res) const
{
1936
  CHARSET_INFO *cs=res.charset();
1937
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1938
			  "tinyint(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1939 1940 1941 1942
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
1943
 Field type short int (2 byte)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1944 1945
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1946
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1947
{
1948
  int not_used;				// We can ignore result from str2int
1949
  char *end;
1950
  long tmp= my_strntol(cs, from, len, 10, &end, &not_used);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1951
  int error= 0;
1952

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1953 1954 1955 1956 1957
  if (unsigned_flag)
  {
    if (tmp < 0)
    {
      tmp=0;
1958
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1959
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1960 1961 1962 1963
    }
    else if (tmp > (uint16) ~0)
    {
      tmp=(uint16) ~0;
1964
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1965
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1966
    }
1967
    else if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
1968
    {
1969
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1970
      error= 1;
1971
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1972 1973 1974 1975 1976 1977
  }
  else
  {
    if (tmp < INT_MIN16)
    {
      tmp= INT_MIN16;
1978
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1979
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1980 1981 1982 1983
    }
    else if (tmp > INT_MAX16)
    {
      tmp=INT_MAX16;
1984
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1985
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1986
    }
1987
    else if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
1988
    {
1989
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1990
      error= 1;
1991
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1992 1993 1994 1995 1996 1997 1998 1999 2000
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,tmp);
  }
  else
#endif
    shortstore(ptr,(short) tmp);
2001
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2002 2003 2004
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2005
int Field_short::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2006
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2007
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2008 2009 2010 2011 2012 2013 2014
  int16 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
2015
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2016
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2017 2018 2019 2020
    }
    else if (nr > (double) (uint16) ~0)
    {
      res=(int16) (uint16) ~0;
2021
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2022
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2023 2024 2025 2026 2027 2028 2029 2030 2031
    }
    else
      res=(int16) (uint16) nr;
  }
  else
  {
    if (nr < (double) INT_MIN16)
    {
      res=INT_MIN16;
2032
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2033
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2034 2035 2036 2037
    }
    else if (nr > (double) INT_MAX16)
    {
      res=INT_MAX16;
2038
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2039
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
    }
    else
      res=(int16) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
2052
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2053 2054
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2055
int Field_short::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2056
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2057
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2058 2059 2060 2061 2062 2063
  int16 res;
  if (unsigned_flag)
  {
    if (nr < 0L)
    {
      res=0;
2064
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2065
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2066 2067 2068 2069
    }
    else if (nr > (longlong) (uint16) ~0)
    {
      res=(int16) (uint16) ~0;
2070
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2071
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2072 2073 2074 2075 2076 2077 2078 2079 2080
    }
    else
      res=(int16) (uint16) nr;
  }
  else
  {
    if (nr < INT_MIN16)
    {
      res=INT_MIN16;
2081
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2082
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2083 2084 2085 2086
    }
    else if (nr > INT_MAX16)
    {
      res=INT_MAX16;
2087
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2088
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
    }
    else
      res=(int16) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
2101
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
}


double Field_short::val_real(void)
{
  short j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint2korr(ptr);
  else
#endif
    shortget(j,ptr);
  return unsigned_flag ? (double) (unsigned short) j : (double) j;
}

longlong Field_short::val_int(void)
{
  short j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint2korr(ptr);
  else
#endif
    shortget(j,ptr);
  return unsigned_flag ? (longlong) (unsigned short) j : (longlong) j;
}

2129

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2130 2131 2132
String *Field_short::val_str(String *val_buffer,
			     String *val_ptr __attribute__((unused)))
{
2133
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2134
  uint length;
2135 2136
  uint mlength=max(field_length+1,7*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
  char *to=(char*) val_buffer->ptr();
  short j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint2korr(ptr);
  else
#endif
    shortget(j,ptr);

  if (unsigned_flag)
2147 2148
    length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10, 
					  (long) (uint16) j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2149
  else
2150
    length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2151 2152 2153 2154 2155 2156 2157
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


2158 2159 2160 2161 2162 2163
bool Field_short::send_binary(Protocol *protocol)
{
  return protocol->store_short(Field_short::val_int());
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
int Field_short::cmp(const char *a_ptr, const char *b_ptr)
{
  short a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    a=sint2korr(a_ptr);
    b=sint2korr(b_ptr);
  }
  else
#endif
  {
    shortget(a,a_ptr);
    shortget(b,b_ptr);
  }

  if (unsigned_flag)
    return ((unsigned short) a < (unsigned short) b) ? -1 :
    ((unsigned short) a > (unsigned short) b) ? 1 : 0;
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_short::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
  if (!table->db_low_byte_first)
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
2194
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2195 2196 2197 2198 2199 2200 2201 2202
    to[1]   = ptr[1];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[1];
    else
2203
      to[0] = (char) (ptr[1] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2204 2205 2206 2207 2208 2209
    to[1]   = ptr[0];
  }
}

void Field_short::sql_type(String &res) const
{
2210
  CHARSET_INFO *cs=res.charset();
2211
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2212
			  "smallint(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2213 2214 2215 2216 2217
  add_zerofill_and_unsigned(res);
}


/****************************************************************************
2218
  Field type medium int (3 byte)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2219 2220
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2221
int Field_medium::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2222
{
2223
  int not_used;				// We can ignore result from str2int
2224
  char *end;
2225
  long tmp= my_strntol(cs, from, len, 10, &end, &not_used);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2226
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2227 2228 2229 2230 2231 2232

  if (unsigned_flag)
  {
    if (tmp < 0)
    {
      tmp=0;
2233
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2234
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2235 2236 2237 2238
    }
    else if (tmp >= (long) (1L << 24))
    {
      tmp=(long) (1L << 24)-1L;
2239
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2240
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2241
    }
2242
    else if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
2243
    {
2244
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2245
      error= 1;
2246
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2247 2248 2249 2250 2251 2252
  }
  else
  {
    if (tmp < INT_MIN24)
    {
      tmp= INT_MIN24;
2253
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2254
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2255 2256 2257 2258
    }
    else if (tmp > INT_MAX24)
    {
      tmp=INT_MAX24;
2259
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2260
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2261
    }
2262
    else if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
2263
    {
2264
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2265
      error= 1;
2266
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2267 2268 2269
  }

  int3store(ptr,tmp);
2270
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2271 2272 2273
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2274
int Field_medium::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2275
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2276
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2277 2278 2279 2280 2281 2282
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      int3store(ptr,0);
2283
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2284
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2285 2286 2287
    }
    else if (nr >= (double) (long) (1L << 24))
    {
2288
      uint32 tmp=(uint32) (1L << 24)-1L;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2289
      int3store(ptr,tmp);
2290
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2291
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2292 2293
    }
    else
2294
      int3store(ptr,(uint32) nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2295 2296 2297 2298 2299 2300 2301
  }
  else
  {
    if (nr < (double) INT_MIN24)
    {
      long tmp=(long) INT_MIN24;
      int3store(ptr,tmp);
2302
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2303
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2304 2305 2306 2307 2308
    }
    else if (nr > (double) INT_MAX24)
    {
      long tmp=(long) INT_MAX24;
      int3store(ptr,tmp);
2309
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2310
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2311 2312 2313 2314
    }
    else
      int3store(ptr,(long) nr);
  }
2315
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2316 2317
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2318
int Field_medium::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2319
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2320
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2321 2322 2323 2324 2325
  if (unsigned_flag)
  {
    if (nr < 0L)
    {
      int3store(ptr,0);
2326
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2327
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2328 2329 2330 2331 2332
    }
    else if (nr >= (longlong) (long) (1L << 24))
    {
      long tmp=(long) (1L << 24)-1L;;
      int3store(ptr,tmp);
2333
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2334
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2335 2336
    }
    else
2337
      int3store(ptr,(uint32) nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2338 2339 2340 2341 2342 2343 2344
  }
  else
  {
    if (nr < (longlong) INT_MIN24)
    {
      long tmp=(long) INT_MIN24;
      int3store(ptr,tmp);
2345
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2346
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2347 2348 2349 2350 2351
    }
    else if (nr > (longlong) INT_MAX24)
    {
      long tmp=(long) INT_MAX24;
      int3store(ptr,tmp);
2352
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2353
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2354 2355 2356 2357
    }
    else
      int3store(ptr,(long) nr);
  }
2358
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2359 2360 2361 2362 2363 2364 2365 2366 2367
}


double Field_medium::val_real(void)
{
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
  return (double) j;
}

2368

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2369 2370 2371 2372 2373 2374
longlong Field_medium::val_int(void)
{
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
  return (longlong) j;
}

2375

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2376 2377 2378
String *Field_medium::val_str(String *val_buffer,
			      String *val_ptr __attribute__((unused)))
{
2379
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2380
  uint length;
2381 2382
  uint mlength=max(field_length+1,10*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2383 2384 2385
  char *to=(char*) val_buffer->ptr();
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);

2386
  length=(uint) cs->cset->long10_to_str(cs,to,mlength,-10,j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2387 2388 2389 2390 2391 2392 2393
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer); /* purecov: inspected */
  return val_buffer;
}


2394 2395 2396 2397 2398 2399
bool Field_medium::send_binary(Protocol *protocol)
{
  return protocol->store_long(Field_medium::val_int());
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
int Field_medium::cmp(const char *a_ptr, const char *b_ptr)
{
  long a,b;
  if (unsigned_flag)
  {
    a=uint3korr(a_ptr);
    b=uint3korr(b_ptr);
  }
  else
  {
    a=sint3korr(a_ptr);
    b=sint3korr(b_ptr);
  }
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_medium::sort_string(char *to,uint length __attribute__((unused)))
{
  if (unsigned_flag)
    to[0] = ptr[2];
  else
    to[0] = (uchar) (ptr[2] ^ 128);		/* Revers signbit */
  to[1] = ptr[1];
  to[2] = ptr[0];
}


void Field_medium::sql_type(String &res) const
{
2429
  CHARSET_INFO *cs=res.charset();
2430
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), 
2431
			  "mediumint(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2432 2433 2434 2435 2436 2437 2438
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
** long int
****************************************************************************/

bar@mysql.com's avatar
bar@mysql.com committed
2439 2440 2441 2442 2443
/*
  A helper function to check whether the next character
  in the string "s" is MINUS SIGN. 
*/
#ifdef HAVE_CHARSET_ucs2
2444 2445 2446 2447 2448 2449
static bool test_if_minus(CHARSET_INFO *cs,
                          const char *s, const char *e)
{
  my_wc_t wc;
  return cs->cset->mb_wc(cs, &wc, (uchar*) s, (uchar*) e) > 0 && wc == '-';
}
bar@mysql.com's avatar
bar@mysql.com committed
2450 2451 2452 2453 2454 2455
#else
/*
  If not UCS2 support is compiled then it is easier
*/
#define test_if_minus(cs, s, e)  (*s == '-')
#endif
2456 2457


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2458
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2459
{
2460
  long tmp;
monty@mysql.com's avatar
monty@mysql.com committed
2461
  int error= 0;
2462
  char *end;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2463
  
2464
  tmp= cs->cset->scan(cs, from, from+len, MY_SEQ_SPACES);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2465 2466
  len-= tmp;
  from+= tmp;
2467
  my_errno=0;
serg@serg.mylan's avatar
serg@serg.mylan committed
2468

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2469 2470
  if (unsigned_flag)
  {
2471
    if (!len || test_if_minus(cs, from, from + len))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2472 2473
    {
      tmp=0;					// Set negative to 0
2474
      my_errno=ERANGE;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2475
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2476 2477
    }
    else
2478
      tmp=(long) my_strntoul(cs,from,len,10,&end,&error);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2479 2480
  }
  else
2481 2482
    tmp=my_strntol(cs,from,len,10,&end,&error);
  if (error ||
2483
      (from+len != end && table->in_use->count_cuted_fields &&
2484
       !test_if_int(from,len,end,cs)))
2485 2486 2487 2488
  {
    if (error != 1)
      error= 2;
  }
2489 2490 2491
#if SIZEOF_LONG > 4
  if (unsigned_flag)
  {
monty@mysql.com's avatar
monty@mysql.com committed
2492
    if ((ulong) tmp > UINT_MAX32)
2493 2494
    {
      tmp= UINT_MAX32;
serg@serg.mylan's avatar
serg@serg.mylan committed
2495 2496
      error= 1;
      my_errno=ERANGE;
2497 2498 2499 2500 2501 2502 2503
    }
  }
  else
  {
    if (tmp > INT_MAX32)
    {
      tmp= INT_MAX32;
serg@serg.mylan's avatar
serg@serg.mylan committed
2504 2505
      error= 1;
      my_errno=ERANGE;
2506 2507 2508 2509
    }
    else if (tmp < INT_MIN32)
    {
      tmp= INT_MIN32;
serg@serg.mylan's avatar
serg@serg.mylan committed
2510 2511
      error= 1;
      my_errno=ERANGE;
2512 2513 2514
    }
  }
#endif
serg@serg.mylan's avatar
serg@serg.mylan committed
2515
  if (error)
2516
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2517 2518 2519 2520 2521 2522 2523 2524
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
2525
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2526 2527 2528
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2529
int Field_long::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2530
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2531
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2532 2533 2534 2535 2536 2537 2538
  int32 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
2539
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2540
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2541
    }
2542
    else if (nr > (double) UINT_MAX32)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2543
    {
2544
      res= UINT_MAX32;
2545
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2546
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2547 2548 2549 2550 2551 2552 2553 2554 2555
    }
    else
      res=(int32) (ulong) nr;
  }
  else
  {
    if (nr < (double) INT_MIN32)
    {
      res=(int32) INT_MIN32;
2556
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2557
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2558 2559 2560 2561
    }
    else if (nr > (double) INT_MAX32)
    {
      res=(int32) INT_MAX32;
2562
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2563
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
    }
    else
      res=(int32) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,res);
  }
  else
#endif
    longstore(ptr,res);
2576
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2577 2578 2579
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2580
int Field_long::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2581
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2582
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2583
  int32 res;
2584 2585 2586 2587 2588 2589 2590 2591

  /* 
    This assert has nothing to do with this method per se, it was put here
    only because it is one of the best places for catching places there its 
    condition is broken.
  */
  DBUG_ASSERT(table->in_use == current_thd);
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2592 2593 2594 2595 2596
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
2597
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2598
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2599 2600 2601 2602
    }
    else if (nr >= (LL(1) << 32))
    {
      res=(int32) (uint32) ~0L;
2603
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2604
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2605 2606 2607 2608 2609 2610 2611 2612 2613
    }
    else
      res=(int32) (uint32) nr;
  }
  else
  {
    if (nr < (longlong) INT_MIN32)
    {
      res=(int32) INT_MIN32;
2614
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2615
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2616 2617 2618 2619
    }
    else if (nr > (longlong) INT_MAX32)
    {
      res=(int32) INT_MAX32;
2620
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2621
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633
    }
    else
      res=(int32) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,res);
  }
  else
#endif
    longstore(ptr,res);
2634
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652
}


double Field_long::val_real(void)
{
  int32 j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return unsigned_flag ? (double) (uint32) j : (double) j;
}

longlong Field_long::val_int(void)
{
  int32 j;
2653 2654
  /* See the comment in Field_long::store(long long) */
  DBUG_ASSERT(table->in_use == current_thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return unsigned_flag ? (longlong) (uint32) j : (longlong) j;
}

String *Field_long::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
2667
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2668
  uint length;
2669 2670
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2671 2672 2673 2674 2675 2676 2677 2678 2679
  char *to=(char*) val_buffer->ptr();
  int32 j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);

2680
  if (unsigned_flag)
2681
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
2682
  else
2683
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2684 2685 2686 2687 2688 2689 2690
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


2691 2692 2693 2694 2695
bool Field_long::send_binary(Protocol *protocol)
{
  return protocol->store_long(Field_long::val_int());
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
int Field_long::cmp(const char *a_ptr, const char *b_ptr)
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    a=sint4korr(a_ptr);
    b=sint4korr(b_ptr);
  }
  else
#endif
  {
    longget(a,a_ptr);
    longget(b,b_ptr);
  }
  if (unsigned_flag)
2712
    return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_long::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
  if (!table->db_low_byte_first)
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
2724
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734
    to[1]   = ptr[1];
    to[2]   = ptr[2];
    to[3]   = ptr[3];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[3];
    else
2735
      to[0] = (char) (ptr[3] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2736 2737 2738 2739 2740 2741 2742 2743 2744
    to[1]   = ptr[2];
    to[2]   = ptr[1];
    to[3]   = ptr[0];
  }
}


void Field_long::sql_type(String &res) const
{
2745
  CHARSET_INFO *cs=res.charset();
2746
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2747
			  "int(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2748 2749 2750 2751
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
2752
 Field type longlong int (8 bytes)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2753 2754
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2755
int Field_longlong::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2756
{
2757 2758
  longlong tmp;
  int error= 0;
2759
  char *end;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2760
  
2761
  tmp= cs->cset->scan(cs, from, from+len, MY_SEQ_SPACES);
2762
  len-= (uint)tmp;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2763
  from+= tmp;
2764
  my_errno=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2765 2766
  if (unsigned_flag)
  {
2767
    if (!len || test_if_minus(cs, from, from + len))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2768 2769
    {
      tmp=0;					// Set negative to 0
2770
      my_errno= ERANGE;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2771
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2772 2773
    }
    else
2774
      tmp=(longlong) my_strntoull(cs,from,len,10,&end,&error);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2775 2776
  }
  else
2777 2778
    tmp=my_strntoll(cs,from,len,10,&end,&error);
  if (error ||
2779
      (from+len != end && table->in_use->count_cuted_fields &&
2780
       !test_if_int(from,len,end,cs)))
2781
  {
2782 2783 2784 2785 2786
    if (error != 1)
    {
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
      error= 2;
    }
2787
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2788 2789 2790 2791 2792 2793 2794 2795
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
2796
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2797 2798 2799
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2800
int Field_longlong::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2801
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2802
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2803 2804 2805 2806 2807 2808 2809
  longlong res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
2810
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2811
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2812 2813 2814 2815
    }
    else if (nr >= (double) ~ (ulonglong) 0)
    {
      res= ~(longlong) 0;
2816
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2817
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2818 2819 2820 2821 2822 2823 2824 2825 2826
    }
    else
      res=(longlong) (ulonglong) nr;
  }
  else
  {
    if (nr <= (double) LONGLONG_MIN)
    {
      res=(longlong) LONGLONG_MIN;
2827
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2828
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2829
    }
2830
    else if (nr >= (double) (ulonglong) LONGLONG_MAX)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2831 2832
    {
      res=(longlong) LONGLONG_MAX;
2833
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2834
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
    }
    else
      res=(longlong) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,res);
  }
  else
#endif
    longlongstore(ptr,res);
2847
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2848 2849 2850
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2851
int Field_longlong::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2852 2853 2854 2855 2856 2857 2858 2859 2860
{
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
2861
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
}


double Field_longlong::val_real(void)
{
  longlong j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    j=sint8korr(ptr);
  }
  else
#endif
    longlongget(j,ptr);
2876 2877 2878 2879 2880 2881 2882
  /* The following is open coded to avoid a bug in gcc 3.3 */
  if (unsigned_flag)
  {
    ulonglong tmp= (ulonglong) j;
    return ulonglong2double(tmp);
  }
  return (double) j;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2883 2884
}

2885

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901
longlong Field_longlong::val_int(void)
{
  longlong j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint8korr(ptr);
  else
#endif
    longlongget(j,ptr);
  return j;
}


String *Field_longlong::val_str(String *val_buffer,
				String *val_ptr __attribute__((unused)))
{
2902
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2903
  uint length;
2904 2905
  uint mlength=max(field_length+1,22*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2906 2907 2908 2909 2910 2911 2912 2913 2914
  char *to=(char*) val_buffer->ptr();
  longlong j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint8korr(ptr);
  else
#endif
    longlongget(j,ptr);

2915
  length=(uint) (cs->cset->longlong10_to_str)(cs,to,mlength,
2916
					unsigned_flag ? 10 : -10, j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2917 2918 2919 2920 2921 2922 2923
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


2924 2925 2926 2927 2928 2929
bool Field_longlong::send_binary(Protocol *protocol)
{
  return protocol->store_longlong(Field_longlong::val_int(), unsigned_flag);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
int Field_longlong::cmp(const char *a_ptr, const char *b_ptr)
{
  longlong a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    a=sint8korr(a_ptr);
    b=sint8korr(b_ptr);
  }
  else
#endif
  {
    longlongget(a,a_ptr);
    longlongget(b,b_ptr);
  }
  if (unsigned_flag)
    return ((ulonglong) a < (ulonglong) b) ? -1 :
    ((ulonglong) a > (ulonglong) b) ? 1 : 0;
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_longlong::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
  if (!table->db_low_byte_first)
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
2959
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973
    to[1]   = ptr[1];
    to[2]   = ptr[2];
    to[3]   = ptr[3];
    to[4]   = ptr[4];
    to[5]   = ptr[5];
    to[6]   = ptr[6];
    to[7]   = ptr[7];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[7];
    else
2974
      to[0] = (char) (ptr[7] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987
    to[1]   = ptr[6];
    to[2]   = ptr[5];
    to[3]   = ptr[4];
    to[4]   = ptr[3];
    to[5]   = ptr[2];
    to[6]   = ptr[1];
    to[7]   = ptr[0];
  }
}


void Field_longlong::sql_type(String &res) const
{
2988
  CHARSET_INFO *cs=res.charset();
2989
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2990
			  "bigint(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2991 2992 2993
  add_zerofill_and_unsigned(res);
}

monty@mysql.com's avatar
monty@mysql.com committed
2994

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2995
/****************************************************************************
monty@mysql.com's avatar
monty@mysql.com committed
2996
  single precision float
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2997 2998
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2999
int Field_float::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3000
{
monty@mysql.com's avatar
monty@mysql.com committed
3001
  int error;
3002
  char *end;
monty@mysql.com's avatar
monty@mysql.com committed
3003
  double nr= my_strntod(cs,(char*) from,len,&end,&error);
3004
  if (error || ((uint) (end-from) != len && table->in_use->count_cuted_fields))
3005
  {
3006
    error= 2;
3007
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
3008
  }
monty@mysql.com's avatar
monty@mysql.com committed
3009 3010
  Field_float::store(nr);
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3011 3012 3013
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3014
int Field_float::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3015 3016
{
  float j;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3017
  int error= 0;
3018

3019
  if (isnan(nr))
3020
  {
3021 3022
    j= 0;
    set_null();
3023
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3024
    error= 1;
3025
  }
3026
  else if (unsigned_flag && nr < 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3027
  {
3028
    j= 0;
3029
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3030
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3031
  }
3032
  else
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3033
  {
3034 3035 3036 3037 3038 3039 3040
    double max_value;
    if (dec >= NOT_FIXED_DEC)
    {
      max_value= FLT_MAX;
    }
    else
    {
3041 3042
      uint tmp=min(field_length,array_elements(log_10)-1);
      max_value= (log_10[tmp]-1)/log_10[dec];
3043 3044 3045 3046 3047 3048
      /*
	The following comparison is needed to not get an overflow if nr
	is close to FLT_MAX
      */
      if (fabs(nr) < FLT_MAX/10.0e+32)
	nr= floor(nr*log_10[dec]+0.5)/log_10[dec];
3049 3050 3051 3052
    }
    if (nr < -max_value)
    {
      j= (float)-max_value;
3053
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3054 3055 3056 3057 3058
      error= 1;
    }
    else if (nr > max_value)
    {
      j= (float)max_value;
3059
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3060 3061 3062 3063
      error= 1;
    }
    else
      j= (float) nr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3064
  }
3065
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3066 3067 3068 3069 3070 3071 3072 3073
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4store(ptr,j);
  }
  else
#endif
    memcpy_fixed(ptr,(byte*) &j,sizeof(j));
3074
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3075 3076 3077
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3078
int Field_float::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3079
{
3080
  return store((double)nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
}


double Field_float::val_real(void)
{
  float j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4get(j,ptr);
  }
  else
#endif
    memcpy_fixed((byte*) &j,ptr,sizeof(j));
  return ((double) j);
}

longlong Field_float::val_int(void)
{
  float j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4get(j,ptr);
  }
  else
#endif
    memcpy_fixed((byte*) &j,ptr,sizeof(j));
  return ((longlong) j);
}


String *Field_float::val_str(String *val_buffer,
			     String *val_ptr __attribute__((unused)))
{
  float nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4get(nr,ptr);
  }
  else
#endif
    memcpy_fixed((byte*) &nr,ptr,sizeof(nr));

3126 3127
  uint to_length=max(field_length,70);
  val_buffer->alloc(to_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175
  char *to=(char*) val_buffer->ptr();

  if (dec >= NOT_FIXED_DEC)
  {
    sprintf(to,"%-*.*g",(int) field_length,FLT_DIG,nr);
    to=strcend(to,' ');
    *to=0;
  }
  else
  {
#ifdef HAVE_FCONVERT
    char buff[70],*pos=buff;
    int decpt,sign,tmp_dec=dec;

    VOID(sfconvert(&nr,tmp_dec,&decpt,&sign,buff));
    if (sign)
    {
      *to++='-';
    }
    if (decpt < 0)
    {					/* val_buffer is < 0 */
      *to++='0';
      if (!tmp_dec)
	goto end;
      *to++='.';
      if (-decpt > tmp_dec)
	decpt= - (int) tmp_dec;
      tmp_dec=(uint) ((int) tmp_dec+decpt);
      while (decpt++ < 0)
	*to++='0';
    }
    else if (decpt == 0)
    {
      *to++= '0';
      if (!tmp_dec)
	goto end;
      *to++='.';
    }
    else
    {
      while (decpt-- > 0)
	*to++= *pos++;
      if (!tmp_dec)
	goto end;
      *to++='.';
    }
    while (tmp_dec--)
      *to++= *pos++;
3176
#else
3177 3178 3179
#ifdef HAVE_SNPRINTF
    to[to_length-1]=0;			// Safety
    snprintf(to,to_length-1,"%.*f",dec,nr);
3180
    to=strend(to);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3181
#else
3182
    to+= my_sprintf(to,(to,"%.*f",dec,nr));
3183
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244
#endif
  }
#ifdef HAVE_FCONVERT
 end:
#endif
  val_buffer->length((uint) (to-val_buffer->ptr()));
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


int Field_float::cmp(const char *a_ptr, const char *b_ptr)
{
  float a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4get(a,a_ptr);
    float4get(b,b_ptr);
  }
  else
#endif
  {
    memcpy_fixed(&a,a_ptr,sizeof(float));
    memcpy_fixed(&b,b_ptr,sizeof(float));
  }
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

#define FLT_EXP_DIG (sizeof(float)*8-FLT_MANT_DIG)

void Field_float::sort_string(char *to,uint length __attribute__((unused)))
{
  float nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4get(nr,ptr);
  }
  else
#endif
    memcpy_fixed(&nr,ptr,sizeof(float));

  uchar *tmp= (uchar*) to;
  if (nr == (float) 0.0)
  {						/* Change to zero string */
    tmp[0]=(uchar) 128;
    bzero((char*) tmp+1,sizeof(nr)-1);
  }
  else
  {
#ifdef WORDS_BIGENDIAN
    memcpy_fixed(tmp,&nr,sizeof(nr));
#else
    tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
#endif
    if (tmp[0] & 128)				/* Negative */
    {						/* make complement */
      uint i;
      for (i=0 ; i < sizeof(nr); i++)
3245
	tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258
    }
    else
    {
      ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
		       (ushort) 32768);
      exp_part+= (ushort) 1 << (16-1-FLT_EXP_DIG);
      tmp[0]= (uchar) (exp_part >> 8);
      tmp[1]= (uchar) exp_part;
    }
  }
}


3259 3260 3261 3262 3263 3264
bool Field_float::send_binary(Protocol *protocol)
{
  return protocol->store((float) Field_float::val_real(), dec, (String*) 0);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
3265 3266 3267
void Field_float::sql_type(String &res) const
{
  if (dec == NOT_FIXED_DEC)
3268
  {
3269
    res.set_ascii("float", 5);
3270
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3271
  else
3272
  {
3273
    CHARSET_INFO *cs= res.charset();
3274
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3275
			    "float(%d,%d)",(int) field_length,dec));
3276
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3277 3278 3279
  add_zerofill_and_unsigned(res);
}

monty@mysql.com's avatar
monty@mysql.com committed
3280

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3281
/****************************************************************************
monty@mysql.com's avatar
monty@mysql.com committed
3282
  double precision floating point numbers
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3283 3284
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3285
int Field_double::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3286
{
monty@mysql.com's avatar
monty@mysql.com committed
3287
  int error;
3288
  char *end;
monty@mysql.com's avatar
monty@mysql.com committed
3289
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
3290
  if (error || ((uint) (end-from) != len && table->in_use->count_cuted_fields))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3291
  {
3292
    error= 2;
3293
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3294
  }
monty@mysql.com's avatar
monty@mysql.com committed
3295 3296
  Field_double::store(nr);
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3297 3298 3299
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3300
int Field_double::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3301
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3302
  int error= 0;
3303

3304 3305 3306 3307
  if (isnan(nr))
  {
    nr= 0;
    set_null();
3308
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3309 3310 3311
    error= 1;
  }
  else if (unsigned_flag && nr < 0)
3312
  {
3313
    nr= 0;
3314
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3315
    error= 1;
3316
  }
3317 3318 3319 3320 3321 3322 3323 3324 3325
  else 
  {
    double max_value;
    if (dec >= NOT_FIXED_DEC)
    {
      max_value= DBL_MAX;
    }
    else
    {
3326 3327
      uint tmp=min(field_length,array_elements(log_10)-1);
      max_value= (log_10[tmp]-1)/log_10[dec];
3328 3329
      if (fabs(nr) < DBL_MAX/10.0e+32)
	nr= floor(nr*log_10[dec]+0.5)/log_10[dec];
3330 3331 3332 3333
    }
    if (nr < -max_value)
    {
      nr= -max_value;
3334
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3335 3336 3337 3338 3339
      error= 1;
    }
    else if (nr > max_value)
    {
      nr= max_value;
3340
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3341 3342 3343 3344
      error= 1;
    }
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3345 3346 3347 3348 3349 3350 3351 3352
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8store(ptr,nr);
  }
  else
#endif
    doublestore(ptr,nr);
3353
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3354 3355 3356
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3357
int Field_double::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3358
{
3359
  return store((double)nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404
}


double Field_double::val_real(void)
{
  double j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8get(j,ptr);
  }
  else
#endif
    doubleget(j,ptr);
  return j;
}

longlong Field_double::val_int(void)
{
  double j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8get(j,ptr);
  }
  else
#endif
    doubleget(j,ptr);
  return ((longlong) j);
}


String *Field_double::val_str(String *val_buffer,
			      String *val_ptr __attribute__((unused)))
{
  double nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8get(nr,ptr);
  }
  else
#endif
    doubleget(nr,ptr);

3405
  uint to_length=max(field_length, DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416
  val_buffer->alloc(to_length);
  char *to=(char*) val_buffer->ptr();

  if (dec >= NOT_FIXED_DEC)
  {
    sprintf(to,"%-*.*g",(int) field_length,DBL_DIG,nr);
    to=strcend(to,' ');
  }
  else
  {
#ifdef HAVE_FCONVERT
bar@mysql.com's avatar
bar@mysql.com committed
3417
    char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
3418
    char *pos= buff;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456
    int decpt,sign,tmp_dec=dec;

    VOID(fconvert(nr,tmp_dec,&decpt,&sign,buff));
    if (sign)
    {
      *to++='-';
    }
    if (decpt < 0)
    {					/* val_buffer is < 0 */
      *to++='0';
      if (!tmp_dec)
	goto end;
      *to++='.';
      if (-decpt > tmp_dec)
	decpt= - (int) tmp_dec;
      tmp_dec=(uint) ((int) tmp_dec+decpt);
      while (decpt++ < 0)
	*to++='0';
    }
    else if (decpt == 0)
    {
      *to++= '0';
      if (!tmp_dec)
	goto end;
      *to++='.';
    }
    else
    {
      while (decpt-- > 0)
	*to++= *pos++;
      if (!tmp_dec)
	goto end;
      *to++='.';
    }
    while (tmp_dec--)
      *to++= *pos++;
#else
#ifdef HAVE_SNPRINTF
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3457
    to[to_length-1]=0;			// Safety
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3458
    snprintf(to,to_length-1,"%.*f",dec,nr);
3459
    to=strend(to);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3460
#else
3461
    to+= my_sprintf(to,(to,"%.*f",dec,nr));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474
#endif
#endif
  }
#ifdef HAVE_FCONVERT
 end:
#endif

  val_buffer->length((uint) (to-val_buffer->ptr()));
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}

3475 3476
bool Field_double::send_binary(Protocol *protocol)
{
3477
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
3478 3479
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492

int Field_double::cmp(const char *a_ptr, const char *b_ptr)
{
  double a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8get(a,a_ptr);
    float8get(b,b_ptr);
  }
  else
#endif
  {
3493 3494
    doubleget(a, a_ptr);
    doubleget(b, b_ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513
  }
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}


#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)

/* The following should work for IEEE */

void Field_double::sort_string(char *to,uint length __attribute__((unused)))
{
  double nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8get(nr,ptr);
  }
  else
#endif
3514
    doubleget(nr,ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3515 3516 3517 3518 3519 3520
  change_double_for_sort(nr, (byte*) to);
}


void Field_double::sql_type(String &res) const
{
3521
  CHARSET_INFO *cs=res.charset();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3522
  if (dec == NOT_FIXED_DEC)
3523
  {
3524
    res.set_ascii("double",6);
3525
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3526
  else
3527
  {
3528
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3529
			    "double(%d,%d)",(int) field_length,dec));
3530
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3531 3532 3533 3534
  add_zerofill_and_unsigned(res);
}


3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578
/*
  TIMESTAMP type.
  Holds datetime values in range from 1970-01-01 00:00:01 UTC to 
  2038-01-01 00:00:00 UTC stored as number of seconds since Unix 
  Epoch in UTC.
  
  Up to one of timestamps columns in the table can be automatically 
  set on row update and/or have NOW() as default value.
  TABLE::timestamp_field points to Field object for such timestamp with 
  auto-set-on-update. TABLE::time_stamp holds offset in record + 1 for this
  field, and is used by handler code which performs updates required.
  
  Actually SQL-99 says that we should allow niladic functions (like NOW())
  as defaults for any field. Current limitations (only NOW() and only 
  for one TIMESTAMP field) are because of restricted binary .frm format 
  and should go away in the future.
  
  Also because of this limitation of binary .frm format we use 5 different
  unireg_check values with TIMESTAMP field to distinguish various cases of
  DEFAULT or ON UPDATE values. These values are:
  
  TIMESTAMP_OLD_FIELD - old timestamp, if there was not any fields with
    auto-set-on-update (or now() as default) in this table before, then this 
    field has NOW() as default and is updated when row changes, else it is 
    field which has 0 as default value and is not automaitcally updated.
  TIMESTAMP_DN_FIELD - field with NOW() as default but not set on update
    automatically (TIMESTAMP DEFAULT NOW())
  TIMESTAMP_UN_FIELD - field which is set on update automatically but has not 
    NOW() as default (but it may has 0 or some other const timestamp as 
    default) (TIMESTAMP ON UPDATE NOW()).
  TIMESTAMP_DNUN_FIELD - field which has now() as default and is auto-set on 
    update. (TIMESTAMP DEFAULT NOW() ON UPDATE NOW())
  NONE - field which is not auto-set on update with some other than NOW() 
    default value (TIMESTAMP DEFAULT 0).

  Note that TIMESTAMP_OLD_FIELD's are never created explicitly now, they are 
  left only for preserving ability to read old tables. Such fields replaced 
  with their newer analogs in CREATE TABLE and in SHOW CREATE TABLE. This is 
  because we want to prefer NONE unireg_check before TIMESTAMP_OLD_FIELD for 
  "TIMESTAMP DEFAULT 'Const'" field. (Old timestamps allowed such 
  specification too but ignored default value for first timestamp, which of 
  course is non-standard.) In most cases user won't notice any change, only
  exception is different behavior of old/new timestamps during ALTER TABLE.
 */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3579 3580

Field_timestamp::Field_timestamp(char *ptr_arg, uint32 len_arg,
3581
                                 uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3582 3583
				 enum utype unireg_check_arg,
				 const char *field_name_arg,
3584 3585
				 struct st_table *table_arg,
				 CHARSET_INFO *cs)
3586
  :Field_str(ptr_arg, 19, null_ptr_arg, null_bit_arg,
3587
	     unireg_check_arg, field_name_arg, table_arg, cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3588
{
3589 3590
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
3591 3592
  if (table && !table->timestamp_field && 
      unireg_check != NONE)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3593
  {
3594 3595
    /* This timestamp has auto-update */
    table->timestamp_field= this;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3596 3597 3598 3599 3600
    flags|=TIMESTAMP_FLAG;
  }
}


3601
/*
3602 3603
  Get auto-set type for TIMESTAMP field.

3604
  SYNOPSIS
3605 3606 3607 3608 3609
    get_auto_set_type()

  DESCRIPTION
    Returns value indicating during which operations this TIMESTAMP field
    should be auto-set to current timestamp.
3610
*/
3611
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
3612
{
3613 3614 3615 3616 3617 3618
  switch (unireg_check)
  {
  case TIMESTAMP_DN_FIELD:
    return TIMESTAMP_AUTO_SET_ON_INSERT;
  case TIMESTAMP_UN_FIELD:
    return TIMESTAMP_AUTO_SET_ON_UPDATE;
3619 3620 3621 3622 3623 3624 3625 3626
  case TIMESTAMP_OLD_FIELD:
    /*
      Altough we can have several such columns in legacy tables this
      function should be called only for first of them (i.e. the one
      having auto-set property).
    */
    DBUG_ASSERT(table->timestamp_field == this);
    /* Fall-through */
3627 3628 3629 3630 3631 3632 3633 3634 3635 3636
  case TIMESTAMP_DNUN_FIELD:
    return TIMESTAMP_AUTO_SET_ON_BOTH;
  default:
    /*
      Normally this function should not be called for TIMESTAMPs without
      auto-set property.
    */
    DBUG_ASSERT(0);
    return TIMESTAMP_NO_AUTO_SET;
  }
3637 3638 3639
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3640
int Field_timestamp::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3641
{
3642 3643 3644 3645 3646 3647 3648
  TIME l_time;
  my_time_t tmp= 0;
  int error;
  bool have_smth_to_conv;
  bool in_dst_time_gap;
  THD *thd= table->in_use;

3649 3650
  have_smth_to_conv= (str_to_datetime(from, len, &l_time, 0, &error) >
                      MYSQL_TIMESTAMP_ERROR);
3651 3652 3653
  
  if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
3654
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
3655 3656 3657 3658 3659 3660 3661

  if (have_smth_to_conv)
  {
    if (!(tmp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                           ER_WARN_DATA_OUT_OF_RANGE,
3662
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
3663 3664 3665 3666 3667 3668 3669
      
      error= 1;
    }
    else if (in_dst_time_gap)
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
                           ER_WARN_INVALID_TIMESTAMP, 
3670
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
3671 3672 3673
      error= 1;
    }
  }
3674 3675
  if (error > 1)
    error= 2;
3676

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3677 3678 3679 3680 3681 3682 3683 3684
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
3685
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3686 3687
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3688
int Field_timestamp::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3689
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3690
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3691 3692
  if (nr < 0 || nr > 99991231235959.0)
  {
3693 3694
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE, 
3695
                         nr, MYSQL_TIMESTAMP_DATETIME);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3696 3697
    nr= 0;					// Avoid overflow on buff
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3698
  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3699
  error|= Field_timestamp::store((longlong) rint(nr));
3700
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3701 3702 3703
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3704
int Field_timestamp::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3705 3706
{
  TIME l_time;
3707 3708 3709 3710
  my_time_t timestamp= 0;
  int error;
  bool in_dst_time_gap;
  THD *thd= table->in_use;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3711

3712
  if (number_to_TIME(nr, &l_time, 0, &error))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3713
  {
3714 3715 3716 3717
    if (!(timestamp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                          ER_WARN_DATA_OUT_OF_RANGE,
3718
                          nr, MYSQL_TIMESTAMP_DATETIME, 1);
3719 3720
      error= 1;
    }
3721
  
3722 3723 3724 3725
    if (in_dst_time_gap)
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
                           ER_WARN_INVALID_TIMESTAMP, 
3726
                           nr, MYSQL_TIMESTAMP_DATETIME, !error);
3727 3728 3729 3730 3731 3732
      error= 1;
    }
  }
  else if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_TRUNCATED,
3733
                         nr, MYSQL_TIMESTAMP_DATETIME, 1);
3734

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3735 3736 3737 3738 3739 3740 3741
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,timestamp);
  }
  else
#endif
3742
    longstore(ptr,(uint32) timestamp);
3743
    
monty@mysql.com's avatar
monty@mysql.com committed
3744
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755
}


double Field_timestamp::val_real(void)
{
  return (double) Field_timestamp::val_int();
}

longlong Field_timestamp::val_int(void)
{
  uint32 temp;
3756 3757
  TIME time_tmp;
  THD  *thd= table->in_use;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3758 3759 3760 3761 3762 3763 3764 3765 3766 3767

#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);

  if (temp == 0L)				// No time
    return(0);					/* purecov: inspected */
3768 3769 3770 3771 3772 3773 3774
  
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp, (my_time_t)temp);
  thd->time_zone_used= 1;
  
  return time_tmp.year * LL(10000000000) + time_tmp.month * LL(100000000) +
         time_tmp.day * 1000000L + time_tmp.hour * 10000L +
         time_tmp.minute * 100 + time_tmp.second;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3775 3776 3777
}


serg@serg.mylan's avatar
serg@serg.mylan committed
3778
String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3779
{
3780
  uint32 temp, temp2;
3781 3782
  TIME time_tmp;
  THD *thd= table->in_use;
monty@mysql.com's avatar
monty@mysql.com committed
3783
  char *to;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3784 3785

  val_buffer->alloc(field_length+1);
monty@mysql.com's avatar
monty@mysql.com committed
3786
  to= (char*) val_buffer->ptr();
3787
  val_buffer->length(field_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3788 3789 3790 3791 3792 3793 3794 3795 3796 3797

#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);

  if (temp == 0L)
  {				      /* Zero time is "000000" */
3798
    val_ptr->set("0000-00-00 00:00:00", 19, &my_charset_bin);
3799
    return val_ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3800
  }
3801
  val_buffer->set_charset(&my_charset_bin);	// Safety
3802 3803 3804
  
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp,(my_time_t)temp);
  thd->time_zone_used= 1;
3805

3806
  temp= time_tmp.year % 100;
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820
  if (temp < YY_PART_YEAR)
  {
    *to++= '2';
    *to++= '0';
  }
  else
  {
    *to++= '1';
    *to++= '9';
  }
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= '-';
3821
  temp=time_tmp.month;
3822 3823 3824 3825
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= '-';
3826
  temp=time_tmp.day;
3827 3828 3829 3830
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ' ';
3831
  temp=time_tmp.hour;
3832 3833 3834 3835
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ':';
3836
  temp=time_tmp.minute;
3837 3838 3839 3840
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ':';
3841
  temp=time_tmp.second;
3842 3843 3844 3845
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3846 3847 3848
  return val_buffer;
}

3849

3850
bool Field_timestamp::get_date(TIME *ltime, uint fuzzydate)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3851 3852
{
  long temp;
3853
  THD *thd= table->in_use;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3854 3855 3856 3857 3858 3859 3860 3861
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);
  if (temp == 0L)
  {				      /* Zero time is "000000" */
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3862 3863
    if (!fuzzydate)
      return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3864 3865 3866 3867
    bzero((char*) ltime,sizeof(*ltime));
  }
  else
  {
3868 3869
    thd->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)temp);
    thd->time_zone_used= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3870 3871 3872 3873 3874 3875
  }
  return 0;
}

bool Field_timestamp::get_time(TIME *ltime)
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3876
  return Field_timestamp::get_date(ltime,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3877 3878
}

3879 3880 3881 3882

bool Field_timestamp::send_binary(Protocol *protocol)
{
  TIME tm;
3883
  Field_timestamp::get_date(&tm, TIME_FUZZY_DATE);
3884 3885 3886 3887
  return protocol->store(&tm);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905
int Field_timestamp::cmp(const char *a_ptr, const char *b_ptr)
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    a=sint4korr(a_ptr);
    b=sint4korr(b_ptr);
  }
  else
#endif
  {
  longget(a,a_ptr);
  longget(b,b_ptr);
  }
  return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
}

3906

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929
void Field_timestamp::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
  if (!table->db_low_byte_first)
  {
    to[0] = ptr[0];
    to[1] = ptr[1];
    to[2] = ptr[2];
    to[3] = ptr[3];
  }
  else
#endif
  {
    to[0] = ptr[3];
    to[1] = ptr[2];
    to[2] = ptr[1];
    to[3] = ptr[0];
  }
}


void Field_timestamp::sql_type(String &res) const
{
3930
  res.set_ascii("timestamp", 9);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3931 3932 3933 3934 3935
}


void Field_timestamp::set_time()
{
3936
  long tmp= (long) table->in_use->query_start();
3937
  set_notnull();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
}

/****************************************************************************
** time type
** In string context: HH:MM:SS
** In number context: HHMMSS
** Stored as a 3 byte unsigned int
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3955
int Field_time::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3956 3957 3958
{
  TIME ltime;
  long tmp;
3959 3960 3961
  int error;

  if (str_to_time(from, len, &ltime, &error))
3962
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3963
    tmp=0L;
3964
    error= 2;
3965
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
3966
                         from, len, MYSQL_TIMESTAMP_TIME, 1);
3967
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3968 3969
  else
  {
3970 3971 3972
    if (error)
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                           ER_WARN_DATA_TRUNCATED,
3973
                           from, len, MYSQL_TIMESTAMP_TIME, 1);
3974

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3975 3976 3977 3978 3979 3980
    if (ltime.month)
      ltime.day=0;
    tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
    if (tmp > 8385959)
    {
      tmp=8385959;
3981 3982
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                           ER_WARN_DATA_OUT_OF_RANGE,
3983
                           from, len, MYSQL_TIMESTAMP_TIME, !error);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3984
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3985
    }
3986 3987
    if (error > 1)
      error= 2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3988
  }
3989
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3990 3991
  if (ltime.neg)
    tmp= -tmp;
3992 3993
  error |= Field_time::store((longlong) tmp);
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3994 3995 3996
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3997
int Field_time::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3998 3999
{
  long tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4000
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4001 4002 4003
  if (nr > 8385959.0)
  {
    tmp=8385959L;
4004
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4005
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4006
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4007 4008 4009 4010
  }
  else if (nr < -8385959.0)
  {
    tmp= -8385959L;
4011
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4012
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4013
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4014 4015 4016 4017 4018 4019 4020 4021 4022
  }
  else
  {
    tmp=(long) floor(fabs(nr));			// Remove fractions
    if (nr < 0)
      tmp= -tmp;
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
    {
      tmp=0;
4023
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4024 4025
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
                           MYSQL_TIMESTAMP_TIME);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4026
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4027 4028 4029
    }
  }
  int3store(ptr,tmp);
4030
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4031 4032 4033
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4034
int Field_time::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4035 4036
{
  long tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4037
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4038 4039 4040
  if (nr > (longlong) 8385959L)
  {
    tmp=8385959L;
4041
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4042 4043
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
                         MYSQL_TIMESTAMP_TIME, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4044
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4045 4046 4047 4048
  }
  else if (nr < (longlong) -8385959L)
  {
    tmp= -8385959L;
4049
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4050 4051
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
                         MYSQL_TIMESTAMP_TIME, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4052
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4053 4054 4055 4056 4057 4058 4059
  }
  else
  {
    tmp=(long) nr;
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
    {
      tmp=0;
4060
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4061 4062
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
                           MYSQL_TIMESTAMP_TIME, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4063
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4064 4065 4066
    }
  }
  int3store(ptr,tmp);
4067
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4068 4069 4070 4071 4072
}


double Field_time::val_real(void)
{
4073
  uint32 j= (uint32) uint3korr(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4074 4075 4076 4077 4078 4079 4080 4081
  return (double) j;
}

longlong Field_time::val_int(void)
{
  return (longlong) sint3korr(ptr);
}

4082 4083 4084 4085 4086 4087

/*
  This function is multi-byte safe as the result string is always of type
  my_charset_bin
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4088 4089 4090
String *Field_time::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
4091
  TIME ltime;
4092
  val_buffer->alloc(19);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4093
  long tmp=(long) sint3korr(ptr);
4094
  ltime.neg= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4095 4096 4097
  if (tmp < 0)
  {
    tmp= -tmp;
4098 4099 4100 4101 4102 4103
    ltime.neg= 1;
  }
  ltime.day= (uint) 0;
  ltime.hour= (uint) (tmp/10000);
  ltime.minute= (uint) (tmp/100 % 100);
  ltime.second= (uint) (tmp % 100);
4104
  make_time((DATE_TIME_FORMAT*) 0, &ltime, val_buffer);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4105 4106 4107
  return val_buffer;
}

4108

monty@mysql.com's avatar
monty@mysql.com committed
4109 4110 4111 4112 4113 4114 4115
/*
  Normally we would not consider 'time' as a vaild date, but we allow
  get_date() here to be able to do things like
  DATE_FORMAT(time, "%l.%i %p")
*/
 
bool Field_time::get_date(TIME *ltime, uint fuzzydate)
4116
{
monty@mysql.com's avatar
monty@mysql.com committed
4117 4118 4119
  long tmp;
  if (!fuzzydate)
  {
4120 4121 4122 4123
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_WARN_DATA_OUT_OF_RANGE,
                        ER(ER_WARN_DATA_OUT_OF_RANGE), field_name,
                        table->in_use->row_count);
monty@mysql.com's avatar
monty@mysql.com committed
4124 4125 4126
    return 1;
  }
  tmp=(long) sint3korr(ptr);
4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140
  ltime->neg=0;
  if (tmp < 0)
  {
    ltime->neg= 1;
    tmp=-tmp;
  }
  ltime->hour=tmp/10000;
  tmp-=ltime->hour*10000;
  ltime->minute=   tmp/100;
  ltime->second= tmp % 100;
  ltime->year= ltime->month= ltime->day= ltime->second_part= 0;
  return 0;
}

monty@mysql.com's avatar
monty@mysql.com committed
4141

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4142 4143 4144 4145 4146 4147 4148 4149 4150
bool Field_time::get_time(TIME *ltime)
{
  long tmp=(long) sint3korr(ptr);
  ltime->neg=0;
  if (tmp < 0)
  {
    ltime->neg= 1;
    tmp=-tmp;
  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4151
  ltime->day= 0;
4152
  ltime->hour=   (int) (tmp/10000);
4153
  tmp-=ltime->hour*10000;
4154 4155
  ltime->minute= (int) tmp/100;
  ltime->second= (int) tmp % 100;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4156
  ltime->second_part=0;
4157
  ltime->time_type= MYSQL_TIMESTAMP_TIME;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4158 4159 4160
  return 0;
}

4161 4162 4163 4164 4165

bool Field_time::send_binary(Protocol *protocol)
{
  TIME tm;
  Field_time::get_time(&tm);
4166 4167
  tm.day= tm.hour/24;				// Move hours to days
  tm.hour-= tm.day*24;
4168
  return protocol->store_time(&tm);
4169 4170 4171
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
4172 4173
int Field_time::cmp(const char *a_ptr, const char *b_ptr)
{
4174 4175 4176
  int32 a,b;
  a=(int32) sint3korr(a_ptr);
  b=(int32) sint3korr(b_ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_time::sort_string(char *to,uint length __attribute__((unused)))
{
  to[0] = (uchar) (ptr[2] ^ 128);
  to[1] = ptr[1];
  to[2] = ptr[0];
}

void Field_time::sql_type(String &res) const
{
4189
  res.set_ascii("time", 4);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4190 4191 4192 4193 4194 4195 4196 4197
}

/****************************************************************************
** year type
** Save in a byte the year 0, 1901->2155
** Can handle 2 byte or 4 byte years!
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4198
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4199
{
4200
  int err;
4201
  char *end;
4202 4203 4204 4205 4206 4207 4208 4209 4210
  long nr= my_strntol(cs, from, len, 10, &end, &err);

  if (err)
  {
    if (table->in_use->count_cuted_fields)
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
    *ptr= 0;
    return 0;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4211 4212 4213 4214

  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155)
  {
    *ptr=0;
4215
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4216
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4217
  }
4218 4219
  if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4220 4221 4222 4223 4224 4225 4226 4227
  if (nr != 0 || len != 4)
  {
    if (nr < YY_PART_YEAR)
      nr+=100;					// 2000 - 2069
    else if (nr > 1900)
      nr-= 1900;
  }
  *ptr= (char) (unsigned char) nr;
4228
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4229 4230
}

monty@mysql.com's avatar
monty@mysql.com committed
4231

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4232
int Field_year::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4233 4234
{
  if (nr < 0.0 || nr >= 2155.0)
4235 4236 4237 4238
  {
    (void) Field_year::store((longlong) -1);
    return 1;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4239
  else
4240
    return Field_year::store((longlong) nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4241 4242
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4243
int Field_year::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4244 4245 4246 4247
{
  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155)
  {
    *ptr=0;
4248
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4249
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4250 4251 4252 4253 4254 4255 4256 4257 4258
  }
  if (nr != 0 || field_length != 4)		// 0000 -> 0; 00 -> 2000
  {
    if (nr < YY_PART_YEAR)
      nr+=100;					// 2000 - 2069
    else if (nr > 1900)
      nr-= 1900;
  }
  *ptr= (char) (unsigned char) nr;
4259
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4260 4261
}

4262 4263 4264
bool Field_year::send_binary(Protocol *protocol)
{
  ulonglong tmp= Field_year::val_int();
4265
  return protocol->store_short(tmp);
4266
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294

double Field_year::val_real(void)
{
  return (double) Field_year::val_int();
}

longlong Field_year::val_int(void)
{
  int tmp= (int) ((uchar*) ptr)[0];
  if (field_length != 4)
    tmp%=100;					// Return last 2 char
  else if (tmp)
    tmp+=1900;
  return (longlong) tmp;
}

String *Field_year::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
  val_buffer->alloc(5);
  val_buffer->length(field_length);
  char *to=(char*) val_buffer->ptr();
  sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int());
  return val_buffer;
}

void Field_year::sql_type(String &res) const
{
4295
  CHARSET_INFO *cs=res.charset();
4296
  res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
4297
			  "year(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4298 4299 4300 4301 4302 4303 4304 4305 4306 4307
}


/****************************************************************************
** date type
** In string context: YYYY-MM-DD
** In number context: YYYYMMDD
** Stored as a 4 byte unsigned int
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4308
int Field_date::store(const char *from, uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4309 4310
{
  TIME l_time;
4311
  uint32 tmp;
4312 4313
  int error;
  
4314
  if (str_to_datetime(from, len, &l_time, 1, &error) <= MYSQL_TIMESTAMP_ERROR)
4315
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4316
    tmp=0;
4317
    error= 2;
4318
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4319
  else
4320
    tmp=(uint32) l_time.year*10000L + (uint32) (l_time.month*100+l_time.day);
4321 4322 4323

  if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
4324
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
4325

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4326 4327 4328 4329 4330 4331 4332 4333
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
4334
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4335 4336 4337
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4338
int Field_date::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4339 4340
{
  long tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4341
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4342 4343 4344 4345 4346
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
    nr=floor(nr/1000000.0);			// Timestamp to date
  if (nr < 0.0 || nr > 99991231.0)
  {
    tmp=0L;
4347 4348
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE, 
4349
                         nr, MYSQL_TIMESTAMP_DATE);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4350
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361
  }
  else
    tmp=(long) rint(nr);
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
4362
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4363 4364 4365
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4366
int Field_date::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4367 4368
{
  long tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4369
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4370 4371 4372 4373 4374
  if (nr >= LL(19000000000000) && nr < LL(99991231235959))
    nr=nr/LL(1000000);			// Timestamp to date
  if (nr < 0 || nr > LL(99991231))
  {
    tmp=0L;
4375 4376
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE,
4377
                         nr, MYSQL_TIMESTAMP_DATE, 0);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4378
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389
  }
  else
    tmp=(long) nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
4390
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4391 4392 4393
}


4394 4395 4396 4397 4398 4399 4400 4401 4402 4403
bool Field_date::send_binary(Protocol *protocol)
{
  longlong tmp= Field_date::val_int();
  TIME tm;
  tm.year= (uint32) tmp/10000L % 10000;
  tm.month= (uint32) tmp/100 % 100;
  tm.day= (uint32) tmp % 100;
  return protocol->store_date(&tm);
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431

double Field_date::val_real(void)
{
  int32 j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return (double) (uint32) j;
}

longlong Field_date::val_int(void)
{
  int32 j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return (longlong) (uint32) j;
}

String *Field_date::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
4432
  TIME ltime;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4433 4434 4435 4436 4437 4438 4439 4440
  val_buffer->alloc(field_length);
  int32 tmp;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    tmp=sint4korr(ptr);
  else
#endif
    longget(tmp,ptr);
4441 4442 4443 4444
  ltime.neg= 0;
  ltime.year= (int) ((uint32) tmp/10000L % 10000);
  ltime.month= (int) ((uint32) tmp/100 % 100);
  ltime.day= (int) ((uint32) tmp % 100);
4445
  make_date((DATE_TIME_FORMAT *) 0, &ltime, val_buffer);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4446 4447 4448
  return val_buffer;
}

4449

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490
int Field_date::cmp(const char *a_ptr, const char *b_ptr)
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    a=sint4korr(a_ptr);
    b=sint4korr(b_ptr);
  }
  else
#endif
  {
    longget(a,a_ptr);
    longget(b,b_ptr);
  }
  return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
}


void Field_date::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
  if (!table->db_low_byte_first)
  {
    to[0] = ptr[0];
    to[1] = ptr[1];
    to[2] = ptr[2];
    to[3] = ptr[3];
  }
  else
#endif
  {
    to[0] = ptr[3];
    to[1] = ptr[2];
    to[2] = ptr[1];
    to[3] = ptr[0];
  }
}

void Field_date::sql_type(String &res) const
{
4491
  res.set_ascii("date", 4);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4492 4493 4494 4495 4496 4497 4498 4499
}

/****************************************************************************
** The new date type
** This is identical to the old date type, but stored on 3 bytes instead of 4
** In number context: YYYYMMDD
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4500
int Field_newdate::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4501 4502 4503
{
  TIME l_time;
  long tmp;
4504
  int error;
4505
  if (str_to_datetime(from, len, &l_time, 1, &error) <= MYSQL_TIMESTAMP_ERROR)
4506
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4507
    tmp=0L;
4508
    error= 2;
4509
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4510 4511
  else
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
4512 4513 4514

  if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
4515
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
4516
    
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4517
  int3store(ptr,tmp);
4518
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4519 4520
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4521
int Field_newdate::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4522 4523
{
  if (nr < 0.0 || nr > 99991231235959.0)
4524 4525
  {
    (void) Field_newdate::store((longlong) -1);
4526
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4527
                         ER_WARN_DATA_TRUNCATED, nr, MYSQL_TIMESTAMP_DATE);
4528 4529
    return 1;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4530
  else
4531
    return Field_newdate::store((longlong) rint(nr));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4532 4533 4534
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4535
int Field_newdate::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4536
{
4537
  int32 tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4538
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4539 4540 4541 4542 4543
  if (nr >= LL(100000000) && nr <= LL(99991231235959))
    nr=nr/LL(1000000);			// Timestamp to date
  if (nr < 0L || nr > 99991231L)
  {
    tmp=0;
4544
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4545 4546
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
                         MYSQL_TIMESTAMP_DATE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4547
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4548 4549 4550
  }
  else
  {
4551
    tmp=(int32) nr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4552 4553 4554
    if (tmp)
    {
      if (tmp < YY_PART_YEAR*10000L)			// Fix short dates
4555
	tmp+= (uint32) 20000000L;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4556
      else if (tmp < 999999L)
4557
	tmp+= (uint32) 19000000L;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4558
    }
4559 4560
    uint month= (uint) ((tmp/100) % 100);
    uint day=   (uint) (tmp%100);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4561 4562 4563
    if (month > 12 || day > 31)
    {
      tmp=0L;					// Don't allow date to change
4564
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4565 4566
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
                           MYSQL_TIMESTAMP_DATE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4567
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4568 4569 4570 4571
    }
    else
      tmp= day + month*32 + (tmp/10000)*16*32;
  }
4572
  int3store(ptr,(int32) tmp);
4573
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4574 4575 4576 4577 4578
}

void Field_newdate::store_time(TIME *ltime,timestamp_type type)
{
  long tmp;
4579
  if (type == MYSQL_TIMESTAMP_DATE || type == MYSQL_TIMESTAMP_DATETIME)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4580 4581 4582 4583
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
  else
  {
    tmp=0;
4584
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4585 4586 4587 4588
  }
  int3store(ptr,tmp);
}

4589 4590 4591 4592 4593 4594
bool Field_newdate::send_binary(Protocol *protocol)
{
  TIME tm;
  Field_newdate::get_date(&tm,0);
  return protocol->store_date(&tm);
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4595 4596 4597 4598 4599 4600 4601 4602

double Field_newdate::val_real(void)
{
  return (double) Field_newdate::val_int();
}

longlong Field_newdate::val_int(void)
{
4603
  ulong j= uint3korr(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4604 4605 4606 4607 4608 4609 4610 4611 4612
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
  return (longlong) j;
}

String *Field_newdate::val_str(String *val_buffer,
			       String *val_ptr __attribute__((unused)))
{
  val_buffer->alloc(field_length);
  val_buffer->length(field_length);
4613
  uint32 tmp=(uint32) uint3korr(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4614 4615 4616 4617
  int part;
  char *pos=(char*) val_buffer->ptr()+10;

  /* Open coded to get more speed */
4618
  *pos--=0;					// End NULL
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4619
  part=(int) (tmp & 31);
4620 4621 4622
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4623
  part=(int) (tmp >> 5 & 15);
4624 4625 4626
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4627
  part=(int) (tmp >> 9);
4628 4629 4630 4631
  *pos--= (char) ('0'+part%10); part/=10;
  *pos--= (char) ('0'+part%10); part/=10;
  *pos--= (char) ('0'+part%10); part/=10;
  *pos=   (char) ('0'+part);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4632 4633 4634
  return val_buffer;
}

4635
bool Field_newdate::get_date(TIME *ltime,uint fuzzydate)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4636 4637 4638
{
  if (is_null())
    return 1;
4639
  uint32 tmp=(uint32) uint3korr(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4640 4641 4642
  ltime->day=   tmp & 31;
  ltime->month= (tmp >> 5) & 15;
  ltime->year=  (tmp >> 9);
4643
  ltime->time_type= MYSQL_TIMESTAMP_DATE;
4644
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4645
  return (!fuzzydate && (!ltime->month || !ltime->day)) ? 1 : 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4646 4647 4648 4649
}

bool Field_newdate::get_time(TIME *ltime)
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4650
  return Field_newdate::get_date(ltime,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4651 4652 4653 4654
}

int Field_newdate::cmp(const char *a_ptr, const char *b_ptr)
{
4655 4656 4657
  uint32 a,b;
  a=(uint32) uint3korr(a_ptr);
  b=(uint32) uint3korr(b_ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_newdate::sort_string(char *to,uint length __attribute__((unused)))
{
  to[0] = ptr[2];
  to[1] = ptr[1];
  to[2] = ptr[0];
}

void Field_newdate::sql_type(String &res) const
{
4670
  res.set_ascii("date", 4);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4671 4672 4673 4674 4675 4676 4677 4678 4679 4680
}


/****************************************************************************
** datetime type
** In string context: YYYY-MM-DD HH:MM:DD
** In number context: YYYYMMDDHHMMDD
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4681
int Field_datetime::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4682
{
4683 4684 4685 4686
  TIME time_tmp;
  int error;
  ulonglong tmp= 0;
  
4687
  if (str_to_datetime(from, len, &time_tmp, 1, &error) > MYSQL_TIMESTAMP_ERROR)
4688 4689 4690 4691 4692
    tmp= TIME_to_ulonglong_datetime(&time_tmp);
  
  if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE,
4693
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
4694

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4695 4696 4697 4698 4699 4700 4701 4702
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
4703
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4704 4705 4706
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4707
int Field_datetime::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4708
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4709
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4710 4711
  if (nr < 0.0 || nr > 99991231235959.0)
  {
4712 4713
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE,
4714
                         nr, MYSQL_TIMESTAMP_DATETIME);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4715
    nr=0.0;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4716
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4717
  }
4718 4719
  error |= Field_datetime::store((longlong) rint(nr));
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4720 4721 4722
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4723
int Field_datetime::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4724
{
4725
  TIME not_used;
4726 4727
  int error;
  longlong initial_nr= nr;
4728
  
4729 4730 4731 4732 4733
  nr= number_to_TIME(nr, &not_used, 1, &error);

  if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_TRUNCATED, initial_nr, 
4734
                         MYSQL_TIMESTAMP_DATETIME, 1);
4735

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4736 4737 4738 4739 4740 4741 4742 4743
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
4744
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4745 4746
}

monty@mysql.com's avatar
monty@mysql.com committed
4747

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4748 4749 4750
void Field_datetime::store_time(TIME *ltime,timestamp_type type)
{
  longlong tmp;
4751 4752 4753 4754
  /*
    We don't perform range checking here since values stored in TIME
    structure always fit into DATETIME range.
  */
4755
  if (type == MYSQL_TIMESTAMP_DATE || type == MYSQL_TIMESTAMP_DATETIME)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4756 4757 4758 4759 4760
    tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*LL(1000000)+
	 (ltime->hour*10000L+ltime->minute*100+ltime->second));
  else
  {
    tmp=0;
4761
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
}

4773 4774 4775
bool Field_datetime::send_binary(Protocol *protocol)
{
  TIME tm;
4776
  Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
4777 4778 4779
  return protocol->store(&tm);
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824

double Field_datetime::val_real(void)
{
  return (double) Field_datetime::val_int();
}

longlong Field_datetime::val_int(void)
{
  longlong j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint8korr(ptr);
  else
#endif
    longlongget(j,ptr);
  return j;
}


String *Field_datetime::val_str(String *val_buffer,
				String *val_ptr __attribute__((unused)))
{
  val_buffer->alloc(field_length);
  val_buffer->length(field_length);
  ulonglong tmp;
  long part1,part2;
  char *pos;
  int part3;

#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    tmp=sint8korr(ptr);
  else
#endif
    longlongget(tmp,ptr);

  /*
    Avoid problem with slow longlong aritmetic and sprintf
  */

  part1=(long) (tmp/LL(1000000));
  part2=(long) (tmp - (ulonglong) part1*LL(1000000));

  pos=(char*) val_buffer->ptr()+19;
  *pos--=0;
4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843
  *pos--= (char) ('0'+(char) (part2%10)); part2/=10;
  *pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
  *pos--= ':';
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= ':';
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= (char) ('0'+(char) part3);
  *pos--= ' ';
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
  *pos--= '-';
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
  *pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
  *pos--= '-';
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos=(char) ('0'+(char) part3);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4844 4845 4846
  return val_buffer;
}

4847
bool Field_datetime::get_date(TIME *ltime, uint fuzzydate)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4848 4849
{
  longlong tmp=Field_datetime::val_int();
4850 4851 4852
  uint32 part1,part2;
  part1=(uint32) (tmp/LL(1000000));
  part2=(uint32) (tmp - (ulonglong) part1*LL(1000000));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4853

4854
  ltime->time_type=	MYSQL_TIMESTAMP_DATETIME;
4855 4856 4857 4858 4859 4860 4861 4862
  ltime->neg=		0;
  ltime->second_part=	0;
  ltime->second=	(int) (part2%100);
  ltime->minute=	(int) (part2/100%100);
  ltime->hour=		(int) (part2/10000);
  ltime->day=		(int) (part1%100);
  ltime->month= 	(int) (part1/100%100);
  ltime->year= 		(int) (part1/10000);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4863
  return (!fuzzydate && (!ltime->month || !ltime->day)) ? 1 : 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4864 4865 4866 4867
}

bool Field_datetime::get_time(TIME *ltime)
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4868
  return Field_datetime::get_date(ltime,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920
}

int Field_datetime::cmp(const char *a_ptr, const char *b_ptr)
{
  longlong a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    a=sint8korr(a_ptr);
    b=sint8korr(b_ptr);
  }
  else
#endif
  {
    longlongget(a,a_ptr);
    longlongget(b,b_ptr);
  }
  return ((ulonglong) a < (ulonglong) b) ? -1 :
    ((ulonglong) a > (ulonglong) b) ? 1 : 0;
}

void Field_datetime::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
  if (!table->db_low_byte_first)
  {
    to[0] = ptr[0];
    to[1] = ptr[1];
    to[2] = ptr[2];
    to[3] = ptr[3];
    to[4] = ptr[4];
    to[5] = ptr[5];
    to[6] = ptr[6];
    to[7] = ptr[7];
  }
  else
#endif
  {
    to[0] = ptr[7];
    to[1] = ptr[6];
    to[2] = ptr[5];
    to[3] = ptr[4];
    to[4] = ptr[3];
    to[5] = ptr[2];
    to[6] = ptr[1];
    to[7] = ptr[0];
  }
}


void Field_datetime::sql_type(String &res) const
{
4921
  res.set_ascii("datetime", 8);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4922 4923 4924 4925 4926 4927 4928 4929 4930
}

/****************************************************************************
** string type
** A string may be varchar or binary
****************************************************************************/

	/* Copy a string and fill with space */

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4931
int Field_string::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4932
{
4933
  int error= 0, well_formed_error;
4934
  uint32 not_used;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4935 4936
  char buff[80];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4937
  uint copy_length;
4938 4939 4940 4941
  
  /* See the comment for Field_long::store(long long) */
  DBUG_ASSERT(table->in_use == current_thd);
  
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4942
  /* Convert character set if nesessary */
4943
  if (String::needs_conversion(length, cs, field_charset, &not_used))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4944
  { 
4945 4946
    uint conv_errors;
    tmpstr.copy(from, length, cs, field_charset, &conv_errors);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4947 4948
    from= tmpstr.ptr();
    length=  tmpstr.length();
4949
    if (conv_errors)
4950
      error= 2;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4951
  }
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4952 4953

  /* 
4954
    Make sure we don't break a multibyte sequence
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4955 4956
    as well as don't copy a malformed data.
  */
4957
  copy_length= field_charset->cset->well_formed_len(field_charset,
4958 4959 4960
                                                    from,from+length,
                                                    field_length/
                                                    field_charset->mbmaxlen,
4961
                                                    &well_formed_error);
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4962 4963 4964 4965 4966
  memcpy(ptr,from,copy_length);
  if (copy_length < field_length)	// Append spaces if shorter
    field_charset->cset->fill(field_charset,ptr+copy_length,
			      field_length-copy_length,' ');
  
4967
  if ((copy_length < length) && table->in_use->count_cuted_fields)
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4968 4969 4970 4971 4972 4973
  {					// Check if we loosed some info
    const char *end=from+length;
    from+= copy_length;
    from+= field_charset->cset->scan(field_charset, from, end,
				     MY_SEQ_SPACES);
    if (from != end)
4974
      error= 2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4975
  }
4976 4977
  if (error)
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
4978
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4979 4980 4981
}


4982 4983 4984 4985
/*
  Store double value in Field_string or Field_varstring.

  SYNOPSIS
serg@serg.mylan's avatar
serg@serg.mylan committed
4986
    store(double nr)
4987 4988 4989 4990 4991 4992
    nr            number

  DESCRIPTION
    Pretty prints double number into field_length characters buffer.
*/

serg@serg.mylan's avatar
serg@serg.mylan committed
4993
int Field_str::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4994
{
4995
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
4996
  uint length;
monty@mysql.com's avatar
monty@mysql.com committed
4997
  bool use_scientific_notation= TRUE;
4998
  uint char_length= field_length / charset()->mbmaxlen;
4999 5000 5001 5002 5003 5004
  /*
    Check fabs(nr) against longest value that can be stored in field,
    which depends on whether the value is < 1 or not, and negative or not
  */
  double anr= fabs(nr);
  int neg= (nr < 0.0) ? 1 : 0;
5005
  if (char_length > 4 && char_length < 32 &&
monty@mysql.com's avatar
monty@mysql.com committed
5006
      (anr < 1.0 ? anr > 1/(log_10[max(0,(int) char_length-neg-2)]) /* -2 for "0." */
5007
                 : anr < log_10[char_length-neg]-1))
monty@mysql.com's avatar
monty@mysql.com committed
5008
    use_scientific_notation= FALSE;
konstantin@mysql.com's avatar
konstantin@mysql.com committed
5009

monty@mysql.com's avatar
monty@mysql.com committed
5010 5011
  length= (uint) my_sprintf(buff, (buff, "%-.*g",
                                   (use_scientific_notation ?
5012 5013
                                    max(0, (int)char_length-neg-5) :
                                    char_length),
monty@mysql.com's avatar
monty@mysql.com committed
5014
                                   nr));
5015 5016 5017 5018 5019
  /*
    +1 below is because "precision" in %g above means the
    max. number of significant digits, not the output width.
    Thus the width can be larger than number of significant digits by 1
    (for decimal point)
5020
    the test for char_length < 5 is for extreme cases,
5021
    like inserting 500.0 in char(1)
5022
  */
5023
  DBUG_ASSERT(char_length < 5 || length <= char_length+1);
5024
  return store((const char *) buff, length, charset());
5025 5026
}

monty@mysql.com's avatar
monty@mysql.com committed
5027

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5028
int Field_string::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5029
{
5030 5031 5032
  char buff[64];
  int  l;
  CHARSET_INFO *cs=charset();
5033
  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),-10,nr);
5034
  return Field_string::store(buff,(uint)l,cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5035 5036 5037 5038 5039
}


double Field_string::val_real(void)
{
5040
  int not_used;
5041
  char *end_not_used;
5042
  CHARSET_INFO *cs=charset();
5043
  return my_strntod(cs, ptr, field_length, &end_not_used, &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5044 5045 5046 5047 5048
}


longlong Field_string::val_int(void)
{
5049
  int not_used;
5050
  CHARSET_INFO *cs=charset();
5051
  return my_strntoll(cs,ptr,field_length,10,NULL,&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5052 5053 5054 5055 5056 5057
}


String *Field_string::val_str(String *val_buffer __attribute__((unused)),
			      String *val_ptr)
{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5058
  uint length= field_charset->cset->lengthsp(field_charset, ptr, field_length);
5059 5060
  /* See the comment for Field_long::store(long long) */
  DBUG_ASSERT(table->in_use == current_thd);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5061
  val_ptr->set((const char*) ptr, length, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5062 5063 5064 5065 5066 5067
  return val_ptr;
}


int Field_string::cmp(const char *a_ptr, const char *b_ptr)
{
monty@mysql.com's avatar
monty@mysql.com committed
5068 5069
  uint a_len, b_len;

monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5070
  if (field_charset->strxfrm_multiply > 1)
5071 5072 5073 5074 5075
  {
    /*
      We have to remove end space to be able to compare multi-byte-characters
      like in latin_de 'ae' and 0xe4
    */
5076
    return field_charset->coll->strnncollsp(field_charset,
5077 5078 5079
                                            (const uchar*) a_ptr, field_length,
                                            (const uchar*) b_ptr,
                                            field_length);
5080
  }
monty@mysql.com's avatar
monty@mysql.com committed
5081 5082 5083 5084 5085 5086 5087 5088
  if (field_charset->mbmaxlen != 1)
  {
    uint char_len= field_length/field_charset->mbmaxlen;
    a_len= my_charpos(field_charset, a_ptr, a_ptr + field_length, char_len);
    b_len= my_charpos(field_charset, b_ptr, b_ptr + field_length, char_len);
  }
  else
    a_len= b_len= field_length;
5089 5090
  return my_strnncoll(field_charset,(const uchar*) a_ptr, a_len,
                                    (const uchar*) b_ptr, b_len);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5091 5092
}

5093

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5094 5095
void Field_string::sort_string(char *to,uint length)
{
5096
  uint tmp=my_strnxfrm(field_charset,
5097 5098
		       (unsigned char *) to, length,
		       (unsigned char *) ptr, field_length);
5099
  DBUG_ASSERT(tmp == length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5100 5101 5102 5103 5104
}


void Field_string::sql_type(String &res) const
{
5105
  THD *thd= table->in_use;
5106
  CHARSET_INFO *cs=res.charset();
5107
  ulong length= cs->cset->snprintf(cs,(char*) res.ptr(),
5108
			    res.alloced_length(), "%s(%d)",
5109 5110 5111
			    (field_length > 3 &&
			     (table->db_options_in_use &
			      HA_OPTION_PACK_RECORD) ?
5112
			      (has_charset() ? "varchar" : "varbinary") :
5113
			      (has_charset() ? "char" : "binary")),
5114
			    (int) field_length / charset()->mbmaxlen);
5115
  res.length(length);
5116 5117 5118
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
      has_charset() && (charset()->state & MY_CS_BINSORT))
    res.append(" binary");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5119 5120 5121
}

char *Field_string::pack(char *to, const char *from, uint max_length)
5122
{
monty@mysql.com's avatar
monty@mysql.com committed
5123 5124
  uint length=      min(field_length,max_length);
  uint char_length= max_length/field_charset->mbmaxlen;
5125
  if (length > char_length)
serg@serg.mylan's avatar
serg@serg.mylan committed
5126
    char_length= my_charpos(field_charset, from, from+length, char_length);
5127
  set_if_smaller(length, char_length);
serg@serg.mylan's avatar
serg@serg.mylan committed
5128 5129
  while (length && from[length-1] == ' ')
    length--;
5130
  *to++= (char) (uchar) length;
5131
  if (field_length > 255)
5132
    *to++= (char) (uchar) (length >> 8);
5133 5134
  memcpy(to, from, length);
  return to+length;
5135 5136 5137
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
5138 5139
const char *Field_string::unpack(char *to, const char *from)
{
5140 5141 5142 5143 5144 5145 5146 5147
  uint length;
  if (field_length > 255)
  {
    length= uint2korr(from);
    from+= 2;
  }
  else
    length= (uint) (uchar) *from++;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5148 5149 5150 5151 5152 5153 5154 5155
  memcpy(to, from, (int) length);
  bfill(to+length, field_length - length, ' ');
  return from+length;
}


int Field_string::pack_cmp(const char *a, const char *b, uint length)
{
5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168
  uint a_length, b_length;
  if (field_length > 255)
  {
    a_length= uint2korr(a);
    b_length= uint2korr(b);
    a+= 2;
    b+= 2;
  }
  else
  {
    a_length= (uint) (uchar) *a++;
    b_length= (uint) (uchar) *b++;
  }
5169 5170 5171
  return my_strnncoll(field_charset,
		      (const uchar*)a,a_length,
		      (const uchar*)b,b_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5172 5173 5174
}


5175 5176
int Field_string::pack_cmp(const char *b, uint length)
{
5177 5178 5179 5180 5181 5182 5183 5184
  uint b_length;
  if (field_length > 255)
  {
    b_length= uint2korr(b);
    b+= 2;
  }
  else
    b_length= (uint) (uchar) *b++;
5185 5186 5187 5188
  char *end= ptr + field_length;
  while (end > ptr && end[-1] == ' ')
    end--;
  uint a_length = (uint) (end - ptr);
5189 5190 5191
  return my_strnncoll(field_charset,
		     (const uchar*)ptr,a_length,
		     (const uchar*)b, b_length);
5192 5193 5194
}


5195
uint Field_string::packed_col_length(const char *data_ptr, uint length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5196
{
5197
  if (length > 255)
5198
    return uint2korr(data_ptr)+2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5199
  else
5200
    return (uint) ((uchar) *data_ptr)+1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5201 5202 5203 5204
}

uint Field_string::max_packed_col_length(uint max_length)
{
5205
  return (max_length > 255 ? 2 : 1)+max_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5206 5207 5208 5209 5210 5211 5212 5213
}


/****************************************************************************
** VARCHAR type  (Not available for the end user yet)
****************************************************************************/


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5214
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5215
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5216
  int error= 0;
5217
  uint32 not_used;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5218 5219
  char buff[80];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
5220

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5221
  /* Convert character set if nesessary */
5222
  if (String::needs_conversion(length, cs, field_charset, &not_used))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5223
  { 
5224 5225
    uint conv_errors;
    tmpstr.copy(from, length, cs, field_charset, &conv_errors);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5226 5227
    from= tmpstr.ptr();
    length=  tmpstr.length();
5228
    if (conv_errors)
5229
      error= 2;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5230
  }
5231
  if (length > field_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5232 5233
  {
    length=field_length;
5234
    error= 2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5235
  }
5236 5237
  if (error)
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
5238
  memcpy(ptr+HA_KEY_BLOB_LENGTH,from,length);
5239
  int2store(ptr, length);
5240
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5241 5242 5243
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5244
int Field_varstring::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5245
{
5246 5247 5248
  char buff[64];
  int  l;
  CHARSET_INFO *cs=charset();
5249
  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),-10,nr);
5250
  return Field_varstring::store(buff,(uint)l,cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5251 5252 5253 5254 5255
}


double Field_varstring::val_real(void)
{
5256
  int not_used;
5257
  uint length=uint2korr(ptr)+HA_KEY_BLOB_LENGTH;
5258
  CHARSET_INFO *cs=charset();
5259 5260 5261
  char *end_not_used;
  return my_strntod(cs, ptr+HA_KEY_BLOB_LENGTH, length, &end_not_used,
                    &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5262 5263 5264 5265 5266
}


longlong Field_varstring::val_int(void)
{
5267
  int not_used;
5268
  uint length=uint2korr(ptr)+HA_KEY_BLOB_LENGTH;
5269
  CHARSET_INFO *cs=charset();
5270
  return my_strntoll(cs,ptr+HA_KEY_BLOB_LENGTH,length,10,NULL, &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5271 5272 5273 5274 5275 5276 5277
}


String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
				 String *val_ptr)
{
  uint length=uint2korr(ptr);
5278
  val_ptr->set((const char*) ptr+HA_KEY_BLOB_LENGTH,length,field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5279 5280 5281 5282 5283 5284 5285 5286 5287
  return val_ptr;
}


int Field_varstring::cmp(const char *a_ptr, const char *b_ptr)
{
  uint a_length=uint2korr(a_ptr);
  uint b_length=uint2korr(b_ptr);
  int diff;
5288 5289 5290 5291 5292
  diff= my_strnncoll(field_charset,
		     (const uchar*) a_ptr+HA_KEY_BLOB_LENGTH,
		     min(a_length,b_length),
		     (const uchar*) b_ptr+HA_KEY_BLOB_LENGTH,
		     min(a_length,b_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5293 5294 5295 5296 5297 5298
  return diff ? diff : (int) (a_length - b_length);
}

void Field_varstring::sort_string(char *to,uint length)
{
  uint tot_length=uint2korr(ptr);
5299 5300 5301 5302
  tot_length= my_strnxfrm(field_charset,
			  (uchar*) to, length,
			  (uchar*) ptr+HA_KEY_BLOB_LENGTH,
			  tot_length);
5303
  DBUG_ASSERT(tot_length == length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5304 5305 5306 5307 5308
}


void Field_varstring::sql_type(String &res) const
{
5309
  CHARSET_INFO *cs=res.charset();
5310
  ulong length= cs->cset->snprintf(cs,(char*) res.ptr(),
5311
			     res.alloced_length(),"varchar(%u)",
5312
			     field_length / charset()->mbmaxlen);
5313
  res.length(length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5314 5315 5316 5317
}

char *Field_varstring::pack(char *to, const char *from, uint max_length)
{
5318
  uint length=uint2korr(from);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5319 5320
  if (length > max_length)
    length=max_length;
5321
  *to++= (char) (length & 255);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5322
  if (max_length > 255)
5323
    *to++= (char) (length >> 8);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5324
  if (length)
5325
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5326 5327 5328 5329
  return to+length;
}


5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347
char *Field_varstring::pack_key(char *to, const char *from, uint max_length)
{
  uint length=uint2korr(from);
  uint char_length= (field_charset->mbmaxlen > 1) ?
              max_length/field_charset->mbmaxlen : max_length;
  from+=HA_KEY_BLOB_LENGTH;
  if (length > char_length)
    char_length= my_charpos(field_charset, from, from+length, char_length);
  set_if_smaller(length, char_length);
  *to++= (char) (length & 255);
  if (max_length > 255)
    *to++= (char) (length >> 8);
  if (length)
    memcpy(to, from, length);
  return to+length;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362
const char *Field_varstring::unpack(char *to, const char *from)
{
  uint length;
  if (field_length > 255)
  {
    length= (uint) (uchar) (*to= *from++);
    to[1]=0;
  }
  else
  {
    length=uint2korr(from);
    to[0] = *from++;
    to[1] = *from++;
  }
  if (length)
5363
    memcpy(to+HA_KEY_BLOB_LENGTH, from, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5364 5365 5366 5367 5368 5369 5370 5371 5372 5373
  return from+length;
}


int Field_varstring::pack_cmp(const char *a, const char *b, uint key_length)
{
  uint a_length;
  uint b_length;
  if (key_length > 255)
  {
5374 5375
    a_length=uint2korr(a); a+= 2;
    b_length=uint2korr(b); b+= 2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5376 5377 5378 5379 5380 5381
  }
  else
  {
    a_length= (uint) (uchar) *a++;
    b_length= (uint) (uchar) *b++;
  }
5382
  return my_strnncoll(field_charset,
5383 5384
		     (const uchar*) a, a_length,
		     (const uchar*) b, b_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5385 5386
}

5387 5388
int Field_varstring::pack_cmp(const char *b, uint key_length)
{
5389 5390
  char *a= ptr+HA_KEY_BLOB_LENGTH;
  uint a_length= uint2korr(ptr);
5391 5392 5393
  uint b_length;
  if (key_length > 255)
  {
5394
    b_length=uint2korr(b); b+= 2;
5395 5396 5397 5398 5399
  }
  else
  {
    b_length= (uint) (uchar) *b++;
  }
5400
  return my_strnncoll(field_charset,
5401 5402
		     (const uchar*) a, a_length,
		     (const uchar*) b, b_length);
5403 5404
}

5405
uint Field_varstring::packed_col_length(const char *data_ptr, uint length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5406
{
5407
  if (length > 255)
5408
    return uint2korr(data_ptr)+HA_KEY_BLOB_LENGTH;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5409
  else
5410
    return (uint) ((uchar) *data_ptr)+1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5411 5412 5413 5414
}

uint Field_varstring::max_packed_col_length(uint max_length)
{
5415
  return (max_length > 255 ? 2 : 1)+max_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5416 5417
}

5418 5419
void Field_varstring::get_key_image(char *buff, uint length, CHARSET_INFO *cs,
				    imagetype type)
5420 5421 5422 5423 5424
{
  uint f_length=uint2korr(ptr);
  if (f_length > length)
    f_length= length;
  int2store(buff,length);
5425
  memcpy(buff+HA_KEY_BLOB_LENGTH, ptr+HA_KEY_BLOB_LENGTH, length);
5426 5427
#ifdef HAVE_purify
  if (f_length < length)
5428
    bzero(buff+HA_KEY_BLOB_LENGTH+f_length, (length-f_length));
5429 5430 5431
#endif
}

5432
void Field_varstring::set_key_image(char *buff,uint length, CHARSET_INFO *cs)
5433 5434
{
  length=uint2korr(buff);			// Real length is here
5435
  (void) Field_varstring::store(buff+HA_KEY_BLOB_LENGTH, length, cs);
5436 5437 5438 5439
}



bk@work.mysql.com's avatar
bk@work.mysql.com committed
5440 5441 5442 5443 5444 5445
/****************************************************************************
** blob type
** A blob is saved as a length and a pointer. The length is stored in the
** packlength slot and may be from 1-4.
****************************************************************************/

5446
Field_blob::Field_blob(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5447 5448
		       enum utype unireg_check_arg, const char *field_name_arg,
		       struct st_table *table_arg,uint blob_pack_length,
5449
		       CHARSET_INFO *cs)
5450
  :Field_str(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5451
	     null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
5452
	     table_arg, cs),
hf@deer.(none)'s avatar
hf@deer.(none) committed
5453
   packlength(blob_pack_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5454 5455 5456 5457 5458 5459 5460
{
  flags|= BLOB_FLAG;
  if (table)
    table->blob_fields++;
}


5461
void Field_blob::store_length(uint32 number)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492
{
  switch (packlength) {
  case 1:
    ptr[0]= (uchar) number;
    break;
  case 2:
#ifdef WORDS_BIGENDIAN
    if (table->db_low_byte_first)
    {
      int2store(ptr,(unsigned short) number);
    }
    else
#endif
      shortstore(ptr,(unsigned short) number);
    break;
  case 3:
    int3store(ptr,number);
    break;
  case 4:
#ifdef WORDS_BIGENDIAN
    if (table->db_low_byte_first)
    {
      int4store(ptr,number);
    }
    else
#endif
      longstore(ptr,number);
  }
}


5493
uint32 Field_blob::get_length(const char *pos)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5494 5495 5496
{
  switch (packlength) {
  case 1:
5497
    return (uint32) (uchar) pos[0];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5498 5499 5500 5501 5502 5503 5504 5505 5506
  case 2:
    {
      uint16 tmp;
#ifdef WORDS_BIGENDIAN
      if (table->db_low_byte_first)
	tmp=sint2korr(pos);
      else
#endif
	shortget(tmp,pos);
5507
      return (uint32) tmp;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5508 5509
    }
  case 3:
5510
    return (uint32) uint3korr(pos);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5511 5512 5513 5514 5515 5516 5517 5518 5519
  case 4:
    {
      uint32 tmp;
#ifdef WORDS_BIGENDIAN
      if (table->db_low_byte_first)
	tmp=uint4korr(pos);
      else
#endif
	longget(tmp,pos);
5520
      return (uint32) tmp;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5521 5522 5523 5524 5525 5526
    }
  }
  return 0;					// Impossible
}


5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562
/*
  Put a blob length field into a record buffer.

  SYNOPSIS
    Field_blob::put_length()
    pos                         Pointer into the record buffer.
    length                      The length value to put.

  DESCRIPTION
    Depending on the maximum length of a blob, its length field is
    put into 1 to 4 bytes. This is a property of the blob object,
    described by 'packlength'.

  RETURN
    nothing
*/

void Field_blob::put_length(char *pos, uint32 length)
{
  switch (packlength) {
  case 1:
    *pos= (char) length;
    break;
  case 2:
    int2store(pos, length);
    break;
  case 3:
    int3store(pos, length);
    break;
  case 4:
    int4store(pos, length);
    break;
  }
}


bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5563
int Field_blob::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5564
{
5565
  int error= 0, well_formed_error;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5566
  if (!length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5567 5568 5569 5570 5571
  {
    bzero(ptr,Field_blob::pack_length());
  }
  else
  {
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
5572
    bool was_conversion;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5573 5574
    char buff[80];
    String tmpstr(buff,sizeof(buff), &my_charset_bin);
5575
    uint copy_length;
5576
    uint32 not_used;
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
5577

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5578
    /* Convert character set if nesessary */
5579 5580
    if ((was_conversion= String::needs_conversion(length, cs, field_charset,
						  &not_used)))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5581
    { 
5582 5583
      uint conv_errors;
      tmpstr.copy(from, length, cs, field_charset, &conv_errors);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5584 5585
      from= tmpstr.ptr();
      length=  tmpstr.length();
5586
      if (conv_errors)
5587
        error= 2;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5588
    }
5589 5590
    
    copy_length= max_data_length();
5591 5592 5593 5594 5595 5596
    /*
      copy_length is ok as last argument to well_formed_len as this is never
      used to limit the length of the data. The cut of long data is done with
      the 'min()' call below.
    */
    copy_length= field_charset->cset->well_formed_len(field_charset,
5597 5598 5599
                                                      from,from +
                                                      min(length, copy_length),
                                                      copy_length,
5600
                                                      &well_formed_error);
5601
    if (copy_length < length)
5602
      error= 2;
5603 5604
    Field_blob::store_length(copy_length);
    if (was_conversion || table->copy_blobs || copy_length <= MAX_FIELD_WIDTH)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5605
    {						// Must make a copy
5606 5607
      if (from != value.ptr())			// For valgrind
      {
5608
	value.copy(from,copy_length,charset());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5609
	from=value.ptr();
5610
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5611 5612 5613
    }
    bmove(ptr+packlength,(char*) &from,sizeof(char*));
  }
5614 5615
  if (error)
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
5616
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5617 5618 5619
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5620
int Field_blob::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5621
{
5622 5623 5624
  CHARSET_INFO *cs=charset();
  value.set(nr, 2, cs);
  return Field_blob::store(value.ptr(),(uint) value.length(), cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5625 5626 5627
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5628
int Field_blob::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5629
{
5630 5631 5632
  CHARSET_INFO *cs=charset();
  value.set(nr, cs);
  return Field_blob::store(value.ptr(), (uint) value.length(), cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5633 5634 5635 5636 5637
}


double Field_blob::val_real(void)
{
5638
  int not_used;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5639
  char *blob;
5640
  char *end_not_used;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5641 5642 5643
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
    return 0.0;
5644
  uint32 length=get_length(ptr);
5645
  CHARSET_INFO *cs=charset();
5646
  return my_strntod(cs,blob,length, &end_not_used, &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5647 5648 5649 5650 5651
}


longlong Field_blob::val_int(void)
{
5652
  int not_used;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5653 5654 5655 5656
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
    return 0;
5657
  uint32 length=get_length(ptr);
5658
  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5659 5660 5661 5662 5663 5664 5665 5666 5667
}


String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
			    String *val_ptr)
{
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
5668
    val_ptr->set("",0,charset());	// A bit safer than ->length(0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5669
  else
5670
    val_ptr->set((const char*) blob,get_length(ptr),charset());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5671 5672 5673 5674
  return val_ptr;
}


5675 5676
int Field_blob::cmp(const char *a,uint32 a_length, const char *b,
		    uint32 b_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5677
{
5678 5679 5680 5681
  return field_charset->coll->strnncoll(field_charset, 
                                        (const uchar*)a, a_length,
                                        (const uchar*)b, b_length,
                                        0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707
}


int Field_blob::cmp(const char *a_ptr, const char *b_ptr)
{
  char *blob1,*blob2;
  memcpy_fixed(&blob1,a_ptr+packlength,sizeof(char*));
  memcpy_fixed(&blob2,b_ptr+packlength,sizeof(char*));
  return Field_blob::cmp(blob1,get_length(a_ptr),
			 blob2,get_length(b_ptr));
}


int Field_blob::cmp_offset(uint row_offset)
{
  return Field_blob::cmp(ptr,ptr+row_offset);
}


int Field_blob::cmp_binary_offset(uint row_offset)
{
  return cmp_binary(ptr, ptr+row_offset);
}


int Field_blob::cmp_binary(const char *a_ptr, const char *b_ptr,
5708
			   uint32 max_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5709 5710 5711
{
  char *a,*b;
  uint diff;
5712
  uint32 a_length,b_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727
  memcpy_fixed(&a,a_ptr+packlength,sizeof(char*));
  memcpy_fixed(&b,b_ptr+packlength,sizeof(char*));
  a_length=get_length(a_ptr);
  if (a_length > max_length)
    a_length=max_length;
  b_length=get_length(b_ptr);
  if (b_length > max_length)
    b_length=max_length;
  diff=memcmp(a,b,min(a_length,b_length));
  return diff ? diff : (int) (a_length - b_length);
}


/* The following is used only when comparing a key */

5728
void Field_blob::get_key_image(char *buff,uint length,
5729
			       CHARSET_INFO *cs, imagetype type)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5730
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5731
  uint32 blob_length= get_length(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5732
  char *blob;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5733

hf@deer.(none)'s avatar
hf@deer.(none) committed
5734
#ifdef HAVE_SPATIAL
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5735
  if (type == itMBR)
5736
  {
5737
    const char *dummy;
5738
    MBR mbr;
5739 5740
    Geometry_buffer buffer;
    Geometry *gobj;
5741 5742 5743 5744 5745 5746 5747

    if (blob_length < SRID_SIZE)
    {
      bzero(buff, SIZEOF_STORED_DOUBLE*4);
      return;
    }
    get_ptr(&blob);
5748 5749 5750
    gobj= Geometry::create_from_wkb(&buffer,
				    blob + SRID_SIZE, blob_length - SRID_SIZE);
    if (gobj->get_mbr(&mbr, &dummy))
5751 5752 5753 5754 5755 5756 5757 5758
      bzero(buff, SIZEOF_STORED_DOUBLE*4);
    else
    {
      float8store(buff,    mbr.xmin);
      float8store(buff+8,  mbr.xmax);
      float8store(buff+16, mbr.ymin);
      float8store(buff+24, mbr.ymax);
    }
5759 5760
    return;
  }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5761
#endif /*HAVE_SPATIAL*/
5762

5763 5764
  get_ptr(&blob);
  uint char_length= length / cs->mbmaxlen;
5765 5766
  char_length= my_charpos(cs, blob, blob + blob_length, char_length);
  set_if_smaller(blob_length, char_length);
5767

5768
  if ((uint32) length > blob_length)
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5769
  {
5770 5771 5772 5773
    /*
      Must clear this as we do a memcmp in opt_range.cc to detect
      identical keys
    */
5774
    bzero(buff+HA_KEY_BLOB_LENGTH+blob_length, (length-blob_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5775
    length=(uint) blob_length;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5776
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5777
  int2store(buff,length);
5778
  memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5779 5780
}

5781
void Field_blob::set_key_image(char *buff,uint length, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5782
{
5783 5784
  length= uint2korr(buff);
  (void) Field_blob::store(buff+HA_KEY_BLOB_LENGTH, length, cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5785 5786
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5787

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5788 5789 5790 5791 5792
int Field_blob::key_cmp(const byte *key_ptr, uint max_key_length)
{
  char *blob1;
  uint blob_length=get_length(ptr);
  memcpy_fixed(&blob1,ptr+packlength,sizeof(char*));
5793 5794 5795 5796
  CHARSET_INFO *cs= charset();
  uint char_length= max_key_length / cs->mbmaxlen;
  char_length= my_charpos(cs, blob1, blob1+blob_length, char_length);
  set_if_smaller(blob_length, char_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5797
  return Field_blob::cmp(blob1,min(blob_length, max_key_length),
5798 5799
			 (char*) key_ptr+HA_KEY_BLOB_LENGTH,
			 uint2korr(key_ptr));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5800 5801 5802 5803
}

int Field_blob::key_cmp(const byte *a,const byte *b)
{
5804 5805
  return Field_blob::cmp((char*) a+HA_KEY_BLOB_LENGTH, uint2korr(a),
			 (char*) b+HA_KEY_BLOB_LENGTH, uint2korr(b));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5806 5807 5808 5809 5810 5811 5812
}


void Field_blob::sort_string(char *to,uint length)
{
  char *blob;
  uint blob_length=get_length();
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5813

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5814 5815 5816 5817 5818
  if (!blob_length)
    bzero(to,length);
  else
  {
    memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5819 5820
    
    blob_length=my_strnxfrm(field_charset,
5821 5822
                            (uchar*) to, length, 
                            (uchar*) blob, blob_length);
5823
    DBUG_ASSERT(blob_length == length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5824 5825 5826 5827 5828 5829 5830
  }
}


void Field_blob::sql_type(String &res) const
{
  const char *str;
5831
  uint length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5832
  switch (packlength) {
5833 5834 5835 5836 5837
  default: str="tiny"; length=4; break;
  case 2:  str="";     length=0; break;
  case 3:  str="medium"; length= 6; break;
  case 4:  str="long";  length=4; break;
  }
5838
  res.set_ascii(str,length);
5839
  if (charset() == &my_charset_bin)
5840 5841 5842 5843
    res.append("blob");
  else
  {
    res.append("text");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5844 5845 5846 5847
  }
}


5848 5849 5850 5851
char *Field_blob::pack(char *to, const char *from, uint max_length)
{
  char *save=ptr;
  ptr=(char*) from;
5852
  uint32 length=get_length();			// Length of from string
5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874
  if (length > max_length)
  {
    ptr=to;
    length=max_length;
    store_length(length);			// Store max length
    ptr=(char*) from;
  }
  else
    memcpy(to,from,packlength);			// Copy length
  if (length)
  {
    get_ptr((char**) &from);
    memcpy(to+packlength, from,length);
  }
  ptr=save;					// Restore org row pointer
  return to+packlength+length;
}


const char *Field_blob::unpack(char *to, const char *from)
{
  memcpy(to,from,packlength);
5875
  uint32 length=get_length(from);
5876 5877 5878 5879 5880 5881 5882 5883
  from+=packlength;
  if (length)
    memcpy_fixed(to+packlength, &from, sizeof(from));
  else
    bzero(to+packlength,sizeof(from));
  return from+length;
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899
/* Keys for blobs are like keys on varchars */

int Field_blob::pack_cmp(const char *a, const char *b, uint key_length)
{
  uint a_length;
  uint b_length;
  if (key_length > 255)
  {
    a_length=uint2korr(a); a+=2;
    b_length=uint2korr(b); b+=2;
  }
  else
  {
    a_length= (uint) (uchar) *a++;
    b_length= (uint) (uchar) *b++;
  }
5900
  return my_strnncoll(field_charset,
5901 5902
		     (const uchar*) a, a_length,
		     (const uchar*) b, b_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5903 5904
}

5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922

int Field_blob::pack_cmp(const char *b, uint key_length)
{
  char *a;
  memcpy_fixed(&a,ptr+packlength,sizeof(char*));
  if (!a)
    return key_length > 0 ? -1 : 0;
  uint a_length=get_length(ptr);
  uint b_length;

  if (key_length > 255)
  {
    b_length=uint2korr(b); b+=2;
  }
  else
  {
    b_length= (uint) (uchar) *b++;
  }
5923
  return my_strnncoll(field_charset,
5924 5925
		     (const uchar*) a, a_length,
		     (const uchar*) b, b_length);
5926 5927
}

5928
/* Create a packed key that will be used for storage from a MySQL row */
5929

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5930 5931
char *Field_blob::pack_key(char *to, const char *from, uint max_length)
{
5932 5933
  char *save=ptr;
  ptr=(char*) from;
5934
  uint32 length=get_length();			// Length of from string
5935 5936 5937 5938 5939 5940 5941
  uint char_length= (field_charset->mbmaxlen > 1) ?
              max_length/field_charset->mbmaxlen : max_length;
  if (length)
    get_ptr((char**) &from);
  if (length > char_length)
    char_length= my_charpos(field_charset, from, from+length, char_length);
  set_if_smaller(length, char_length);
5942 5943 5944
  *to++= (uchar) length;
  if (max_length > 255)				// 2 byte length
    *to++= (uchar) (length >> 8);
5945
  memcpy(to, from, length);
5946 5947 5948 5949
  ptr=save;					// Restore org row pointer
  return to+length;
}

5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993

/*
  Unpack a blob key into a record buffer.

  SYNOPSIS
    Field_blob::unpack_key()
    to                          Pointer into the record buffer.
    from                        Pointer to the packed key.
    max_length                  Key length limit from key description.

  DESCRIPTION
    A blob key has a maximum size of 64K-1.
    In its packed form, the length field is one or two bytes long,
    depending on 'max_length'.
    Depending on the maximum length of a blob, its length field is
    put into 1 to 4 bytes. This is a property of the blob object,
    described by 'packlength'.
    Blobs are internally stored apart from the record buffer, which
    contains a pointer to the blob buffer.

  RETURN
    Pointer into 'from' past the last byte copied from packed key.
*/

const char *Field_blob::unpack_key(char *to, const char *from, uint max_length)
{
  /* get length of the blob key */
  uint32 length= *((uchar*) from++);
  if (max_length > 255)
    length+= (*((uchar*) from++)) << 8;

  /* put the length into the record buffer */
  put_length(to, length);

  /* put the address of the blob buffer or NULL */
  if (length)
    memcpy_fixed(to + packlength, &from, sizeof(from));
  else
    bzero(to + packlength, sizeof(from));

  /* point to first byte of next field in 'from' */
  return from + length;
}

5994 5995 5996 5997 5998 5999
/* Create a packed key that will be used for storage from a MySQL key */

char *Field_blob::pack_key_from_key_image(char *to, const char *from,
					  uint max_length)
{
  uint length=uint2korr(from);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6000 6001
  if (length > max_length)
    length=max_length;
6002
  *to++= (char) (length & 255);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6003
  if (max_length > 255)
6004
    *to++= (char) (length >> 8);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6005
  if (length)
6006
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6007 6008 6009
  return to+length;
}

6010
uint Field_blob::packed_col_length(const char *data_ptr, uint length)
6011 6012
{
  if (length > 255)
6013
    return uint2korr(data_ptr)+2;
6014
  else
6015
    return (uint) ((uchar) *data_ptr)+1;
6016 6017 6018 6019 6020 6021
}

uint Field_blob::max_packed_col_length(uint max_length)
{
  return (max_length > 255 ? 2 : 1)+max_length;
}
6022

6023

hf@deer.(none)'s avatar
hf@deer.(none) committed
6024
#ifdef HAVE_SPATIAL
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
6025 6026 6027 6028 6029

void Field_geom::get_key_image(char *buff, uint length, CHARSET_INFO *cs,
			       imagetype type)
{
  char *blob;
6030
  const char *dummy;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
6031
  MBR mbr;
6032 6033 6034
  ulong blob_length= get_length(ptr);
  Geometry_buffer buffer;
  Geometry *gobj;
6035 6036 6037 6038 6039 6040 6041

  if (blob_length < SRID_SIZE)
  {
    bzero(buff, SIZEOF_STORED_DOUBLE*4);
    return;
  }
  get_ptr(&blob);
6042 6043 6044
  gobj= Geometry::create_from_wkb(&buffer,
				  blob + SRID_SIZE, blob_length - SRID_SIZE);
  if (gobj->get_mbr(&mbr, &dummy))
6045 6046 6047 6048 6049 6050 6051 6052
    bzero(buff, SIZEOF_STORED_DOUBLE*4);
  else
  {
    float8store(buff, mbr.xmin);
    float8store(buff + 8, mbr.xmax);
    float8store(buff + 16, mbr.ymin);
    float8store(buff + 24, mbr.ymax);
  }
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
6053 6054 6055 6056 6057 6058 6059 6060 6061 6062
}


void Field_geom::set_key_image(char *buff, uint length, CHARSET_INFO *cs)
{
  Field_blob::set_key_image(buff, length, cs);
}

void Field_geom::sql_type(String &res) const
{
6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089
  CHARSET_INFO *cs= &my_charset_latin1;
  switch (geom_type)
  {
    case GEOM_POINT:
     res.set("point", 5, cs);
     break;
    case GEOM_LINESTRING:
     res.set("linestring", 10, cs);
     break;
    case GEOM_POLYGON:
     res.set("polygon", 7, cs);
     break;
    case GEOM_MULTIPOINT:
     res.set("multipoint", 10, cs);
     break;
    case GEOM_MULTILINESTRING:
     res.set("multilinestring", 15, cs);
     break;
    case GEOM_MULTIPOLYGON:
     res.set("multipolygon", 12, cs);
     break;
    case GEOM_GEOMETRYCOLLECTION:
     res.set("geometrycollection", 18, cs);
     break;
    default:
     res.set("geometry", 8, cs);
  }
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
6090 6091 6092 6093 6094 6095 6096 6097 6098
}


int Field_geom::store(const char *from, uint length, CHARSET_INFO *cs)
{
  if (!length)
    bzero(ptr, Field_blob::pack_length());
  else
  {
6099 6100 6101 6102 6103
    // Check given WKB
    uint32 wkb_type;
    if (length < SRID_SIZE + WKB_HEADER_SIZE + SIZEOF_STORED_DOUBLE*2)
      goto err;
    wkb_type= uint4korr(from + WKB_HEADER_SIZE);
6104
    if (wkb_type < (uint32) Geometry::wkb_point ||
6105
	wkb_type > (uint32) Geometry::wkb_end)
6106
      return -1;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
6107 6108 6109 6110 6111 6112 6113 6114 6115
    Field_blob::store_length(length);
    if (table->copy_blobs || length <= MAX_FIELD_WIDTH)
    {						// Must make a copy
      value.copy(from, length, cs);
      from= value.ptr();
    }
    bmove(ptr + packlength, (char*) &from, sizeof(char*));
  }
  return 0;
6116 6117 6118

err:
  bzero(ptr, Field_blob::pack_length());  
6119
  return -1;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
6120 6121
}

hf@deer.(none)'s avatar
hf@deer.(none) committed
6122
#endif /*HAVE_SPATIAL*/
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
6123

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183
/****************************************************************************
** enum type.
** This is a string which only can have a selection of different values.
** If one uses this string in a number context one gets the type number.
****************************************************************************/

enum ha_base_keytype Field_enum::key_type() const
{
  switch (packlength) {
  default: return HA_KEYTYPE_BINARY;
  case 2: return HA_KEYTYPE_USHORT_INT;
  case 3: return HA_KEYTYPE_UINT24;
  case 4: return HA_KEYTYPE_ULONG_INT;
  case 8: return HA_KEYTYPE_ULONGLONG;
  }
}

void Field_enum::store_type(ulonglong value)
{
  switch (packlength) {
  case 1: ptr[0]= (uchar) value;  break;
  case 2:
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,(unsigned short) value);
  }
  else
#endif
    shortstore(ptr,(unsigned short) value);
  break;
  case 3: int3store(ptr,(long) value); break;
  case 4:
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,value);
  }
  else
#endif
    longstore(ptr,(long) value);
  break;
  case 8:
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,value);
  }
  else
#endif
    longlongstore(ptr,value); break;
  }
}


/*
** Note. Storing a empty string in a enum field gives a warning
** (if there isn't a empty value in the enum)
*/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6184
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6185
{
6186
  int err= 0;
6187
  uint32 not_used;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6188 6189
  char buff[80];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
6190

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6191
  /* Convert character set if nesessary */
6192
  if (String::needs_conversion(length, cs, field_charset, &not_used))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6193
  { 
6194 6195
    uint dummy_errors;
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6196 6197 6198
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
6199 6200

  /* Remove end space */
6201
  length= field_charset->cset->lengthsp(field_charset, from, length);
6202
  uint tmp=find_type2(typelib, from, length, field_charset);
6203
  if (!tmp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6204
  {
6205
    if (length < 6) // Can't be more than 99999 enums
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6206
    {
6207
      /* This is for reading numbers with LOAD DATA INFILE */
6208
      char *end;
6209 6210
      tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
      if (err || end != from+length || tmp > typelib->count)
6211 6212
      {
	tmp=0;
6213
	set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
6214
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6215 6216
    }
    else
6217
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6218
  }
6219
  store_type((ulonglong) tmp);
6220
  return err;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6221 6222 6223
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6224
int Field_enum::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6225
{
6226
  return Field_enum::store((longlong) nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6227 6228 6229
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6230
int Field_enum::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6231
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6232
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6233 6234
  if ((uint) nr > typelib->count || nr == 0)
  {
6235
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6236
    nr=0;
6237
    error=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6238 6239
  }
  store_type((ulonglong) (uint) nr);
6240
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299
}


double Field_enum::val_real(void)
{
  return (double) Field_enum::val_int();
}


longlong Field_enum::val_int(void)
{
  switch (packlength) {
  case 1:
    return (longlong) (uchar) ptr[0];
  case 2:
    {
      uint16 tmp;
#ifdef WORDS_BIGENDIAN
      if (table->db_low_byte_first)
	tmp=sint2korr(ptr);
      else
#endif
	shortget(tmp,ptr);
      return (longlong) tmp;
    }
  case 3:
    return (longlong) uint3korr(ptr);
  case 4:
    {
      uint32 tmp;
#ifdef WORDS_BIGENDIAN
      if (table->db_low_byte_first)
	tmp=uint4korr(ptr);
      else
#endif
	longget(tmp,ptr);
      return (longlong) tmp;
    }
  case 8:
    {
      longlong tmp;
#ifdef WORDS_BIGENDIAN
      if (table->db_low_byte_first)
	tmp=sint8korr(ptr);
      else
#endif
	longlongget(tmp,ptr);
      return tmp;
    }
  }
  return 0;					// impossible
}


String *Field_enum::val_str(String *val_buffer __attribute__((unused)),
			    String *val_ptr)
{
  uint tmp=(uint) Field_enum::val_int();
  if (!tmp || tmp > typelib->count)
monty@mysql.com's avatar
monty@mysql.com committed
6300
    val_ptr->set("", 0, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6301 6302
  else
    val_ptr->set((const char*) typelib->type_names[tmp-1],
6303
		 typelib->type_lengths[tmp-1],
6304
		 field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332
  return val_ptr;
}

int Field_enum::cmp(const char *a_ptr, const char *b_ptr)
{
  char *old=ptr;
  ptr=(char*) a_ptr;
  ulonglong a=Field_enum::val_int();
  ptr=(char*) b_ptr;
  ulonglong b=Field_enum::val_int();
  ptr=old;
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_enum::sort_string(char *to,uint length __attribute__((unused)))
{
  ulonglong value=Field_enum::val_int();
  to+=packlength-1;
  for (uint i=0 ; i < packlength ; i++)
  {
    *to-- = (uchar) (value & 255);
    value>>=8;
  }
}


void Field_enum::sql_type(String &res) const
{
6333 6334 6335
  char buffer[255];
  String enum_item(buffer, sizeof(buffer), res.charset());

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6336 6337 6338 6339
  res.length(0);
  res.append("enum(");

  bool flag=0;
6340 6341
  uint *len= typelib->type_lengths;
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6342
  {
6343
    uint dummy_errors;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6344 6345
    if (flag)
      res.append(',');
6346
    /* convert to res.charset() == utf8, then quote */
6347
    enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
6348 6349
    append_unescaped(&res, enum_item.ptr(), enum_item.length());
    flag= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6350 6351 6352 6353 6354
  }
  res.append(')');
}


6355 6356 6357 6358 6359 6360 6361 6362
/*
   set type.
   This is a string which can have a collection of different values.
   Each string value is separated with a ','.
   For example "One,two,five"
   If one uses this string in a number context one gets the bits as a longlong
   number.
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6363 6364


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6365
int Field_set::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6366
{
monty@mysql.com's avatar
monty@mysql.com committed
6367
  bool got_warning= 0;
6368
  int err= 0;
6369 6370
  char *not_used;
  uint not_used2;
6371
  uint32 not_used_offset;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6372 6373
  char buff[80];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6374

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6375
  /* Convert character set if nesessary */
6376
  if (String::needs_conversion(length, cs, field_charset, &not_used_offset))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6377
  { 
6378 6379
    uint dummy_errors;
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6380 6381 6382
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
6383 6384
  ulonglong tmp= find_set(typelib, from, length, field_charset,
                          &not_used, &not_used2, &got_warning);
6385 6386 6387
  if (!tmp && length && length < 22)
  {
    /* This is for reading numbers with LOAD DATA INFILE */
6388
    char *end;
6389 6390
    tmp=my_strntoull(cs,from,length,10,&end,&err);
    if (err || end != from+length ||
6391
	tmp > (ulonglong) (((longlong) 1 << typelib->count) - (longlong) 1))
6392
    {
6393
      tmp=0;      
6394
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
6395
    }
6396
  }
monty@mysql.com's avatar
monty@mysql.com committed
6397
  else if (got_warning)
6398
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
6399
  store_type(tmp);
6400
  return err;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6401 6402 6403
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6404
int Field_set::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6405
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6406
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6407 6408 6409
  if ((ulonglong) nr > (ulonglong) (((longlong) 1 << typelib->count) -
				    (longlong) 1))
  {
6410
    nr&= (longlong) (((longlong) 1 << typelib->count) - (longlong) 1);    
6411
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
6412
    error=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6413 6414
  }
  store_type((ulonglong) nr);
6415
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6416 6417 6418 6419 6420 6421 6422 6423 6424 6425
}


String *Field_set::val_str(String *val_buffer,
			   String *val_ptr __attribute__((unused)))
{
  ulonglong tmp=(ulonglong) Field_enum::val_int();
  uint bitnr=0;

  val_buffer->length(0);
6426
  val_buffer->set_charset(field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6427 6428 6429 6430 6431
  while (tmp && bitnr < (uint) typelib->count)
  {
    if (tmp & 1)
    {
      if (val_buffer->length())
6432
	val_buffer->append(&field_separator, 1, &my_charset_latin1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6433
      String str(typelib->type_names[bitnr],
6434
		 typelib->type_lengths[bitnr],
6435
		 field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446
      val_buffer->append(str);
    }
    tmp>>=1;
    bitnr++;
  }
  return val_buffer;
}


void Field_set::sql_type(String &res) const
{
6447 6448 6449
  char buffer[255];
  String set_item(buffer, sizeof(buffer), res.charset());

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6450 6451 6452 6453
  res.length(0);
  res.append("set(");

  bool flag=0;
6454 6455
  uint *len= typelib->type_lengths;
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6456
  {
6457
    uint dummy_errors;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6458 6459
    if (flag)
      res.append(',');
6460
    /* convert to res.charset() == utf8, then quote */
6461
    set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
6462 6463
    append_unescaped(&res, set_item.ptr(), set_item.length());
    flag= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6464 6465 6466 6467 6468 6469 6470 6471
  }
  res.append(')');
}

/* returns 1 if the fields are equally defined */

bool Field::eq_def(Field *field)
{
6472
  if (real_type() != field->real_type() || charset() != field->charset() ||
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6473 6474 6475 6476 6477 6478 6479 6480 6481 6482
      pack_length() != field->pack_length())
    return 0;
  return 1;
}

bool Field_enum::eq_def(Field *field)
{
  if (!Field::eq_def(field))
    return 0;
  TYPELIB *from_lib=((Field_enum*) field)->typelib;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6483

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6484 6485 6486
  if (typelib->count < from_lib->count)
    return 0;
  for (uint i=0 ; i < from_lib->count ; i++)
6487 6488 6489 6490 6491
    if (my_strnncoll(field_charset,
                     (const uchar*)typelib->type_names[i],
                     strlen(typelib->type_names[i]),
                     (const uchar*)from_lib->type_names[i],
                     strlen(from_lib->type_names[i])))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6492 6493 6494 6495 6496
      return 0;
  return 1;
}

bool Field_num::eq_def(Field *field)
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6497
{
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513
  if (!Field::eq_def(field))
    return 0;
  Field_num *from_num= (Field_num*) field;

  if (unsigned_flag != from_num->unsigned_flag ||
      zerofill && !from_num->zerofill && !zero_pack() ||
      dec != from_num->dec)
    return 0;
  return 1;
}


/*****************************************************************************
** Handling of field and create_field
*****************************************************************************/

6514 6515
void create_field::create_length_to_internal_length(void)
{
6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533
  switch (sql_type) {
  case MYSQL_TYPE_TINY_BLOB:
  case MYSQL_TYPE_MEDIUM_BLOB:
  case MYSQL_TYPE_LONG_BLOB:
  case MYSQL_TYPE_BLOB:
  case MYSQL_TYPE_VAR_STRING:
  case MYSQL_TYPE_STRING:
    length*= charset->mbmaxlen;
    pack_length= calc_pack_length(sql_type == FIELD_TYPE_VAR_STRING ?
                                  FIELD_TYPE_STRING : sql_type, length);
    break;
  case MYSQL_TYPE_ENUM:
  case MYSQL_TYPE_SET:
    length*= charset->mbmaxlen;
    break;
  default:
    /* do nothing */
    break;
6534 6535 6536
  }
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6537
/*
6538
  Make a field from the .frm file info
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6539 6540 6541 6542 6543 6544 6545
*/

uint32 calc_pack_length(enum_field_types type,uint32 length)
{
  switch (type) {
  case FIELD_TYPE_STRING:
  case FIELD_TYPE_DECIMAL: return (length);
6546
  case FIELD_TYPE_VAR_STRING: return (length+HA_KEY_BLOB_LENGTH);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561
  case FIELD_TYPE_YEAR:
  case FIELD_TYPE_TINY	: return 1;
  case FIELD_TYPE_SHORT : return 2;
  case FIELD_TYPE_INT24:
  case FIELD_TYPE_NEWDATE:
  case FIELD_TYPE_TIME:   return 3;
  case FIELD_TYPE_TIMESTAMP:
  case FIELD_TYPE_DATE:
  case FIELD_TYPE_LONG	: return 4;
  case FIELD_TYPE_FLOAT : return sizeof(float);
  case FIELD_TYPE_DOUBLE: return sizeof(double);
  case FIELD_TYPE_DATETIME:
  case FIELD_TYPE_LONGLONG: return 8;	/* Don't crash if no longlong */
  case FIELD_TYPE_NULL	: return 0;
  case FIELD_TYPE_TINY_BLOB:	return 1+portable_sizeof_char_ptr;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6562
  case FIELD_TYPE_BLOB:		return 2+portable_sizeof_char_ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6563 6564
  case FIELD_TYPE_MEDIUM_BLOB:	return 3+portable_sizeof_char_ptr;
  case FIELD_TYPE_LONG_BLOB:	return 4+portable_sizeof_char_ptr;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6565
  case FIELD_TYPE_GEOMETRY:	return 4+portable_sizeof_char_ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6566 6567
  case FIELD_TYPE_SET:
  case FIELD_TYPE_ENUM: abort(); return 0;	// This shouldn't happen
6568
  default: return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6569
  }
6570
  return 0;					// Keep compiler happy
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587
}


uint pack_length_to_packflag(uint type)
{
  switch (type) {
    case 1: return f_settype((uint) FIELD_TYPE_TINY);
    case 2: return f_settype((uint) FIELD_TYPE_SHORT);
    case 3: return f_settype((uint) FIELD_TYPE_INT24);
    case 4: return f_settype((uint) FIELD_TYPE_LONG);
    case 8: return f_settype((uint) FIELD_TYPE_LONGLONG);
  }
  return 0;					// This shouldn't happen
}


Field *make_field(char *ptr, uint32 field_length,
6588
		  uchar *null_pos, uchar null_bit,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6589
		  uint pack_flag,
6590
		  enum_field_types field_type,
6591
		  CHARSET_INFO *field_charset,
6592
		  Field::geometry_type geom_type,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6593 6594 6595 6596 6597 6598 6599 6600 6601 6602
		  Field::utype unireg_check,
		  TYPELIB *interval,
		  const char *field_name,
		  struct st_table *table)
{
  if (!f_maybe_null(pack_flag))
  {
    null_pos=0;
    null_bit=0;
  }
6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614

  switch (field_type)
  {
    case FIELD_TYPE_DATE:
    case FIELD_TYPE_NEWDATE:
    case FIELD_TYPE_TIME:
    case FIELD_TYPE_DATETIME:
    case FIELD_TYPE_TIMESTAMP:
      field_charset= &my_charset_bin;
    default: break;
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6615 6616 6617
  if (f_is_alpha(pack_flag))
  {
    if (!f_is_packed(pack_flag))
monty@mysql.com's avatar
monty@mysql.com committed
6618 6619
    {
      if (field_type == FIELD_TYPE_STRING ||
6620
          field_type == FIELD_TYPE_DECIMAL ||   // 3.23 or 4.0 string
monty@mysql.com's avatar
monty@mysql.com committed
6621 6622 6623 6624 6625 6626
          field_type == FIELD_TYPE_VAR_STRING)
        return new Field_string(ptr,field_length,null_pos,null_bit,
                                unireg_check, field_name, table,
                                field_charset);
      return 0;                                 // Error
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6627 6628 6629 6630 6631

    uint pack_length=calc_pack_length((enum_field_types)
				      f_packtype(pack_flag),
				      field_length);

hf@deer.(none)'s avatar
hf@deer.(none) committed
6632
#ifdef HAVE_SPATIAL
6633 6634
    if (f_is_geom(pack_flag))
      return new Field_geom(ptr,null_pos,null_bit,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6635
			    unireg_check, field_name, table,
6636
			    pack_length, geom_type);
hf@deer.(none)'s avatar
hf@deer.(none) committed
6637
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6638 6639 6640
    if (f_is_blob(pack_flag))
      return new Field_blob(ptr,null_pos,null_bit,
			    unireg_check, field_name, table,
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
6641
			    pack_length, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6642 6643 6644 6645 6646
    if (interval)
    {
      if (f_is_enum(pack_flag))
	return new Field_enum(ptr,field_length,null_pos,null_bit,
				  unireg_check, field_name, table,
6647
				  pack_length, interval, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6648 6649 6650
      else
	return new Field_set(ptr,field_length,null_pos,null_bit,
			     unireg_check, field_name, table,
6651
			     pack_length, interval, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6652 6653 6654
    }
  }

6655
  switch (field_type) {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699
  case FIELD_TYPE_DECIMAL:
    return new Field_decimal(ptr,field_length,null_pos,null_bit,
			     unireg_check, field_name, table,
			     f_decimals(pack_flag),
			     f_is_zerofill(pack_flag) != 0,
			     f_is_dec(pack_flag) == 0);
  case FIELD_TYPE_FLOAT:
    return new Field_float(ptr,field_length,null_pos,null_bit,
			   unireg_check, field_name, table,
			   f_decimals(pack_flag),
			   f_is_zerofill(pack_flag) != 0,
			   f_is_dec(pack_flag)== 0);
  case FIELD_TYPE_DOUBLE:
    return new Field_double(ptr,field_length,null_pos,null_bit,
			    unireg_check, field_name, table,
			    f_decimals(pack_flag),
			    f_is_zerofill(pack_flag) != 0,
			    f_is_dec(pack_flag)== 0);
  case FIELD_TYPE_TINY:
    return new Field_tiny(ptr,field_length,null_pos,null_bit,
			  unireg_check, field_name, table,
			  f_is_zerofill(pack_flag) != 0,
			  f_is_dec(pack_flag) == 0);
  case FIELD_TYPE_SHORT:
    return new Field_short(ptr,field_length,null_pos,null_bit,
			   unireg_check, field_name, table,
			   f_is_zerofill(pack_flag) != 0,
			   f_is_dec(pack_flag) == 0);
  case FIELD_TYPE_INT24:
    return new Field_medium(ptr,field_length,null_pos,null_bit,
			    unireg_check, field_name, table,
			    f_is_zerofill(pack_flag) != 0,
			    f_is_dec(pack_flag) == 0);
  case FIELD_TYPE_LONG:
    return new Field_long(ptr,field_length,null_pos,null_bit,
			   unireg_check, field_name, table,
			   f_is_zerofill(pack_flag) != 0,
			   f_is_dec(pack_flag) == 0);
  case FIELD_TYPE_LONGLONG:
    return new Field_longlong(ptr,field_length,null_pos,null_bit,
			      unireg_check, field_name, table,
			      f_is_zerofill(pack_flag) != 0,
			      f_is_dec(pack_flag) == 0);
  case FIELD_TYPE_TIMESTAMP:
6700 6701 6702
    return new Field_timestamp(ptr,field_length, null_pos, null_bit,
                               unireg_check, field_name, table,
                               field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6703 6704 6705 6706 6707
  case FIELD_TYPE_YEAR:
    return new Field_year(ptr,field_length,null_pos,null_bit,
			  unireg_check, field_name, table);
  case FIELD_TYPE_DATE:
    return new Field_date(ptr,null_pos,null_bit,
6708
			  unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6709 6710
  case FIELD_TYPE_NEWDATE:
    return new Field_newdate(ptr,null_pos,null_bit,
6711
			     unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6712 6713
  case FIELD_TYPE_TIME:
    return new Field_time(ptr,null_pos,null_bit,
6714
			  unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6715 6716
  case FIELD_TYPE_DATETIME:
    return new Field_datetime(ptr,null_pos,null_bit,
6717
			      unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6718
  case FIELD_TYPE_NULL:
6719
    return new Field_null(ptr,field_length,unireg_check,field_name,table, field_charset);
6720 6721
  default:					// Impossible (Wrong version)
    break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6722
  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6723
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6724 6725 6726 6727 6728
}


/* Create a field suitable for create of table */

6729
create_field::create_field(Field *old_field,Field *orig_field)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6730 6731 6732 6733 6734 6735 6736 6737
{
  field=      old_field;
  field_name=change=old_field->field_name;
  length=     old_field->field_length;
  flags=      old_field->flags;
  unireg_check=old_field->unireg_check;
  pack_length=old_field->pack_length();
  sql_type=   old_field->real_type();
6738 6739
  charset=    old_field->charset();		// May be NULL ptr
  comment=    old_field->comment;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6740 6741 6742 6743 6744

  /* Fix if the original table had 4 byte pointer blobs */
  if (flags & BLOB_FLAG)
    pack_length= (pack_length- old_field->table->blob_ptr_size +
		  portable_sizeof_char_ptr);
6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755

  switch (sql_type)
  {
    case FIELD_TYPE_BLOB:
      switch (pack_length - portable_sizeof_char_ptr)
      {
        case  1: sql_type= FIELD_TYPE_TINY_BLOB; break;
        case  2: sql_type= FIELD_TYPE_BLOB; break;
        case  3: sql_type= FIELD_TYPE_MEDIUM_BLOB; break;
        default: sql_type= FIELD_TYPE_LONG_BLOB; break;
      }
6756
      length=(length+charset->mbmaxlen-1)/charset->mbmaxlen; // QQ: Probably not needed
6757
      break;
6758 6759
    case MYSQL_TYPE_ENUM:
    case MYSQL_TYPE_SET:
6760 6761
    case FIELD_TYPE_STRING:
    case FIELD_TYPE_VAR_STRING:
6762
      length=(length+charset->mbmaxlen-1)/charset->mbmaxlen;
6763 6764 6765 6766
      break;
    default:
      break;
  }
6767

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6768 6769 6770
  decimals= old_field->decimals();
  if (sql_type == FIELD_TYPE_STRING)
  {
6771
    /* Change CHAR -> VARCHAR if dynamic record length */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6772 6773 6774 6775 6776 6777 6778
    sql_type=old_field->type();
    decimals=0;
  }
  if (flags & (ENUM_FLAG | SET_FLAG))
    interval= ((Field_enum*) old_field)->typelib;
  else
    interval=0;
6779
  def=0;
6780
  if (!old_field->is_real_null() && ! (flags & BLOB_FLAG) &&
6781
      old_field->ptr && orig_field)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6782 6783
  {
    char buff[MAX_FIELD_WIDTH],*pos;
6784
    String tmp(buff,sizeof(buff), charset);
6785

6786
    /* Get the value from default_values */
6787
    my_ptrdiff_t diff= (my_ptrdiff_t) (orig_field->table->rec_buff_length*2);
6788
    orig_field->move_field(diff);		// Points now at default_values
6789
    bool is_null=orig_field->is_real_null();
6790
    orig_field->val_str(&tmp);
6791
    orig_field->move_field(-diff);		// Back to record[0]
6792
    if (!is_null)
6793 6794 6795
    {
      pos= (char*) sql_memdup(tmp.ptr(),tmp.length()+1);
      pos[tmp.length()]=0;
6796
      def= new Item_string(pos, tmp.length(), charset);
6797
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6798
  }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6799
#ifdef HAVE_SPATIAL
6800 6801 6802 6803
  if (sql_type == FIELD_TYPE_GEOMETRY)
  {
    geom_type= ((Field_geom*)old_field)->geom_type;
  }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6804
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6805
}
6806 6807 6808


/* Warning handling */
6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828

/*
  Produce warning or note about data saved into field

  SYNOPSYS
    set_warning()
      level            - level of message (Note/Warning/Error)
      code             - error code of message to be produced
      cuted_increment  - whenever we should increase cut fields count or not

  NOTE
    This function won't produce warning and increase cut fields counter
    if count_cuted_fields == FIELD_CHECK_IGNORE for current thread.

  RETURN VALUE
    true  - if count_cuted_fields == FIELD_CHECK_IGNORE
    false - otherwise
*/
bool 
Field::set_warning(const uint level, const uint code, int cuted_increment)
6829
{
6830
  THD *thd= table->in_use;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6831 6832
  if (thd->count_cuted_fields)
  {
6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924
    thd->cuted_fields+= cuted_increment;
    push_warning_printf(thd, (MYSQL_ERROR::enum_warning_level) level,
                        code, ER(code), field_name, thd->row_count);
    return 0;
  }
  return 1;
}


/*
  Produce warning or note about datetime string data saved into field

  SYNOPSYS
    set_warning()
      level            - level of message (Note/Warning/Error)
      code             - error code of message to be produced
      str              - string value which we tried to save
      str_len          - length of string which we tried to save
      ts_type          - type of datetime value (datetime/date/time)
      cuted_increment  - whenever we should increase cut fields count or not
  
  NOTE
    This function will always produce some warning but won't increase cut 
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current 
    thread.
*/
void 
Field::set_datetime_warning(const uint level, const uint code, 
                            const char *str, uint str_length, 
                            timestamp_type ts_type, int cuted_increment)
{
  if (set_warning(level, code, cuted_increment))
    make_truncated_value_warning(table->in_use, str, str_length, ts_type);
}


/*
  Produce warning or note about integer datetime value saved into field

  SYNOPSYS
    set_warning()
      level            - level of message (Note/Warning/Error)
      code             - error code of message to be produced
      nr               - numeric value which we tried to save
      ts_type          - type of datetime value (datetime/date/time)
      cuted_increment  - whenever we should increase cut fields count or not
  
  NOTE
    This function will always produce some warning but won't increase cut 
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current 
    thread.
*/
void 
Field::set_datetime_warning(const uint level, const uint code, 
                            longlong nr, timestamp_type ts_type,
                            int cuted_increment)
{
  if (set_warning(level, code, cuted_increment))
  {
    char str_nr[22];
    char *str_end= longlong10_to_str(nr, str_nr, -10);
    make_truncated_value_warning(table->in_use, str_nr, str_end - str_nr, 
                                 ts_type);
  }
}


/*
  Produce warning or note about double datetime data saved into field

  SYNOPSYS
    set_warning()
      level            - level of message (Note/Warning/Error)
      code             - error code of message to be produced
      nr               - double value which we tried to save
      ts_type          - type of datetime value (datetime/date/time)
  
  NOTE
    This function will always produce some warning but won't increase cut 
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current 
    thread.
*/
void 
Field::set_datetime_warning(const uint level, const uint code, 
                            double nr, timestamp_type ts_type)
{
  if (set_warning(level, code, 1))
  {
    /* DBL_DIG is enough to print '-[digits].E+###' */
    char str_nr[DBL_DIG + 8];
    uint str_len= my_sprintf(str_nr, (str_nr, "%g", nr));
    make_truncated_value_warning(table->in_use, str_nr, str_len, ts_type);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6925
  }
6926
}
6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947

/*
  maximum possible display length for blob

  SYNOPSIS
    Field_blob::max_length()

  RETURN
    length
*/
uint32 Field_blob::max_length()
{
  switch (packlength)
  {
  case 1:
    return 255;
  case 2:
    return 65535;
  case 3:
    return 16777215;
  case 4:
6948
    return (uint32) 4294967295U;
6949 6950 6951 6952 6953
  default:
    DBUG_ASSERT(0); // we should never go here
    return 0;
  }
}