~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.cc

  • Committer: Stewart Smith
  • Date: 2010-07-27 00:49:32 UTC
  • mto: (1720.1.1 drizzle)
  • mto: This revision was merged to the branch mainline in revision 1721.
  • Revision ID: stewart@flamingspork.com-20100727004932-basq3vx9szmmbswm
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
#include "drizzled/decimal.h"
41
41
#include "drizzled/calendar.h"
42
42
#include "drizzled/temporal.h"
43
 
#ifdef NOTYETIMPLEMENTED
44
 
#include "drizzled/temporal_interval.h"
45
 
#endif
46
43
#include "drizzled/temporal_format.h"
47
44
#include "drizzled/time_functions.h"
48
45
#include "time.h"
49
46
 
50
47
#include <time.h>
51
48
 
 
49
#include <cstdio>
52
50
#include <ostream>
53
51
#include <iomanip>
54
52
#include <vector>
795
793
 
796
794
  return *this;
797
795
}
798
 
#ifdef NOTYETIMPLEMENTED
799
 
Date& Date::operator+=(const TemporalIntervalYear &rhs)
800
 
{
801
 
  /* Simple one...add the years and adjust for any leaps */
802
 
  int64_t new_years= _years;
803
 
  new_years+= rhs._years;
804
 
  if (new_years > DRIZZLE_MAX_YEARS_SQL)
805
 
  {
806
 
    /* 
807
 
     * Set everything to zero. We got an overflow.
808
 
     * @TODO Exceptions would be great here...
809
 
     */
810
 
    _reset();
811
 
    _overflow= true;
812
 
    return *this;
813
 
  }
814
 
  _years= (uint32_t) new_years;
815
 
  if (_months == 2 && _days == 29 && days_in_gregorian_year_month(_years, _months) != 366)
816
 
    _days= 28;
817
 
  return *this;
818
 
819
 
 
820
 
Date& Date::operator-=(const TemporalIntervalYear &rhs)
821
 
{
822
 
  /* Simple one...subtract the years and adjust for any leaps */
823
 
  int64_t new_years= _years;
824
 
  new_years-= rhs._years;
825
 
  if (new_years < 0)
826
 
  {
827
 
    /* 
828
 
     * Set everything to zero. We got an overflow.
829
 
     * @TODO Exceptions would be great here...
830
 
     */
831
 
    _reset();
832
 
    _overflow= true;
833
 
    return *this;
834
 
  }
835
 
  _years= (uint32_t) new_years;
836
 
  if (_months == 2 && _days == 29 && days_in_gregorian_year_month(_years, _months) != 366)
837
 
    _days= 28;
838
 
  return *this;
839
 
840
 
 
841
 
Date& Date::operator+=(const TemporalIntervalDayOrWeek &rhs)
842
 
{
843
 
  /* Simple one...add the days */
844
 
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, _days) + rhs._days;
845
 
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
846
 
  return *this;
847
 
848
 
 
849
 
Date& Date::operator-=(const TemporalIntervalDayOrWeek &rhs)
850
 
{
851
 
  /* Simple one...subtract the days */
852
 
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, _days) - rhs._days;
853
 
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
854
 
  return *this;
855
 
856
 
 
857
 
Date& Date::operator+=(const TemporalIntervalYearMonth &rhs)
858
 
{
859
 
  /* Simple one...add the months in the period adjust */
860
 
  int64_t period= (_years * 12) + (rhs._years * 12) + (_months - 1) + rhs._months;
861
 
  int64_t new_years= (period / 12);
862
 
  if (new_years > DRIZZLE_MAX_YEARS_SQL)
863
 
  {
864
 
    /* 
865
 
     * Set everything to zero. We got an overflow.
866
 
     * @TODO Exceptions would be great here...
867
 
     */
868
 
    _reset();
869
 
    _overflow= true;
870
 
    return *this;
871
 
  }
872
 
  _years= (uint32_t) new_years;
873
 
  _months= (uint32_t) (period % 12) + 1;
874
 
  
875
 
  /* Adjust day if the new month doesn't have enough days */
876
 
  uint32_t days_in_new_month= days_in_gregorian_year_month(_years, _months);
877
 
  if (_days > days_in_new_month)
878
 
    _days= days_in_new_month;
879
 
  return *this;
880
 
881
 
 
882
 
Date& Date::operator-=(const TemporalIntervalYearMonth &rhs)
883
 
{
884
 
  /* Simple one...subtract the months in the period and adjust */
885
 
  int64_t period= (_years * 12) - (rhs._years * 12) + (_months - 1) - rhs._months;
886
 
  int64_t new_years= (period / 12);
887
 
  if (new_years < 0)
888
 
  {
889
 
    /* 
890
 
     * Set everything to zero. We got an overflow.
891
 
     * @TODO Exceptions would be great here...
892
 
     */
893
 
    _reset();
894
 
    _overflow= true;
895
 
    return *this;
896
 
  }
897
 
  _years= (uint32_t) (period / 12);
898
 
  _months= (uint32_t) (period % 12) + 1;
899
 
  
900
 
  /* Adjust day if the new month doesn't have enough days */
901
 
  uint32_t days_in_new_month= days_in_gregorian_year_month(_years, _months);
902
 
  if (_days > days_in_new_month)
903
 
    _days= days_in_new_month;
904
 
  return *this;
905
 
906
 
 
907
 
Date& Date::operator+=(const TemporalIntervalDayOrLess &rhs)
908
 
{
909
 
  /* 
910
 
   * Convert the temporal and the interval into a number of 
911
 
   * microseconds, then add them together and convert the
912
 
   * resulting microseconds back into a broken-down temporal
913
 
   * component.
914
 
   */
915
 
  int64_t new_seconds;
916
 
  int64_t new_microseconds;
917
 
  int64_t extra_sec;
918
 
  int64_t new_days;
919
 
  new_microseconds= _useconds + rhs._useconds;
920
 
  extra_sec= new_microseconds / INT64_C(1000000);
921
 
  new_microseconds= new_microseconds % INT64_C(1000000);
922
 
 
923
 
  new_seconds= ((_days - 1) * 3600 * 24) + (_hours * 3600) + (_minutes * 60) + _seconds;
924
 
  new_seconds+= (rhs._days * 3600 * 24) + (rhs._hours * 3600) + (rhs._minutes * 60) + rhs._seconds;
925
 
  new_seconds+= extra_sec;
926
 
 
927
 
  if (new_microseconds < 0)
928
 
  {
929
 
    new_microseconds+= INT64_C(1000000);
930
 
    new_seconds--;
931
 
  }
932
 
  
933
 
  new_days= new_seconds / (3600 * 24L);
934
 
  new_seconds-= new_days * 3600 * 24L;
935
 
  if (new_seconds < 0)
936
 
  {
937
 
    new_days--;
938
 
    new_seconds+= 3600 * 24L;
939
 
  }
940
 
  _useconds= (uint32_t) new_microseconds;
941
 
  _seconds= (uint32_t) (new_seconds % 60);
942
 
  _minutes= (uint32_t) ((new_seconds / 60) % 60);
943
 
  _hours= (uint32_t) (new_seconds / 3600);
944
 
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, 1) + new_days;
945
 
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
946
 
  return *this;
947
 
}
948
 
 
949
 
Date& Date::operator-=(const TemporalIntervalDayOrLess &rhs)
950
 
{
951
 
  /* 
952
 
   * Convert the temporal and the interval into a number of 
953
 
   * microseconds, then subtract them from each other and convert 
954
 
   * the resulting microseconds back into a broken-down temporal
955
 
   * component.
956
 
   */
957
 
  int64_t new_seconds;
958
 
  int64_t new_microseconds;
959
 
  int64_t extra_sec;
960
 
  int64_t new_days;
961
 
  new_microseconds= _useconds - rhs._useconds;
962
 
  extra_sec= new_microseconds / INT64_C(1000000);
963
 
  new_microseconds= new_microseconds % INT64_C(1000000);
964
 
 
965
 
  new_seconds= ((_days - 1) * 3600 * 24) + (_hours * 3600) + (_minutes * 60) + _seconds;
966
 
  new_seconds-= (rhs._days * 3600 * 24) + (rhs._hours * 3600) + (rhs._minutes * 60) + rhs._seconds;
967
 
  new_seconds+= extra_sec;
968
 
 
969
 
  if (new_microseconds < 0)
970
 
  {
971
 
    new_microseconds+= INT64_C(1000000);
972
 
    new_seconds--;
973
 
  }
974
 
  
975
 
  new_days= new_seconds / (3600 * 24L);
976
 
  new_seconds-= new_days * 3600 * 24L;
977
 
  if (new_seconds < 0)
978
 
  {
979
 
    new_days--;
980
 
    new_seconds+= 3600 * 24L;
981
 
  }
982
 
  _useconds= (uint32_t) new_microseconds;
983
 
  _seconds= (uint32_t) (new_seconds % 60);
984
 
  _minutes= (uint32_t) ((new_seconds / 60) % 60);
985
 
  _hours= (uint32_t) (new_seconds / 3600);
986
 
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, 1) + new_days;
987
 
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
988
 
  return *this;
989
 
}
990
 
#endif /* NOTYETIMPLEMENTED */
 
796
 
991
797
/*
992
798
 * Comparison operators between a Date and a Timestamp
993
799
 */
1019
825
}
1020
826
bool Date::operator>(const Timestamp& rhs)
1021
827
{
1022
 
  return ! (*this < rhs);
 
828
  return ! (*this <= rhs);
1023
829
}
1024
830
bool Date::operator>=(const Timestamp& rhs)
1025
831
{
1026
 
  return ! (*this <= rhs);
 
832
  return ! (*this < rhs);
1027
833
}
1028
834
/*
1029
835
 * Comparison operators between a Timestamp and a Date
1056
862
}
1057
863
bool Timestamp::operator>(const Date& rhs)
1058
864
{
1059
 
  return ! (*this < rhs);
 
865
  return ! (*this <= rhs);
1060
866
}
1061
867
bool Timestamp::operator>=(const Date& rhs)
1062
868
{
1063
 
  return ! (*this <= rhs);
 
869
  return ! (*this < rhs);
1064
870
}
1065
871
/*
1066
872
 * Comparison operators between a Timestamp and a DateTime
1109
915
}
1110
916
bool Timestamp::operator>(const DateTime& rhs)
1111
917
{
1112
 
  return ! (*this < rhs);
 
918
  return ! (*this <= rhs);
1113
919
}
1114
920
bool Timestamp::operator>=(const DateTime& rhs)
1115
921
{
1116
 
  return ! (*this <= rhs);
 
922
  return ! (*this < rhs);
1117
923
}
1118
924
/*
1119
925
 * Comparison operators between two Timestamps
1136
942
}
1137
943
bool Timestamp::operator>(const Timestamp& rhs)
1138
944
{
1139
 
  return ! (*this < rhs);
 
945
  return ! (*this <= rhs);
1140
946
}
1141
947
bool Timestamp::operator>=(const Timestamp& rhs)
1142
948
{
1143
 
  return ! (*this <= rhs);
 
949
  return ! (*this < rhs);
1144
950
}
1145
951
 
1146
952
/**
1341
1147
bool Time::from_int32_t(const int32_t from)
1342
1148
{
1343
1149
  uint32_t copy_from= (uint32_t) from;
1344
 
  _hours= copy_from % INT32_C(10000);
1345
 
  _minutes= copy_from % INT32_C(100);
1346
 
  _seconds= copy_from & 3; /* Masks off all but last 2 digits */
 
1150
  _hours= copy_from / INT32_C(10000);
 
1151
  _minutes= (copy_from % INT32_C(10000)) / INT32_C(100);
 
1152
  _seconds= copy_from % INT32_C(100); /* Masks off all but last 2 digits */
1347
1153
  return is_valid();
1348
1154
}
1349
1155
 
1382
1188
    else if (copy_from <  DRIZZLE_YY_PART_YEAR * 10000000000LL + 101000000LL)
1383
1189
      return false;
1384
1190
    else if (copy_from <= 991231235959LL)
1385
 
      copy_from= copy_from + 19000000000000LL;          /* YYMMDDHHMMSS, 1970-1999 */
 
1191
      copy_from= copy_from + 19000000000000LL;    /* YYMMDDHHMMSS, 1970-1999 */
1386
1192
  }
1387
1193
 
1388
1194
  part1= (int64_t) (copy_from / 1000000LL);