21
21
This file defines all compare functions
24
#include <drizzled/server_includes.h>
25
#include <drizzled/sql_select.h>
26
#include <drizzled/error.h>
27
#include <drizzled/item/cmpfunc.h>
28
#include <drizzled/cached_item.h>
29
#include <drizzled/item/cache_int.h>
30
#include <drizzled/item/int_with_ref.h>
31
#include <drizzled/function/bit.h>
32
#include <drizzled/check_stack_overrun.h>
24
#include "drizzled/server_includes.h"
25
#include "drizzled/sql_select.h"
26
#include "drizzled/error.h"
27
#include "drizzled/temporal.h"
28
#include "drizzled/item/cmpfunc.h"
29
#include "drizzled/cached_item.h"
30
#include "drizzled/item/cache_int.h"
31
#include "drizzled/item/int_with_ref.h"
32
#include "drizzled/function/bit.h"
33
#include "drizzled/check_stack_overrun.h"
730
731
int result and the other item (b or a) is an item with string result.
731
732
If the second item is a constant one then it's checked to be
732
733
convertible to the DATE/DATETIME type. If the constant can't be
733
converted to a DATE/DATETIME then the compare_datetime() comparator
734
isn't used and the warning about wrong DATE/DATETIME value is issued.
734
converted to a DATE/DATETIME then an error is issued back to the Session.
735
735
In all other cases (date-[int|real|decimal]/[int|real|decimal]-date)
736
736
the comparison is handled by other comparators.
737
738
If the datetime comparator can be used and one the operands of the
738
739
comparison is a string constant that was successfully converted to a
739
740
DATE/DATETIME type then the result of the conversion is returned in the
782
783
(str_arg->type() != Item::FUNC_ITEM ||
783
784
((Item_func*)str_arg)->functype() != Item_func::GUSERVAR_FUNC))
785
Session *session= current_session;
787
* OK, we are here if we've got a date field (or something which can be
788
* compared as a date field) on one side of the equation, and a constant
789
* string on the other side. In this case, we must verify that the constant
790
* string expression can indeed be evaluated as a datetime. If it cannot,
791
* we throw an error here and stop processsing. Bad data should ALWAYS
792
* produce an error, and no implicit conversion or truncation should take place.
794
* If the conversion to a DateTime temporal is successful, then we convert
795
* the Temporal instance to a uint64_t for the comparison operator, which
796
* compares date(times) using int64_t semantics.
800
* Does a uint64_t conversion really have to happen here? Fields return int64_t
801
* from val_int(), not uint64_t...
788
String tmp, *str_val= 0;
789
enum enum_drizzle_timestamp_type t_type= (date_arg->field_type() == DRIZZLE_TYPE_DATE ?
790
DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME);
806
/* DateTime used to pick up as many string conversion possibilities as possible. */
807
drizzled::DateTime temporal;
792
809
str_val= str_arg->val_str(&tmp);
793
if (str_arg->null_value)
794
return CMP_DATE_DFLT;
795
value= get_date_from_str(session, str_val, t_type, date_arg->name, &error);
797
return CMP_DATE_DFLT;
813
* If we are here, it is most likely due to the comparison item
814
* being a NULL. Although this is incorrect (SQL demands that the term IS NULL
815
* be used, not = NULL since no item can be equal to NULL).
817
* So, return gracefully.
819
return CMP_DATE_DFLT;
821
if (! temporal.from_string(str_val->c_ptr(), str_val->length()))
823
/* Chuck an error. Bad datetime input. */
824
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), str_val->c_ptr());
825
return CMP_DATE_DFLT; /* :( What else can I return... */
828
/* String conversion was good. Convert to an integer for comparison purposes. */
830
temporal.to_int64_t(&int_value);
831
value= (uint64_t) int_value;
799
834
*const_value= value;