diff --git a/.bzrignore b/.bzrignore index 82adbf625f3cf1fc956a481a6348cb31de549bdf..b1bde9bc6ed1ad6d3d3d8f4730073ef88e12bcff 100644 --- a/.bzrignore +++ b/.bzrignore @@ -617,3 +617,4 @@ vio/test-sslclient vio/test-sslserver vio/viotest-ssl include/readline/*.h +libmysqld/item_geofunc.cc diff --git a/libmysqld/Makefile.am b/libmysqld/Makefile.am index 93702e5b6d082242c60bfcac78d2fee58aad4c8e..d127112819e4f80155dd81ced64d43f9ff555709 100644 --- a/libmysqld/Makefile.am +++ b/libmysqld/Makefile.am @@ -42,7 +42,7 @@ sqlsources = derror.cc field.cc field_conv.cc filesort.cc \ hostname.cc init.cc password.c \ item.cc item_buff.cc item_cmpfunc.cc item_create.cc \ item_func.cc item_strfunc.cc item_sum.cc item_timefunc.cc \ - item_uniq.cc item_subselect.cc item_row.cc\ + item_geofunc.cc item_uniq.cc item_subselect.cc item_row.cc\ key.cc lock.cc log.cc log_event.cc mf_iocache.cc\ mini_client.cc protocol.cc net_serv.cc opt_ft.cc opt_range.cc \ opt_sum.cc procedure.cc records.cc sql_acl.cc \ diff --git a/sql/Makefile.am b/sql/Makefile.am index 608b959a8b9f3ecfe253e70cd69a223c5f129478..6e7f2f08a1d38daa0ec8a6b31a5ccb3bec905eb5 100644 --- a/sql/Makefile.am +++ b/sql/Makefile.am @@ -46,7 +46,7 @@ mysqld_LDADD = @MYSQLD_EXTRA_LDFLAGS@ \ noinst_HEADERS = item.h item_func.h item_sum.h item_cmpfunc.h \ item_strfunc.h item_timefunc.h item_uniq.h \ item_create.h item_subselect.h item_row.h \ - mysql_priv.h \ + mysql_priv.h item_geofunc.h \ procedure.h sql_class.h sql_lex.h sql_list.h \ sql_manager.h sql_map.h sql_string.h unireg.h \ field.h handler.h \ @@ -62,7 +62,7 @@ mysqld_SOURCES = sql_lex.cc sql_handler.cc \ item.cc item_sum.cc item_buff.cc item_func.cc \ item_cmpfunc.cc item_strfunc.cc item_timefunc.cc \ thr_malloc.cc item_create.cc item_subselect.cc \ - item_row.cc \ + item_row.cc item_geofunc.cc \ field.cc key.cc sql_class.cc sql_list.cc \ net_serv.cc protocol.cc lock.cc my_lock.c \ sql_string.cc sql_manager.cc sql_map.cc \ diff --git a/sql/item.h b/sql/item.h index 470937f8ee739ffca775306a332bea491573b2ea..2d285bbe434b2c7d45214ae92e816eccd3c48bc4 100644 --- a/sql/item.h +++ b/sql/item.h @@ -644,6 +644,7 @@ class Item_int_with_ref :public Item_int #include "item_row.h" #include "item_cmpfunc.h" #include "item_strfunc.h" +#include "item_geofunc.h" #include "item_timefunc.h" #include "item_uniq.h" #include "item_subselect.h" diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index 4f55357f2886449b21c054f765d587a8e8083556..299014689f03b1068f8b9cee6c0b25d6a3fb3535 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -2363,85 +2363,3 @@ longlong Item_cond_xor::val_int() } return (longlong) result; } - -/**************************************************************** - Classes and functions for spatial relations -*****************************************************************/ - -longlong Item_func_spatial_rel::val_int() -{ - String *res1= args[0]->val_str(&tmp_value1); - String *res2= args[1]->val_str(&tmp_value2); - Geometry g1, g2; - MBR mbr1, mbr2; - - if ((null_value= (args[0]->null_value || - args[1]->null_value || - g1.create_from_wkb(res1->ptr() + SRID_SIZE, - res1->length() - SRID_SIZE) || - g2.create_from_wkb(res2->ptr() + SRID_SIZE, - res2->length() - SRID_SIZE) || - g1.get_mbr(&mbr1) || - g2.get_mbr(&mbr2)))) - return 0; - - switch (spatial_rel) - { - case SP_CONTAINS_FUNC: - return mbr1.contains(&mbr2); - case SP_WITHIN_FUNC: - return mbr1.within(&mbr2); - case SP_EQUALS_FUNC: - return mbr1.equals(&mbr2); - case SP_DISJOINT_FUNC: - return mbr1.disjoint(&mbr2); - case SP_INTERSECTS_FUNC: - return mbr1.intersects(&mbr2); - case SP_TOUCHES_FUNC: - return mbr1.touches(&mbr2); - case SP_OVERLAPS_FUNC: - return mbr1.overlaps(&mbr2); - case SP_CROSSES_FUNC: - return 0; - default: - break; - } - - null_value=1; - return 0; -} - -longlong Item_func_isempty::val_int() -{ - String tmp; - null_value=0; - return args[0]->null_value ? 1 : 0; -} - -longlong Item_func_issimple::val_int() -{ - String tmp; - String *wkb=args[0]->val_str(&tmp); - - if ((null_value= (!wkb || args[0]->null_value ))) - return 0; - /* TODO: Ramil or Holyfoot, add real IsSimple calculation */ - return 0; -} - -longlong Item_func_isclosed::val_int() -{ - String tmp; - String *swkb= args[0]->val_str(&tmp); - Geometry geom; - int isclosed; - - null_value= (!swkb || - args[0]->null_value || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE) || - !GEOM_METHOD_PRESENT(geom,is_closed) || - geom.is_closed(&isclosed)); - - return (longlong) isclosed; -} diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index ff469deab302da5e1448031f56f3c884b73da51a..4438b9020111a244bb7e1d8515bfc5651e57a133 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -850,79 +850,3 @@ inline Item *and_conds(Item *a,Item *b) } Item *and_expressions(Item *a, Item *b, Item **org_item); - -/************************************************************** - Spatial relations -***************************************************************/ - -class Item_func_spatial_rel :public Item_bool_func2 -{ - enum Functype spatial_rel; -public: - Item_func_spatial_rel(Item *a,Item *b, enum Functype sp_rel) : - Item_bool_func2(a,b) { spatial_rel = sp_rel; } - longlong val_int(); - enum Functype functype() const - { - switch (spatial_rel) { - case SP_CONTAINS_FUNC: - return SP_WITHIN_FUNC; - case SP_WITHIN_FUNC: - return SP_CONTAINS_FUNC; - default: - return spatial_rel; - } - } - enum Functype rev_functype() const { return spatial_rel; } - const char *func_name() const - { - switch (spatial_rel) { - case SP_CONTAINS_FUNC: - return "contains"; - case SP_WITHIN_FUNC: - return "within"; - case SP_EQUALS_FUNC: - return "equals"; - case SP_DISJOINT_FUNC: - return "disjoint"; - case SP_INTERSECTS_FUNC: - return "intersects"; - case SP_TOUCHES_FUNC: - return "touches"; - case SP_CROSSES_FUNC: - return "crosses"; - case SP_OVERLAPS_FUNC: - return "overlaps"; - default: - return "sp_unknown"; - } - } -}; - - -class Item_func_isempty :public Item_bool_func -{ -public: - Item_func_isempty(Item *a) :Item_bool_func(a) {} - longlong val_int(); - optimize_type select_optimize() const { return OPTIMIZE_NONE; } - const char *func_name() const { return "isempty"; } -}; - -class Item_func_issimple :public Item_bool_func -{ -public: - Item_func_issimple(Item *a) :Item_bool_func(a) {} - longlong val_int(); - optimize_type select_optimize() const { return OPTIMIZE_NONE; } - const char *func_name() const { return "issimple"; } -}; - -class Item_func_isclosed :public Item_bool_func -{ -public: - Item_func_isclosed(Item *a) :Item_bool_func(a) {} - longlong val_int(); - optimize_type select_optimize() const { return OPTIMIZE_NONE; } - const char *func_name() const { return "isclosed"; } -}; diff --git a/sql/item_func.cc b/sql/item_func.cc index 7ed9e5c85f633a2fdfff91e9c8222c5c9077e8cd..6482d81f4843167885f7a2fe45d7feb659502c95 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -2680,139 +2680,3 @@ longlong Item_func_is_used_lock::val_int() null_value=0; return ull->thread_id; } - - - -/************************************************************************** - Spatial functions -***************************************************************************/ - -longlong Item_func_dimension::val_int() -{ - uint32 dim; - String *swkb= args[0]->val_str(&value); - Geometry geom; - - null_value= (!swkb || - args[0]->null_value || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE) || - geom.dimension(&dim)); - return (longlong) dim; -} - -longlong Item_func_numinteriorring::val_int() -{ - uint32 num; - String *swkb= args[0]->val_str(&value); - Geometry geom; - - null_value= (!swkb || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE) || - !GEOM_METHOD_PRESENT(geom, num_interior_ring) || - geom.num_interior_ring(&num)); - return (longlong) num; -} - -longlong Item_func_numgeometries::val_int() -{ - uint32 num= 0; - String *swkb= args[0]->val_str(&value); - Geometry geom; - - null_value= (!swkb || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE) || - !GEOM_METHOD_PRESENT(geom, num_geometries) || - geom.num_geometries(&num)); - return (longlong) num; -} - -longlong Item_func_numpoints::val_int() -{ - uint32 num; - String *swkb= args[0]->val_str(&value); - Geometry geom; - - null_value= (!swkb || - args[0]->null_value || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE) || - !GEOM_METHOD_PRESENT(geom, num_points) || - geom.num_points(&num)); - return (longlong) num; -} - - -double Item_func_x::val() -{ - double res; - String *swkb= args[0]->val_str(&value); - Geometry geom; - - null_value= (!swkb || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE) || - !GEOM_METHOD_PRESENT(geom, get_x) || - geom.get_x(&res)); - return res; -} - - -double Item_func_y::val() -{ - double res; - String *swkb= args[0]->val_str(&value); - Geometry geom; - - null_value= (!swkb || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE) || - !GEOM_METHOD_PRESENT(geom, get_y) || - geom.get_y(&res)); - return res; -} - - -double Item_func_area::val() -{ - double res; - String *swkb= args[0]->val_str(&value); - Geometry geom; - - null_value= (!swkb || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE) || - !GEOM_METHOD_PRESENT(geom, area) || - geom.area(&res)); - return res; -} - - -double Item_func_glength::val() -{ - double res; - String *swkb= args[0]->val_str(&value); - Geometry geom; - - null_value= (!swkb || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE) || - !GEOM_METHOD_PRESENT(geom, length) || - geom.length(&res)); - return res; -} - - -longlong Item_func_srid::val_int() -{ - String *swkb= args[0]->val_str(&value); - Geometry geom; - - null_value= (!swkb || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE)); - uint32 res= uint4korr(swkb->ptr()); - return (longlong) res; -} diff --git a/sql/item_func.h b/sql/item_func.h index e1d6156c12cbcba47b7ec0c42401d18684c29978..8ef2b85de5246605153b92337a7f7e389a33c1e4 100644 --- a/sql/item_func.h +++ b/sql/item_func.h @@ -1037,101 +1037,6 @@ class Item_func_match :public Item_real_func }; -class Item_func_dimension :public Item_int_func -{ - String value; -public: - Item_func_dimension(Item *a) :Item_int_func(a) {} - longlong val_int(); - const char *func_name() const { return "dimension"; } - void fix_length_and_dec() { max_length=10; } -}; - - -class Item_func_x :public Item_real_func -{ - String value; -public: - Item_func_x(Item *a) :Item_real_func(a) {} - double val(); - const char *func_name() const { return "x"; } -}; - - -class Item_func_y :public Item_real_func -{ - String value; -public: - Item_func_y(Item *a) :Item_real_func(a) {} - double val(); - const char *func_name() const { return "y"; } -}; - - -class Item_func_numgeometries :public Item_int_func -{ - String value; -public: - Item_func_numgeometries(Item *a) :Item_int_func(a) {} - longlong val_int(); - const char *func_name() const { return "numgeometries"; } - void fix_length_and_dec() { max_length=10; } -}; - - -class Item_func_numinteriorring :public Item_int_func -{ - String value; -public: - Item_func_numinteriorring(Item *a) :Item_int_func(a) {} - longlong val_int(); - const char *func_name() const { return "numinteriorring"; } - void fix_length_and_dec() { max_length=10; } -}; - - -class Item_func_numpoints :public Item_int_func -{ - String value; -public: - Item_func_numpoints(Item *a) :Item_int_func(a) {} - longlong val_int(); - const char *func_name() const { return "numpoints"; } - void fix_length_and_dec() { max_length=10; } -}; - - -class Item_func_area :public Item_real_func -{ - String value; -public: - Item_func_area(Item *a) :Item_real_func(a) {} - double val(); - const char *func_name() const { return "area"; } -}; - - -class Item_func_glength :public Item_real_func -{ - String value; -public: - Item_func_glength(Item *a) :Item_real_func(a) {} - double val(); - const char *func_name() const { return "glength"; } -}; - - -class Item_func_srid: public Item_int_func -{ - String value; -public: - Item_func_srid(Item *a): Item_int_func(a) {} - longlong val_int(); - const char *func_name() const { return "srid"; } - void fix_length_and_dec() { max_length= 10; } -}; - - class Item_func_match_nl :public Item_func_match { public: diff --git a/sql/item_geofunc.cc b/sql/item_geofunc.cc new file mode 100644 index 0000000000000000000000000000000000000000..06ba67cbc73e330a1e5cdf6b60c84daf82c9039e --- /dev/null +++ b/sql/item_geofunc.cc @@ -0,0 +1,653 @@ +/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB + + 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. + + 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. + + 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 defines all spatial functions */ + +#ifdef __GNUC__ +#pragma implementation // gcc: Class implementation +#endif + +#include "mysql_priv.h" +#include "sql_acl.h" +#include <m_ctype.h> + +String *Item_func_geometry_from_text::val_str(String *str) +{ + Geometry geom; + String arg_val; + String *wkt= args[0]->val_str(&arg_val); + GTextReadStream trs(wkt->ptr(), wkt->length()); + uint32 srid; + + if ((arg_count == 2) && !args[1]->null_value) + srid= args[1]->val_int(); + else + srid= 0; + + if (str->reserve(SRID_SIZE, 512)) + return 0; + str->length(0); + str->q_append(srid); + if ((null_value=(args[0]->null_value || geom.create_from_wkt(&trs, str, 0)))) + return 0; + return str; +} + + +void Item_func_geometry_from_text::fix_length_and_dec() +{ + max_length=MAX_BLOB_WIDTH; +} + + +String *Item_func_geometry_from_wkb::val_str(String *str) +{ + String arg_val; + String *wkb= args[0]->val_str(&arg_val); + Geometry geom; + uint32 srid; + + if ((arg_count == 2) && !args[1]->null_value) + srid= args[1]->val_int(); + else + srid= 0; + + if (str->reserve(SRID_SIZE, 512)) + return 0; + str->length(0); + str->q_append(srid); + if ((null_value= (args[0]->null_value || + geom.create_from_wkb(wkb->ptr(), wkb->length())))) + return 0; + + str->append(*wkb); + return str; +} + + +void Item_func_geometry_from_wkb::fix_length_and_dec() +{ + max_length=MAX_BLOB_WIDTH; +} + + +String *Item_func_as_text::val_str(String *str) +{ + String arg_val; + String *swkb= args[0]->val_str(&arg_val); + Geometry geom; + + if ((null_value= (args[0]->null_value || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE)))) + return 0; + + str->length(0); + + if ((null_value= geom.as_wkt(str))) + return 0; + + return str; +} + +void Item_func_as_text::fix_length_and_dec() +{ + max_length=MAX_BLOB_WIDTH; +} + +String *Item_func_as_wkb::val_str(String *str) +{ + String arg_val; + String *swkb= args[0]->val_str(&arg_val); + Geometry geom; + + if ((null_value= (args[0]->null_value || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE)))) + return 0; + + str->copy(swkb->ptr() + SRID_SIZE, swkb->length() - SRID_SIZE, + &my_charset_bin); + return str; +} + +void Item_func_as_wkb::fix_length_and_dec() +{ + max_length= MAX_BLOB_WIDTH; +} + +String *Item_func_geometry_type::val_str(String *str) +{ + String *swkb= args[0]->val_str(str); + Geometry geom; + + if ((null_value= (args[0]->null_value || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE)))) + return 0; + str->copy(geom.get_class_info()->m_name, + strlen(geom.get_class_info()->m_name), + default_charset_info); + return str; +} + + +String *Item_func_envelope::val_str(String *str) +{ + String arg_val; + String *swkb= args[0]->val_str(&arg_val); + Geometry geom; + + if ((null_value= args[0]->null_value || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE))) + return 0; + + uint32 srid= uint4korr(swkb->ptr()); + str->length(0); + if (str->reserve(SRID_SIZE, 512)) + return 0; + str->q_append(srid); + return (null_value= geom.envelope(str)) ? 0 : str; +} + + +String *Item_func_centroid::val_str(String *str) +{ + String arg_val; + String *swkb= args[0]->val_str(&arg_val); + Geometry geom; + + if ((null_value= args[0]->null_value || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE) || + !GEOM_METHOD_PRESENT(geom, centroid))) + return 0; + + if (str->reserve(SRID_SIZE, 512)) + return 0; + str->length(0); + uint32 srid= uint4korr(swkb->ptr()); + str->q_append(srid); + + return (null_value= geom.centroid(str)) ? 0 : str; +} + + +/* + Spatial decomposition functions +*/ + +String *Item_func_spatial_decomp::val_str(String *str) +{ + String arg_val; + String *swkb= args[0]->val_str(&arg_val); + Geometry geom; + + if ((null_value= (args[0]->null_value || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE)))) + return 0; + + null_value= 1; + if (str->reserve(SRID_SIZE, 512)) + return 0; + str->length(0); + uint32 srid= uint4korr(swkb->ptr()); + str->q_append(srid); + switch(decomp_func) + { + case SP_STARTPOINT: + if (!GEOM_METHOD_PRESENT(geom,start_point) || geom.start_point(str)) + goto ret; + break; + + case SP_ENDPOINT: + if (!GEOM_METHOD_PRESENT(geom,end_point) || geom.end_point(str)) + goto ret; + break; + + case SP_EXTERIORRING: + if (!GEOM_METHOD_PRESENT(geom,exterior_ring) || geom.exterior_ring(str)) + goto ret; + break; + + default: + goto ret; + } + null_value= 0; + +ret: + return null_value ? 0 : str; +} + + +String *Item_func_spatial_decomp_n::val_str(String *str) +{ + String arg_val; + String *swkb= args[0]->val_str(&arg_val); + long n= (long) args[1]->val_int(); + Geometry geom; + + if ((null_value= (args[0]->null_value || args[1]->null_value || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE)))) + return 0; + + null_value= 1; + if (str->reserve(SRID_SIZE, 512)) + return 0; + str->length(0); + uint32 srid= uint4korr(swkb->ptr()); + str->q_append(srid); + switch(decomp_func_n) + { + case SP_POINTN: + if (!GEOM_METHOD_PRESENT(geom,point_n) || geom.point_n(n,str)) + goto ret; + break; + + case SP_GEOMETRYN: + if (!GEOM_METHOD_PRESENT(geom,geometry_n) || geom.geometry_n(n,str)) + goto ret; + break; + + case SP_INTERIORRINGN: + if (!GEOM_METHOD_PRESENT(geom,interior_ring_n) || + geom.interior_ring_n(n,str)) + goto ret; + break; + + default: + goto ret; + } + null_value= 0; + +ret: + return null_value ? 0 : str; +} + + +/* + Functions to concatinate various spatial objects +*/ + + +/* +* Concatinate doubles into Point +*/ + + +String *Item_func_point::val_str(String *str) +{ + double x= args[0]->val(); + double y= args[1]->val(); + + if ( (null_value= (args[0]->null_value || + args[1]->null_value || + str->realloc(1 + 4 + 8 + 8)))) + return 0; + + str->length(0); + str->q_append((char)Geometry::wkbNDR); + str->q_append((uint32)Geometry::wkbPoint); + str->q_append(x); + str->q_append(y); + return str; +} + + +/* + Concatinates various items into various collections + with checkings for valid wkb type of items. + For example, MultiPoint can be a collection of Points only. + coll_type contains wkb type of target collection. + item_type contains a valid wkb type of items. + In the case when coll_type is wkbGeometryCollection, + we do not check wkb type of items, any is valid. +*/ + +String *Item_func_spatial_collection::val_str(String *str) +{ + String arg_value; + uint i; + + null_value= 1; + + str->length(0); + if (str->reserve(1 + 4 + 4, 512)) + return 0; + + str->q_append((char) Geometry::wkbNDR); + str->q_append((uint32) coll_type); + str->q_append((uint32) arg_count); + + for (i= 0; i < arg_count; ++i) + { + String *res= args[i]->val_str(&arg_value); + if (args[i]->null_value) + goto ret; + + if ( coll_type == Geometry::wkbGeometryCollection ) + { + /* + In the case of GeometryCollection we don't need + any checkings for item types, so just copy them + into target collection + */ + if ((null_value= str->reserve(res->length(), 512))) + goto ret; + + str->q_append(res->ptr(), res->length()); + } + else + { + enum Geometry::wkbType wkb_type; + uint32 len=res->length(); + const char *data= res->ptr() + 1; + + /* + In the case of named collection we must to + check that items are of specific type, let's + do this checking now + */ + + if (len < 5) + goto ret; + wkb_type= (Geometry::wkbType) uint4korr(data); + data+= 4; + len-= 5; + if (wkb_type != item_type) + goto ret; + + switch (coll_type) { + case Geometry::wkbMultiPoint: + case Geometry::wkbMultiLineString: + case Geometry::wkbMultiPolygon: + if (len < WKB_HEADER_SIZE) + goto ret; + + data-= WKB_HEADER_SIZE; + len+= WKB_HEADER_SIZE; + if (str->reserve(len, 512)) + goto ret; + str->q_append(data, len); + break; + + case Geometry::wkbLineString: + if (str->reserve(POINT_DATA_SIZE, 512)) + goto ret; + str->q_append(data, POINT_DATA_SIZE); + break; + + case Geometry::wkbPolygon: + { + uint32 n_points; + double x1, y1, x2, y2; + + if (len < 4 + 2 * POINT_DATA_SIZE) + goto ret; + + uint32 llen= len; + const char *ldata= data; + + n_points= uint4korr(data); + data+= 4; + float8get(x1, data); + data+= 8; + float8get(y1, data); + data+= 8; + + data+= (n_points - 2) * POINT_DATA_SIZE; + + float8get(x2, data); + float8get(y2, data + 8); + + if ((x1 != x2) || (y1 != y2)) + goto ret; + + if (str->reserve(llen, 512)) + goto ret; + str->q_append(ldata, llen); + } + break; + + default: + goto ret; + } + } + } + + if (str->length() > current_thd->variables.max_allowed_packet) + goto ret; + + null_value = 0; + +ret: + return null_value ? 0 : str; +} + +/* + Functions for spatial relations +*/ + +longlong Item_func_spatial_rel::val_int() +{ + String *res1= args[0]->val_str(&tmp_value1); + String *res2= args[1]->val_str(&tmp_value2); + Geometry g1, g2; + MBR mbr1, mbr2; + + if ((null_value= (args[0]->null_value || + args[1]->null_value || + g1.create_from_wkb(res1->ptr() + SRID_SIZE, + res1->length() - SRID_SIZE) || + g2.create_from_wkb(res2->ptr() + SRID_SIZE, + res2->length() - SRID_SIZE) || + g1.get_mbr(&mbr1) || + g2.get_mbr(&mbr2)))) + return 0; + + switch (spatial_rel) + { + case SP_CONTAINS_FUNC: + return mbr1.contains(&mbr2); + case SP_WITHIN_FUNC: + return mbr1.within(&mbr2); + case SP_EQUALS_FUNC: + return mbr1.equals(&mbr2); + case SP_DISJOINT_FUNC: + return mbr1.disjoint(&mbr2); + case SP_INTERSECTS_FUNC: + return mbr1.intersects(&mbr2); + case SP_TOUCHES_FUNC: + return mbr1.touches(&mbr2); + case SP_OVERLAPS_FUNC: + return mbr1.overlaps(&mbr2); + case SP_CROSSES_FUNC: + return 0; + default: + break; + } + + null_value=1; + return 0; +} + +longlong Item_func_isempty::val_int() +{ + String tmp; + null_value=0; + return args[0]->null_value ? 1 : 0; +} + +longlong Item_func_issimple::val_int() +{ + String tmp; + String *wkb=args[0]->val_str(&tmp); + + if ((null_value= (!wkb || args[0]->null_value ))) + return 0; + /* TODO: Ramil or Holyfoot, add real IsSimple calculation */ + return 0; +} + +longlong Item_func_isclosed::val_int() +{ + String tmp; + String *swkb= args[0]->val_str(&tmp); + Geometry geom; + int isclosed; + + null_value= (!swkb || + args[0]->null_value || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE) || + !GEOM_METHOD_PRESENT(geom,is_closed) || + geom.is_closed(&isclosed)); + + return (longlong) isclosed; +} + +/* + Numerical functions +*/ + +longlong Item_func_dimension::val_int() +{ + uint32 dim; + String *swkb= args[0]->val_str(&value); + Geometry geom; + + null_value= (!swkb || + args[0]->null_value || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE) || + geom.dimension(&dim)); + return (longlong) dim; +} + +longlong Item_func_numinteriorring::val_int() +{ + uint32 num; + String *swkb= args[0]->val_str(&value); + Geometry geom; + + null_value= (!swkb || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE) || + !GEOM_METHOD_PRESENT(geom, num_interior_ring) || + geom.num_interior_ring(&num)); + return (longlong) num; +} + +longlong Item_func_numgeometries::val_int() +{ + uint32 num= 0; + String *swkb= args[0]->val_str(&value); + Geometry geom; + + null_value= (!swkb || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE) || + !GEOM_METHOD_PRESENT(geom, num_geometries) || + geom.num_geometries(&num)); + return (longlong) num; +} + +longlong Item_func_numpoints::val_int() +{ + uint32 num; + String *swkb= args[0]->val_str(&value); + Geometry geom; + + null_value= (!swkb || + args[0]->null_value || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE) || + !GEOM_METHOD_PRESENT(geom, num_points) || + geom.num_points(&num)); + return (longlong) num; +} + +double Item_func_x::val() +{ + double res; + String *swkb= args[0]->val_str(&value); + Geometry geom; + + null_value= (!swkb || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE) || + !GEOM_METHOD_PRESENT(geom, get_x) || + geom.get_x(&res)); + return res; +} + +double Item_func_y::val() +{ + double res; + String *swkb= args[0]->val_str(&value); + Geometry geom; + + null_value= (!swkb || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE) || + !GEOM_METHOD_PRESENT(geom, get_y) || + geom.get_y(&res)); + return res; +} + +double Item_func_area::val() +{ + double res; + String *swkb= args[0]->val_str(&value); + Geometry geom; + + null_value= (!swkb || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE) || + !GEOM_METHOD_PRESENT(geom, area) || + geom.area(&res)); + return res; +} + +double Item_func_glength::val() +{ + double res; + String *swkb= args[0]->val_str(&value); + Geometry geom; + + null_value= (!swkb || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE) || + !GEOM_METHOD_PRESENT(geom, length) || + geom.length(&res)); + return res; +} + +longlong Item_func_srid::val_int() +{ + String *swkb= args[0]->val_str(&value); + Geometry geom; + + null_value= (!swkb || + geom.create_from_wkb(swkb->ptr() + SRID_SIZE, + swkb->length() - SRID_SIZE)); + uint32 res= uint4korr(swkb->ptr()); + return (longlong) res; +} diff --git a/sql/item_geofunc.h b/sql/item_geofunc.h new file mode 100644 index 0000000000000000000000000000000000000000..21e94735f310bef2eab325b80cea87d10e9b118a --- /dev/null +++ b/sql/item_geofunc.h @@ -0,0 +1,360 @@ +/* Copyright (C) 2000-2003 MySQL AB + + 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. + + 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. + + 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 defines all spatial functions */ + +#ifdef __GNUC__ +#pragma interface /* gcc class implementation */ +#endif + +#define SRID_SIZE sizeof(uint32) + +class Item_func_geometry_from_text: public Item_str_func +{ +public: + Item_func_geometry_from_text(Item *a) :Item_str_func(a) {} + Item_func_geometry_from_text(Item *a, Item *srid) :Item_str_func(a, srid) {} + const char *func_name() const { return "geometryfromtext"; } + String *val_str(String *); + void fix_length_and_dec(); +}; + +class Item_func_geometry_from_wkb: public Item_str_func +{ +public: + Item_func_geometry_from_wkb(Item *a): Item_str_func(a) {} + Item_func_geometry_from_wkb(Item *a, Item *srid): Item_str_func(a, srid) {} + const char *func_name() const { return "geometryfromwkb"; } + String *val_str(String *); + void fix_length_and_dec(); +}; + +class Item_func_as_text: public Item_str_func +{ +public: + Item_func_as_text(Item *a): Item_str_func(a) {} + const char *func_name() const { return "astext"; } + String *val_str(String *); + void fix_length_and_dec(); +}; + +class Item_func_as_wkb: public Item_str_func +{ +public: + Item_func_as_wkb(Item *a): Item_str_func(a) {} + const char *func_name() const { return "aswkb"; } + String *val_str(String *); + void fix_length_and_dec(); +}; + +class Item_func_geometry_type: public Item_str_func +{ +public: + Item_func_geometry_type(Item *a): Item_str_func(a) {} + String *val_str(String *); + const char *func_name() const { return "geometrytype"; } + void fix_length_and_dec() + { + max_length=20; // "GeometryCollection" is the most long + }; +}; + +class Item_func_centroid: public Item_str_func +{ +public: + Item_func_centroid(Item *a): Item_str_func(a) {} + const char *func_name() const { return "centroid"; } + String *val_str(String *); + void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} +}; + +class Item_func_envelope: public Item_str_func +{ +public: + Item_func_envelope(Item *a): Item_str_func(a) {} + const char *func_name() const { return "envelope"; } + String *val_str(String *); + void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} +}; + +class Item_func_point: public Item_str_func +{ +public: + Item_func_point(Item *a, Item *b): Item_str_func(a, b) {} + Item_func_point(Item *a, Item *b, Item *srid): Item_str_func(a, b, srid) {} + const char *func_name() const { return "point"; } + String *val_str(String *); + void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} +}; + +class Item_func_spatial_decomp: public Item_str_func +{ + enum Functype decomp_func; +public: + Item_func_spatial_decomp(Item *a, Item_func::Functype ft) : + Item_str_func(a) { decomp_func = ft; } + const char *func_name() const + { + switch (decomp_func) + { + case SP_STARTPOINT: + return "startpoint"; + case SP_ENDPOINT: + return "endpoint"; + case SP_EXTERIORRING: + return "exteriorring"; + default: + return "spatial_decomp_unknown"; + } + } + String *val_str(String *); + void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} +}; + +class Item_func_spatial_decomp_n: public Item_str_func +{ + enum Functype decomp_func_n; +public: + Item_func_spatial_decomp_n(Item *a, Item *b, Item_func::Functype ft): + Item_str_func(a, b) { decomp_func_n = ft; } + const char *func_name() const + { + switch (decomp_func_n) + { + case SP_POINTN: + return "pointn"; + case SP_GEOMETRYN: + return "geometryn"; + case SP_INTERIORRINGN: + return "interiorringn"; + default: + return "spatial_decomp_n_unknown"; + } + } + String *val_str(String *); + void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} +}; + +class Item_func_spatial_collection: public Item_str_func +{ + String tmp_value; + enum Geometry::wkbType coll_type; + enum Geometry::wkbType item_type; +public: + Item_func_spatial_collection( + List<Item> &list, enum Geometry::wkbType ct, enum Geometry::wkbType it): + Item_str_func(list) + { + coll_type=ct; + item_type=it; + } + String *val_str(String *); + void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} + const char *func_name() const { return "multipoint"; } +}; + +#ifdef HAVE_COMPRESS + +class Item_func_compress: public Item_str_func +{ + String buffer; +public: + Item_func_compress(Item *a):Item_str_func(a){} + String *val_str(String *); + void fix_length_and_dec(){max_length= (args[0]->max_length*120)/100+12;} + const char *func_name() const{return "compress";} +}; + +class Item_func_uncompress: public Item_str_func +{ + String buffer; +public: + Item_func_uncompress(Item *a): Item_str_func(a){} + String *val_str(String *); + void fix_length_and_dec(){max_length= MAX_BLOB_WIDTH;} + const char *func_name() const{return "uncompress";} +}; + +#endif + +/* + Spatial relations +*/ + +class Item_func_spatial_rel: public Item_bool_func2 +{ + enum Functype spatial_rel; +public: + Item_func_spatial_rel(Item *a,Item *b, enum Functype sp_rel) : + Item_bool_func2(a,b) { spatial_rel = sp_rel; } + longlong val_int(); + enum Functype functype() const + { + switch (spatial_rel) { + case SP_CONTAINS_FUNC: + return SP_WITHIN_FUNC; + case SP_WITHIN_FUNC: + return SP_CONTAINS_FUNC; + default: + return spatial_rel; + } + } + enum Functype rev_functype() const { return spatial_rel; } + const char *func_name() const + { + switch (spatial_rel) { + case SP_CONTAINS_FUNC: + return "contains"; + case SP_WITHIN_FUNC: + return "within"; + case SP_EQUALS_FUNC: + return "equals"; + case SP_DISJOINT_FUNC: + return "disjoint"; + case SP_INTERSECTS_FUNC: + return "intersects"; + case SP_TOUCHES_FUNC: + return "touches"; + case SP_CROSSES_FUNC: + return "crosses"; + case SP_OVERLAPS_FUNC: + return "overlaps"; + default: + return "sp_unknown"; + } + } +}; + +class Item_func_isempty: public Item_bool_func +{ +public: + Item_func_isempty(Item *a): Item_bool_func(a) {} + longlong val_int(); + optimize_type select_optimize() const { return OPTIMIZE_NONE; } + const char *func_name() const { return "isempty"; } +}; + +class Item_func_issimple: public Item_bool_func +{ +public: + Item_func_issimple(Item *a): Item_bool_func(a) {} + longlong val_int(); + optimize_type select_optimize() const { return OPTIMIZE_NONE; } + const char *func_name() const { return "issimple"; } +}; + +class Item_func_isclosed: public Item_bool_func +{ +public: + Item_func_isclosed(Item *a): Item_bool_func(a) {} + longlong val_int(); + optimize_type select_optimize() const { return OPTIMIZE_NONE; } + const char *func_name() const { return "isclosed"; } +}; + +class Item_func_dimension: public Item_int_func +{ + String value; +public: + Item_func_dimension(Item *a): Item_int_func(a) {} + longlong val_int(); + const char *func_name() const { return "dimension"; } + void fix_length_and_dec() { max_length=10; } +}; + +class Item_func_x: public Item_real_func +{ + String value; +public: + Item_func_x(Item *a): Item_real_func(a) {} + double val(); + const char *func_name() const { return "x"; } +}; + + +class Item_func_y: public Item_real_func +{ + String value; +public: + Item_func_y(Item *a): Item_real_func(a) {} + double val(); + const char *func_name() const { return "y"; } +}; + + +class Item_func_numgeometries: public Item_int_func +{ + String value; +public: + Item_func_numgeometries(Item *a): Item_int_func(a) {} + longlong val_int(); + const char *func_name() const { return "numgeometries"; } + void fix_length_and_dec() { max_length=10; } +}; + + +class Item_func_numinteriorring: public Item_int_func +{ + String value; +public: + Item_func_numinteriorring(Item *a): Item_int_func(a) {} + longlong val_int(); + const char *func_name() const { return "numinteriorring"; } + void fix_length_and_dec() { max_length=10; } +}; + + +class Item_func_numpoints: public Item_int_func +{ + String value; +public: + Item_func_numpoints(Item *a): Item_int_func(a) {} + longlong val_int(); + const char *func_name() const { return "numpoints"; } + void fix_length_and_dec() { max_length=10; } +}; + + +class Item_func_area: public Item_real_func +{ + String value; +public: + Item_func_area(Item *a): Item_real_func(a) {} + double val(); + const char *func_name() const { return "area"; } +}; + + +class Item_func_glength: public Item_real_func +{ + String value; +public: + Item_func_glength(Item *a): Item_real_func(a) {} + double val(); + const char *func_name() const { return "glength"; } +}; + + +class Item_func_srid: public Item_int_func +{ + String value; +public: + Item_func_srid(Item *a): Item_int_func(a) {} + longlong val_int(); + const char *func_name() const { return "srid"; } + void fix_length_and_dec() { max_length= 10; } +}; diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index ae48aeb115a9fffe751202d75180a70c07b76c24..065690950186ef5cce419cf54c6a34c978f62435 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -2522,428 +2522,6 @@ String *Item_func_quote::val_str(String *str) return 0; } - -/******************************************************* -General functions for spatial objects -********************************************************/ - -String *Item_func_geometry_from_text::val_str(String *str) -{ - Geometry geom; - String arg_val; - String *wkt= args[0]->val_str(&arg_val); - GTextReadStream trs(wkt->ptr(), wkt->length()); - uint32 srid; - - if ((arg_count == 2) && !args[1]->null_value) - srid= args[1]->val_int(); - else - srid= 0; - - if (str->reserve(SRID_SIZE, 512)) - return 0; - str->length(0); - str->q_append(srid); - if ((null_value=(args[0]->null_value || geom.create_from_wkt(&trs, str, 0)))) - return 0; - return str; -} - - -void Item_func_geometry_from_text::fix_length_and_dec() -{ - max_length=MAX_BLOB_WIDTH; -} - - -String *Item_func_geometry_from_wkb::val_str(String *str) -{ - String arg_val; - String *wkb= args[0]->val_str(&arg_val); - Geometry geom; - uint32 srid; - - if ((arg_count == 2) && !args[1]->null_value) - srid= args[1]->val_int(); - else - srid= 0; - - if (str->reserve(SRID_SIZE, 512)) - return 0; - str->length(0); - str->q_append(srid); - if ((null_value= (args[0]->null_value || - geom.create_from_wkb(wkb->ptr(), wkb->length())))) - return 0; - - str->append(*wkb); - return str; -} - - -void Item_func_geometry_from_wkb::fix_length_and_dec() -{ - max_length=MAX_BLOB_WIDTH; -} - - -String *Item_func_as_text::val_str(String *str) -{ - String arg_val; - String *swkb= args[0]->val_str(&arg_val); - Geometry geom; - - if ((null_value= (args[0]->null_value || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE)))) - return 0; - - str->length(0); - - if ((null_value= geom.as_wkt(str))) - return 0; - - return str; -} - -void Item_func_as_text::fix_length_and_dec() -{ - max_length=MAX_BLOB_WIDTH; -} - -String *Item_func_as_wkb::val_str(String *str) -{ - String arg_val; - String *swkb= args[0]->val_str(&arg_val); - Geometry geom; - - if ((null_value= (args[0]->null_value || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE)))) - return 0; - - str->copy(swkb->ptr() + SRID_SIZE, swkb->length() - SRID_SIZE, - &my_charset_bin); - return str; -} - -void Item_func_as_wkb::fix_length_and_dec() -{ - max_length= MAX_BLOB_WIDTH; -} - -String *Item_func_geometry_type::val_str(String *str) -{ - String *swkb= args[0]->val_str(str); - Geometry geom; - - if ((null_value= (args[0]->null_value || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE)))) - return 0; - str->copy(geom.get_class_info()->m_name, - strlen(geom.get_class_info()->m_name), - default_charset_info); - return str; -} - - -String *Item_func_envelope::val_str(String *str) -{ - String arg_val; - String *swkb= args[0]->val_str(&arg_val); - Geometry geom; - - if ((null_value= args[0]->null_value || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE))) - return 0; - - uint32 srid= uint4korr(swkb->ptr()); - str->length(0); - if (str->reserve(SRID_SIZE, 512)) - return 0; - str->q_append(srid); - return (null_value= geom.envelope(str)) ? 0 : str; -} - - -String *Item_func_centroid::val_str(String *str) -{ - String arg_val; - String *swkb= args[0]->val_str(&arg_val); - Geometry geom; - - if ((null_value= args[0]->null_value || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE) || - !GEOM_METHOD_PRESENT(geom, centroid))) - return 0; - - if (str->reserve(SRID_SIZE, 512)) - return 0; - str->length(0); - uint32 srid= uint4korr(swkb->ptr()); - str->q_append(srid); - - return (null_value= geom.centroid(str)) ? 0 : str; -} - - -/*********************************************** - Spatial decomposition functions -***********************************************/ - -String *Item_func_spatial_decomp::val_str(String *str) -{ - String arg_val; - String *swkb= args[0]->val_str(&arg_val); - Geometry geom; - - if ((null_value= (args[0]->null_value || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE)))) - return 0; - - null_value= 1; - if (str->reserve(SRID_SIZE, 512)) - return 0; - str->length(0); - uint32 srid= uint4korr(swkb->ptr()); - str->q_append(srid); - switch(decomp_func) - { - case SP_STARTPOINT: - if (!GEOM_METHOD_PRESENT(geom,start_point) || geom.start_point(str)) - goto ret; - break; - - case SP_ENDPOINT: - if (!GEOM_METHOD_PRESENT(geom,end_point) || geom.end_point(str)) - goto ret; - break; - - case SP_EXTERIORRING: - if (!GEOM_METHOD_PRESENT(geom,exterior_ring) || geom.exterior_ring(str)) - goto ret; - break; - - default: - goto ret; - } - null_value= 0; - -ret: - return null_value ? 0 : str; -} - - -String *Item_func_spatial_decomp_n::val_str(String *str) -{ - String arg_val; - String *swkb= args[0]->val_str(&arg_val); - long n= (long) args[1]->val_int(); - Geometry geom; - - if ((null_value= (args[0]->null_value || args[1]->null_value || - geom.create_from_wkb(swkb->ptr() + SRID_SIZE, - swkb->length() - SRID_SIZE)))) - return 0; - - null_value= 1; - if (str->reserve(SRID_SIZE, 512)) - return 0; - str->length(0); - uint32 srid= uint4korr(swkb->ptr()); - str->q_append(srid); - switch(decomp_func_n) - { - case SP_POINTN: - if (!GEOM_METHOD_PRESENT(geom,point_n) || geom.point_n(n,str)) - goto ret; - break; - - case SP_GEOMETRYN: - if (!GEOM_METHOD_PRESENT(geom,geometry_n) || geom.geometry_n(n,str)) - goto ret; - break; - - case SP_INTERIORRINGN: - if (!GEOM_METHOD_PRESENT(geom,interior_ring_n) || - geom.interior_ring_n(n,str)) - goto ret; - break; - - default: - goto ret; - } - null_value= 0; - -ret: - return null_value ? 0 : str; -} - - - -/*********************************************** -Functions to concatinate various spatial objects -************************************************/ - - -/* -* Concatinate doubles into Point -*/ - - -String *Item_func_point::val_str(String *str) -{ - double x= args[0]->val(); - double y= args[1]->val(); - - if ( (null_value= (args[0]->null_value || - args[1]->null_value || - str->realloc(1 + 4 + 8 + 8)))) - return 0; - - str->length(0); - str->q_append((char)Geometry::wkbNDR); - str->q_append((uint32)Geometry::wkbPoint); - str->q_append(x); - str->q_append(y); - return str; -} - - -/* - Concatinates various items into various collections - with checkings for valid wkb type of items. - For example, MultiPoint can be a collection of Points only. - coll_type contains wkb type of target collection. - item_type contains a valid wkb type of items. - In the case when coll_type is wkbGeometryCollection, - we do not check wkb type of items, any is valid. -*/ - -String *Item_func_spatial_collection::val_str(String *str) -{ - String arg_value; - uint i; - - null_value= 1; - - str->length(0); - if (str->reserve(1 + 4 + 4, 512)) - return 0; - - str->q_append((char) Geometry::wkbNDR); - str->q_append((uint32) coll_type); - str->q_append((uint32) arg_count); - - for (i= 0; i < arg_count; ++i) - { - String *res= args[i]->val_str(&arg_value); - if (args[i]->null_value) - goto ret; - - if ( coll_type == Geometry::wkbGeometryCollection ) - { - /* - In the case of GeometryCollection we don't need - any checkings for item types, so just copy them - into target collection - */ - if ((null_value= str->reserve(res->length(), 512))) - goto ret; - - str->q_append(res->ptr(), res->length()); - } - else - { - enum Geometry::wkbType wkb_type; - uint32 len=res->length(); - const char *data= res->ptr() + 1; - - /* - In the case of named collection we must to - check that items are of specific type, let's - do this checking now - */ - - if (len < 5) - goto ret; - wkb_type= (Geometry::wkbType) uint4korr(data); - data+= 4; - len-= 5; - if (wkb_type != item_type) - goto ret; - - switch (coll_type) { - case Geometry::wkbMultiPoint: - case Geometry::wkbMultiLineString: - case Geometry::wkbMultiPolygon: - if (len < WKB_HEADER_SIZE) - goto ret; - - data-= WKB_HEADER_SIZE; - len+= WKB_HEADER_SIZE; - if (str->reserve(len, 512)) - goto ret; - str->q_append(data, len); - break; - - case Geometry::wkbLineString: - if (str->reserve(POINT_DATA_SIZE, 512)) - goto ret; - str->q_append(data, POINT_DATA_SIZE); - break; - - case Geometry::wkbPolygon: - { - uint32 n_points; - double x1, y1, x2, y2; - - if (len < 4 + 2 * POINT_DATA_SIZE) - goto ret; - - uint32 llen= len; - const char *ldata= data; - - n_points= uint4korr(data); - data+= 4; - float8get(x1, data); - data+= 8; - float8get(y1, data); - data+= 8; - - data+= (n_points - 2) * POINT_DATA_SIZE; - - float8get(x2, data); - float8get(y2, data + 8); - - if ((x1 != x2) || (y1 != y2)) - goto ret; - - if (str->reserve(llen, 512)) - goto ret; - str->q_append(ldata, llen); - } - break; - - default: - goto ret; - } - } - } - - if (str->length() > current_thd->variables.max_allowed_packet) - goto ret; - - null_value = 0; - -ret: - return null_value ? 0 : str; -} - #ifdef HAVE_COMPRESS #include "../zlib/zlib.h" diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h index 8ae5c995375e43ebdaed8f69275c137b58d10272..65aad3a3fffb9a9454142728a51b9edf186e1a95 100644 --- a/sql/item_strfunc.h +++ b/sql/item_strfunc.h @@ -628,242 +628,3 @@ class Item_func_collation :public Item_str_func set_charset(default_charset()); }; }; - - -/******************************************************* -Spatial functions -********************************************************/ - -#define SRID_SIZE sizeof(uint32) - -class Item_func_geometry_from_text :public Item_str_func -{ -public: - Item_func_geometry_from_text(Item *a) :Item_str_func(a) {} - Item_func_geometry_from_text(Item *a, Item *srid) :Item_str_func(a, srid) {} - const char *func_name() const { return "geometryfromtext"; } - String *val_str(String *); - void fix_length_and_dec(); -}; - -class Item_func_geometry_from_wkb: public Item_str_func -{ -public: - Item_func_geometry_from_wkb(Item *a) :Item_str_func(a) {} - Item_func_geometry_from_wkb(Item *a, Item *srid) :Item_str_func(a, srid) {} - const char *func_name() const { return "geometryfromwkb"; } - String *val_str(String *); - void fix_length_and_dec(); -}; - -class Item_func_as_text :public Item_str_func -{ -public: - Item_func_as_text(Item *a) :Item_str_func(a) {} - const char *func_name() const { return "astext"; } - String *val_str(String *); - void fix_length_and_dec(); -}; - -class Item_func_as_wkb :public Item_str_func -{ -public: - Item_func_as_wkb(Item *a) :Item_str_func(a) {} - const char *func_name() const { return "aswkb"; } - String *val_str(String *); - void fix_length_and_dec(); -}; - -class Item_func_geometry_type :public Item_str_func -{ -public: - Item_func_geometry_type(Item *a) :Item_str_func(a) {} - String *val_str(String *); - const char *func_name() const { return "geometrytype"; } - void fix_length_and_dec() - { - max_length=20; // "GeometryCollection" is the most long - }; -}; - -class Item_func_centroid :public Item_str_func -{ -public: - Item_func_centroid(Item *a) :Item_str_func(a) {} - const char *func_name() const { return "centroid"; } - String *val_str(String *); - void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} -}; - -class Item_func_envelope :public Item_str_func -{ -public: - Item_func_envelope(Item *a) :Item_str_func(a) {} - const char *func_name() const { return "envelope"; } - String *val_str(String *); - void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} -}; - -class Item_func_point :public Item_str_func -{ -public: - Item_func_point(Item *a, Item *b) :Item_str_func(a, b) {} - Item_func_point(Item *a, Item *b, Item *srid) :Item_str_func(a, b, srid) {} - const char *func_name() const { return "point"; } - String *val_str(String *); - void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} -}; - -class Item_func_spatial_decomp :public Item_str_func -{ - enum Functype decomp_func; -public: - Item_func_spatial_decomp(Item *a, Item_func::Functype ft) : - Item_str_func(a) { decomp_func = ft; } - const char *func_name() const - { - switch (decomp_func) - { - case SP_STARTPOINT: - return "startpoint"; - case SP_ENDPOINT: - return "endpoint"; - case SP_EXTERIORRING: - return "exteriorring"; - default: - return "spatial_decomp_unknown"; - } - } - String *val_str(String *); - void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} -}; - -class Item_func_spatial_decomp_n :public Item_str_func -{ - enum Functype decomp_func_n; -public: - Item_func_spatial_decomp_n(Item *a, Item *b, Item_func::Functype ft) : - Item_str_func(a, b) { decomp_func_n = ft; } - const char *func_name() const - { - switch (decomp_func_n) - { - case SP_POINTN: - return "pointn"; - case SP_GEOMETRYN: - return "geometryn"; - case SP_INTERIORRINGN: - return "interiorringn"; - default: - return "spatial_decomp_n_unknown"; - } - } - String *val_str(String *); - void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} -}; - - -class Item_func_spatial_collection :public Item_str_func -{ - String tmp_value; - enum Geometry::wkbType coll_type; - enum Geometry::wkbType item_type; -public: - Item_func_spatial_collection( - List<Item> &list, enum Geometry::wkbType ct, enum Geometry::wkbType it) : - Item_str_func(list) - { - coll_type=ct; - item_type=it; - } - String *val_str(String *); - void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} - const char *func_name() const { return "multipoint"; } -}; - -#ifdef HAVE_COMPRESS - -class Item_func_compress : public Item_str_func -{ - String buffer; -public: - Item_func_compress(Item *a):Item_str_func(a){} - String *val_str(String *); - void fix_length_and_dec(){max_length= (args[0]->max_length*120)/100+12;} - const char *func_name() const{return "compress";} -}; - -class Item_func_uncompress : public Item_str_func -{ - String buffer; -public: - Item_func_uncompress(Item *a):Item_str_func(a){} - String *val_str(String *); - void fix_length_and_dec(){max_length= MAX_BLOB_WIDTH;} - const char *func_name() const{return "uncompress";} -}; - -#endif - -/* -class Item_func_multipoint :public Item_str_func -{ - String tmp_value; -public: - Item_func_multipoint(List<Item> &list) :Item_str_func(list) {} - String *val_str(String *); - void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} - const char *func_name() const { return "multipoint"; } -}; - -class Item_func_linestring :public Item_str_func -{ - String tmp_value; -public: - Item_func_linestring(List<Item> &list) :Item_str_func(list) {} - String *val_str(String *); - void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} - const char *func_name() const { return "linestring"; } -}; - -class Item_func_multilinestring :public Item_str_func -{ - String tmp_value; -public: - Item_func_multilinestring(List<Item> &list) :Item_str_func(list) {} - String *val_str(String *); - void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} - const char *func_name() const { return "multilinestring"; } -}; - -class Item_func_polygon :public Item_str_func -{ - String tmp_value; -public: - Item_func_polygon(List<Item> &list) :Item_str_func(list) {} - String *val_str(String *); - void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} - const char *func_name() const { return "polygon"; } -}; - -class Item_func_multipolygon :public Item_str_func -{ - String tmp_value; -public: - Item_func_multipolygon(List<Item> &list) :Item_str_func(list) {} - String *val_str(String *); - void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} - const char *func_name() const { return "multipolygon"; } -}; - -class Item_func_geometrycollection :public Item_str_func -{ - String tmp_value; -public: - Item_func_geometrycollection(List<Item> &list) :Item_str_func(list) {} - String *val_str(String *); - void fix_length_and_dec(){max_length=MAX_BLOB_WIDTH;} - const char *func_name() const { return "geometrycollection"; } -}; - -*/