diff options
38 files changed, 1243 insertions, 1201 deletions
diff --git a/.bzrignore b/.bzrignore index 042a672eb7c..bfde1876164 100644 --- a/.bzrignore +++ b/.bzrignore @@ -277,6 +277,7 @@ help.h include/my_config.h include/my_global.h include/mysql_version.h +include/readline/*.h include/readline/readline.h include/widec.h innobase/autom4te-2.53.cache/output.0 @@ -347,6 +348,7 @@ libmysqld/item_buff.cc libmysqld/item_cmpfunc.cc libmysqld/item_create.cc libmysqld/item_func.cc +libmysqld/item_geofunc.cc libmysqld/item_row.cc libmysqld/item_strfunc.cc libmysqld/item_sum.cc @@ -503,6 +505,7 @@ scripts/make_win_src_distribution scripts/msql2mysql scripts/mysql_config scripts/mysql_convert_table_format +scripts/mysql_create_system_tables scripts/mysql_explain_log scripts/mysql_find_rows scripts/mysql_fix_extensions @@ -616,5 +619,3 @@ vio/test-ssl vio/test-sslclient vio/test-sslserver vio/viotest-ssl -include/readline/*.h -scripts/mysql_create_system_tables diff --git a/client/mysqldump.c b/client/mysqldump.c index 86f03b46038..18705b9e375 100644 --- a/client/mysqldump.c +++ b/client/mysqldump.c @@ -77,7 +77,7 @@ static my_bool verbose=0,tFlag=0,cFlag=0,dFlag=0,quick= 1, extended_insert= 1, lock_tables=1,ignore_errors=0,flush_logs=0,replace=0, ignore=0,opt_drop=1,opt_keywords=0,opt_lock=1,opt_compress=0, opt_delayed=0,create_options=1,opt_quoted=0,opt_databases=0, - opt_alldbs=0,opt_create_db=0,opt_first_slave=0, + opt_alldbs=0,opt_create_db=0,opt_first_slave=0,opt_set_names=0, opt_autocommit=0,opt_master_data,opt_disable_keys=1,opt_xml=0, opt_delete_master_logs=0, tty_password=0, opt_single_transaction=0; @@ -85,7 +85,7 @@ static MYSQL mysql_connection,*sock=0; static char insert_pat[12 * 1024],*opt_password=0,*current_user=0, *current_host=0,*path=0,*fields_terminated=0, *lines_terminated=0, *enclosed=0, *opt_enclosed=0, *escaped=0, - *where=0, *default_charset= (char *)MYSQL_DEFAULT_CHARSET_NAME, + *where=0, *default_charset= (char *) "binary", *opt_compatible_mode_str= 0, *err_ptr= 0; static ulong opt_compatible_mode= 0; @@ -212,6 +212,10 @@ static struct my_option my_long_options[] = (gptr*) &tFlag, (gptr*) &tFlag, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, {"no-data", 'd', "No row information.", (gptr*) &dFlag, (gptr*) &dFlag, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, + {"no-set-names", 'N', + "'SET NAMES charset_name' will not be put in the output.", + (gptr*) &opt_set_names, (gptr*) &opt_set_names, 0, GET_BOOL, NO_ARG, 0, 0, + 0, 0, 0, 0}, {"set-variable", 'O', "Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value.", 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, @@ -345,6 +349,8 @@ static void write_header(FILE *sql_file, char *db_name) sql_file); fprintf(sql_file, "-- Server version\t%s\n", mysql_get_server_info(&mysql_connection)); + if (!opt_set_names) + fprintf(sql_file,"\n/*!40101 SET NAMES %s*/;\n",default_charset); } return; } /* write_header */ @@ -557,6 +563,7 @@ static int dbConnect(char *host, char *user,char *passwd) if (shared_memory_base_name) mysql_options(&mysql_connection,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name); #endif + mysql_options(&mysql_connection, MYSQL_SET_CHARSET_NAME, default_charset); if (!(sock= mysql_real_connect(&mysql_connection,host,user,passwd, NULL,opt_mysql_port,opt_mysql_unix_port, 0))) diff --git a/client/mysqlshow.c b/client/mysqlshow.c index 9b376872ff8..5bb611b713f 100644 --- a/client/mysqlshow.c +++ b/client/mysqlshow.c @@ -30,6 +30,7 @@ static my_string host=0,opt_password=0,user=0; static my_bool opt_show_keys=0,opt_compress=0,opt_status=0, tty_password=0; static uint opt_verbose=0; +static char *default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME; #ifdef HAVE_SMEM static char *shared_memory_base_name=0; @@ -115,6 +116,8 @@ int main(int argc, char **argv) if (shared_memory_base_name) mysql_options(&mysql,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name); #endif + mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, default_charset); + if (!(mysql_real_connect(&mysql,host,user,opt_password, (first_argument_uses_wildcards) ? "" : argv[0],opt_mysql_port,opt_mysql_unix_port, 0))) @@ -155,6 +158,9 @@ static struct my_option my_long_options[] = {"character-sets-dir", 'c', "Directory where character sets are", (gptr*) &charsets_dir, (gptr*) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, + {"default-character-set", OPT_DEFAULT_CHARSET, + "Set the default character set.", (gptr*) &default_charset, + (gptr*) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, {"compress", 'C', "Use compression in server/client protocol", (gptr*) &opt_compress, (gptr*) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, @@ -558,7 +564,7 @@ list_fields(MYSQL *mysql,const char *db,const char *table, mysql_error(mysql)); return 1; } - end=strmov(strmov(query,"show /*!32332 FULL */ columns from "),table); + end=strmov(strmov(strmov(query,"show /*!32332 FULL */ columns from `"),table),"`"); if (wild && wild[0]) strxmov(end," like '",wild,"'",NullS); if (mysql_query(mysql,query) || !(result=mysql_store_result(mysql))) @@ -580,7 +586,7 @@ list_fields(MYSQL *mysql,const char *db,const char *table, print_res_top(result); if (opt_show_keys) { - end=strmov(strmov(query,"show keys from "),table); + end=strmov(strmov(strmov(query,"show keys from `"),table),"`"); if (mysql_query(mysql,query) || !(result=mysql_store_result(mysql))) { fprintf(stderr,"%s: Cannot list keys in db: %s, table: %s: %s\n", diff --git a/libmysqld/Makefile.am b/libmysqld/Makefile.am index 781dadadd71..c3c99583ad9 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 \ protocol.cc net_serv.cc opt_ft.cc opt_range.cc \ opt_sum.cc procedure.cc records.cc sql_acl.cc \ diff --git a/mysql-test/r/alter_table.result b/mysql-test/r/alter_table.result index 7bd836acefd..6c5e06a289f 100644 --- a/mysql-test/r/alter_table.result +++ b/mysql-test/r/alter_table.result @@ -156,9 +156,9 @@ create table t1 (i int unsigned not null auto_increment primary key); alter table t1 rename t2; alter table t2 rename t1, add c char(10) comment "no comment"; show columns from t1; -Field Type Collation Null Key Default Extra -i int(10) unsigned NULL PRI NULL auto_increment -c char(10) latin1_swedish_ci YES NULL +Field Type Null Key Default Extra +i int(10) unsigned PRI NULL auto_increment +c char(10) YES NULL drop table t1; create table t1 (a int, b int); insert into t1 values(1,100), (2,100), (3, 100); diff --git a/mysql-test/r/cast.result b/mysql-test/r/cast.result index 42164a2111b..a14ea4d61a6 100644 --- a/mysql-test/r/cast.result +++ b/mysql-test/r/cast.result @@ -28,6 +28,7 @@ cast("2001-1-1" as DATE) cast("2001-1-1" as DATETIME) select cast("1:2:3" as TIME); cast("1:2:3" as TIME) 01:02:03 +set names binary; select cast(_latin1'test' as char character set latin2); cast(_latin1'test' as char character set latin2) test diff --git a/mysql-test/r/create.result b/mysql-test/r/create.result index bd8343428c2..45b37dfb719 100644 --- a/mysql-test/r/create.result +++ b/mysql-test/r/create.result @@ -82,37 +82,37 @@ drop table t1,t2; create table t1(x varchar(50) ); create table t2 select x from t1 where 1=2; describe t1; -Field Type Collation Null Key Default Extra -x varchar(50) latin1_swedish_ci YES NULL +Field Type Null Key Default Extra +x varchar(50) YES NULL describe t2; -Field Type Collation Null Key Default Extra -x char(50) latin1_swedish_ci YES NULL +Field Type Null Key Default Extra +x char(50) YES NULL drop table t2; create table t2 select now() as a , curtime() as b, curdate() as c , 1+1 as d , 1.0 + 1 as e , 33333333333333333 + 3 as f; describe t2; -Field Type Collation Null Key Default Extra -a datetime NULL 0000-00-00 00:00:00 -b time NULL 00:00:00 -c date NULL 0000-00-00 -d bigint(17) NULL 0 -e double(18,1) NULL 0.0 -f bigint(17) NULL 0 +Field Type Null Key Default Extra +a datetime 0000-00-00 00:00:00 +b time 00:00:00 +c date 0000-00-00 +d bigint(17) 0 +e double(18,1) 0.0 +f bigint(17) 0 drop table t2; create table t2 select CAST("2001-12-29" AS DATE) as d, CAST("20:45:11" AS TIME) as t, CAST("2001-12-29 20:45:11" AS DATETIME) as dt; describe t2; -Field Type Collation Null Key Default Extra -d date NULL 0000-00-00 -t time NULL 00:00:00 -dt datetime NULL 0000-00-00 00:00:00 +Field Type Null Key Default Extra +d date 0000-00-00 +t time 00:00:00 +dt datetime 0000-00-00 00:00:00 drop table t1,t2; create table t1 (a tinyint); create table t2 (a int) select * from t1; describe t1; -Field Type Collation Null Key Default Extra -a tinyint(4) NULL YES NULL +Field Type Null Key Default Extra +a tinyint(4) YES NULL describe t2; -Field Type Collation Null Key Default Extra -a int(11) NULL YES NULL +Field Type Null Key Default Extra +a int(11) YES NULL drop table if exists t2; create table t2 (a int, a float) select * from t1; Duplicate column name 'a' diff --git a/mysql-test/r/ctype_collate.result b/mysql-test/r/ctype_collate.result index d2ae3950eaf..00a642d9ee6 100644 --- a/mysql-test/r/ctype_collate.result +++ b/mysql-test/r/ctype_collate.result @@ -489,8 +489,8 @@ t1 CREATE TABLE `t1` ( `latin1_f` char(32) NOT NULL default '' ) TYPE=MyISAM CHARSET=latin1 SHOW FIELDS FROM t1; -Field Type Collation Null Key Default Extra -latin1_f char(32) latin1_swedish_ci +Field Type Null Key Default Extra +latin1_f char(32) ALTER TABLE t1 CHANGE latin1_f latin1_f CHAR(32) CHARACTER SET latin1 COLLATE latin1_bin; SHOW CREATE TABLE t1; @@ -499,8 +499,8 @@ t1 CREATE TABLE `t1` ( `latin1_f` char(32) character set latin1 collate latin1_bin default NULL ) TYPE=MyISAM CHARSET=latin1 SHOW FIELDS FROM t1; -Field Type Collation Null Key Default Extra -latin1_f char(32) latin1_bin YES NULL +Field Type Null Key Default Extra +latin1_f char(32) YES NULL ALTER TABLE t1 CHARACTER SET latin1 COLLATE latin1_bin; SHOW CREATE TABLE t1; Table Create Table @@ -508,8 +508,8 @@ t1 CREATE TABLE `t1` ( `latin1_f` char(32) collate latin1_bin default NULL ) TYPE=MyISAM CHARSET=latin1 COLLATE=latin1_bin SHOW FIELDS FROM t1; -Field Type Collation Null Key Default Extra -latin1_f char(32) latin1_bin YES NULL +Field Type Null Key Default Extra +latin1_f char(32) YES NULL SET CHARACTER SET 'latin1'; SHOW VARIABLES LIKE 'character_set_client'; Variable_name Value diff --git a/mysql-test/r/ctype_recoding.result b/mysql-test/r/ctype_recoding.result index fb9e8224111..23a90be1306 100644 --- a/mysql-test/r/ctype_recoding.result +++ b/mysql-test/r/ctype_recoding.result @@ -13,8 +13,8 @@ Table Create Table `ÐÏÌÅ` char(32) character set koi8r NOT NULL default '' ) TYPE=MyISAM CHARSET=latin1 SHOW FIELDS FROM ÔÁÂÌÉÃÁ; -Field Type Collation Null Key Default Extra -ÐÏÌÅ char(32) koi8r_general_ci +Field Type Null Key Default Extra +ÐÏÌÅ char(32) SET CHARACTER SET cp1251; SHOW TABLES; Tables_in_test @@ -25,8 +25,8 @@ Table Create Table `ïîëå` char(32) character set koi8r NOT NULL default '' ) TYPE=MyISAM CHARSET=latin1 SHOW FIELDS FROM òàáëèöà; -Field Type Collation Null Key Default Extra -ïîëå char(32) koi8r_general_ci +Field Type Null Key Default Extra +ïîëå char(32) SET CHARACTER SET utf8; SHOW TABLES; Tables_in_test @@ -37,8 +37,8 @@ Table Create Table `поле` char(32) character set koi8r NOT NULL default '' ) TYPE=MyISAM CHARSET=latin1 SHOW FIELDS FROM таблица; -Field Type Collation Null Key Default Extra -поле char(32) koi8r_general_ci +Field Type Null Key Default Extra +поле char(32) SET CHARACTER SET koi8r; DROP TABLE ÔÁÂÌÉÃÁ; SET CHARACTER SET default; diff --git a/mysql-test/r/gis.result b/mysql-test/r/gis.result index 746cdecdfdb..4317daea0b3 100644 --- a/mysql-test/r/gis.result +++ b/mysql-test/r/gis.result @@ -8,37 +8,37 @@ CREATE TABLE mp (fid INTEGER NOT NULL PRIMARY KEY, g MULTIPOLYGON); CREATE TABLE gc (fid INTEGER NOT NULL PRIMARY KEY, g GEOMETRYCOLLECTION); CREATE TABLE geo (fid INTEGER NOT NULL PRIMARY KEY, g GEOMETRY); SHOW FIELDS FROM pt; -Field Type Collation Null Key Default Extra -fid int(11) NULL PRI 0 -g point NULL YES NULL +Field Type Null Key Default Extra +fid int(11) PRI 0 +g point YES NULL SHOW FIELDS FROM ls; -Field Type Collation Null Key Default Extra -fid int(11) NULL PRI 0 -g linestring NULL YES NULL +Field Type Null Key Default Extra +fid int(11) PRI 0 +g linestring YES NULL SHOW FIELDS FROM p; -Field Type Collation Null Key Default Extra -fid int(11) NULL PRI 0 -g polygon NULL YES NULL +Field Type Null Key Default Extra +fid int(11) PRI 0 +g polygon YES NULL SHOW FIELDS FROM mpt; -Field Type Collation Null Key Default Extra -fid int(11) NULL PRI 0 -g multipoint NULL YES NULL +Field Type Null Key Default Extra +fid int(11) PRI 0 +g multipoint YES NULL SHOW FIELDS FROM mls; -Field Type Collation Null Key Default Extra -fid int(11) NULL PRI 0 -g multilinestring NULL YES NULL +Field Type Null Key Default Extra +fid int(11) PRI 0 +g multilinestring YES NULL SHOW FIELDS FROM mp; -Field Type Collation Null Key Default Extra -fid int(11) NULL PRI 0 -g multipolygon NULL YES NULL +Field Type Null Key Default Extra +fid int(11) PRI 0 +g multipolygon YES NULL SHOW FIELDS FROM gc; -Field Type Collation Null Key Default Extra -fid int(11) NULL PRI 0 -g geometrycollection NULL YES NULL +Field Type Null Key Default Extra +fid int(11) PRI 0 +g geometrycollection YES NULL SHOW FIELDS FROM geo; -Field Type Collation Null Key Default Extra -fid int(11) NULL PRI 0 -g geometry NULL YES NULL +Field Type Null Key Default Extra +fid int(11) PRI 0 +g geometry YES NULL INSERT INTO pt VALUES (101, PointFromText('POINT(10 10)')), (102, PointFromText('POINT(20 10)')), @@ -366,27 +366,27 @@ gc geometrycollection, gm geometry ); SHOW FIELDS FROM g1; -Field Type Collation Null Key Default Extra -pt point NULL YES NULL -ln linestring NULL YES NULL -pg polygon NULL YES NULL -mpt multipoint NULL YES NULL -mln multilinestring NULL YES NULL -mpg multipolygon NULL YES NULL -gc geometrycollection NULL YES NULL -gm geometry NULL YES NULL +Field Type Null Key Default Extra +pt point YES NULL +ln linestring YES NULL +pg polygon YES NULL +mpt multipoint YES NULL +mln multilinestring YES NULL +mpg multipolygon YES NULL +gc geometrycollection YES NULL +gm geometry YES NULL ALTER TABLE g1 ADD fid INT NOT NULL; SHOW FIELDS FROM g1; -Field Type Collation Null Key Default Extra -pt point NULL YES NULL -ln linestring NULL YES NULL -pg polygon NULL YES NULL -mpt multipoint NULL YES NULL -mln multilinestring NULL YES NULL -mpg multipolygon NULL YES NULL -gc geometrycollection NULL YES NULL -gm geometry NULL YES NULL -fid int(11) NULL 0 +Field Type Null Key Default Extra +pt point YES NULL +ln linestring YES NULL +pg polygon YES NULL +mpt multipoint YES NULL +mln multilinestring YES NULL +mpg multipolygon YES NULL +gc geometrycollection YES NULL +gm geometry YES NULL +fid int(11) 0 DROP TABLE g1; SELECT AsText(GeometryFromWKB(AsWKB(GeometryFromText('POINT(1 4)')))); AsText(GeometryFromWKB(AsWKB(GeometryFromText('POINT(1 4)')))) diff --git a/mysql-test/r/have_ucs2.require b/mysql-test/r/have_ucs2.require index 4f2ef7e83e4..c53250aeaef 100644 --- a/mysql-test/r/have_ucs2.require +++ b/mysql-test/r/have_ucs2.require @@ -1,2 +1,2 @@ -Collation Charset Id D C Sortlen -ucs2_general_ci ucs2 35 Y Y 1 +Collation Charset Id Default Compiled Sortlen +ucs2_general_ci ucs2 35 Yes Yes 1 diff --git a/mysql-test/r/have_ujis.require b/mysql-test/r/have_ujis.require index b4b0a4d256c..b4de2234ec7 100644 --- a/mysql-test/r/have_ujis.require +++ b/mysql-test/r/have_ujis.require @@ -1,2 +1,2 @@ -Collation Charset Id D C Sortlen -ujis_japanese_ci ujis 12 Y Y 0 +Collation Charset Id Default Compiled Sortlen +ujis_japanese_ci ujis 12 Yes Yes 0 diff --git a/mysql-test/r/innodb.result b/mysql-test/r/innodb.result index 0f65c8b0fe3..71dceddab29 100644 --- a/mysql-test/r/innodb.result +++ b/mysql-test/r/innodb.result @@ -906,8 +906,8 @@ id select_type table type possible_keys key key_len ref rows Extra drop table t1; create table t1 (t int not null default 1, key (t)) type=innodb; desc t1; -Field Type Collation Null Key Default Extra -t int(11) NULL MUL 1 +Field Type Null Key Default Extra +t int(11) MUL 1 drop table t1; CREATE TABLE t1 ( number bigint(20) NOT NULL default '0', diff --git a/mysql-test/r/show_check.result b/mysql-test/r/show_check.result index c1f2adc1e31..b867eae5e8d 100644 --- a/mysql-test/r/show_check.result +++ b/mysql-test/r/show_check.result @@ -155,13 +155,13 @@ t1 CREATE TABLE `t1` ( drop table t1; create table t1 (a decimal(9,2), b decimal (9,0), e double(9,2), f double(5,0), h float(3,2), i float(3,0)); show columns from t1; -Field Type Collation Null Key Default Extra -a decimal(9,2) NULL YES NULL -b decimal(9,0) NULL YES NULL -e double(9,2) NULL YES NULL -f double(5,0) NULL YES NULL -h float(3,2) NULL YES NULL -i float(3,0) NULL YES NULL +Field Type Null Key Default Extra +a decimal(9,2) YES NULL +b decimal(9,0) YES NULL +e double(9,2) YES NULL +f double(5,0) YES NULL +h float(3,2) YES NULL +i float(3,0) YES NULL show full columns from t1; Field Type Collation Null Key Default Extra Privileges Comment a decimal(9,2) NULL YES NULL select,insert,update,references @@ -229,16 +229,16 @@ type_bool type_tiny type_short type_mediumint type_bigint type_decimal type_nume drop table t1; create table t1 (c decimal, d double, f float, r real); show columns from t1; -Field Type Collation Null Key Default Extra -c decimal(10,0) NULL YES NULL -d double NULL YES NULL -f float NULL YES NULL -r double NULL YES NULL +Field Type Null Key Default Extra +c decimal(10,0) YES NULL +d double YES NULL +f float YES NULL +r double YES NULL drop table t1; create table t1 (c decimal(3,3), d double(3,3), f float(3,3)); show columns from t1; -Field Type Collation Null Key Default Extra -c decimal(4,3) NULL YES NULL -d double(4,3) NULL YES NULL -f float(4,3) NULL YES NULL +Field Type Null Key Default Extra +c decimal(4,3) YES NULL +d double(4,3) YES NULL +f float(4,3) YES NULL drop table t1; diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index 41620bb6e7f..511517ebdfd 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -1134,3 +1134,17 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ref salary salary 5 const 1 Using where 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away drop table t1; +CREATE TABLE t1 ( +ID int(10) unsigned NOT NULL auto_increment, +SUB_ID int(3) unsigned NOT NULL default '0', +REF_ID int(10) unsigned default NULL, +REF_SUB int(3) unsigned default '0', +PRIMARY KEY (ID,SUB_ID), +UNIQUE KEY t1_PK (ID,SUB_ID), +KEY t1_FK (REF_ID,REF_SUB), +KEY t1_REFID (REF_ID) +) TYPE=MyISAM CHARSET=cp1251; +INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL); +SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2); +REF_ID +DROP TABLE t1; diff --git a/mysql-test/r/type_blob.result b/mysql-test/r/type_blob.result index f97e2bc06b5..ce1cd392819 100644 --- a/mysql-test/r/type_blob.result +++ b/mysql-test/r/type_blob.result @@ -1,22 +1,22 @@ drop table if exists t1,t2,t3,t4,t5,t6,t7; CREATE TABLE t1 (a blob, b text, c blob(250), d text(70000), e text(70000000)); show columns from t1; -Field Type Collation Null Key Default Extra -a blob NULL YES NULL -b text latin1_swedish_ci YES NULL -c blob NULL YES NULL -d mediumtext latin1_swedish_ci YES NULL -e longtext latin1_swedish_ci YES NULL +Field Type Null Key Default Extra +a blob YES NULL +b text YES NULL +c blob YES NULL +d mediumtext YES NULL +e longtext YES NULL CREATE TABLE t2 (a char(257), b varchar(70000) binary, c varchar(70000000)); Warnings: Warning 1244 Converting column 'a' from CHAR to TEXT Warning 1244 Converting column 'b' from CHAR to BLOB Warning 1244 Converting column 'c' from CHAR to TEXT show columns from t2; -Field Type Collation Null Key Default Extra -a text latin1_swedish_ci YES NULL -b mediumblob NULL YES NULL -c longtext latin1_swedish_ci YES NULL +Field Type Null Key Default Extra +a text YES NULL +b mediumblob YES NULL +c longtext YES NULL create table t3 (a long, b long byte); show create TABLE t3; Table Create Table diff --git a/mysql-test/t/cast.test b/mysql-test/t/cast.test index 59b8f3b0c75..8241d491465 100644 --- a/mysql-test/t/cast.test +++ b/mysql-test/t/cast.test @@ -16,6 +16,7 @@ select cast("1:2:3" as TIME); # # Character set convertion # +set names binary; select cast(_latin1'test' as char character set latin2); select cast(_koi8r'ÔÅÓÔ' as char character set cp1251); create table t1 select cast(_koi8r'ÔÅÓÔ' as char character set cp1251) as t; diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index 8377a756c5b..e47409046cd 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -716,3 +716,17 @@ create table t1 (id int not null auto_increment primary key, salary int, key(sal insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000); explain SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1); drop table t1; + +CREATE TABLE t1 ( + ID int(10) unsigned NOT NULL auto_increment, + SUB_ID int(3) unsigned NOT NULL default '0', + REF_ID int(10) unsigned default NULL, + REF_SUB int(3) unsigned default '0', + PRIMARY KEY (ID,SUB_ID), + UNIQUE KEY t1_PK (ID,SUB_ID), + KEY t1_FK (REF_ID,REF_SUB), + KEY t1_REFID (REF_ID) +) TYPE=MyISAM CHARSET=cp1251; +INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL); +SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2); +DROP TABLE t1; diff --git a/sql/Makefile.am b/sql/Makefile.am index eeaf96ce190..c3b7c77e252 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 470937f8ee7..2d285bbe434 100644 --- a/sql/item.h +++ b/sql/item.h @@ -644,6 +644,7 @@ public: #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 4f55357f288..299014689f0 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 ff469deab30..4438b902011 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 7ed9e5c85f6..6482d81f484 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 e1d6156c12c..8ef2b85de52 100644 --- a/sql/item_func.h +++ b/sql/item_func.h @@ -1037,101 +1037,6 @@ public: }; -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 00000000000..06ba67cbc73 --- /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 00000000000..21e94735f31 --- /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 ae48aeb115a..06569095018 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -2522,428 +2522,6 @@ null: 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 8ae5c995375..65aad3a3fff 100644 --- a/sql/item_strfunc.h +++ b/sql/item_strfunc.h @@ -628,242 +628,3 @@ public: 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"; } -}; - -*/ diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index c749fba616f..8c9b3eeebde 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -79,8 +79,10 @@ void Item_subselect::select_transformer(THD *thd, st_select_lex_unit *unit) } -bool Item_subselect::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) +bool Item_subselect::fix_fields(THD *thd_param, TABLE_LIST *tables, Item **ref) { + thd= thd_param; + if (substitution) { (*ref)= substitution; @@ -115,6 +117,20 @@ bool Item_subselect::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) return res; } +bool Item_subselect::exec() +{ + MEM_ROOT *old_root= my_pthread_getspecific_ptr(MEM_ROOT*, THR_MALLOC); + if (&thd->mem_root != old_root) + { + my_pthread_setspecific_ptr(THR_MALLOC, &thd->mem_root); + int res= engine->exec(); + my_pthread_setspecific_ptr(THR_MALLOC, old_root); + return (res); + } + else + return engine->exec(); +} + Item::Type Item_subselect::type() const { return SUBSELECT_ITEM; @@ -253,12 +269,12 @@ bool Item_singlerow_subselect::null_inside() void Item_singlerow_subselect::bring_value() { - engine->exec(); + exec(); } double Item_singlerow_subselect::val () { - if (!engine->exec() && !value->null_value) + if (!exec() && !value->null_value) { null_value= 0; return value->val(); @@ -272,7 +288,7 @@ double Item_singlerow_subselect::val () longlong Item_singlerow_subselect::val_int () { - if (!engine->exec() && !value->null_value) + if (!exec() && !value->null_value) { null_value= 0; return value->val_int(); @@ -286,7 +302,7 @@ longlong Item_singlerow_subselect::val_int () String *Item_singlerow_subselect::val_str (String *str) { - if (!engine->exec() && !value->null_value) + if (!exec() && !value->null_value) { null_value= 0; return value->val_str(str); @@ -354,7 +370,7 @@ void Item_exists_subselect::fix_length_and_dec() double Item_exists_subselect::val () { - if (engine->exec()) + if (exec()) { reset(); return 0; @@ -364,7 +380,7 @@ double Item_exists_subselect::val () longlong Item_exists_subselect::val_int () { - if (engine->exec()) + if (exec()) { reset(); return 0; @@ -374,7 +390,7 @@ longlong Item_exists_subselect::val_int () String *Item_exists_subselect::val_str(String *str) { - if (engine->exec()) + if (exec()) { reset(); return 0; @@ -385,7 +401,7 @@ String *Item_exists_subselect::val_str(String *str) double Item_in_subselect::val () { - if (engine->exec()) + if (exec()) { reset(); null_value= 1; @@ -398,7 +414,7 @@ double Item_in_subselect::val () longlong Item_in_subselect::val_int () { - if (engine->exec()) + if (exec()) { reset(); null_value= 1; @@ -411,7 +427,7 @@ longlong Item_in_subselect::val_int () String *Item_in_subselect::val_str(String *str) { - if (engine->exec()) + if (exec()) { reset(); null_value= 1; diff --git a/sql/item_subselect.h b/sql/item_subselect.h index fc4dad5a6b3..3ed3f2af0e9 100644 --- a/sql/item_subselect.h +++ b/sql/item_subselect.h @@ -36,6 +36,8 @@ class Item_subselect :public Item_result_field my_bool engine_owner; /* Is this item owner of engine */ my_bool value_assigned; /* value already assigned to subselect */ protected: + /* thread handler, will be assigned in fix_fields only */ + THD *thd; /* substitution instead of subselect in case of optimization */ Item *substitution; /* engine that perform execution of subselect (single select or union) */ @@ -81,6 +83,7 @@ public: return null_value; } bool fix_fields(THD *thd, TABLE_LIST *tables, Item **ref); + bool exec(); virtual void fix_length_and_dec(); table_map used_tables() const; diff --git a/sql/mysqld.cc b/sql/mysqld.cc index 224bfe5f878..7011f6dcd36 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -2048,7 +2048,7 @@ static int init_common_variables(const char *conf_file_name, int argc, } global_system_variables.character_set_server= default_charset_info; global_system_variables.character_set_database= default_charset_info; - global_system_variables.character_set_results= NULL; + global_system_variables.character_set_results= default_charset_info; global_system_variables.character_set_client= default_charset_info; global_system_variables.collation_connection= default_charset_info; @@ -4689,7 +4689,7 @@ static void mysql_init_variables(void) /* Set default values for some option variables */ global_system_variables.character_set_server= default_charset_info; global_system_variables.character_set_database= default_charset_info; - global_system_variables.character_set_results= NULL; + global_system_variables.character_set_results= default_charset_info; global_system_variables.character_set_client= default_charset_info; global_system_variables.collation_connection= default_charset_info; global_system_variables.table_type= DB_TYPE_MYISAM; diff --git a/sql/protocol.cc b/sql/protocol.cc index 848321c1576..7abbf3ce85b 100644 --- a/sql/protocol.cc +++ b/sql/protocol.cc @@ -499,7 +499,7 @@ bool Protocol::send_fields(List<Item> *list, uint flag) String tmp((char*) buff,sizeof(buff),&my_charset_bin); Protocol_simple prot(thd); String *packet= prot.storage_packet(); - CHARSET_INFO *thd_charset= thd->charset(); + CHARSET_INFO *thd_charset= thd->variables.character_set_results; DBUG_ENTER("send_fields"); if (flag & 1) diff --git a/sql/set_var.cc b/sql/set_var.cc index eb44dd1dbcb..60b0d24430d 100644 --- a/sql/set_var.cc +++ b/sql/set_var.cc @@ -1355,7 +1355,7 @@ CHARSET_INFO ** sys_var_character_set_results::ci_ptr(THD *thd, enum_var_type ty void sys_var_character_set_results::set_default(THD *thd, enum_var_type type) { if (type == OPT_GLOBAL) - global_system_variables.character_set_results= NULL; + global_system_variables.character_set_results= default_charset_info; else thd->variables.character_set_results= global_system_variables.character_set_results; } diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index b1e0b2dc91d..9ebb6305f70 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -59,7 +59,7 @@ static void refresh_status(void); static bool append_file_to_dir(THD *thd, char **filename_ptr, char *table_name); -inline bool single_table_command_access(THD *thd, ulong privilege, +static bool single_table_command_access(THD *thd, ulong privilege, TABLE_LIST *tables, int *res); const char *any_db="*any*"; // Special symbol for check_access @@ -669,9 +669,12 @@ check_connections(THD *thd) global_system_variables.character_set_client; thd->variables.collation_connection= global_system_variables.collation_connection; + thd->variables.character_set_results= + global_system_variables.character_set_results; } else { + thd->variables.character_set_results= thd->variables.collation_connection= thd->variables.character_set_client; } @@ -1319,7 +1322,8 @@ restore_user: break; #else { - char *fields; + char *fields, *pend; + String convname; TABLE_LIST table_list; statistic_increment(com_stat[SQLCOM_SHOW_FIELDS],&LOCK_status); bzero((char*) &table_list,sizeof(table_list)); @@ -1329,8 +1333,11 @@ restore_user: break; } thd->free_list=0; - table_list.alias= table_list.real_name= thd->strdup(packet); - packet=strend(packet)+1; + pend= strend(packet); + convname.copy(packet, pend-packet, + thd->variables.character_set_client, system_charset_info); + table_list.alias= table_list.real_name= convname.c_ptr(); + packet= pend+1; // command not cachable => no gap for data base name if (!(thd->query=fields=thd->memdup(packet,thd->query_length+1))) break; @@ -3116,7 +3123,7 @@ error: 1 - access denied */ -inline bool single_table_command_access(THD *thd, ulong privilege, +static bool single_table_command_access(THD *thd, ulong privilege, TABLE_LIST *tables, int *res) { @@ -3128,12 +3135,14 @@ inline bool single_table_command_access(THD *thd, ulong privilege, tables->next= 0; if (grant_option && check_grant(thd, privilege, tables)) return 1; - tables->next= subselects_tables; // check rights on tables of subselect (if exists) - if (subselects_tables && - (*res= check_table_access(thd, SELECT_ACL, subselects_tables))) - return 1; + if (subselects_tables) + { + tables->next= subselects_tables; + if ((*res= check_table_access(thd, SELECT_ACL, subselects_tables))) + return 1; + } return 0; } diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 16934e33798..803280c56c6 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -679,7 +679,8 @@ mysqld_show_fields(THD *thd, TABLE_LIST *table_list,const char *wild, List<Item> field_list; field_list.push_back(new Item_empty_string("Field",NAME_LEN)); field_list.push_back(new Item_empty_string("Type",40)); - field_list.push_back(new Item_empty_string("Collation",40)); + if (verbose) + field_list.push_back(new Item_empty_string("Collation",40)); field_list.push_back(new Item_empty_string("Null",1)); field_list.push_back(new Item_empty_string("Key",3)); field_list.push_back(item=new Item_empty_string("Default",NAME_LEN)); @@ -719,7 +720,8 @@ mysqld_show_fields(THD *thd, TABLE_LIST *table_list,const char *wild, protocol->store(field->field_name, system_charset_info); field->sql_type(type); protocol->store(type.ptr(), type.length(), system_charset_info); - protocol->store(field->has_charset() ? field->charset()->name : "NULL", + if (verbose) + protocol->store(field->has_charset() ? field->charset()->name : "NULL", system_charset_info); pos=(byte*) ((flags & NOT_NULL_FLAG) && field->type() != FIELD_TYPE_TIMESTAMP ? @@ -1436,8 +1438,8 @@ static bool write_collation(Protocol *protocol, CHARSET_INFO *cs) protocol->store(cs->name, system_charset_info); protocol->store(cs->csname, system_charset_info); protocol->store_short((longlong) cs->number); - protocol->store((cs->state & MY_CS_PRIMARY) ? "Y" : "",system_charset_info); - protocol->store((cs->state & MY_CS_COMPILED)? "Y" : "",system_charset_info); + protocol->store((cs->state & MY_CS_PRIMARY) ? "Yes" : "",system_charset_info); + protocol->store((cs->state & MY_CS_COMPILED)? "Yes" : "",system_charset_info); protocol->store_short((longlong) cs->strxfrm_multiply); return protocol->write(); } @@ -1456,8 +1458,8 @@ int mysqld_show_collations(THD *thd, const char *wild) field_list.push_back(new Item_empty_string("Collation",30)); field_list.push_back(new Item_empty_string("Charset",30)); field_list.push_back(new Item_return_int("Id",11, FIELD_TYPE_SHORT)); - field_list.push_back(new Item_empty_string("D",30)); - field_list.push_back(new Item_empty_string("C",30)); + field_list.push_back(new Item_empty_string("Default",30)); + field_list.push_back(new Item_empty_string("Compiled",30)); field_list.push_back(new Item_return_int("Sortlen",3, FIELD_TYPE_SHORT)); if (protocol->send_fields(&field_list, 1)) diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 3f96826d445..1002d06be88 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -4452,7 +4452,7 @@ option_value: net_printf(thd,ER_COLLATION_CHARSET_MISMATCH,$3->name,$2->csname); YYABORT; } - lex->var_list.push_back(new set_var_collation_client($3,$3,NULL)); + lex->var_list.push_back(new set_var_collation_client($3,$3,$3)); } | PASSWORD equal text_or_password { diff --git a/sql/udf_example.cc b/sql/udf_example.cc index dfe8177bfce..7f4417bf8fe 100644 --- a/sql/udf_example.cc +++ b/sql/udf_example.cc @@ -284,8 +284,8 @@ char *metaphon(UDF_INIT *initid, UDF_ARGS *args, char *result, for (n = ntrans + 1, n_end = ntrans + sizeof(ntrans)-2; word != w_end && n < n_end; word++ ) - if ( my_isalpha ( my_charset_latin1, *word )) - *n++ = my_toupper ( my_charset_latin1, *word ); + if ( my_isalpha ( &my_charset_latin1, *word )) + *n++ = my_toupper ( &my_charset_latin1, *word ); if ( n == ntrans + 1 ) /* return empty string if 0 bytes */ { diff --git a/strings/ctype-ucs2.c b/strings/ctype-ucs2.c index 17bc920a381..8cb8f84f281 100644 --- a/strings/ctype-ucs2.c +++ b/strings/ctype-ucs2.c @@ -25,6 +25,10 @@ #ifdef HAVE_CHARSET_ucs2 +#ifndef EILSEQ +#define EILSEQ ENOENT +#endif + extern MY_UNICASE_INFO *uni_plane[256]; static uchar ctype_ucs2[] = { |