home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / sharewar / mysql / data1.cab / Development / bench / crash-me < prev    next >
Encoding:
Text File  |  2001-11-02  |  105.1 KB  |  3,532 lines

  1. #!/my/gnu/bin/perl -w
  2. # Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Library General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. # Library General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Library General Public
  15. # License along with this library; if not, write to the Free
  16. # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  17. # MA 02111-1307, USA
  18.  
  19. # Written by Monty for the TCX/Monty Program/Detron benchmark suite.
  20. # Empress and PostgreSQL patches by Luuk de Boer
  21. # Extensions for ANSI SQL and Mimer by Bengt Gunne
  22. # Some additions and corrections by Matthias Urlich
  23. #
  24. # This programs tries to find all limits for a sql server
  25. # It gets the name from what it does to most servers :)
  26. #
  27. # Be sure to use --help before running this!
  28. #
  29. # If you want to add support for another server, add a new package for the
  30. # server in server-cfg.  You only have to support the 'new' and 'version'
  31. # functions. new doesn't need to have any limits if one doesn't want to
  32. # use the benchmarks.
  33. #
  34.  
  35. # TODO:
  36. # CMT includes types and functions which are synonyms for other types
  37. # and functions, including those in SQL9x. It should label those synonyms
  38. # as such, and clarify ones such as "mediumint" with comments such as
  39. # "3-byte int" or "same as xxx".
  40.  
  41. $version="1.56";
  42.  
  43. use DBI;
  44. use Getopt::Long;
  45. chomp($pwd = `pwd`); $pwd = "." if ($pwd eq '');
  46. require "$pwd/server-cfg" || die "Can't read Configuration file: $!\n";
  47.  
  48. $opt_server="mysql"; $opt_host="localhost"; $opt_database="test";
  49. $opt_dir="limits";
  50. $opt_user=$opt_password="";
  51. $opt_debug=$opt_help=$opt_Information=$opt_restart=$opt_force=$opt_quick=0;
  52. $opt_log_all_queries=$opt_fix_limit_file=$opt_batch_mode=0;
  53. $opt_db_start_cmd="";           # the db server start command
  54. $opt_check_server=0;        # Check if server is alive before each query
  55. $opt_sleep=10;                  # time to sleep while starting the db server
  56. $limit_changed=0;               # For configure file
  57. $reconnect_count=0;
  58. $opt_comment=$opt_config_file=$opt_log_queries_to_file="";
  59. $limits{'crash_me_safe'}='yes';
  60. $prompts{'crash_me_safe'}='crash me safe';
  61. $limits{'operating_system'}= machine();
  62. $prompts{'operating_system'}='crash-me tested on';
  63. $retry_limit=3;
  64.  
  65. GetOptions("Information","help","server=s","debug","user=s","password=s","database=s","restart","force","quick","log-all-queries","comment=s","host=s","fix-limit-file","dir=s","db-start-cmd=s","sleep=s","batch-mode","config-file=s","log-queries-to-file=s","check-server") || usage();
  66. usage() if ($opt_help || $opt_Information);
  67.  
  68. $opt_config_file="$pwd/$opt_dir/$opt_server.cfg" if (length($opt_config_file) == 0);
  69.  
  70. if ($opt_fix_limit_file)
  71. {
  72.   print "Fixing limit file for $opt_server\n";
  73.   read_config_data();
  74.   $limit_changed=1;
  75.   save_all_config_data();
  76.   exit 0;
  77. }
  78.  
  79. $server=get_server($opt_server,$opt_host,$opt_database);
  80. $opt_server=$server->{'cmp_name'};
  81.  
  82. $|=1;                           # For debugging
  83.  
  84. print "Running $0 $version on '",($server_version=$server->version()),"'\n\n";
  85. print "I hope you didn't have anything important running on this server....\n";
  86. read_config_data();
  87. if ($limit_changed)             # Must have been restarted
  88. {
  89.   save_config_data('crash_me_safe','no',"crash me safe");
  90. }
  91.  
  92. if (!$opt_force && !$opt_batch_mode)
  93. {
  94.   server_info();
  95. }
  96. else
  97. {
  98.   print "Using --force.  I assume you know what you are doing...\n";
  99. }
  100. print "\n";
  101.  
  102. save_config_data('crash_me_version',$version,"crash me version");
  103. if ($server_version)
  104. {
  105.   save_config_data('server_version',$server_version,"server version");
  106. }
  107. if (length($opt_comment))
  108. {
  109.   save_config_data('user_comment',$opt_comment,"comment");
  110. }
  111.  
  112. $opt_log=0;
  113. if (length($opt_log_queries_to_file))
  114. {
  115.   open(LOG,">$opt_log_queries_to_file") || die "Can't open file $opt_log_queries_to_file\n";
  116.   $opt_log=1;
  117. }
  118.  
  119. #
  120. # Set up some limits that's regared as unlimited
  121. # We don't want to take up all resources from the server...
  122. #
  123.  
  124. $max_connections="+1000";       # Number of simultaneous connections
  125. $max_buffer_size="+16000000";   # size of communication buffer.
  126. $max_string_size="+8000000";    # Enough for this test
  127. $max_name_length="+512";        # Actually 256, but ...
  128. $max_keys="+64";                # Probably too big.
  129. $max_join_tables="+64";         # Probably too big.
  130. $max_columns="+8192";           # Probably too big.
  131. $max_row_length=$max_string_size;
  132. $max_key_length="+8192";        # Big enough
  133. $max_order_by="+64";        # Big enough
  134. $max_expressions="+10000";
  135. $max_big_expressions="+100";
  136. $max_stacked_expressions="+2000";
  137. $query_size=$max_buffer_size;
  138. $longreadlen=16000000;        # For retrieval buffer
  139.  
  140.  
  141. #
  142. # First do some checks that needed for the rest of the benchmark
  143. #
  144. use sigtrap;               # Must be removed with perl5.005_2 on Win98
  145. $SIG{PIPE} = 'IGNORE';
  146. $SIG{SEGV} = sub {warn('SEGFAULT')};
  147. $dbh=safe_connect();
  148.  
  149. #
  150. # Test if the database require RESTRICT/CASCADE after DROP TABLE
  151. #
  152.  
  153. # Really remove the crash_me table
  154. $prompt="drop table require cascade/restrict";
  155. $drop_attr="";
  156. $dbh->do("drop table crash_me");
  157. $dbh->do("drop table crash_me cascade");
  158. if (!safe_query(["create table crash_me (a integer not null)",
  159.          "drop table crash_me"]))
  160. {
  161.   $dbh->do("drop table crash_me cascade");  
  162.   if (safe_query(["create table crash_me (a integer not null)",
  163.           "drop table crash_me cascade"]))
  164.   {
  165.     save_config_data('drop_requires_cascade',"yes","$prompt");
  166.     $drop_attr="cascade";
  167.   }
  168.   else
  169.   {
  170.     die "Can't create and drop table 'crash_me'\n";
  171.   }
  172. }
  173. else
  174. {
  175.   save_config_data('drop_requires_cascade',"no","$prompt");
  176.   $drop_attr="";
  177. }
  178.  
  179. # Remove tables from old runs
  180. $dbh->do("drop table crash_me $drop_attr");
  181. $dbh->do("drop table crash_me2 $drop_attr");
  182. $dbh->do("drop table crash_me3 $drop_attr");
  183. $dbh->do("drop table crash_q $drop_attr");
  184. $dbh->do("drop table crash_q1 $drop_attr");
  185.  
  186. $prompt="Tables without primary key";
  187. if (!safe_query(["create table crash_me (a integer not null,b char(10) not null)",
  188.          "insert into crash_me (a,b) values (1,'a')"]))
  189. {
  190.   if (!safe_query(["create table crash_me (a integer not null,b char(10) not null, primary key (a))",
  191.            "insert into crash_me (a,b) values (1,'a')"]))
  192.   {
  193.     die "Can't create table 'crash_me' with one record: $DBI::errstr\n";
  194.   }
  195.   save_config_data('no_primary_key',"no",$prompt);
  196. }
  197. else
  198. {
  199.   save_config_data('no_primary_key',"yes",$prompt);
  200. }
  201.  
  202. #
  203. #  Define strings for character NULL and numeric NULL used in expressions
  204. #
  205. $char_null=$server->{'char_null'};
  206. $numeric_null=$server->{'numeric_null'};
  207. if ($char_null eq '')
  208. {
  209.   $char_null="NULL";
  210. }
  211. if ($numeric_null eq '')
  212. {
  213.   $numeric_null="NULL";
  214. }
  215.  
  216. print "$prompt: $limits{'no_primary_key'}\n";
  217.  
  218. report("SELECT without FROM",'select_without_from',"select 1");
  219. if ($limits{'select_without_from'} ne "yes")
  220. {
  221.   $end_query=" from crash_me";
  222.   $check_connect="select a from crash_me";
  223. }
  224. else
  225. {
  226.   $end_query="";
  227.   $check_connect="select 1";
  228. }
  229.  
  230. assert($check_connect);
  231. assert("select a from crash_me where b<'b'");
  232.  
  233. report("Select constants",'select_constants',"select 1 $end_query");
  234. report("Select table_name.*",'table_wildcard',
  235.        "select crash_me.* from crash_me");
  236. report("Allows \' and \" as string markers",'quote_with_"',
  237.        'select a from crash_me where b<"c"');
  238. check_and_report("Double '' as ' in strings",'double_quotes',[],
  239.          "select 'Walker''s' $end_query",[],"Walker's",1);
  240. check_and_report("Multiple line strings","multi_strings",[],
  241.          "select a from crash_me where b < 'a'\n'b'",[],"1",0);
  242. check_and_report("\" as identifier quote (ANSI SQL)",'quote_ident_with_"',[],
  243.          'select "A" from crash_me',[],"1",0);
  244. check_and_report("\` as identifier quote",'quote_ident_with_`',[],
  245.          'select `A` from crash_me',[],"1",0);
  246. check_and_report("[] as identifier quote",'quote_ident_with_[',[],
  247.          'select [A] from crash_me',[],"1",0);
  248.  
  249. report("Column alias","column_alias","select a as ab from crash_me");
  250. report("Table alias","table_alias","select b.a from crash_me as b");
  251. report("Functions",'functions',"select 1+1 $end_query");
  252. report("Group functions",'group_functions',"select count(*) from crash_me");
  253. report("Group functions with distinct",'group_distinct_functions',
  254.        "select count(distinct a) from crash_me");
  255. report("Group by",'group_by',"select a from crash_me group by a");
  256. report("Group by position",'group_by_position',
  257.        "select a from crash_me group by 1");
  258. report("Group by alias",'group_by_alias',
  259.        "select a as ab from crash_me group by ab");
  260. report("Group on unused column",'group_on_unused',
  261.        "select count(*) from crash_me group by a");
  262.  
  263. report("Order by",'order_by',"select a from crash_me order by a");
  264. report("Order by position",'order_by_position',
  265.        "select a from crash_me order by 1");
  266. report("Order by function","order_by_function",
  267.        "select a from crash_me order by a+1");
  268. report("Order by on unused column",'order_on_unused',
  269.        "select b from crash_me order by a");
  270. check_and_report("Order by DESC is remembered",'order_by_remember_desc',
  271.          ["create table crash_q (s int,s1 int)",
  272.           "insert into crash_q values(1,1)",
  273.           "insert into crash_q values(3,1)",
  274.           "insert into crash_q values(2,1)"],
  275.          "select s,s1 from crash_q order by s1 DESC,s",
  276.          ["drop table crash_q $drop_attr"],[3,2,1],7,undef(),3);
  277. report("Compute",'compute',
  278.        "select a from crash_me order by a compute sum(a) by a");
  279. report("INSERT with Value lists",'insert_multi_value',
  280.        "create table crash_q (s char(10))",
  281.        "insert into crash_q values ('a'),('b')",
  282.        "drop table crash_q $drop_attr");
  283. report("INSERT with set syntax",'insert_with_set',
  284.        "create table crash_q (a integer)",
  285.        "insert into crash_q SET a=1",
  286.        "drop table crash_q $drop_attr");
  287. report("allows end ';'","end_colon", "select * from crash_me;");
  288. try_and_report("LIMIT number of rows","select_limit",
  289.            ["with LIMIT",
  290.         "select * from crash_me limit 1"],
  291.            ["with TOP",
  292.         "select TOP 1 * from crash_me"]);
  293. report("SELECT with LIMIT #,#","select_limit2", "select * from crash_me limit 1,1");
  294.  
  295. # The following alter table commands MUST be kept together!
  296. if ($dbh->do("create table crash_q (a integer, b integer,c1 CHAR(10))"))
  297. {
  298.   report("Alter table add column",'alter_add_col',
  299.      "alter table crash_q add d integer");
  300.   report_one("Alter table add many columns",'alter_add_multi_col',
  301.          [["alter table crash_q add (f integer,g integer)","yes"],
  302.           ["alter table crash_q add f integer, add g integer","with add"],
  303.           ["alter table crash_q add f integer,g integer","without add"]] );
  304.   report("Alter table change column",'alter_change_col',
  305.      "alter table crash_q change a e char(50)");
  306.  
  307.   # informix can only change data type with modify
  308.   report_one("Alter table modify column",'alter_modify_col',
  309.          [["alter table crash_q modify c1 CHAR(20)","yes"],
  310.           ["alter table crash_q alter c1 CHAR(20)","with alter"]]);
  311.   report("Alter table alter column default",'alter_alter_col',
  312.      "alter table crash_q alter b set default 10");
  313.   report_one("Alter table drop column",'alter_drop_col',
  314.          [["alter table crash_q drop column b","yes"],
  315.           ["alter table crash_q drop column b restrict","with restrict/cascade"]]);
  316.   report("Alter table rename table",'alter_rename_table',
  317.      "alter table crash_q rename to crash_q1");
  318. }
  319. # Make sure both tables will be dropped, even if rename fails.
  320. $dbh->do("drop table crash_q1 $drop_attr");
  321. $dbh->do("drop table crash_q $drop_attr");
  322.  
  323. report("rename table","rename_table",
  324.        "create table crash_q (a integer, b integer,c1 CHAR(10))",
  325.        "rename table crash_q to crash_q1",
  326.        "drop table crash_q1 $drop_attr");
  327. # Make sure both tables will be dropped, even if rename fails.
  328. $dbh->do("drop table crash_q1 $drop_attr");
  329. $dbh->do("drop table crash_q $drop_attr");
  330.  
  331. report("truncate","truncate_table",
  332.        "create table crash_q (a integer, b integer,c1 CHAR(10))",
  333.        "truncate table crash_q",
  334.        "drop table crash_q $drop_attr");
  335.  
  336. if ($dbh->do("create table crash_q (a integer, b integer,c1 CHAR(10))") &&
  337.     $dbh->do("create table crash_q1 (a integer, b integer,c1 CHAR(10) not null)"))
  338. {
  339.   report("Alter table add constraint",'alter_add_constraint',
  340.      "alter table crash_q add constraint c2 check(a > b)");
  341.   report_one("Alter table drop constraint",'alter_drop_constraint',
  342.          [["alter table crash_q drop constraint c2","yes"],
  343.           ["alter table crash_q drop constraint c2 restrict","with restrict/cascade"]]);
  344.   report("Alter table add unique",'alter_add_unique',
  345.      "alter table crash_q add constraint u1 unique(c1)");
  346.   try_and_report("Alter table drop unique",'alter_drop_unique',
  347.          ["with constraint",
  348.           "alter table crash_q drop constraint u1"],
  349.          ["with constraint and restrict/cascade",
  350.           "alter table crash_q drop constraint u1 restrict"],
  351.          ["with drop key",
  352.           "alter table crash_q drop key c1"]);
  353.   try_and_report("Alter table add primary key",'alter_add_primary_key',
  354.          ["with constraint",
  355.           "alter table crash_q1 add constraint p1 primary key(c1)"],
  356.          ["with add primary key",
  357.           "alter table crash_q1 add primary key(c1)"]);
  358.   report("Alter table add foreign key",'alter_add_foreign_key',
  359.      "alter table crash_q add constraint f1 foreign key(c1) references crash_q1(c1)");
  360.   try_and_report("Alter table drop foreign key",'alter_drop_foreign_key',
  361.          ["with drop constraint",
  362.           "alter table crash_q drop constraint f1"],
  363.          ["with drop constraint and restrict/cascade",
  364.           "alter table crash_q drop constraint f1 restrict"],
  365.          ["with drop foreign key",
  366.           "alter table crash_q drop foreign key f1"]);
  367.   try_and_report("Alter table drop primary key",'alter_drop_primary_key',
  368.          ["drop constraint",
  369.           "alter table crash_q1 drop constraint p1 restrict"],
  370.          ["drop primary key",
  371.           "alter table crash_q1 drop primary key"]);
  372. }
  373. $dbh->do("drop table crash_q $drop_attr");
  374. $dbh->do("drop table crash_q1 $drop_attr");
  375.  
  376. check_and_report("Case insensitive compare","case_insensitive_strings",
  377.          [],"select b from crash_me where b = 'A'",[],'a',1);
  378. check_and_report("Ignore end space in compare","ignore_end_space",
  379.          [],"select b from crash_me where b = 'a '",[],'a',1);
  380. check_and_report("Group on column with null values",'group_by_null',
  381.          ["create table crash_q (s char(10))",
  382.           "insert into crash_q values(null)",
  383.           "insert into crash_q values(null)"],
  384.          "select count(*),s from crash_q group by s",
  385.          ["drop table crash_q $drop_attr"],2,0);
  386.  
  387. $prompt="Having";
  388. if (!defined($limits{'having'}))
  389. {                               # Complicated because of postgreSQL
  390.   if (!safe_query_result("select a from crash_me group by a having a > 0",1,0))
  391.   {
  392.     if (!safe_query_result("select a from crash_me group by a having a < 0",
  393.                1,0))
  394.     { save_config_data("having","error",$prompt); }
  395.     else
  396.     { save_config_data("having","yes",$prompt); }
  397.   }
  398.   else
  399.   { save_config_data("having","no",$prompt); }
  400. }
  401. print "$prompt: $limits{'having'}\n";
  402.  
  403. if ($limits{'having'} eq 'yes')
  404. {
  405.   report("Having with group function","having_with_group",
  406.      "select a from crash_me group by a having count(*) = 1");
  407. }
  408.  
  409. if ($limits{'column_alias'} eq 'yes')
  410. {
  411.   report("Order by alias",'order_by_alias',
  412.      "select a as ab from crash_me order by ab");
  413.   if ($limits{'having'} eq 'yes')
  414.   {
  415.     report("Having on alias","having_with_alias",
  416.        "select a as ab from crash_me group by a having ab > 0");
  417.   }
  418. }
  419. report("binary numbers (0b1001)","binary_numbers","select 0b1001 $end_query");
  420. report("hex numbers (0x41)","hex_numbers","select 0x41 $end_query");
  421. report("binary strings (b'0110')","binary_strings","select b'0110' $end_query");
  422. report("hex strings (x'1ace')","hex_strings","select x'1ace' $end_query");
  423.  
  424. report_result("Value of logical operation (1=1)","logical_value",
  425.           "select (1=1) $end_query");
  426.  
  427. $logical_value= $limits{'logical_value'};
  428.  
  429. $false=0;
  430. $result="no";
  431. if ($res=safe_query("select (1=1)=true $end_query")) {
  432.   $false="false";
  433.   $result="yes";
  434. }
  435. save_config_data('has_true_false',$result,"TRUE and FALSE");
  436.  
  437. #
  438. # Check how many connections the server can handle:
  439. # We can't test unlimited connections, because this may take down the
  440. # server...
  441. #
  442.  
  443. $prompt="Simultaneous connections (installation default)";
  444. print "$prompt: ";
  445. if (defined($limits{'connections'}))
  446. {
  447.   print "$limits{'connections'}\n";
  448. }
  449. else
  450. {
  451.   @connect=($dbh);
  452.  
  453.   for ($i=1; $i < $max_connections ; $i++)
  454.   {
  455.     if (!($dbh=DBI->connect($server->{'data_source'},$opt_user,$opt_password,
  456.               { PrintError => 0})))
  457.     {
  458.       print "Last connect error: $DBI::errstr\n" if ($opt_debug);
  459.       last;
  460.     }
  461.     $dbh->{LongReadLen}= $longreadlen; # Set retrieval buffer
  462.     print "." if ($opt_debug);
  463.     push(@connect,$dbh);
  464.   }
  465.   print "$i\n";
  466.   save_config_data('connections',$i,$prompt);
  467.   foreach $dbh (@connect)
  468.   {
  469.     print "#" if ($opt_debug);
  470.     $dbh->disconnect || warn $dbh->errstr;           # close connection
  471.   }
  472.  
  473.   $#connect=-1;                 # Free connections
  474.  
  475.   if ($i == 0)
  476.   {
  477.     print "Can't connect to server: $DBI::errstr.  Please start it and try again\n";
  478.     exit 1;
  479.   }
  480.   $dbh=safe_connect();
  481. }
  482.  
  483.  
  484. #
  485. # Check size of communication buffer, strings...
  486. #
  487.  
  488. $prompt="query size";
  489. print "$prompt: ";
  490. if (!defined($limits{'query_size'}))
  491. {
  492.   $query="select ";
  493.   $first=64;
  494.   $end=$max_buffer_size;
  495.   $select= $limits{'select_without_from'} eq 'yes' ? 1 : 'a';
  496.  
  497.   assert($query . "$select$end_query");
  498.  
  499.   $first=$limits{'restart'}{'low'} if ($limits{'restart'}{'low'});
  500.  
  501.   if ($limits{'restart'}{'tohigh'})
  502.   {
  503.     $end = $limits{'restart'}{'tohigh'} - 1;
  504.     print "\nRestarting this with low limit: $first and high limit: $end\n";
  505.     delete $limits{'restart'};
  506.     $first=$first+int(($end-$first+4)/5);           # Prefere lower on errors
  507.   }
  508.   for ($i=$first ; $i < $end ; $i*=2)
  509.   {
  510.     last if (!safe_query($query . (" " x ($i - length($query)-length($end_query) -1)) . "$select$end_query"));
  511.     $first=$i;
  512.     save_config_data("restart",$i,"") if ($opt_restart);
  513.   }
  514.   $end=$i;
  515.  
  516.   if ($i < $max_buffer_size)
  517.   {
  518.     while ($first != $end)
  519.     {
  520.       $i=int(($first+$end+1)/2);
  521.       if (safe_query($query .
  522.              (" " x ($i - length($query)-length($end_query) -1)) .
  523.              "$select$end_query"))
  524.       {
  525.     $first=$i;
  526.       }
  527.       else
  528.       {
  529.     $end=$i-1;
  530.       }
  531.     }
  532.   }
  533.   save_config_data('query_size',$end,$prompt);
  534. }
  535. $query_size=$limits{'query_size'};
  536.  
  537. print "$limits{'query_size'}\n";
  538. #
  539. # Test database types
  540. #
  541.  
  542. @sql_types=("character(1)","char(1)","char varying(1)", "character varying(1)",
  543.         "boolean",
  544.         "varchar(1)",
  545.         "integer","int","smallint",
  546.         "numeric(9,2)","decimal(6,2)","dec(6,2)",
  547.         "bit", "bit(2)","bit varying(2)","float","float(8)","real",
  548.         "double precision", "date","time","timestamp",
  549.         "interval year", "interval year to month",
  550.             "interval month",
  551.             "interval day", "interval day to hour", "interval day to minute",
  552.             "interval day to second",
  553.             "interval hour", "interval hour to minute", "interval hour to second",
  554.             "interval minute", "interval minute to second",
  555.             "interval second",
  556.         "national character varying(20)",
  557.         "national character(20)","nchar(1)",
  558.         "national char varying(20)","nchar varying(20)",
  559.         "national character varying(20)",
  560.         "timestamp with time zone");
  561. @odbc_types=("binary(1)","varbinary(1)","tinyint","bigint",
  562.          "datetime");
  563. @extra_types=("blob","byte","long varbinary","image","text","text(10)",
  564.           "mediumtext",
  565.           "long varchar(1)", "varchar2(257)",
  566.           "mediumint","middleint","int unsigned",
  567.           "int1","int2","int3","int4","int8","uint",
  568.           "money","smallmoney","float4","float8","smallfloat",
  569.           "float(6,2)","double",
  570.           "enum('red')","set('red')", "int(5) zerofill", "serial",
  571.           "char(10) binary","int not null auto_increment,unique(q)",
  572.           "abstime","year","datetime","smalldatetime","timespan","reltime",
  573.           # Sybase types
  574.           "int not null identity,unique(q)",
  575.           # postgres types
  576.           "box","bool","circle","polygon","point","line","lseg","path",
  577.           "interval", "serial", "inet", "cidr", "macaddr",
  578.  
  579.           # oracle types
  580.           "varchar2(16)","nvarchar2(16)","number(9,2)","number(9)",
  581.           "number", "long","raw(16)","long raw","rowid","mlslabel","clob",
  582.           "nclob","bfile"
  583.           );
  584.  
  585. @types=(["sql",\@sql_types],
  586.     ["odbc",\@odbc_types],
  587.     ["extra",\@extra_types]);
  588.  
  589. foreach $types (@types)
  590. {
  591.   print "\nSupported $types->[0] types\n";
  592.   $tmp=@$types->[1];
  593.   foreach $use_type (@$tmp)
  594.   {
  595.     $type=$use_type;
  596.     $type =~ s/\(.*\)/(1 arg)/;
  597.     if (index($use_type,",")>= 0)
  598.     {
  599.       $type =~ s/\(1 arg\)/(2 arg)/;
  600.     }
  601.     if (($tmp2=index($type,",unique")) >= 0)
  602.     {
  603.       $type=substr($type,0,$tmp2);
  604.     }
  605.     $tmp2=$type;
  606.     $tmp2 =~ s/ /_/g;
  607.     $tmp2 =~ s/_not_null//g;
  608.     report("Type $type","type_$types->[0]_$tmp2",
  609.        "create table crash_q (q $use_type)",
  610.        "drop table crash_q $drop_attr");
  611.   }
  612. }
  613.  
  614. #
  615. # Test some type limits
  616. #
  617.  
  618. check_and_report("Remembers end space in char()","remember_end_space",
  619.          ["create table crash_q (a char(10))",
  620.           "insert into crash_q values('hello ')"],
  621.          "select a from crash_q where a = 'hello '",
  622.          ["drop table crash_q $drop_attr"],
  623.          'hello ',6);
  624.  
  625. check_and_report("Remembers end space in varchar()",
  626.          "remember_end_space_varchar",
  627.          ["create table crash_q (a varchar(10))",
  628.           "insert into crash_q values('hello ')"],
  629.          "select a from crash_q where a = 'hello '",
  630.          ["drop table crash_q $drop_attr"],
  631.          'hello ',6);
  632.  
  633. check_and_report("Supports 0000-00-00 dates","date_zero",
  634.          ["create table crash_me2 (a date not null)",
  635.           "insert into crash_me2 values ('0000-00-00')"],
  636.          "select a from crash_me2",
  637.          ["drop table crash_me2 $drop_attr"],
  638.          "0000-00-00",1);
  639.  
  640. check_and_report("Supports 0001-01-01 dates","date_one",
  641.          ["create table crash_me2 (a date not null)",
  642.           "insert into crash_me2 values (DATE '0001-01-01')"],
  643.          "select a from crash_me2",
  644.          ["drop table crash_me2 $drop_attr"],
  645.          "0001-01-01",1);
  646.  
  647. check_and_report("Supports 9999-12-31 dates","date_last",
  648.          ["create table crash_me2 (a date not null)",
  649.           "insert into crash_me2 values (DATE '9999-12-31')"],
  650.          "select a from crash_me2",
  651.          ["drop table crash_me2 $drop_attr"],
  652.          "9999-12-31",1);
  653.  
  654. check_and_report("Supports 'infinity dates","date_infinity",
  655.          ["create table crash_me2 (a date not null)",
  656.           "insert into crash_me2 values ('infinity')"],
  657.          "select a from crash_me2",
  658.          ["drop table crash_me2 $drop_attr"],
  659.          "infinity",1);
  660.  
  661. if (!defined($limits{'date_with_YY'}))
  662. {
  663.     check_and_report("Supports YY-MM-DD dates","date_with_YY",
  664.              ["create table crash_me2 (a date not null)",
  665.               "insert into crash_me2 values ('98-03-03')"],
  666.              "select a from crash_me2",
  667.              ["drop table crash_me2 $drop_attr"],
  668.              "1998-03-03",5);
  669.     if ($limits{'date_with_YY'} eq "yes")
  670.     {
  671.     undef($limits{'date_with_YY'});
  672.     check_and_report("Supports YY-MM-DD 2000 compilant dates",
  673.              "date_with_YY",
  674.              ["create table crash_me2 (a date not null)",
  675.               "insert into crash_me2 values ('10-03-03')"],
  676.              "select a from crash_me2",
  677.              ["drop table crash_me2 $drop_attr"],
  678.              "2010-03-03",5);
  679.     }
  680. }
  681.  
  682. if (($limits{'type_extra_float(2_arg)'} eq "yes" ||
  683.     $limits{'type_sql_decimal(2_arg)'} eq "yes") &&
  684.     (!defined($limits{'storage_of_float'})))
  685. {
  686.   my $type=$limits{'type_extra_float(2_arg)'} eq "yes" ? "float(4,1)" :
  687.     "decimal(4,1)";
  688.   my $result="undefined";
  689.   if (execute_and_check(["create table crash_q (q1 $type)",
  690.              "insert into crash_q values(1.14)"],
  691.             "select q1 from crash_q",
  692.             ["drop table crash_q $drop_attr"],1.1,0) &&
  693.       execute_and_check(["create table crash_q (q1 $type)",
  694.              "insert into crash_q values(1.16)"],
  695.             "select q1 from crash_q",
  696.             ["drop table crash_q $drop_attr"],1.1,0))
  697.   {
  698.     $result="truncate";
  699.   }
  700.   elsif (execute_and_check(["create table crash_q (q1 $type)",
  701.                 "insert into crash_q values(1.14)"],
  702.                "select q1 from crash_q",
  703.                ["drop table crash_q $drop_attr"],1.1,0) &&
  704.      execute_and_check(["create table crash_q (q1 $type)",
  705.                 "insert into crash_q values(1.16)"],
  706.                "select q1 from crash_q",
  707.                ["drop table crash_q $drop_attr"],1.2,0))
  708.   {
  709.     $result="round";
  710.   }
  711.   elsif (execute_and_check(["create table crash_q (q1 $type)",
  712.                 "insert into crash_q values(1.14)"],
  713.                "select q1 from crash_q",
  714.                ["drop table crash_q $drop_attr"],1.14,0) &&
  715.      execute_and_check(["create table crash_q (q1 $type)",
  716.                 "insert into crash_q values(1.16)"],
  717.                "select q1 from crash_q",
  718.                ["drop table crash_q $drop_attr"],1.16,0))
  719.   {
  720.     $result="exact";
  721.   }
  722.   $prompt="Storage of float values";
  723.   print "$prompt: $result\n";
  724.   save_config_data("storage_of_float", $result, $prompt);
  725. }
  726.  
  727. try_and_report("Type for row id", "rowid",
  728.            ["rowid",
  729.         "create table crash_q (a rowid)","drop table crash_q $drop_attr"],
  730.            ["auto_increment",
  731.         "create table crash_q (a int not null auto_increment, primary key(a))","drop table crash_q $drop_attr"],
  732.            ["oid",
  733.         "create table crash_q (a oid, primary key(a))","drop table crash_q $drop_attr"],
  734.            ["serial",
  735.         "create table crash_q (a serial, primary key(a))","drop table crash_q $drop_attr"]);
  736.  
  737. try_and_report("Automatic row id", "automatic_rowid",
  738.            ["_rowid",
  739.         "create table crash_q (a int not null, primary key(a))",
  740.         "insert into crash_q values (1)",
  741.         "select _rowid from crash_q",
  742.         "drop table crash_q $drop_attr"]);
  743.  
  744. #
  745. # Test functions
  746. #
  747.  
  748. @sql_functions=
  749.   (["+, -, * and /","+","5*3-4/2+1",14,0],
  750.    ["ANSI SQL SUBSTRING","substring","substring('abcd' from 2 for 2)","bc",1],
  751.    ["BIT_LENGTH","bit_length","bit_length('abc')",24,0],
  752.    ["searched CASE","searched_case","case when 1 > 2 then 'false' when 2 > 1 then 'true' end", "true",1],
  753.    ["simple CASE","simple_case","case 2 when 1 then 'false' when 2 then 'true' end", "true",1],
  754.    ["CAST","cast","CAST(1 as CHAR)","1",1],
  755.    ["CHARACTER_LENGTH","character_length","character_length('abcd')","4",0],
  756.    ["CHAR_LENGTH","char_length","char_length(b)","10",0],
  757.    ["CHAR_LENGTH(constant)","char_length(constant)","char_length('abcd')","4",0],
  758.    ["COALESCE","coalesce","coalesce($char_null,'bcd','qwe')","bcd",1],
  759.    ["CURRENT_DATE","current_date","current_date",0,2],
  760.    ["CURRENT_TIME","current_time","current_time",0,2],
  761.    ["CURRENT_TIMESTAMP","current_timestamp","current_timestamp",0,2],
  762.    ["CURRENT_USER","current_user","current_user",0,2],
  763.    ["EXTRACT","extract_sql","extract(minute from timestamp '2000-02-23 18:43:12.987')",43,0],
  764.    ["LOCALTIME","localtime","localtime",0,2],
  765.    ["LOCALTIMESTAMP","localtimestamp","localtimestamp",0,2],
  766.    ["LOWER","lower","LOWER('ABC')","abc",1],
  767.    ["NULLIF with strings","nullif_string","NULLIF(NULLIF('first','second'),'first')",undef(),4],
  768.    ["NULLIF with numbers","nullif_num","NULLIF(NULLIF(1,2),1)",undef(),4],
  769.    ["OCTET_LENGTH","octet_length","octet_length('abc')",3,0],
  770.    ["POSITION","position","position('ll' in 'hello')",3,0],
  771.    ["SESSION_USER","session_user","session_user",0,2],
  772.    ["SYSTEM_USER","system_user","system_user",0,2],
  773.    ["TRIM","trim","trim(trailing from trim(LEADING FROM ' abc '))","abc",3],
  774.    ["UPPER","upper","UPPER('abc')","ABC",1],
  775.    ["USER","user","user"],
  776.    ["concatenation with ||","concat_as_||","'abc' || 'def'","abcdef",1],
  777.    );
  778.  
  779. @odbc_functions=
  780.   (["ASCII", "ascii", "ASCII('A')","65",0],
  781.    ["CHAR", "char", "CHAR(65)"  ,"A",1],
  782.    ["CONCAT(2 arg)","concat", "concat('a','b')","ab",1],
  783.    ["DIFFERENCE()","difference","difference('abc','abe')",0,2],
  784.    ["INSERT","insert","insert('abcd',2,2,'ef')","aefd",1],
  785.    ["LEFT","left","left('abcd',2)","ab",1],
  786.    ["LTRIM","ltrim","ltrim('   abcd')","abcd",1],
  787.    ["REAL LENGTH","length","length('abcd ')","5",0],
  788.    ["ODBC LENGTH","length_without_space","length('abcd ')","4",0],
  789.    ["LOCATE(2 arg)","locate_2","locate('bcd','abcd')","2",0],
  790.    ["LOCATE(3 arg)","locate_3","locate('bcd','abcd',3)","0",0],
  791.    ["LCASE","lcase","lcase('ABC')","abc",1],
  792.    ["REPEAT","repeat","repeat('ab',3)","ababab",1],
  793.    ["REPLACE","replace","replace('abbaab','ab','ba')","bababa",1],
  794.    ["RIGHT","right","right('abcd',2)","cd",1],
  795.    ["RTRIM","rtrim","rtrim(' abcd  ')"," abcd",1],
  796.    ["SPACE","space","space(5)","     ",3],
  797.    ["SOUNDEX","soundex","soundex('hello')",0,2],
  798.    ["ODBC SUBSTRING","substring","substring('abcd',3,2)","cd",1],
  799.    ["UCASE","ucase","ucase('abc')","ABC",1],
  800.  
  801.    ["ABS","abs","abs(-5)",5,0],
  802.    ["ACOS","acos","acos(0)","1.570796",0],
  803.    ["ASIN","asin","asin(1)","1.570796",0],
  804.    ["ATAN","atan","atan(1)","0.785398",0],
  805.    ["ATAN2","atan2","atan2(1,0)","1.570796",0],
  806.    ["CEILING","ceiling","ceiling(-4.5)",-4,0],
  807.    ["COS","cos","cos(0)","1.00000",0],
  808.    ["COT","cot","cot(1)","0.64209262",0],
  809.    ["DEGREES","degrees","degrees(6.283185)","360",0],
  810.    ["EXP","exp","exp(1.0)","2.718282",0],
  811.    ["FLOOR","floor","floor(2.5)","2",0],
  812.    ["LOG","log","log(2)","0.693147",0],
  813.    ["LOG10","log10","log10(10)","1",0],
  814.    ["MOD","mod","mod(11,7)","4",0],
  815.    ["PI","pi","pi()","3.141593",0],
  816.    ["POWER","power","power(2,4)","16",0],
  817.    ["RAND","rand","rand(1)",0,2],       # Any value is acceptable
  818.    ["RADIANS","radians","radians(360)","6.283185",0],
  819.    ["ROUND(2 arg)","round","round(5.63,2)","5.6",0],
  820.    ["SIGN","sign","sign(-5)",-1,0],
  821.    ["SIN","sin","sin(1)","0.841471",0],
  822.    ["SQRT","sqrt","sqrt(4)",2,0],
  823.    ["TAN","tan","tan(1)","1.557408",0],
  824.    ["TRUNCATE","truncate","truncate(18.18,-1)",10,0],
  825.    ["NOW","now","now()",0,2],           # Any value is acceptable
  826.    ["CURDATE","curdate","curdate()",0,2],
  827.    ["DAYNAME","dayname","dayname(DATE '1997-02-01')","",2],
  828.    ["MONTH","month","month(DATE '1997-02-01')","",2],
  829.    ["MONTHNAME","monthname","monthname(DATE '1997-02-01')","",2],
  830.    ["DAYOFMONTH","dayofmonth","dayofmonth(DATE '1997-02-01')",1,0],
  831.    ["DAYOFWEEK","dayofweek","dayofweek(DATE '1997-02-01')",7,0],
  832.    ["DAYOFYEAR","dayofyear","dayofyear(DATE '1997-02-01')",32,0],
  833.    ["QUARTER","quarter","quarter(DATE '1997-02-01')",1,0],
  834.    ["WEEK","week","week(DATE '1997-02-01')",5,0],
  835.    ["YEAR","year","year(DATE '1997-02-01')",1997,0],
  836.    ["CURTIME","curtime","curtime()",0,2],
  837.    ["HOUR","hour","hour('12:13:14')",12,0],
  838.    ["ANSI HOUR","hour_time","hour(TIME '12:13:14')",12,0],
  839.    ["MINUTE","minute","minute('12:13:14')",13,0],
  840.    ["SECOND","second","second('12:13:14')",14,0],
  841.    ["TIMESTAMPADD","timestampadd",
  842.     "timestampadd(SQL_TSI_SECOND,1,'1997-01-01 00:00:00')",
  843.     "1997-01-01 00:00:01",1],
  844.    ["TIMESTAMPDIFF","timestampdiff",
  845.     "timestampdiff(SQL_TSI_SECOND,'1997-01-01 00:00:02', '1997-01-01 00:00:01')","1",0],
  846.    ["USER()","user()","user()",0,2],
  847.    ["DATABASE","database","database()",0,2],
  848.    ["IFNULL","ifnull","ifnull(2,3)",2,0],
  849.    ["ODBC syntax LEFT & RIGHT", "fn_left",
  850.     "{ fn LEFT( { fn RIGHT('abcd',2) },1) }","c",1],
  851.    );
  852.  
  853. @extra_functions=
  854.   (
  855.    ["& (bitwise and)",'&',"5 & 3",1,0],
  856.    ["| (bitwise or)",'|',"1 | 2",3,0],
  857.    ["<< and >> (bitwise shifts)",'binary_shifts',"(1 << 4) >> 2",4,0],
  858.    ["<> in SELECT","<>","1<>1","0",0],
  859.    ["=","=","(1=1)",1,$logical_value],
  860.    ["~* (case insensitive compare)","~*","'hi' ~* 'HI'",1,$logical_value],
  861.    ["ADD_MONTHS","add_months","add_months('1997-01-01',1)","1997-02-01",0], # oracle the date plus n months
  862.    ["AND and OR in SELECT","and_or","1=1 AND 2=2",$logical_value,0],
  863.    ["AND as '&&'",'&&',"1=1 && 2=2",$logical_value,0],
  864.    ["ASCII_CHAR", "ascii_char", "ASCII_CHAR(65)","A",1],
  865.    ["ASCII_CODE", "ascii_code", "ASCII_CODE('A')","65",0],
  866.    ["ATN2","atn2","atn2(1,0)","1.570796",0],
  867.    ["BETWEEN in SELECT","between","5 between 4 and 6",$logical_value,0],
  868.    ["BIT_COUNT","bit_count","bit_count(5)",2,0],
  869.    ["CEIL","ceil","ceil(-4.5)",-4,0], # oracle
  870.    ["CHARINDEX","charindex","charindex('a','crash')",3,0],
  871.    ["CHR", "chr", "CHR(65)"  ,"A",1], # oracle
  872.    ["CONCAT(list)","concat_list", "concat('a','b','c','d')","abcd",1],
  873.    ["CONVERT","convert","convert(CHAR,5)","5",1],
  874.    ["COSH","cosh","cosh(0)","1",0], # oracle hyperbolic cosine of n.
  875.    ["DATEADD","dateadd","dateadd(day,3,'Nov 30 1997')",0,2],
  876.    ["DATEDIFF","datediff","datediff(month,'Oct 21 1997','Nov 30 1997')",0,2],
  877.    ["DATENAME","datename","datename(month,'Nov 30 1997')",0,2],
  878.    ["DATEPART","datepart","datepart(month,'July 20 1997')",0,2],
  879.    ["DATE_FORMAT","date_format", "date_format('1997-01-02 03:04:05','M W D Y y m d h i s w')", 0,2],
  880.    ["ELT","elt","elt(2,'ONE','TWO','THREE')","TWO",1],
  881.    ["ENCRYPT","encrypt","encrypt('hello')",0,2],
  882.    ["FIELD","field","field('IBM','NCA','ICL','SUN','IBM','DIGITAL')",4,0],
  883.    ["FORMAT","format","format(1234.5555,2)","1,234.56",1],
  884.    ["FROM_DAYS","from_days","from_days(729024)","1996-01-01",1],
  885.    ["FROM_UNIXTIME","from_unixtime","from_unixtime(0)",0,2],
  886.    ["GETDATE","getdate","getdate()",0,2],
  887.    ["GREATEST","greatest","greatest('HARRY','HARRIOT','HAROLD')","HARRY",1], # oracle
  888.    ["IF","if", "if(5,6,7)",6,0],
  889.    ["IN on numbers in SELECT","in_num","2 in (3,2,5,9,5,1)",$logical_value,0],
  890.    ["IN on strings in SELECT","in_str","'monty' in ('david','monty','allan')", $logical_value,0],
  891.    ["INITCAP","initcap","initcap('the soap')","The Soap",1], # oracle Returns char, with the first letter of each word in uppercase
  892.    ["INSTR (Oracle syntax)", "instr_oracle", "INSTR('CORPORATE FLOOR','OR',3,2)"  ,"14",0], # oracle instring
  893.    ["INSTRB", "instrb", "INSTRB('CORPORATE FLOOR','OR',5,2)"  ,"27",0], # oracle instring in bytes
  894.    ["INTERVAL","interval","interval(55,10,20,30,40,50,60,70,80,90,100)",5,0],
  895.    ["LAST_DAY","last_day","last_day('1997-04-01')","1997-04-30",0], # oracle last day of month of date
  896.    ["LAST_INSERT_ID","last_insert_id","last_insert_id()",0,2],
  897.    ["LEAST","least","least('HARRY','HARRIOT','HAROLD')","HAROLD",1], # oracle
  898.    ["LENGTHB","lengthb","lengthb('CANDIDE')","14",0], # oracle length in bytes
  899.    ["LIKE ESCAPE in SELECT","like_escape","'%' like 'a%' escape 'a'",$logical_value,0],
  900.    ["LIKE in SELECT","like","'a' like 'a%'",$logical_value,0],
  901.    ["LN","ln","ln(95)","4.55387689",0], # oracle natural logarithm of n
  902.    ["LOCATE as INSTR","instr","instr('hello','ll')",3,0],
  903.    ["LOG(m,n)","log(m_n)","log(10,100)","2",0], # oracle logarithm, base m, of n
  904.    ["LOGN","logn","logn(2)","0.693147",0], # informix
  905.    ["LPAD","lpad","lpad('hi',4,'??')",'??hi',3],
  906.    ["MDY","mdy","mdy(7,1,1998)","1998-07-01",0], # informix
  907.    ["MOD as %","%","10%7","3",0],
  908.    ["MONTHS_BETWEEN","months_between","months_between('1997-02-02','1997-01-01')","1.03225806",0], # oracle number of months between 2 dates
  909.    ["NOT BETWEEN in SELECT","not_between","5 not between 4 and 6",0,0],
  910.    ["NOT LIKE in SELECT","not_like","'a' not like 'a%'",0,0],
  911.    ["NOT as '!' in SELECT","!","! 1",0,0],
  912.    ["NOT in SELECT","not","not $false",$logical_value,0],
  913.    ["ODBC CONVERT","odbc_convert","convert(5,SQL_CHAR)","5",1],
  914.    ["OR as '||'",'||',"1=0 || 1=1",$logical_value,0],
  915.    ["PASSWORD","password","password('hello')",0,2],
  916.    ["PASTE", "paste", "paste('ABCDEFG',3,2,'1234')","AB1234EFG",1],
  917.    ["PATINDEX","patindex","patindex('%a%','crash')",3,0],
  918.    ["PERIOD_ADD","period_add","period_add(9602,-12)",199502,0],
  919.    ["PERIOD_DIFF","period_diff","period_diff(199505,199404)",13,0],
  920.    ["POW","pow","pow(3,2)",9,0],
  921.    ["RANGE","range","range(a)","0.0",0], # informix range(a) = max(a) - min(a)
  922.    ["REGEXP in SELECT","regexp","'a' regexp '^(a|b)*\$'",$logical_value,0],
  923.    ["REPLICATE","replicate","replicate('a',5)","aaaaa",1],
  924.    ["REVERSE","reverse","reverse('abcd')","dcba",1],
  925.    ["ROOT","root","root(4)",2,0], # informix
  926.    ["ROUND(1 arg)","round1","round(5.63)","6",0],
  927.    ["RPAD","rpad","rpad('hi',4,'??')",'hi??',3],
  928.    ["SEC_TO_TIME","sec_to_time","sec_to_time(5001)","01:23:21",1],
  929.    ["SINH","sinh","sinh(1)","1.17520119",0], # oracle hyperbolic sine of n
  930.    ["STR","str","str(123.45,5,1)",123.5,3],
  931.    ["STRCMP","strcmp","strcmp('abc','adc')",-1,0],
  932.    ["STUFF","stuff","stuff('abc',2,3,'xyz')",'axyz',3],
  933.    ["SUBSTRB", "substrb", "SUBSTRB('ABCDEFG',5,4.2)"  ,"CD",1], # oracle substring with bytes
  934.    ["SUBSTRING as MID","mid","mid('hello',3,2)","ll",1],
  935.    ["SUBSTRING_INDEX","substring_index","substring_index('www.tcx.se','.',-2)", "tcx.se",1],
  936.    ["SYSDATE","sysdate","sysdate()",0,2],
  937.    ["TAIL","tail","tail('ABCDEFG',3)","EFG",0],
  938.    ["TANH","tanh","tanh(1)","0.462117157",0], # oracle hyperbolic tangent of n
  939.    ["TIME_TO_SEC","time_to_sec","time_to_sec('01:23:21')","5001",0],
  940.    ["TO_DAYS","to_days","to_days(DATE '1996-01-01')",729024,0],
  941.    ["TRANSLATE","translate","translate('abc','bc','de')",'ade',3],
  942.    ["TRIM; Many char extension","trim_many_char","trim(':!' FROM ':abc!')","abc",3],
  943.    ["TRIM; Substring extension","trim_substring","trim('cb' FROM 'abccb')","abc",3],
  944.    ["TRUNC","trunc","trunc(18.18,-1)",10,0], # oracle
  945.    ["UID","uid","uid",0,2], # oracle uid from user
  946.    ["UNIX_TIMESTAMP","unix_timestamp","unix_timestamp()",0,2],
  947.    ["USERENV","userenv","userenv",0,2], # oracle user enviroment
  948.    ["VERSION","version","version()",0,2],
  949.    ["WEEKDAY","weekday","weekday(DATE '1997-11-29')",5,0],
  950.    ["automatic num->string convert","auto_num2string","concat('a',2)","a2",1],
  951.    ["automatic string->num convert","auto_string2num","'1'+2",3,0],
  952.    ["concatenation with +","concat_as_+","'abc' + 'def'","abcdef",1],
  953.    );
  954.  
  955. @sql_group_functions=
  956.   (
  957.    ["AVG","avg","avg(a)",1,0],
  958.    ["COUNT (*)","count_*","count(*)",1,0],
  959.    ["COUNT column name","count_column","count(a)",1,0],
  960.    ["COUNT(DISTINCT expr)","count_distinct","count(distinct a)",1,0],
  961.    ["MAX on numbers","max","max(a)",1,0],
  962.    ["MAX on strings","max_str","max(b)","a",1],
  963.    ["MIN on numbers","min","min(a)",1,0],
  964.    ["MIN on strings","min_str","min(b)","a",1],
  965.    ["SUM","sum","sum(a)",1,0],
  966.    ["ANY","any","any(a)",$logical_value,0],
  967.    ["EVERY","every","every(a)",$logical_value,0],
  968.    ["SOME","some","some(a)",$logical_value,0],
  969.    );
  970.  
  971. @extra_group_functions=
  972.   (
  973.    ["BIT_AND",'bit_and',"bit_and(a)",1,0],
  974.    ["BIT_OR", 'bit_or', "bit_or(a)",1,0],
  975.    ["COUNT(DISTINCT expr,expr,...)","count_distinct_list","count(distinct a,b)",1,0],
  976.    ["STD","std","std(a)",0,0],
  977.    ["STDDEV","stddev","stddev(a)",0,0],
  978.    ["VARIANCE","variance","variance(a)",0,0],
  979.    );
  980.  
  981. @where_functions=
  982. (
  983.  ["= ALL","eq_all","b =all (select b from crash_me)",1,0],
  984.  ["= ANY","eq_any","b =any (select b from crash_me)",1,0],
  985.  ["= SOME","eq_some","b =some (select b from crash_me)",1,0],
  986.  ["BETWEEN","between","5 between 4 and 6",1,0],
  987.  ["EXISTS","exists","exists (select * from crash_me)",1,0],
  988.  ["IN on numbers","in_num","2 in (3,2,5,9,5,1)",1,0],
  989.  ["LIKE ESCAPE","like_escape","b like '%' escape 'a'",1,0],
  990.  ["LIKE","like","b like 'a%'",1,0],
  991.  ["MATCH UNIQUE","match_unique","1 match unique (select a from crash_me)",1,0],
  992.  ["MATCH","match","1 match (select a from crash_me)",1,0],
  993.  ["MATCHES","matches","b matcjhes 'a*'",1,0],
  994.  ["NOT BETWEEN","not_between","7 not between 4 and 6",1,0],
  995.  ["NOT EXISTS","not_exists","not exists (select * from crash_me where a = 2)",1,0],
  996.  ["NOT LIKE","not_like","b not like 'b%'",1,0],
  997.  ["NOT UNIQUE","not_unique","not unique (select * from crash_me where a = 2)",1,0],
  998.  ["UNIQUE","unique","unique (select * from crash_me)",1,0],
  999.  );
  1000.  
  1001. @types=(["sql",\@sql_functions,0],
  1002.     ["odbc",\@odbc_functions,0],
  1003.     ["extra",\@extra_functions,0],
  1004.     ["where",\@where_functions,0]);
  1005.  
  1006. @group_types=(["sql",\@sql_group_functions,0],
  1007.           ["extra",\@extra_group_functions,0]);
  1008.  
  1009.  
  1010. foreach $types (@types)
  1011. {
  1012.   print "\nSupported $types->[0] functions\n";
  1013.   $tmp=@$types->[1];
  1014.   foreach $type (@$tmp)
  1015.   {
  1016.     if (defined($limits{"func_$types->[0]_$type->[1]"}))
  1017.     {
  1018.       next;
  1019.     }
  1020.     if ($types->[0] eq "where")
  1021.     {
  1022.       check_and_report("Function $type->[0]","func_$types->[0]_$type->[1]",
  1023.                [],"select a from crash_me where $type->[2]",[],
  1024.                $type->[3],$type->[4]);
  1025.     }
  1026.     elsif ($limits{'functions'} eq 'yes')
  1027.     {
  1028.       if (($type->[2] =~ /char_length\(b\)/) && (!$end_query))
  1029.       {
  1030.     my $tmp= $type->[2];
  1031.     $tmp .= " from crash_me ";
  1032.     undef($limits{"func_$types->[0]_$type->[1]"});
  1033.     check_and_report("Function $type->[0]",
  1034.              "func_$types->[0]_$type->[1]",
  1035.              [],"select $tmp ",[],
  1036.              $type->[3],$type->[4]);
  1037.       }
  1038.       else
  1039.       {
  1040.     undef($limits{"func_$types->[0]_$type->[1]"});
  1041.     $result = check_and_report("Function $type->[0]",
  1042.                 "func_$types->[0]_$type->[1]",
  1043.                 [],"select $type->[2] $end_query",[],
  1044.                 $type->[3],$type->[4]);
  1045.     if (!$result)
  1046.     {
  1047.       # check without type specifyer
  1048.       if ($type->[2] =~ /DATE /)
  1049.       {
  1050.         my $tmp= $type->[2];
  1051.         $tmp =~ s/DATE //;
  1052.         undef($limits{"func_$types->[0]_$type->[1]"});
  1053.         $result = check_and_report("Function $type->[0]",
  1054.                   "func_$types->[0]_$type->[1]",
  1055.                   [],"select $tmp $end_query",[],
  1056.                   $type->[3],$type->[4]);
  1057.       }
  1058.       if (!$result)
  1059.       {
  1060.         if ($types->[0] eq "odbc" && ! ($type->[2] =~ /\{fn/))
  1061.         {
  1062.          my $tmp= $type->[2];
  1063.          # Check by converting to ODBC format
  1064.          undef($limits{"func_$types->[0]_$type->[1]"});
  1065.          $tmp= "{fn $tmp }";
  1066.          $tmp =~ s/('1997-\d\d-\d\d \d\d:\d\d:\d\d')/{ts $1}/g;
  1067.          $tmp =~ s/(DATE '1997-\d\d-\d\d')/{d $1}/g;
  1068.          $tmp =~ s/(TIME '12:13:14')/{t $1}/g;
  1069.          $tmp =~ s/DATE //;
  1070.          $tmp =~ s/TIME //;
  1071.          check_and_report("Function $type->[0]",
  1072.                   "func_$types->[0]_$type->[1]",
  1073.                   [],"select $tmp $end_query",[],
  1074.                   $type->[3],$type->[4]);
  1075.         }
  1076.       }
  1077.         }
  1078.       }
  1079.     }
  1080.   }
  1081. }
  1082.  
  1083. if ($limits{'functions'} eq 'yes')
  1084. {
  1085.   foreach $types (@group_types)
  1086.   {
  1087.     print "\nSupported $types->[0] group functions\n";
  1088.     $tmp=@$types->[1];
  1089.     foreach $type (@$tmp)
  1090.     {
  1091.       check_and_report("Group function $type->[0]",
  1092.                "group_func_$types->[0]_$type->[1]",
  1093.                [],"select $type->[2],a from crash_me group by a",[],
  1094.                $type->[3],$type->[4]);
  1095.     }
  1096.   }
  1097.   print "\n";
  1098.   report("mixing of integer and float in expression","float_int_expr",
  1099.      "select 1+1.0 $end_query");
  1100.   if ($limits{'func_odbc_exp'} eq 'yes')
  1101.   {
  1102.     report("No need to cast from integer to float",
  1103.        "dont_require_cast_to_float", "select exp(1) $end_query");
  1104.   }
  1105.   check_and_report("Is 1+NULL = NULL","null_num_expr",
  1106.            [],"select 1+$numeric_null $end_query",[],undef(),4);
  1107.   $tmp=sql_concat("'a'",$char_null);
  1108.   if (defined($tmp))
  1109.   {
  1110.     check_and_report("Is $tmp = NULL", "null_concat_expr", [],
  1111.              "select $tmp $end_query",[], undef(),4);
  1112.   }
  1113.   $prompt="Need to cast NULL for arithmetic";
  1114.   save_config_data("Need_cast_for_null",
  1115.            ($numeric_null eq "NULL") ? "no" : "yes",
  1116.            $prompt);
  1117. }
  1118. else
  1119. {
  1120.   print "\n";
  1121. }
  1122.  
  1123.  
  1124. report("LIKE on numbers","like_with_number",
  1125.        "create table crash_q (a int,b int)",
  1126.        "insert into crash_q values(10,10)",
  1127.        "select * from crash_q where a like '10'",
  1128.        "drop table crash_q $drop_attr");
  1129.  
  1130. report("column LIKE column","like_with_column",
  1131.        "create table crash_q (a char(10),b char(10))",
  1132.        "insert into crash_q values('abc','abc')",
  1133.        "select * from crash_q where a like b",
  1134.        "drop table crash_q $drop_attr");
  1135.  
  1136. report("update of column= -column","NEG",
  1137.        "create table crash_q (a integer)",
  1138.        "insert into crash_q values(10)",
  1139.        "update crash_q set a=-a",
  1140.        "drop table crash_q $drop_attr");
  1141.  
  1142. if ($limits{'func_odbc_left'} eq 'yes' ||
  1143.     $limits{'func_odbc_substring'} eq 'yes')
  1144. {
  1145.   my $type= ($limits{'func_odbc_left'} eq 'yes' ?
  1146.          "left(a,4)" : "substring(a for 4)");
  1147.  
  1148.     check_and_report("String functions on date columns","date_as_string",
  1149.              ["create table crash_me2 (a date not null)",
  1150.               "insert into crash_me2 values ('1998-03-03')"],
  1151.              "select $type from crash_me2",
  1152.              ["drop table crash_me2 $drop_attr"],
  1153.              "1998",1);
  1154. }
  1155.  
  1156.  
  1157. $tmp=sql_concat("b","b");
  1158. if (defined($tmp))
  1159. {
  1160.   check_and_report("char are space filled","char_is_space_filled",
  1161.            [],"select $tmp from crash_me where b = 'a         '",[],
  1162.            'a         a         ',6);
  1163. }
  1164.  
  1165. if (!defined($limits{'multi_table_update'}))
  1166. {
  1167.   if (check_and_report("Update with many tables","multi_table_update",
  1168.            ["create table crash_q (a integer,b char(10))",
  1169.             "insert into crash_q values(1,'c')",
  1170.             "update crash_q left join crash_me on crash_q.a=crash_me.a set crash_q.b=crash_me.b"],
  1171.            "select b from crash_q",
  1172.            ["drop table crash_q $drop_attr"],
  1173.            "a",1,undef(),2))
  1174.   {
  1175.     check_and_report("Update with many tables","multi_table_update",
  1176.              ["create table crash_q (a integer,b char(10))",
  1177.               "insert into crash_q values(1,'c')",
  1178.               "update crash_q,crash_me set crash_q.b=crash_me.b where crash_q.a=crash_me.a"],
  1179.              "select b from crash_q",
  1180.              ["drop table crash_q $drop_attr"],
  1181.              "a",1,
  1182.             1);
  1183.   }
  1184. }
  1185.  
  1186. report("DELETE FROM table1,table2...","multi_table_delete",
  1187.        "create table crash_q (a integer,b char(10))",
  1188.        "insert into crash_q values(1,'c')",
  1189.        "delete crash_q.* from crash_q,crash_me where crash_q.a=crash_me.a",
  1190.        "drop table crash_q $drop_attr");
  1191.  
  1192. check_and_report("Update with sub select","select_table_update",
  1193.          ["create table crash_q (a integer,b char(10))",
  1194.           "insert into crash_q values(1,'c')",
  1195.           "update crash_q set b= (select b from crash_me where crash_q.a = crash_me.a)"],
  1196.          "select b from crash_q",
  1197.          ["drop table crash_q $drop_attr"],
  1198.          "a",1);
  1199.  
  1200. check_and_report("Calculate 1--1","minus_neg",[],
  1201.          "select a--1 from crash_me",[],0,2);
  1202.  
  1203. report("ANSI SQL simple joins","simple_joins",
  1204.        "select crash_me.a from crash_me, crash_me t0");
  1205.  
  1206. #
  1207. # Check max string size, and expression limits
  1208. #
  1209. $found=undef;
  1210. foreach $type (('mediumtext','text','text()','blob','long'))
  1211. {
  1212.   if ($limits{"type_extra_$type"} eq 'yes')
  1213.   {
  1214.     $found=$type;
  1215.     last;
  1216.   }
  1217. }
  1218. if (defined($found))
  1219. {
  1220.   $found =~ s/\(\)/\(%d\)/;
  1221.   find_limit("max text or blob size","max_text_size",
  1222.          new query_many(["create table crash_q (q $found)",
  1223.                  "insert into crash_q values ('%s')"],
  1224.                 "select * from crash_q","%s",
  1225.                 ["drop table crash_q $drop_attr"],
  1226.                 min($max_string_size,$limits{'query_size'}-30)));
  1227.  
  1228. }
  1229.  
  1230. # It doesn't make lots of sense to check for string lengths much bigger than
  1231. # what can be stored...
  1232.  
  1233. find_limit(($prompt="constant string size in where"),"where_string_size",
  1234.        new query_repeat([],"select a from crash_me where b >='",
  1235.                 "","","1","","'"));
  1236. if ($limits{'where_string_size'} == 10)
  1237. {
  1238.   save_config_data('where_string_size','nonstandard',$prompt);
  1239. }
  1240.  
  1241. if ($limits{'select_constants'} eq 'yes')
  1242. {
  1243.   find_limit("constant string size in SELECT","select_string_size",
  1244.          new query_repeat([],"select '","","","a","","'$end_query"));
  1245. }
  1246.  
  1247. goto no_functions if ($limits{'functions'} ne "yes");
  1248.  
  1249. if ($limits{'func_odbc_repeat'} eq 'yes')
  1250. {
  1251.   find_limit("return string size from function","repeat_string_size",
  1252.          new query_many([],
  1253.                 "select repeat('a',%d) $end_query","%s",
  1254.                 [],
  1255.                 $max_string_size,0));
  1256. }
  1257.  
  1258. $tmp=find_limit("simple expressions","max_expressions",
  1259.         new query_repeat([],"select 1","","","+1","",$end_query,
  1260.                  undef(),$max_expressions));
  1261.  
  1262. if ($tmp > 10)
  1263. {
  1264.   $tmp= "(1" . ( '+1' x ($tmp-10) ) . ")";
  1265.   find_limit("big expressions", "max_big_expressions",
  1266.          new query_repeat([],"select 0","","","+$tmp","",$end_query,
  1267.                   undef(),$max_big_expressions));
  1268. }
  1269.  
  1270. find_limit("stacked expressions", "max_stack_expression",
  1271.        new query_repeat([],"select 1","","","+(1",")",$end_query,
  1272.                 undef(),$max_stacked_expressions));
  1273.  
  1274. no_functions:
  1275.  
  1276. if (!defined($limits{'max_conditions'}))
  1277. {
  1278.   find_limit("OR and AND in WHERE","max_conditions",
  1279.          new query_repeat([],
  1280.                   "select a from crash_me where a=1 and b='a'","",
  1281.                   "", " or a=%d and b='%d'","","","",
  1282.                   [],($query_size-42)/29,undef,2));
  1283.   $limits{'max_conditions'}*=2;
  1284. }
  1285. # The 42 is the length of the constant part.
  1286. # The 29 is the length of the variable part, plus two seven-digit numbers.
  1287.  
  1288. find_limit("tables in join", "join_tables",
  1289.        new query_repeat([],
  1290.                 "select crash_me.a",",t%d.a","from crash_me",
  1291.                 ",crash_me t%d","","",[],$max_join_tables,undef,
  1292.                 1));
  1293.  
  1294. # Different CREATE TABLE options
  1295.  
  1296. report("primary key in create table",'primary_key_in_create',
  1297.        "create table crash_q (q integer not null,primary key (q))",
  1298.        "drop table crash_q $drop_attr");
  1299.  
  1300. report("unique in create table",'unique_in_create',
  1301.        "create table crash_q (q integer not null,unique (q))",
  1302.        "drop table crash_q $drop_attr");
  1303.  
  1304. if ($limits{'unique_in_create'} eq 'yes')
  1305. {
  1306.   report("unique null in create",'unique_null_in_create',
  1307.      "create table crash_q (q integer,unique (q))",
  1308.      "insert into crash_q (q) values (NULL)",
  1309.      "insert into crash_q (q) values (NULL)",
  1310.      "insert into crash_q (q) values (1)",
  1311.      "drop table crash_q $drop_attr");
  1312. }
  1313.  
  1314. report("default value for column",'create_default',
  1315.        "create table crash_q (q integer default 10 not null)",
  1316.        "drop table crash_q $drop_attr");
  1317.  
  1318. report("default value function for column",'create_default_func',
  1319.        "create table crash_q (q integer not null,q1 integer default (1+1))",
  1320.        "drop table crash_q $drop_attr");
  1321.  
  1322. report("temporary tables",'temporary_table',
  1323.        "create temporary table crash_q (q integer not null)",
  1324.        "drop table crash_q $drop_attr");
  1325.  
  1326. report_one("create table from select",'create_table_select',
  1327.        [["create table crash_q SELECT * from crash_me","yes"],
  1328.         ["create table crash_q AS SELECT * from crash_me","with AS"]]);
  1329. $dbh->do("drop table crash_q $drop_attr");
  1330.  
  1331. report("index in create table",'index_in_create',
  1332.        "create table crash_q (q integer not null,index (q))",
  1333.        "drop table crash_q $drop_attr");
  1334.  
  1335. # The following must be executed as we need the value of end_drop_keyword
  1336. # later
  1337. if (!(defined($limits{'create_index'}) && defined($limits{'drop_index'})))
  1338. {
  1339.   if ($res=safe_query("create index crash_q on crash_me (a)"))
  1340.   {
  1341.     $res="yes";
  1342.     $drop_res="yes";
  1343.     $end_drop_keyword="";
  1344.     if (!safe_query("drop index crash_q"))
  1345.     {
  1346.       # Can't drop the standard way; Check if mSQL
  1347.       if (safe_query("drop index crash_q from crash_me"))
  1348.       {
  1349.         $drop_res="with 'FROM'";    # Drop is not ANSI SQL
  1350.         $end_drop_keyword="drop index %i from %t";
  1351.       }
  1352.       # else check if Access or MySQL
  1353.       elsif (safe_query("drop index crash_q on crash_me"))
  1354.       {
  1355.         $drop_res="with 'ON'";    # Drop is not ANSI SQL
  1356.         $end_drop_keyword="drop index %i on %t";
  1357.       }
  1358.       # else check if MS-SQL
  1359.       elsif (safe_query("drop index crash_me.crash_q"))
  1360.       {
  1361.         $drop_res="with 'table.index'"; # Drop is not ANSI SQL
  1362.         $end_drop_keyword="drop index %t.%i";
  1363.       }
  1364.     }
  1365.     else
  1366.     {
  1367.       # Old MySQL 3.21 supports only the create index syntax
  1368.       # This means that the second create doesn't give an error.
  1369.       $res=safe_query(["create index crash_q on crash_me (a)",
  1370.                    "create index crash_q on crash_me (a)",
  1371.                    "drop index crash_q"]);
  1372.       $res= $res ? 'ignored' : 'yes';
  1373.     }
  1374.   }
  1375.   else
  1376.   {
  1377.     $drop_res=$res='no';
  1378.   }
  1379.   save_config_data('create_index',$res,"create index");
  1380.   save_config_data('drop_index',$drop_res,"drop index");
  1381.  
  1382.   print "create index: $limits{'create_index'}\n";
  1383.   print "drop index: $limits{'drop_index'}\n";
  1384. }
  1385.  
  1386. # check if we can have 'NULL' as a key
  1387. check_and_report("null in index","null_in_index",
  1388.          [create_table("crash_q",["a char(10)"],["(a)"]),
  1389.           "insert into crash_q values (NULL)"],
  1390.          "select * from crash_q",
  1391.          ["drop table crash_q $drop_attr"],
  1392.          undef(),4);
  1393.  
  1394. if ($limits{'unique_in_create'} eq 'yes')
  1395. {
  1396.   report("null in unique index",'null_in_unique',
  1397.           create_table("crash_q",["q integer"],["unique(q)"]),
  1398.      "insert into crash_q (q) values(NULL)",
  1399.      "insert into crash_q (q) values(NULL)",
  1400.      "drop table crash_q $drop_attr");
  1401.   report("null combination in unique index",'nulls_in_unique',
  1402.           create_table("crash_q",["q integer,q1 integer"],["unique(q,q1)"]),
  1403.      "insert into crash_q (q,q1) values(1,NULL)",
  1404.      "insert into crash_q (q,q1) values(1,NULL)",
  1405.      "drop table crash_q $drop_attr");
  1406. }
  1407.  
  1408. if ($limits{'null_in_unique'} eq 'yes')
  1409. {
  1410.   report("null in unique index",'multi_null_in_unique',
  1411.           create_table("crash_q",["q integer, x integer"],["unique(q)"]),
  1412.      "insert into crash_q(x) values(1)",
  1413.      "insert into crash_q(x) values(2)",
  1414.      "drop table crash_q $drop_attr");
  1415. }
  1416.  
  1417. if ($limits{'create_index'} ne 'no')
  1418. {
  1419.   $end_drop=$end_drop_keyword;
  1420.   $end_drop =~ s/%i/crash_q/;
  1421.   $end_drop =~ s/%t/crash_me/;
  1422.   report("index on column part (extension)","index_parts",,
  1423.      "create index crash_q on crash_me (b(5))",
  1424.      $end_drop);
  1425.   $end_drop=$end_drop_keyword;
  1426.   $end_drop =~ s/%i/crash_me/;
  1427.   $end_drop =~ s/%t/crash_me/;
  1428.   report("different namespace for index",
  1429.      "index_namespace",
  1430.      "create index crash_me on crash_me (b)",
  1431.      $end_drop);
  1432. }
  1433.  
  1434. if (!report("case independent table names","table_name_case",
  1435.         "create table crash_q (q integer)",
  1436.         "drop table CRASH_Q $drop_attr"))
  1437. {
  1438.   safe_query("drop table crash_q $drop_attr");
  1439. }
  1440.  
  1441. if (!report("drop table if exists","drop_if_exists",
  1442.         "create table crash_q (q integer)",
  1443.         "drop table if exists crash_q $drop_attr"))
  1444. {
  1445.   safe_query("drop table crash_q $drop_attr");
  1446. }
  1447.  
  1448. report("create table if not exists","create_if_not_exists",
  1449.        "create table crash_q (q integer)",
  1450.        "create table if not exists crash_q (q integer)");
  1451. safe_query("drop table crash_q $drop_attr");
  1452.  
  1453. #
  1454. # test of different join types
  1455. #
  1456.  
  1457. assert("create table crash_me2 (a integer not null,b char(10) not null, c1 integer)");
  1458. assert("insert into crash_me2 (a,b,c1) values (1,'b',1)");
  1459. assert("create table crash_me3 (a integer not null,b char(10) not null)");
  1460. assert("insert into crash_me3 (a,b) values (1,'b')");
  1461.  
  1462. report("inner join","inner_join",
  1463.        "select crash_me.a from crash_me inner join crash_me2 ON crash_me.a=crash_me2.a");
  1464. report("left outer join","left_outer_join",
  1465.        "select crash_me.a from crash_me left join crash_me2 ON crash_me.a=crash_me2.a");
  1466. report("natural left outer join","natural_left_outer_join",
  1467.        "select c1 from crash_me natural left join crash_me2");
  1468. report("left outer join using","left_outer_join_using",
  1469.        "select c1 from crash_me left join crash_me2 using (a)");
  1470. report("left outer join odbc style","odbc_left_outer_join",
  1471.        "select crash_me.a from { oj crash_me left outer join crash_me2 ON crash_me.a=crash_me2.a }");
  1472. report("right outer join","right_outer_join",
  1473.        "select crash_me.a from crash_me right join crash_me2 ON crash_me.a=crash_me2.a");
  1474. report("full outer join","full_outer_join",
  1475.        "select crash_me.a from crash_me full join crash_me2 ON crash_me.a=crash_me2.a");
  1476. report("cross join (same as from a,b)","cross_join",
  1477.        "select crash_me.a from crash_me cross join crash_me3");
  1478. report("natural join","natural_join",
  1479.        "select * from crash_me natural join crash_me3");
  1480. report("union","union",
  1481.        "select * from crash_me union select a,b from crash_me3");
  1482. report("union all","union_all",
  1483.        "select * from crash_me union all select a,b from crash_me3");
  1484. report("intersect","intersect",
  1485.        "select * from crash_me intersect select * from crash_me3");
  1486. report("intersect all","intersect_all",
  1487.        "select * from crash_me intersect all select * from crash_me3");
  1488. report("except","except",
  1489.        "select * from crash_me except select * from crash_me3");
  1490. report("except all","except_all",
  1491.        "select * from crash_me except all select * from crash_me3");
  1492. report("except","except",
  1493.        "select * from crash_me except select * from crash_me3");
  1494. report("except all","except_all",
  1495.        "select * from crash_me except all select * from crash_me3");
  1496. report("minus","minus",
  1497.        "select * from crash_me minus select * from crash_me3"); # oracle ...
  1498.  
  1499. report("natural join (incompatible lists)","natural_join_incompat",
  1500.        "select c1 from crash_me natural join crash_me2");
  1501. report("union (incompatible lists)","union_incompat",
  1502.        "select * from crash_me union select a,b from crash_me2");
  1503. report("union all (incompatible lists)","union_all_incompat",
  1504.        "select * from crash_me union all select a,b from crash_me2");
  1505. report("intersect (incompatible lists)","intersect_incompat",
  1506.        "select * from crash_me intersect select * from crash_me2");
  1507. report("intersect all (incompatible lists)","intersect_all_incompat",
  1508.        "select * from crash_me intersect all select * from crash_me2");
  1509. report("except (incompatible lists)","except_incompat",
  1510.        "select * from crash_me except select * from crash_me2");
  1511. report("except all (incompatible lists)","except_all_incompat",
  1512.        "select * from crash_me except all select * from crash_me2");
  1513. report("except (incompatible lists)","except_incompat",
  1514.        "select * from crash_me except select * from crash_me2");
  1515. report("except all (incompatible lists)","except_all_incompat",
  1516.        "select * from crash_me except all select * from crash_me2");
  1517. report("minus (incompatible lists)","minus_incompat",
  1518.        "select * from crash_me minus select * from crash_me2"); # oracle ...
  1519.  
  1520. assert("drop table crash_me2 $drop_attr");
  1521. assert("drop table crash_me3 $drop_attr");
  1522.  
  1523. # somethings to be added here ....
  1524. # FOR UNION - INTERSECT - EXCEPT -> CORRESPONDING [ BY ]
  1525. # after subqueries:
  1526. # >ALL | ANY | SOME - EXISTS - UNIQUE
  1527.  
  1528. if (report("subqueries","subqueries",
  1529.        "select a from crash_me where crash_me.a in (select max(a) from crash_me)"))
  1530. {
  1531.     $tmp=new query_repeat([],"select a from crash_me","","",
  1532.               " where a in (select a from crash_me",")",
  1533.               "",[],$max_join_tables);
  1534.     find_limit("recursive subqueries", "recursive_subqueries",$tmp);
  1535. }
  1536.  
  1537. report("insert INTO ... SELECT ...","insert_select",
  1538.        "create table crash_q (a int)",
  1539.        "insert into crash_q (a) SELECT crash_me.a from crash_me",
  1540.        "drop table crash_q $drop_attr");
  1541.  
  1542. report_trans("transactions","transactions",
  1543.          [create_table("crash_q",["a integer not null"],[]),
  1544.           "insert into crash_q values (1)"],
  1545.          "select * from crash_q",
  1546.          "drop table crash_q $drop_attr"
  1547.         );
  1548.  
  1549. report("atomic updates","atomic_updates",
  1550.        create_table("crash_q",["a integer not null"],["primary key (a)"]),
  1551.        "insert into crash_q values (2)",
  1552.        "insert into crash_q values (3)",
  1553.        "insert into crash_q values (1)",
  1554.        "update crash_q set a=a+1",
  1555.        "drop table crash_q $drop_attr");
  1556.  
  1557. if ($limits{'atomic_updates'} eq 'yes')
  1558. {
  1559.   report_fail("atomic_updates_with_rollback","atomic_updates_with_rollback",
  1560.           create_table("crash_q",["a integer not null"],
  1561.                ["primary key (a)"]),
  1562.           "insert into crash_q values (2)",
  1563.           "insert into crash_q values (3)",
  1564.           "insert into crash_q values (1)",
  1565.           "update crash_q set a=a+1 where a < 3",
  1566.           "drop table crash_q $drop_attr");
  1567. }
  1568.  
  1569. # To add with the views:
  1570. # DROP VIEW - CREAT VIEW *** [ WITH [ CASCADE | LOCAL ] CHECK OPTION ]
  1571. report("views","views",
  1572.        "create view crash_q as select a from crash_me",
  1573.        "drop view crash_q $drop_attr");
  1574.  
  1575. report("foreign key syntax","foreign_key_syntax",
  1576.        create_table("crash_q",["a integer not null"],["primary key (a)"]),
  1577.        create_table("crash_q2",["a integer not null",
  1578.                 "foreign key (a) references crash_q (a)"],
  1579.             []),
  1580.        "insert into crash_q values (1)",
  1581.        "insert into crash_q2 values (1)",
  1582.        "drop table crash_q2 $drop_attr",
  1583.        "drop table crash_q $drop_attr");
  1584.  
  1585. if ($limits{'foreign_key_syntax'} eq 'yes')
  1586. {
  1587.   report_fail("foreign keys","foreign_key",
  1588.           create_table("crash_q",["a integer not null"],
  1589.                ["primary key (a)"]),
  1590.           create_table("crash_q2",["a integer not null",
  1591.                        "foreign key (a) references crash_q (a)"],
  1592.                []),
  1593.           "insert into crash_q values (1)",
  1594.           "insert into crash_q2 values (2)",
  1595.           "drop table crash_q2 $drop_attr",
  1596.           "drop table crash_q $drop_attr");
  1597. }
  1598.  
  1599. report("Create SCHEMA","create_schema",
  1600.        "create schema crash_schema create table crash_q (a int) create table crash_q2(b int)",
  1601.        "drop schema crash_schema cascade");
  1602.  
  1603. if ($limits{'foreign_key'} eq 'yes')
  1604. {
  1605.   if ($limits{'create_schema'} eq 'yes')
  1606.   {
  1607.     report("Circular foreign keys","foreign_key_circular",
  1608.            "create schema crash_schema create table crash_q (a int primary key, b int, foreign key (b) references crash_q2(a)) create table crash_q2(a int, b int, primary key(a), foreign key (b) references crash_q(a))",
  1609.            "drop schema crash_schema cascade");
  1610.   }
  1611. }
  1612.  
  1613. report("Column constraints","constraint_check",
  1614.        "create table crash_q (a int check (a>0))",
  1615.        "drop table crash_q $drop_attr");
  1616.  
  1617. report("Table constraints","constraint_check_table",
  1618.        "create table crash_q (a int ,b int, check (a>b))",
  1619.        "drop table crash_q $drop_attr");
  1620.  
  1621. report("Named constraints","constraint_check",
  1622.        "create table crash_q (a int ,b int, constraint abc check (a>b))",
  1623.        "drop table crash_q $drop_attr");
  1624.  
  1625. report("NULL constraint (SyBase style)","constraint_null",
  1626.        "create table crash_q (a int null)",
  1627.        "drop table crash_q $drop_attr");
  1628.  
  1629. report("Triggers (ANSI SQL)","psm_trigger",
  1630.        "create table crash_q (a int ,b int)",
  1631.        "create trigger crash_trigger after insert on crash_q referencing new table as new_a when (localtime > time '18:00:00') begin atomic end",
  1632.        "insert into crash_q values(1,2)",
  1633.        "drop trigger crash_trigger",
  1634.        "drop table crash_q $drop_attr");
  1635.  
  1636. report("PSM procedures (ANSI SQL)","psm_procedures",
  1637.        "create table crash_q (a int,b int)",
  1638.        "create procedure crash_proc(in a1 int, in b1 int) language sql modifies sql data begin declare c1 int; set c1 = a1 + b1; insert into crash_q(a,b) values (a1,c1); end",
  1639.        "call crash_proc(1,10)",
  1640.        "drop procedure crash_proc",
  1641.        "drop table crash_q $drop_attr");
  1642.  
  1643. report("PSM modules (ANSI SQL)","psm_modules",
  1644.        "create table crash_q (a int,b int)",
  1645.        "create module crash_m declare procedure crash_proc(in a1 int, in b1 int) language sql modifies sql data begin declare c1 int; set c1 = a1 + b1; insert into crash_q(a,b) values (a1,c1); end; declare procedure crash_proc2(INOUT a int, in b int) contains sql set a = b + 10; end module",
  1646.        "call crash_proc(1,10)",
  1647.        "drop module crash_m cascade",
  1648.        "drop table crash_q cascade $drop_attr");
  1649.  
  1650. report("PSM functions (ANSI SQL)","psm_functions",
  1651.        "create table crash_q (a int)",
  1652.        "create function crash_func(in a1 int, in b1 int) returns int language sql deterministic contains sql begin return a1 * b1; end",
  1653.        "insert into crash_q values(crash_func(2,4))",
  1654.        "select a,crash_func(a,2) from crash_q",
  1655.        "drop function crash_func cascade",
  1656.        "drop table crash_q $drop_attr");
  1657.  
  1658. report("Domains (ANSI SQL)","domains",
  1659.        "create domain crash_d as varchar(10) default 'Empty' check (value <> 'abcd')",
  1660.        "create table crash_q(a crash_d, b int)",
  1661.        "insert into crash_q(a,b) values('xyz',10)",
  1662.        "insert into crash_q(b) values(10)",
  1663.        "drop table crash_q $drop_attr",
  1664.        "drop domain crash_d");
  1665.  
  1666.  
  1667. if (!defined($limits{'lock_tables'}))
  1668. {
  1669.   report("lock table","lock_tables",
  1670.      "lock table crash_me READ",
  1671.      "unlock tables");
  1672.   if ($limits{'lock_tables'} eq 'no')
  1673.   {
  1674.     delete $limits{'lock_tables'};
  1675.     report("lock table","lock_tables",
  1676.        "lock table crash_me IN SHARE MODE");
  1677.   }
  1678. }
  1679.  
  1680. if (!report("many tables to drop table","multi_drop",
  1681.        "create table crash_q (a int)",
  1682.        "create table crash_q2 (a int)",
  1683.        "drop table crash_q,crash_q2 $drop_attr"))
  1684. {
  1685.   $dbh->do("drop table crash_q $drop_attr");
  1686.   $dbh->do("drop table crash_q2 $drop_attr");
  1687. }
  1688.  
  1689. if (!report("drop table with cascade/restrict","drop_restrict",
  1690.        "create table crash_q (a int)",
  1691.        "drop table crash_q restrict"))
  1692. {
  1693.   $dbh->do("drop table crash_q $drop_attr");
  1694. }
  1695.  
  1696.  
  1697. report("-- as comment (ANSI)","comment_--",
  1698.        "select * from crash_me -- Testing of comments");
  1699. report("// as comment","comment_//",
  1700.        "select * from crash_me // Testing of comments");
  1701. report("# as comment","comment_#",
  1702.        "select * from crash_me # Testing of comments");
  1703. report("/* */ as comment","comment_/**/",
  1704.        "select * from crash_me /* Testing of comments */");
  1705.  
  1706. #
  1707. # Check things that fails one some servers
  1708. #
  1709.  
  1710. # Empress can't insert empty strings in a char() field
  1711. report("insert empty string","insert_empty_string",
  1712.        create_table("crash_q",["a char(10) not null,b char(10)"],[]),
  1713.        "insert into crash_q values ('','')",
  1714.        "drop table crash_q $drop_attr");
  1715.  
  1716. report("Having with alias","having_with_alias",
  1717.        create_table("crash_q",["a integer"],[]),
  1718.        "insert into crash_q values (10)",
  1719.        "select sum(a) as b from crash_q group by a having b > 0",
  1720.        "drop table crash_q $drop_attr");
  1721.  
  1722. #
  1723. # test name limits
  1724. #
  1725.  
  1726. find_limit("table name length","max_table_name",
  1727.        new query_many(["create table crash_q%s (q integer)",
  1728.                "insert into crash_q%s values(1)"],
  1729.                "select * from crash_q%s",1,
  1730.                ["drop table crash_q%s $drop_attr"],
  1731.                $max_name_length,7,1));
  1732.  
  1733. find_limit("column name length","max_column_name",
  1734.        new query_many(["create table crash_q (q%s integer)",
  1735.               "insert into crash_q (q%s) values(1)"],
  1736.               "select q%s from crash_q",1,
  1737.               ["drop table crash_q $drop_attr"],
  1738.                $max_name_length,1));
  1739.  
  1740. if ($limits{'column_alias'} eq 'yes')
  1741. {
  1742.   find_limit("select alias name length","max_select_alias_name",
  1743.        new query_many(undef,
  1744.               "select b as %s from crash_me",undef,
  1745.               undef, $max_name_length));
  1746. }
  1747.  
  1748. find_limit("table alias name length","max_table_alias_name",
  1749.        new query_many(undef,
  1750.               "select %s.b from crash_me %s",
  1751.               undef,
  1752.               undef, $max_name_length));
  1753.  
  1754. $end_drop_keyword = "drop index %i" if (!$end_drop_keyword);
  1755. $end_drop=$end_drop_keyword;
  1756. $end_drop =~ s/%i/crash_q%s/;
  1757. $end_drop =~ s/%t/crash_me/;
  1758.  
  1759. if ($limits{'create_index'} ne 'no')
  1760. {
  1761.   find_limit("index name length","max_index_name",
  1762.          new query_many(["create index crash_q%s on crash_me (a)"],
  1763.                 undef,undef,
  1764.                 [$end_drop],
  1765.                 $max_name_length,7));
  1766. }
  1767.  
  1768. find_limit("max char() size","max_char_size",
  1769.        new query_many(["create table crash_q (q char(%d))",
  1770.                "insert into crash_q values ('%s')"],
  1771.               "select * from crash_q","%s",
  1772.               ["drop table crash_q $drop_attr"],
  1773.               min($max_string_size,$limits{'query_size'})));
  1774.  
  1775. if ($limits{'type_sql_varchar(1_arg)'} eq 'yes')
  1776. {
  1777.   find_limit("max varchar() size","max_varchar_size",
  1778.          new query_many(["create table crash_q (q varchar(%d))",
  1779.                  "insert into crash_q values ('%s')"],
  1780.                 "select * from crash_q","%s",
  1781.                 ["drop table crash_q $drop_attr"],
  1782.                 min($max_string_size,$limits{'query_size'})));
  1783. }
  1784.  
  1785. $found=undef;
  1786. foreach $type (('mediumtext','text','text()','blob','long'))
  1787. {
  1788.   if ($limits{"type_extra_$type"} eq 'yes')
  1789.   {
  1790.     $found=$type;
  1791.     last;
  1792.   }
  1793. }
  1794. if (defined($found))
  1795. {
  1796.   $found =~ s/\(\)/\(%d\)/;
  1797.   find_limit("max text or blob size","max_text_size",
  1798.          new query_many(["create table crash_q (q $found)",
  1799.                  "insert into crash_q values ('%s')"],
  1800.                 "select * from crash_q","%s",
  1801.                 ["drop table crash_q $drop_attr"],
  1802.                 min($max_string_size,$limits{'query_size'}-30)));
  1803.  
  1804. }
  1805.  
  1806. $tmp=new query_repeat([],"create table crash_q (a integer","","",
  1807.               ",a%d integer","",")",["drop table crash_q $drop_attr"],
  1808.               $max_columns);
  1809. $tmp->{'offset'}=1;
  1810. find_limit("Columns in table","max_columns",$tmp);
  1811.  
  1812. # Make a field definition to be used when testing keys
  1813.  
  1814. $key_definitions="q0 integer not null";
  1815. $key_fields="q0";
  1816. for ($i=1; $i < min($limits{'max_columns'},$max_keys) ; $i++)
  1817. {
  1818.   $key_definitions.=",q$i integer not null";
  1819.   $key_fields.=",q$i";
  1820. }
  1821. $key_values="1," x $i;
  1822. chop($key_values);
  1823.  
  1824. if ($limits{'unique_in_create'} eq 'yes')
  1825. {
  1826.   find_limit("unique indexes","max_unique_index",
  1827.          new query_table("create table crash_q (q integer",
  1828.                  ",q%d integer not null,unique (q%d)",")",
  1829.                  ["insert into crash_q (q,%f) values (1,%v)"],
  1830.                  "select q from crash_q",1,
  1831.                  "drop table crash_q $drop_attr",
  1832.                  $max_keys,0));
  1833.  
  1834.   find_limit("index parts","max_index_parts",
  1835.          new query_table("create table crash_q ($key_definitions,unique (q0",
  1836.                  ",q%d","))",
  1837.                  ["insert into crash_q ($key_fields) values ($key_values)"],
  1838.                  "select q0 from crash_q",1,
  1839.                  "drop table crash_q $drop_attr",
  1840.                  $max_keys,1));
  1841.  
  1842.   find_limit("max index part length","max_index_part_length",
  1843.          new query_many(["create table crash_q (q char(%d) not null,unique(q))",
  1844.                  "insert into crash_q (q) values ('%s')"],
  1845.                 "select q from crash_q","%s",
  1846.                 ["drop table crash_q $drop_attr"],
  1847.                 $limits{'max_char_size'},0));
  1848.  
  1849.   if ($limits{'type_sql_varchar(1_arg)'} eq 'yes')
  1850.   {
  1851.     find_limit("index varchar part length","max_index_varchar_part_length",
  1852.          new query_many(["create table crash_q (q varchar(%d) not null,unique(q))",
  1853.                  "insert into crash_q (q) values ('%s')"],
  1854.                 "select q from crash_q","%s",
  1855.                 ["drop table crash_q $drop_attr"],
  1856.                 $limits{'max_varchar_size'},0));
  1857.   }
  1858. }
  1859.  
  1860.  
  1861. if ($limits{'create_index'} ne 'no')
  1862. {
  1863.   if ($limits{'create_index'} eq 'ignored' ||
  1864.       $limits{'unique_in_create'} eq 'yes')
  1865.   {                                     # This should be true
  1866.     save_config_data('max_index',$limits{'max_unique_index'},"max index");
  1867.     print "indexes: $limits{'max_index'}\n";
  1868.   }
  1869.   else
  1870.   {
  1871.     if (!defined($limits{'max_index'}))
  1872.     {
  1873.       assert("create table crash_q ($key_definitions)");
  1874.       for ($i=1; $i <= min($limits{'max_columns'},$max_keys) ; $i++)
  1875.       {
  1876.     last if (!safe_query("create index crash_q$i on crash_q (q$i)"));
  1877.       }
  1878.       save_config_data('max_index',$i == $max_keys ? $max_keys : $i,
  1879.                "max index");
  1880.       while ( --$i > 0)
  1881.       {
  1882.     $end_drop=$end_drop_keyword;
  1883.     $end_drop =~ s/%i/crash_q$i/;
  1884.     $end_drop =~ s/%t/crash_q/;
  1885.     assert($end_drop);
  1886.       }
  1887.       assert("drop table crash_q $drop_attr");
  1888.     }
  1889.     print "indexs: $limits{'max_index'}\n";
  1890.     if (!defined($limits{'max_unique_index'}))
  1891.     {
  1892.       assert("create table crash_q ($key_definitions)");
  1893.       for ($i=0; $i < min($limits{'max_columns'},$max_keys) ; $i++)
  1894.       {
  1895.     last if (!safe_query("create unique index crash_q$i on crash_q (q$i)"));
  1896.       }
  1897.       save_config_data('max_unique_index',$i == $max_keys ? $max_keys : $i,
  1898.                "max unique index");
  1899.       while ( --$i >= 0)
  1900.       {
  1901.     $end_drop=$end_drop_keyword;
  1902.     $end_drop =~ s/%i/crash_q$i/;
  1903.     $end_drop =~ s/%t/crash_q/;
  1904.     assert($end_drop);
  1905.       }
  1906.       assert("drop table crash_q $drop_attr");
  1907.     }
  1908.     print "unique indexes: $limits{'max_unique_index'}\n";
  1909.     if (!defined($limits{'max_index_parts'}))
  1910.     {
  1911.       assert("create table crash_q ($key_definitions)");
  1912.       $end_drop=$end_drop_keyword;
  1913.       $end_drop =~ s/%i/crash_q1%d/;
  1914.       $end_drop =~ s/%t/crash_q/;
  1915.       find_limit("index parts","max_index_parts",
  1916.          new query_table("create index crash_q1%d on crash_q (q0",
  1917.                  ",q%d",")",
  1918.                  [],
  1919.                  undef,undef,
  1920.                  $end_drop,
  1921.                  $max_keys,1));
  1922.       assert("drop table crash_q $drop_attr");
  1923.     }
  1924.     else
  1925.     {
  1926.       print "index parts: $limits{'max_index_parts'}\n";
  1927.     }
  1928.     $end_drop=$end_drop_keyword;
  1929.     $end_drop =~ s/%i/crash_q2%d/;
  1930.     $end_drop =~ s/%t/crash_me/;
  1931.  
  1932.     find_limit("index part length","max_index_part_length",
  1933.            new query_many(["create table crash_q (q char(%d))",
  1934.                    "create index crash_q2%d on crash_q (q)",
  1935.                    "insert into crash_q values('%s')"],
  1936.                   "select q from crash_q",
  1937.                   "%s",
  1938.                   [ $end_drop,
  1939.                    "drop table crash_q $drop_attr"],
  1940.                   min($limits{'max_char_size'},"+8192")));
  1941.   }
  1942. }
  1943.  
  1944. find_limit("index length","max_index_length",
  1945.        new query_index_length("create table crash_q ",
  1946.                   "drop table crash_q $drop_attr",
  1947.                   $max_key_length));
  1948.  
  1949. find_limit("max table row length (without blobs)","max_row_length",
  1950.        new query_row_length("crash_q ",
  1951.                 "not null",
  1952.                 "drop table crash_q $drop_attr",
  1953.                 min($max_row_length,
  1954.                     $limits{'max_columns'}*
  1955.                     min($limits{'max_char_size'},255))));
  1956.  
  1957. find_limit("table row length with nulls (without blobs)",
  1958.        "max_row_length_with_null",
  1959.        new query_row_length("crash_q ",
  1960.                 "",
  1961.                 "drop table crash_q $drop_attr",
  1962.                 $limits{'max_row_length'}*2));
  1963.  
  1964. find_limit("number of columns in order by","columns_in_order_by",
  1965.        new query_many(["create table crash_q (%F)",
  1966.                "insert into crash_q values(%v)",
  1967.                "insert into crash_q values(%v)"],
  1968.               "select * from crash_q order by %f",
  1969.               undef(),
  1970.               ["drop table crash_q $drop_attr"],
  1971.               $max_order_by));
  1972.  
  1973. find_limit("number of columns in group by","columns_in_group_by",
  1974.        new query_many(["create table crash_q (%F)",
  1975.                "insert into crash_q values(%v)",
  1976.                "insert into crash_q values(%v)"],
  1977.               "select %f from crash_q group by %f",
  1978.               undef(),
  1979.               ["drop table crash_q $drop_attr"],
  1980.               $max_order_by));
  1981.  
  1982. #
  1983. # End of test
  1984. #
  1985.  
  1986. $dbh->do("drop table crash_me $drop_attr");        # Remove temporary table
  1987.  
  1988. print "crash-me safe: $limits{'crash_me_safe'}\n";
  1989. print "reconnected $reconnect_count times\n";
  1990.  
  1991. $dbh->disconnect || warn $dbh->errstr;
  1992. save_all_config_data();
  1993. exit 0;
  1994.  
  1995. sub usage
  1996. {
  1997.     print <<EOF;
  1998. $0  Ver $version
  1999.  
  2000. This program tries to find all limits and capabilities for a SQL
  2001. server.  As it will use the server in some 'unexpected' ways, one
  2002. shouldn\'t have anything important running on it at the same time this
  2003. program runs!  There is a slight chance that something unexpected may
  2004. happen....
  2005.  
  2006. As all used queries are legal according to some SQL standard. any
  2007. reasonable SQL server should be able to run this test without any
  2008. problems.
  2009.  
  2010. All questions is cached in $opt_dir/'server_name'.cfg that future runs will use
  2011. limits found in previous runs. Remove this file if you want to find the
  2012. current limits for your version of the database server.
  2013.  
  2014. This program uses some table names while testing things. If you have any
  2015. tables with the name of 'crash_me' or 'crash_qxxxx' where 'x' is a number,
  2016. they will be deleted by this test!
  2017.  
  2018. $0 takes the following options:
  2019.  
  2020. --help or --Information
  2021.   Shows this help
  2022.  
  2023. --batch-mode
  2024.   Don\'t ask any questions, quit on errors.
  2025.  
  2026. --comment='some comment'
  2027.   Add this comment to the crash-me limit file
  2028.  
  2029. --check-server
  2030.   Do a new connection to the server every time crash-me checks if the server
  2031.   is alive.  This can help in cases where the server starts returning wrong
  2032.   data because of an earlier select.
  2033.  
  2034. --database='database' (Default $opt_database)
  2035.   Create test tables in this database.
  2036.  
  2037. --dir='limits'
  2038.   Save crash-me output in this directory
  2039.  
  2040. --debug
  2041.   Lots of printing to help debugging if something goes wrong.
  2042.  
  2043. --fix-limit-file
  2044.   Reformat the crash-me limit file.  crash-me is not run!
  2045.  
  2046. --force
  2047.   Start test at once, without a warning screen and without questions.
  2048.   This is a option for the very brave.
  2049.   Use this in your cron scripts to test your database every night.
  2050.  
  2051. --log-all-queries
  2052.   Prints all queries that are executed. Mostly used for debugging crash-me.
  2053.  
  2054. --log-queries-to-file='filename'
  2055.   Log full queries to file.
  2056.  
  2057. --host='hostname' (Default $opt_host)
  2058.   Run tests on this host.
  2059.  
  2060. --password='password'
  2061.   Password for the current user.
  2062.  
  2063. --restart
  2064.   Save states during each limit tests. This will make it possible to continue
  2065.   by restarting with the same options if there is some bug in the DBI or
  2066.   DBD driver that caused $0 to die!
  2067.  
  2068. --server='server name'  (Default $opt_server)
  2069.   Run the test on the given server.
  2070.   Known servers names are: Access, Adabas, AdabasD, Empress, Oracle, Informix, DB2, Mimer, mSQL, MS-SQL, MySQL, Pg, Solid or Sybase.
  2071.   For others $0 can\'t report the server version.
  2072.  
  2073. --user='user_name'
  2074.   User name to log into the SQL server.
  2075.  
  2076. --start-cmd='command to restart server'
  2077.   Automaticly restarts server with this command if the database server dies.
  2078.  
  2079. --sleep='time in seconds' (Default $opt_sleep)
  2080.   Wait this long before restarting server.
  2081.  
  2082. EOF
  2083.   exit(0);
  2084. }
  2085.  
  2086.  
  2087. sub server_info
  2088. {
  2089.   my ($ok,$tmp);
  2090.   $ok=0;
  2091.   print "\nNOTE: You should be familiar with '$0 --help' before continuing!\n\n";
  2092.   if (lc($opt_server) eq "mysql")
  2093.   {
  2094.     $ok=1;
  2095.     print <<EOF;
  2096. This test should not crash MySQL if it was distributed together with the
  2097. running MySQL version.
  2098. If this is the case you can probably continue without having to worry about
  2099. destroying something.
  2100. EOF
  2101.   }
  2102.   elsif (lc($opt_server) eq "msql")
  2103.   {
  2104.     print <<EOF;
  2105. This test will take down mSQL repeatedly while finding limits.
  2106. To make this test easier, start mSQL in another terminal with something like:
  2107.  
  2108. while (true); do /usr/local/mSQL/bin/msql2d ; done
  2109.  
  2110. You should be sure that no one is doing anything important with mSQL and that
  2111. you have privileges to restart it!
  2112. It may take awhile to determinate the number of joinable tables, so prepare to
  2113. wait!
  2114. EOF
  2115.   }
  2116.   elsif (lc($opt_server) eq "solid")
  2117.   {
  2118.     print <<EOF;
  2119. This test will take down Solid server repeatedly while finding limits.
  2120. You should be sure that no one is doing anything important with Solid
  2121. and that you have privileges to restart it!
  2122.  
  2123. If you are running Solid without logging and/or backup YOU WILL LOSE!
  2124. Solid does not write data from the cache often enough. So if you continue
  2125. you may lose tables and data that you entered hours ago!
  2126.  
  2127. Solid will also take a lot of memory running this test. You will nead
  2128. at least 234M free!
  2129.  
  2130. When doing the connect test Solid server or the perl api will hang when
  2131. freeing connections. Kill this program and restart it to continue with the
  2132. test. You don\'t have to use --restart for this case.
  2133. EOF
  2134.     if (!$opt_restart)
  2135.     {
  2136.       print "\nWhen DBI/Solid dies you should run this program repeatedly\n";
  2137.       print "with --restart until all tests have completed\n";
  2138.     }
  2139.   }
  2140.   elsif (lc($opt_server) eq "pg")
  2141.   {
  2142.     print <<EOF;
  2143. This test will crash postgreSQL when calculating the number of joinable tables!
  2144. You should be sure that no one is doing anything important with postgreSQL
  2145. and that you have privileges to restart it!
  2146. EOF
  2147.   }
  2148.   else
  2149.   {
  2150.     print <<EOF;
  2151. This test may crash $opt_server repeatedly while finding limits!
  2152. You should be sure that no one is doing anything important with $opt_server
  2153. and that you have privileges to restart it!
  2154. EOF
  2155.   }
  2156.   print <<EOF;
  2157.  
  2158. Some of the tests you are about to execute may require a lot of
  2159. memory.  Your tests WILL adversely affect system performance. It's
  2160. not uncommon that either this crash-me test program, or the actual
  2161. database back-end, will DIE with an out-of-memory error. So might
  2162. any other program on your system if it requests more memory at the
  2163. wrong time.
  2164.  
  2165. Note also that while crash-me tries to find limits for the database server
  2166. it will make a lot of queries that can't be categorized as 'normal'.  It's
  2167. not unlikely that crash-me finds some limit bug in your server so if you
  2168. run this test you have to be prepared that your server may die during it!
  2169.  
  2170. We, the creators of this utility, are not responsible in any way if your
  2171. database server unexpectedly crashes while this program tries to find the
  2172. limitations of your server. By accepting the following question with 'yes',
  2173. you agree to the above!
  2174.  
  2175. You have been warned!
  2176.  
  2177. EOF
  2178.  
  2179.   #
  2180.   # No default reply here so no one can blame us for starting the test
  2181.   # automaticly.
  2182.   #
  2183.   for (;;)
  2184.   {
  2185.     print "Start test (yes/no) ? ";
  2186.     $tmp=<STDIN>; chomp($tmp); $tmp=lc($tmp);
  2187.     last if ($tmp =~ /^yes$/i);
  2188.     exit 1 if ($tmp =~ /^n/i);
  2189.     print "\n";
  2190.   }
  2191. }
  2192.  
  2193. sub machine
  2194. {
  2195.   $name= `uname -s -r -m`;
  2196.   if ($?)
  2197.   {
  2198.     $name= `uname -s -m`;
  2199.   }
  2200.   if ($?)
  2201.   {
  2202.     $name= `uname -s`;
  2203.   }
  2204.   if ($?)
  2205.   {
  2206.     $name= `uname`;
  2207.   }
  2208.   if ($?)
  2209.   {
  2210.     $name="unknown";
  2211.   }
  2212.   chomp($name); $name =~ s/[\n\r]//g;
  2213.   return $name;
  2214. }
  2215.  
  2216.  
  2217. #
  2218. # Help functions that we need
  2219. #
  2220.  
  2221. sub safe_connect
  2222. {
  2223.   my ($object)=@_;
  2224.   my ($dbh,$tmp);
  2225.  
  2226.   for (;;)
  2227.   {
  2228.     if (($dbh=DBI->connect($server->{'data_source'},$opt_user,$opt_password,
  2229.                { PrintError => 0, AutoCommit => 1})))
  2230.     {
  2231.       $dbh->{LongReadLen}= 16000000; # Set max retrieval buffer
  2232.       return $dbh;
  2233.     }
  2234.     print "Error: $DBI::errstr;  $server->{'data_source'}  - '$opt_user' - '$opt_password'\n";
  2235.     print "I got the above error when connecting to $opt_server\n";
  2236.     if (defined($object) && defined($object->{'limit'}))
  2237.     {
  2238.       print "This check was done with limit: $object->{'limit'}.\nNext check will be done with a smaller limit!\n";
  2239.       $object=undef();
  2240.     }
  2241.     save_config_data('crash_me_safe','no',"crash me safe");
  2242.     if ($opt_db_start_cmd)
  2243.     {
  2244.       print "Restarting the db server with:\n'$opt_db_start_cmd'\n";
  2245.       system("$opt_db_start_cmd");
  2246.       print "Waiting $opt_sleep seconds so the server can initialize\n";
  2247.       sleep $opt_sleep;
  2248.     }
  2249.     else
  2250.     {
  2251.       exit(1) if ($opt_batch_mode);
  2252.       print "Can you check/restart it so I can continue testing?\n";
  2253.       for (;;)
  2254.       {
  2255.     print "Continue test (yes/no) ? [yes] ";
  2256.     $tmp=<STDIN>; chomp($tmp); $tmp=lc($tmp);
  2257.     $tmp = "yes" if ($tmp eq "");
  2258.     last if (index("yes",$tmp) >= 0);
  2259.     exit 1 if (index("no",$tmp) >= 0);
  2260.     print "\n";
  2261.       }
  2262.     }
  2263.   }
  2264. }
  2265.  
  2266. #
  2267. # Check if the server is upp and running. If not, ask the user to restart it
  2268. #
  2269.  
  2270. sub check_connect
  2271. {
  2272.   my ($object)=@_;
  2273.   my ($sth);
  2274.   print "Checking connection\n" if ($opt_log_all_queries);
  2275.   # The following line will not work properly with interbase
  2276.   if ($opt_check_server && defined($check_connect) && $dbh->{AutoCommit} != 0)
  2277.   {
  2278.     
  2279.     $dbh->disconnect;
  2280.     $dbh=safe_connect($object);
  2281.     return;
  2282.   }
  2283.   return if (defined($check_connect) && defined($dbh->do($check_connect)));
  2284.   $dbh->disconnect || warn $dbh->errstr;
  2285.   print "\nreconnecting\n" if ($opt_debug);
  2286.   $reconnect_count++;
  2287.   undef($dbh);
  2288.   $dbh=safe_connect($object);
  2289. }
  2290.  
  2291. #
  2292. # print query if debugging
  2293. #
  2294. sub print_query
  2295. {
  2296.   my ($query)=@_;
  2297.   $last_error=$DBI::errstr;
  2298.   if ($opt_debug)
  2299.   {
  2300.     if (length($query) > 130)
  2301.     {
  2302.       $query=substr($query,0,120) . "...(" . (length($query)-120) . ")";
  2303.     }
  2304.     printf "\nGot error from query: '%s'\n%s\n",$query,$DBI::errstr;
  2305.   }
  2306. }
  2307.  
  2308. #
  2309. # Do one or many queries. Return 1 if all was ok
  2310. # Note that all rows are executed (to ensure that we execute drop table commands)
  2311. #
  2312.  
  2313. sub safe_query
  2314. {
  2315.   my($queries)=@_;
  2316.   my($query,$ok,$retry_ok,$retry,@tmp,$sth);
  2317.   $ok=1;
  2318.   if (ref($queries) ne "ARRAY")
  2319.   {
  2320.     push(@tmp,$queries);
  2321.     $queries= \@tmp;
  2322.   }
  2323.   foreach $query (@$queries)
  2324.   {
  2325.     printf "query1: %-80.80s ...(%d - %d)\n",$query,length($query),$retry_limit  if ($opt_log_all_queries);
  2326.     print LOG "$query;\n" if ($opt_log);
  2327.     if (length($query) > $query_size)
  2328.     {
  2329.       $ok=0;
  2330.       next;
  2331.     }
  2332.  
  2333.     $retry_ok=0;
  2334.     for ($retry=0; $retry < $retry_limit ; $retry++)
  2335.     {
  2336.       if (! ($sth=$dbh->prepare($query)))
  2337.       {
  2338.     print_query($query);
  2339.     $retry=100 if (!$server->abort_if_fatal_error());
  2340.     # Force a reconnect because of Access drop table bug!
  2341.     if ($retry == $retry_limit-2)
  2342.     {
  2343.       print "Forcing disconnect to retry query\n" if ($opt_debug);
  2344.       $dbh->disconnect || warn $dbh->errstr;
  2345.     }
  2346.     check_connect();        # Check that server is still up
  2347.       }
  2348.       else
  2349.       {
  2350.         if (!$sth->execute())
  2351.         {
  2352.        print_query($query);
  2353.       $retry=100 if (!$server->abort_if_fatal_error());
  2354.       # Force a reconnect because of Access drop table bug!
  2355.       if ($retry == $retry_limit-2)
  2356.       {
  2357.         print "Forcing disconnect to retry query\n" if ($opt_debug);
  2358.         $dbh->disconnect || warn $dbh->errstr;
  2359.       }
  2360.       check_connect();        # Check that server is still up
  2361.         }
  2362.         else
  2363.         {
  2364.       $retry = $retry_limit;
  2365.       $retry_ok = 1;
  2366.         }
  2367.         $sth->finish;
  2368.       }
  2369.     }
  2370.     $ok=0 if (!$retry_ok);
  2371.     if ($query =~ /create/i && $server->reconnect_on_errors())
  2372.     {
  2373.       print "Forcing disconnect to retry query\n" if ($opt_debug);
  2374.       $dbh->disconnect || warn $dbh->errstr;
  2375.       $dbh=safe_connect();
  2376.     }
  2377.   }
  2378.   return $ok;
  2379. }
  2380.  
  2381.  
  2382. #
  2383. # Do a query on a query package object.
  2384. #
  2385.  
  2386. sub limit_query
  2387. {
  2388.   my($object,$limit)=@_;
  2389.   my ($query,$result,$retry,$sth);
  2390.  
  2391.   $query=$object->query($limit);
  2392.   $result=safe_query($query);
  2393.   if (!$result)
  2394.   {
  2395.     $object->cleanup();
  2396.     return 0;
  2397.   }
  2398.   if (defined($query=$object->check_query()))
  2399.   {
  2400.     for ($retry=0 ; $retry < $retry_limit ; $retry++)
  2401.     {
  2402.       printf "query2: %-80.80s\n",$query if ($opt_log_all_queries);
  2403.       print LOG "$query;\n" if ($opt_log);
  2404.       if (($sth= $dbh->prepare($query)))
  2405.       {
  2406.     if ($sth->execute)
  2407.     {
  2408.       $result= $object->check($sth);
  2409.       $sth->finish;
  2410.       $object->cleanup();
  2411.       return $result;
  2412.     }
  2413.     print_query($query);
  2414.     $sth->finish;
  2415.       }
  2416.       else
  2417.       {
  2418.     print_query($query);
  2419.       }
  2420.       $retry=100 if (!$server->abort_if_fatal_error()); # No need to continue
  2421.       if ($retry == $retry_limit-2)
  2422.       {
  2423.     print "Forcing discoennect to retry query\n" if ($opt_debug);
  2424.     $dbh->disconnect || warn $dbh->errstr;
  2425.       }
  2426.       check_connect($object);   # Check that server is still up
  2427.     }
  2428.     $result=0;                  # Query failed
  2429.   }
  2430.   $object->cleanup();
  2431.   return $result;               # Server couldn't handle the query
  2432. }
  2433.  
  2434.  
  2435. sub report
  2436. {
  2437.   my ($prompt,$limit,@queries)=@_;
  2438.   print "$prompt: ";
  2439.   if (!defined($limits{$limit}))
  2440.   {
  2441.     save_config_data($limit,safe_query(\@queries) ? "yes" : "no",$prompt);
  2442.   }
  2443.   print "$limits{$limit}\n";
  2444.   return $limits{$limit} ne "no";
  2445. }
  2446.  
  2447. sub report_fail
  2448. {
  2449.   my ($prompt,$limit,@queries)=@_;
  2450.   print "$prompt: ";
  2451.   if (!defined($limits{$limit}))
  2452.   {
  2453.     save_config_data($limit,safe_query(\@queries) ? "no" : "yes",$prompt);
  2454.   }
  2455.   print "$limits{$limit}\n";
  2456.   return $limits{$limit} ne "no";
  2457. }
  2458.  
  2459.  
  2460. # Return true if one of the queries is ok
  2461.  
  2462. sub report_one
  2463. {
  2464.   my ($prompt,$limit,$queries)=@_;
  2465.   my ($query,$res,$result);
  2466.   print "$prompt: ";
  2467.   if (!defined($limits{$limit}))
  2468.   {
  2469.     $result="no";
  2470.     foreach $query (@$queries)
  2471.     {
  2472.       if (safe_query($query->[0]))
  2473.       {
  2474.     $result= $query->[1];
  2475.     last;
  2476.       }
  2477.     }
  2478.     save_config_data($limit,$result,$prompt);
  2479.   }
  2480.   print "$limits{$limit}\n";
  2481.   return $limits{$limit} ne "no";
  2482. }
  2483.  
  2484.  
  2485. # Execute query and save result as limit value.
  2486.  
  2487. sub report_result
  2488. {
  2489.   my ($prompt,$limit,$query)=@_;
  2490.   my($error);
  2491.   print "$prompt: ";
  2492.   if (!defined($limits{$limit}))
  2493.   {
  2494.     $error=safe_query_result($query,"1",2);
  2495.     save_config_data($limit,$error ? "not supported" : $last_result,$prompt);
  2496.   }
  2497.   print "$limits{$limit}\n";
  2498.   return $limits{$limit} ne "no";
  2499. }
  2500.  
  2501. sub report_trans
  2502. {
  2503.   my ($prompt,$limit,$queries,$check,$clear)=@_;
  2504.   print "$prompt: ";
  2505.   if (!defined($limits{$limit}))
  2506.   {
  2507.     eval {undef($dbh->{AutoCommit})};
  2508.     if (!$@)
  2509.     {
  2510.       if (safe_query(\@$queries))
  2511.       {
  2512.       $rc = $dbh->rollback;
  2513.       if ($rc) {
  2514.         $dbh->{AutoCommit} = 1;
  2515.         if (safe_query_result($check,"","")) {
  2516.           save_config_data($limit,"yes",$prompt);
  2517.         }
  2518.         safe_query($clear);
  2519.       } else {
  2520.         $dbh->{AutoCommit} = 1;
  2521.         safe_query($clear);
  2522.         save_config_data($limit,"error",$prompt);
  2523.       }
  2524.       } else {
  2525.         save_config_data($limit,"error",$prompt);
  2526.       }
  2527.       $dbh->{AutoCommit} = 1;
  2528.     }
  2529.     else
  2530.     {
  2531.       save_config_data($limit,"no",$prompt);
  2532.     }
  2533.     safe_query($clear);
  2534.   }
  2535.   print "$limits{$limit}\n";
  2536.   return $limits{$limit} ne "no";
  2537. }
  2538.  
  2539.  
  2540. sub check_and_report
  2541. {
  2542.   my ($prompt,$limit,$pre,$query,$post,$answer,$string_type,$skip_prompt,
  2543.       $function)=@_;
  2544.   my ($tmp);
  2545.   $function=0 if (!defined($function));
  2546.  
  2547.   print "$prompt: " if (!defined($skip_prompt));
  2548.   if (!defined($limits{$limit}))
  2549.   {
  2550.     $tmp=1-safe_query(\@$pre);
  2551.     $tmp=safe_query_result($query,$answer,$string_type) if (!$tmp);
  2552.     safe_query(\@$post);
  2553.     if ($function == 3)        # Report error as 'no'.
  2554.     {
  2555.       $function=0;
  2556.       $tmp= -$tmp;
  2557.     }
  2558.     if ($function == 0 ||
  2559.     $tmp != 0 && $function == 1 ||
  2560.     $tmp == 0 && $function== 2)
  2561.     {
  2562.       save_config_data($limit, $tmp == 0 ? "yes" : $tmp == 1 ? "no" : "error",
  2563.                $prompt);
  2564.       print "$limits{$limit}\n";
  2565.       return $function == 0 ? $limits{$limit} eq "yes" : 0;
  2566.     }
  2567.     return 1;            # more things to check
  2568.   }
  2569.   print "$limits{$limit}\n";
  2570.   return 0 if ($function);
  2571.   return $limits{$limit} eq "yes";
  2572. }
  2573.  
  2574.  
  2575. sub try_and_report
  2576. {
  2577.   my ($prompt,$limit,@tests)=@_;
  2578.   my ($tmp,$test,$type);
  2579.  
  2580.   print "$prompt: ";
  2581.   if (!defined($limits{$limit}))
  2582.   {
  2583.     $type="no";            # Not supported
  2584.     foreach $test (@tests)
  2585.     {
  2586.       my $tmp_type= shift(@$test);
  2587.       if (safe_query(\@$test))
  2588.       {
  2589.     $type=$tmp_type;
  2590.     goto outer;
  2591.       }
  2592.     }
  2593.   outer:
  2594.     save_config_data($limit, $type, $prompt);
  2595.   }
  2596.   print "$limits{$limit}\n";
  2597.   return $limits{$limit} ne "no";
  2598. }
  2599.  
  2600. #
  2601. # Just execute the query and check values;  Returns 1 if ok
  2602. #
  2603.  
  2604. sub execute_and_check
  2605. {
  2606.   my ($pre,$query,$post,$answer,$string_type)=@_;
  2607.   my ($tmp);
  2608.  
  2609.   $tmp=safe_query(\@$pre);
  2610.   $tmp=safe_query_result($query,$answer,$string_type) == 0 if ($tmp);
  2611.   safe_query(\@$post);
  2612.   return $tmp;
  2613. }
  2614.  
  2615.  
  2616. # returns 0 if ok, 1 if error, -1 if wrong answer
  2617. # Sets $last_result to value of query
  2618.  
  2619. sub safe_query_result
  2620. {
  2621.   my ($query,$answer,$result_type)=@_;
  2622.   my ($sth,$row,$result,$retry);
  2623.   undef($last_result);
  2624.  
  2625.   printf "\nquery3: %-80.80s\n",$query  if ($opt_log_all_queries);
  2626.   print LOG "$query;\n" if ($opt_log);
  2627.   for ($retry=0; $retry < $retry_limit ; $retry++)
  2628.   {
  2629.     if (!($sth=$dbh->prepare($query)))
  2630.     {
  2631.       print_query($query);
  2632.       if ($server->abort_if_fatal_error())
  2633.       {
  2634.     check_connect();    # Check that server is still up
  2635.     next;            # Retry again
  2636.       }
  2637.       check_connect();        # Check that server is still up
  2638.       return 1;
  2639.     }
  2640.     if (!$sth->execute)
  2641.     {
  2642.       print_query($query);
  2643.       if ($server->abort_if_fatal_error())
  2644.       {
  2645.     check_connect();    # Check that server is still up
  2646.     next;            # Retry again
  2647.       }
  2648.       check_connect();        # Check that server is still up
  2649.       return 1;
  2650.     }
  2651.     else
  2652.     {
  2653.       last;
  2654.     }
  2655.   }
  2656.   if (!($row=$sth->fetchrow_arrayref))
  2657.   {
  2658.     print "\nquery: $query didn't return any result\n" if ($opt_debug);
  2659.     $sth->finish;
  2660.     return ($result_type == 8) ? 0 : 1;
  2661.   }
  2662.   if($result_type == 8) {
  2663.     $sth->finish;
  2664.     return 1;
  2665.   }
  2666.   $result=0;                      # Ok
  2667.   $last_result= $row->[0];    # Save for report_result;
  2668.   if ($result_type == 0)    # Compare numbers
  2669.   {
  2670.     $row->[0] =~ s/,/,/;    # Fix if ',' is used instead of '.'
  2671.     if ($row->[0] != $answer && (abs($row->[0]- $answer)/
  2672.                  (abs($row->[0]) + abs($answer))) > 0.01)
  2673.     {
  2674.       $result=-1;
  2675.     }
  2676.   }
  2677.   elsif ($result_type == 1)    # Compare where end space may differ
  2678.   {
  2679.     $row->[0] =~ s/\s+$//;
  2680.     $result=-1 if ($row->[0] ne $answer);
  2681.   }
  2682.   elsif ($result_type == 3)    # This should be a exact match
  2683.   {
  2684.     $result= -1 if ($row->[0] ne $answer);
  2685.   }
  2686.   elsif ($result_type == 4)    # If results should be NULL
  2687.   {
  2688.     $result= -1 if (defined($row->[0]));
  2689.   }
  2690.   elsif ($result_type == 5)    # Result should have given prefix
  2691.   {
  2692.     $result= -1 if (length($row->[0]) < length($answer) &&
  2693.             substring($row->[0],1,length($answer)) ne $answer);
  2694.   }
  2695.   elsif ($result_type == 6)    # Exact match but ignore errors
  2696.   {
  2697.     $result= 1 if ($row->[0] ne $answer);
  2698.   }
  2699.   elsif ($result_type == 7)    # Compare against array of numbers
  2700.   {
  2701.     if ($row->[0] != $answer->[0])
  2702.     {
  2703.       $result= -1;
  2704.     }
  2705.     else
  2706.     {
  2707.       my ($value);
  2708.       shift @$answer;
  2709.       while (($row=$sth->fetchrow_arrayref))
  2710.       {
  2711.     $value=shift(@$answer);
  2712.     if (!defined($value))
  2713.     {
  2714.       print "\nquery: $query returned to many results\n"
  2715.         if ($opt_debug);
  2716.       $result= 1;
  2717.       last;
  2718.     }
  2719.     if ($row->[0] != $value)
  2720.     {
  2721.       $result= -1;
  2722.       last;
  2723.     }
  2724.       }
  2725.       if ($#$answer != -1)
  2726.       {
  2727.     print "\nquery: $query returned too few results\n"
  2728.       if ($opt_debug);
  2729.     $result= 1;
  2730.       }
  2731.     }
  2732.   }
  2733.   $sth->finish;
  2734.   print "\nquery: '$query' returned '$row->[0]' instead of '$answer'\n"
  2735.     if ($opt_debug && $result && $result_type != 7);
  2736.   return $result;
  2737. }
  2738.  
  2739. #
  2740. # Find limit using binary search.  This is a weighed binary search that
  2741. # will prefere lower limits to get the server to crash as few times as possible
  2742. #
  2743.  
  2744. sub find_limit()
  2745. {
  2746.   my ($prompt,$limit,$query)=@_;
  2747.   my ($first,$end,$i,$tmp);
  2748.   print "$prompt: ";
  2749.   if (defined($end=$limits{$limit}))
  2750.   {
  2751.     print "$end (cache)\n";
  2752.     return $end;
  2753.   }
  2754.   if (defined($query->{'init'}) && !defined($end=$limits{'restart'}{'tohigh'}))
  2755.   {
  2756.     if (!safe_query($query->{'init'}))
  2757.     {
  2758.       $query->cleanup();
  2759.       return "error";
  2760.     }
  2761.   }
  2762.  
  2763.   if (!limit_query($query,1))           # This must work
  2764.   {
  2765.     print "\nMaybe fatal error: Can't check '$prompt' for limit=1\nerror: $last_error\n";
  2766.     return "error";
  2767.   }
  2768.  
  2769.   $first=0;
  2770.   $first=$limits{'restart'}{'low'} if ($limits{'restart'}{'low'});
  2771.  
  2772.   if (defined($end=$limits{'restart'}{'tohigh'}))
  2773.   {
  2774.     $end--;
  2775.     print "\nRestarting this with low limit: $first and high limit: $end\n";
  2776.     delete $limits{'restart'};
  2777.     $i=$first+int(($end-$first+4)/5);           # Prefere lower on errors
  2778.   }
  2779.   else
  2780.   {
  2781.     $end= $query->max_limit();
  2782.     $i=int(($end+$first)/2);
  2783.   }
  2784.  
  2785.   unless(limit_query($query,0+$end)) {
  2786.     while ($first < $end)
  2787.     {
  2788.       print "." if ($opt_debug);
  2789.       save_config_data("restart",$i,"") if ($opt_restart);
  2790.       if (limit_query($query,$i))
  2791.       {
  2792.         $first=$i;
  2793.         $i=$first+int(($end-$first+1)/2); # to be a bit faster to go up
  2794.       }
  2795.       else
  2796.       {
  2797.         $end=$i-1;
  2798.         $i=$first+int(($end-$first+4)/5); # Prefere lower on errors
  2799.       }
  2800.     }
  2801.   }
  2802.   $end+=$query->{'offset'} if ($end && defined($query->{'offset'}));
  2803.   if ($end >= $query->{'max_limit'} &&
  2804.       substr($query->{'max_limit'},0,1) eq '+')
  2805.   {
  2806.     $end= $query->{'max_limit'};
  2807.   }
  2808.   print "$end\n";
  2809.   save_config_data($limit,$end,$prompt);
  2810.   delete $limits{'restart'};
  2811.   return $end;
  2812. }
  2813.  
  2814. #
  2815. # Check that the query works!
  2816. #
  2817.  
  2818. sub assert
  2819. {
  2820.   my($query)=@_;
  2821.  
  2822.   if (!safe_query($query))
  2823.   {
  2824.     $query=join("; ",@$query) if (ref($query) eq "ARRAY");
  2825.     print "\nFatal error:\nquery: '$query'\nerror: $DBI::errstr\n";
  2826.     exit 1;
  2827.   }
  2828. }
  2829.  
  2830.  
  2831. sub read_config_data
  2832. {
  2833.   my ($key,$limit,$prompt);
  2834.   if (-e $opt_config_file)
  2835.   {
  2836.     open(CONFIG_FILE,"+<$opt_config_file") ||
  2837.       die "Can't open configure file $opt_config_file\n";
  2838.     print "Reading old values from cache: $opt_config_file\n";
  2839.   }
  2840.   else
  2841.   {
  2842.     open(CONFIG_FILE,"+>>$opt_config_file") ||
  2843.       die "Can't create configure file $opt_config_file: $!\n";
  2844.   }
  2845.   select CONFIG_FILE;
  2846.   $|=1;
  2847.   select STDOUT;
  2848.   while (<CONFIG_FILE>)
  2849.   {
  2850.     chomp;
  2851.     if (/^(\S+)=([^\#]*[^\#\s])\s*(\# .*)*$/)
  2852.     {
  2853.       $key=$1; $limit=$2 ; $prompt=$3;
  2854.       if (!$opt_quick || $limit =~ /\d/ || $key =~ /crash_me/)
  2855.       {
  2856.     if ($key !~ /restart/i)
  2857.     {
  2858.       $limits{$key}=$limit;
  2859.       $prompts{$key}=length($prompt) ? substr($prompt,2) : "";
  2860.       delete $limits{'restart'};
  2861.     }
  2862.     else
  2863.     {
  2864.       $limit_changed=1;
  2865.       if ($limit > $limits{'restart'}{'tohigh'})
  2866.       {
  2867.         $limits{'restart'}{'low'} = $limits{'restart'}{'tohigh'};
  2868.       }
  2869.       $limits{'restart'}{'tohigh'} = $limit;
  2870.     }
  2871.       }
  2872.     }
  2873.     elsif (!/^\s*$/ && !/^\#/)
  2874.     {
  2875.       die "Wrong config row: $_\n";
  2876.     }
  2877.   }
  2878. }
  2879.  
  2880.  
  2881. sub save_config_data
  2882. {
  2883.   my ($key,$limit,$prompt)=@_;
  2884.   $prompts{$key}=$prompt;
  2885.   return if (defined($limits{$key}) && $limits{$key} eq $limit);
  2886.   if (!defined($limit) || $limit eq "")
  2887.   {
  2888.     die "Undefined limit for $key\n";
  2889.   }
  2890.   print CONFIG_FILE "$key=$limit\t# $prompt\n";
  2891.   $limits{$key}=$limit;
  2892.   $limit_changed=1;
  2893.   if (($opt_restart && $limits{'operating_system'} =~ /windows/i) ||
  2894.                ($limits{'operating_system'} =~ /NT/))
  2895.   {
  2896.     # If perl crashes in windows, everything is lost (Wonder why? :)
  2897.     close CONFIG_FILE;
  2898.     open(CONFIG_FILE,"+>>$opt_config_file") ||
  2899.       die "Can't reopen configure file $opt_config_file: $!\n";
  2900.   }
  2901. }
  2902.  
  2903.  
  2904. sub save_all_config_data
  2905. {
  2906.   my ($key,$tmp);
  2907.   close CONFIG_FILE;
  2908.   return if (!$limit_changed);
  2909.   open(CONFIG_FILE,">$opt_config_file") ||
  2910.     die "Can't create configure file $opt_config_file: $!\n";
  2911.   select CONFIG_FILE;
  2912.   $|=1;
  2913.   select STDOUT;
  2914.   delete $limits{'restart'};
  2915.  
  2916.   print CONFIG_FILE "#This file is automaticly generated by crash-me $version\n\n";
  2917.   foreach $key (sort keys %limits)
  2918.   {
  2919.     $tmp="$key=$limits{$key}";
  2920.     print CONFIG_FILE $tmp . ("\t" x (int((32-min(length($tmp),32)+7)/8)+1)) .
  2921.       "# $prompts{$key}\n";
  2922.   }
  2923.   close CONFIG_FILE;
  2924. }
  2925.  
  2926.  
  2927. sub check_repeat
  2928. {
  2929.   my ($sth,$limit)=@_;
  2930.   my ($row);
  2931.  
  2932.   return 0 if (!($row=$sth->fetchrow_arrayref));
  2933.   return (defined($row->[0]) && ('a' x $limit) eq $row->[0]) ? 1 : 0;
  2934. }
  2935.  
  2936.  
  2937. sub min
  2938. {
  2939.   my($min)=$_[0];
  2940.   my($i);
  2941.   for ($i=1 ; $i <= $#_; $i++)
  2942.   {
  2943.     $min=$_[$i] if ($min > $_[$i]);
  2944.   }
  2945.   return $min;
  2946. }
  2947.  
  2948. sub sql_concat
  2949. {
  2950.   my ($a,$b)= @_;
  2951.   return "$a || $b" if ($limits{'func_sql_concat_as_||'} eq 'yes');
  2952.   return "concat($a,$b)" if ($limits{'func_odbc_concat'} eq 'yes');
  2953.   return "$a + $b" if ($limits{'func_extra_concat_as_+'} eq 'yes');
  2954.   return undef;
  2955. }
  2956.  
  2957. #
  2958. # Returns a list of statements to create a table in a portable manner
  2959. # but still utilizing features in the databases.
  2960. #
  2961.  
  2962. sub create_table
  2963. {
  2964.   my($table_name,$fields,$index) = @_;
  2965.   my($query,$nr,$parts,@queries,@index);
  2966.  
  2967.   $query="create table $table_name (";
  2968.   $nr=0;
  2969.   foreach $field (@$fields)
  2970.   {
  2971.     $query.= $field . ',';
  2972.   }
  2973.   foreach $index (@$index)
  2974.   {
  2975.     $index =~ /\(([^\(]*)\)$/i;
  2976.     $parts=$1;
  2977.     if ($index =~ /^primary key/)
  2978.     {
  2979.       if ($limits{'primary_key_in_create'} eq 'yes')
  2980.       {
  2981.     $query.= $index . ',';
  2982.       }
  2983.       else
  2984.       {
  2985.     push(@queries,
  2986.          "create unique index ${table_name}_prim on $table_name ($parts)");
  2987.       }
  2988.     }
  2989.     elsif ($index =~ /^unique/)
  2990.     {
  2991.       if ($limits{'unique_in_create'} eq 'yes')
  2992.       {
  2993.     $query.= "unique ($parts),";
  2994.       }
  2995.       else
  2996.       {
  2997.     $nr++;
  2998.     push(@queries,
  2999.          "create unique index ${table_name}_$nr on $table_name ($parts)");
  3000.  
  3001.       }
  3002.     }
  3003.     else
  3004.     {
  3005.       if ($limits{'index_in_create'} eq 'yes')
  3006.       {
  3007.     $query.= "index ($parts),";
  3008.       }
  3009.       else
  3010.       {
  3011.     $nr++;
  3012.     push(@queries,
  3013.          "create index ${table_name}_$nr on $table_name ($1)");
  3014.       }
  3015.     }
  3016.   }
  3017.   chop($query);
  3018.   $query.= ')';
  3019.   unshift(@queries,$query);
  3020.   return @queries;
  3021. }
  3022.  
  3023.  
  3024. #
  3025. # This is used by some query packages to change:
  3026. # %d -> limit
  3027. # %s -> 'a' x limit
  3028. # %v -> "1,1,1,1,1" where there are 'limit' number of ones
  3029. # %f -> q1,q2,q3....
  3030. # %F -> q1 integer,q2 integer,q3 integer....
  3031.  
  3032. sub fix_query
  3033. {
  3034.   my ($query,$limit)=@_;
  3035.   my ($repeat,$i);
  3036.  
  3037.   return $query if !(defined($query));
  3038.   $query =~ s/%d/$limit/g;
  3039.   if ($query =~ /%s/)
  3040.   {
  3041.     $repeat= 'a' x $limit;
  3042.     $query =~ s/%s/$repeat/g;
  3043.   }
  3044.   if ($query =~ /%v/)
  3045.   {
  3046.     $repeat= '1,' x $limit;
  3047.     chop($repeat);
  3048.     $query =~ s/%v/$repeat/g;
  3049.   }
  3050.   if ($query =~ /%f/)
  3051.   {
  3052.     $repeat="";
  3053.     for ($i=1 ; $i <= $limit ; $i++)
  3054.     {
  3055.       $repeat.="q$i,";
  3056.     }
  3057.     chop($repeat);
  3058.     $query =~ s/%f/$repeat/g;
  3059.   }
  3060.   if ($query =~ /%F/)
  3061.   {
  3062.     $repeat="";
  3063.     for ($i=1 ; $i <= $limit ; $i++)
  3064.     {
  3065.       $repeat.="q$i integer,";
  3066.     }
  3067.     chop($repeat);
  3068.     $query =~ s/%F/$repeat/g;
  3069.   }
  3070.   return $query;
  3071. }
  3072.  
  3073.  
  3074. #
  3075. # Different query packages
  3076. #
  3077.  
  3078. package query_repeat;
  3079.  
  3080. sub new
  3081. {
  3082.   my ($type,$init,$query,$add1,$add_mid,$add,$add_end,$end_query,$cleanup,
  3083.       $max_limit, $check, $offset)=@_;
  3084.   my $self={};
  3085.   if (defined($init) && $#$init != -1)
  3086.   {
  3087.     $self->{'init'}=$init;
  3088.   }
  3089.   $self->{'query'}=$query;
  3090.   $self->{'add1'}=$add1;
  3091.   $self->{'add_mid'}=$add_mid;
  3092.   $self->{'add'}=$add;
  3093.   $self->{'add_end'}=$add_end;
  3094.   $self->{'end_query'}=$end_query;
  3095.   $self->{'cleanup'}=$cleanup;
  3096.   $self->{'max_limit'}=(defined($max_limit) ? $max_limit : $main::query_size);
  3097.   $self->{'check'}=$check;
  3098.   $self->{'offset'}=$offset;
  3099.   $self->{'printf'}= ($add =~ /%d/);
  3100.   bless $self;
  3101. }
  3102.  
  3103. sub query
  3104. {
  3105.   my ($self,$limit)=@_;
  3106.   if (!$self->{'printf'})
  3107.   {
  3108.     return $self->{'query'} . ($self->{'add'} x $limit) .
  3109.       ($self->{'add_end'} x $limit) . $self->{'end_query'};
  3110.   }
  3111.   my ($tmp,$tmp2,$tmp3,$i);
  3112.   $tmp=$self->{'query'};
  3113.   if ($self->{'add1'})
  3114.   {
  3115.     for ($i=0; $i < $limit ; $i++)
  3116.     {
  3117.       $tmp3 = $self->{'add1'};
  3118.       $tmp3 =~ s/%d/$i/g;
  3119.       $tmp  .= $tmp3;
  3120.     }
  3121.   }
  3122.   $tmp .= " ".$self->{'add_mid'};
  3123.   if ($self->{'add'})
  3124.   {
  3125.     for ($i=0; $i < $limit ; $i++)
  3126.     {
  3127.       $tmp2 = $self->{'add'};
  3128.       $tmp2 =~ s/%d/$i/g;
  3129.       $tmp  .= $tmp2;
  3130.     }
  3131.   }
  3132.   return ($tmp .
  3133.       ($self->{'add_end'} x $limit) . $self->{'end_query'});
  3134. }
  3135.  
  3136. sub max_limit
  3137. {
  3138.   my ($self)=@_;
  3139.   my $tmp;
  3140.   $tmp=int(($main::limits{"query_size"}-length($self->{'query'})
  3141.         -length($self->{'add_mid'})-length($self->{'end_query'}))/
  3142.        (length($self->{'add1'})+
  3143.        length($self->{'add'})+length($self->{'add_end'})));
  3144.   return main::min($self->{'max_limit'},$tmp);
  3145. }
  3146.  
  3147.  
  3148. sub cleanup
  3149. {
  3150.   my ($self)=@_;
  3151.   my($tmp,$statement);
  3152.   $tmp=$self->{'cleanup'};
  3153.   foreach $statement (@$tmp)
  3154.   {
  3155.     main::safe_query($statement) if (defined($statement) && length($statement));
  3156.   }
  3157. }
  3158.  
  3159. sub check
  3160. {
  3161.   my ($self,$sth)=@_;
  3162.   my $check=$self->{'check'};
  3163.   return &$check($sth,$self->{'limit'}) if (defined($check));
  3164.   return 1;
  3165. }
  3166.  
  3167. sub check_query
  3168. {
  3169.   return undef;
  3170. }
  3171.  
  3172.  
  3173. package query_num;
  3174.  
  3175. sub new
  3176. {
  3177.   my ($type,$query,$end_query,$cleanup,$max_limit,$check)=@_;
  3178.   my $self={};
  3179.   $self->{'query'}=$query;
  3180.   $self->{'end_query'}=$end_query;
  3181.   $self->{'cleanup'}=$cleanup;
  3182.   $self->{'max_limit'}=$max_limit;
  3183.   $self->{'check'}=$check;
  3184.   bless $self;
  3185. }
  3186.  
  3187.  
  3188. sub query
  3189. {
  3190.   my ($self,$i)=@_;
  3191.   $self->{'limit'}=$i;
  3192.   return "$self->{'query'}$i$self->{'end_query'}";
  3193. }
  3194.  
  3195. sub max_limit
  3196. {
  3197.   my ($self)=@_;
  3198.   return $self->{'max_limit'};
  3199. }
  3200.  
  3201. sub cleanup
  3202. {
  3203.   my ($self)=@_;
  3204.   my($statement);
  3205.   foreach $statement ($self->{'$cleanup'})
  3206.   {
  3207.     main::safe_query($statement) if (defined($statement) && length($statement));
  3208.   }
  3209. }
  3210.  
  3211.  
  3212. sub check
  3213. {
  3214.   my ($self,$sth)=@_;
  3215.   my $check=$self->{'check'};
  3216.   return &$check($sth,$self->{'limit'}) if (defined($check));
  3217.   return 1;
  3218. }
  3219.  
  3220. sub check_query
  3221. {
  3222.   return undef;
  3223. }
  3224.  
  3225. #
  3226. # This package is used when testing CREATE TABLE!
  3227. #
  3228.  
  3229. package query_table;
  3230.  
  3231. sub new
  3232. {
  3233.   my ($type,$query, $add, $end_query, $extra_init, $safe_query, $check,
  3234.       $cleanup, $max_limit, $offset)=@_;
  3235.   my $self={};
  3236.   $self->{'query'}=$query;
  3237.   $self->{'add'}=$add;
  3238.   $self->{'end_query'}=$end_query;
  3239.   $self->{'extra_init'}=$extra_init;
  3240.   $self->{'safe_query'}=$safe_query;
  3241.   $self->{'check'}=$check;
  3242.   $self->{'cleanup'}=$cleanup;
  3243.   $self->{'max_limit'}=$max_limit;
  3244.   $self->{'offset'}=$offset;
  3245.   bless $self;
  3246. }
  3247.  
  3248.  
  3249. sub query
  3250. {
  3251.   my ($self,$limit)=@_;
  3252.   $self->{'limit'}=$limit;
  3253.   $self->cleanup();     # Drop table before create
  3254.  
  3255.   my ($tmp,$tmp2,$i,$query,@res);
  3256.   $tmp =$self->{'query'};
  3257.   $tmp =~ s/%d/$limit/g;
  3258.   for ($i=1; $i <= $limit ; $i++)
  3259.   {
  3260.     $tmp2 = $self->{'add'};
  3261.     $tmp2 =~ s/%d/$i/g;
  3262.     $tmp  .= $tmp2;
  3263.   }
  3264.   push(@res,$tmp . $self->{'end_query'});
  3265.   $tmp=$self->{'extra_init'};
  3266.   foreach $query (@$tmp)
  3267.   {
  3268.     push(@res,main::fix_query($query,$limit));
  3269.   }
  3270.   return \@res;
  3271. }
  3272.  
  3273.  
  3274. sub max_limit
  3275. {
  3276.   my ($self)=@_;
  3277.   return $self->{'max_limit'};
  3278. }
  3279.  
  3280.  
  3281. sub check_query
  3282. {
  3283.   my ($self)=@_;
  3284.   return main::fix_query($self->{'safe_query'},$self->{'limit'});
  3285. }
  3286.  
  3287. sub check
  3288. {
  3289.   my ($self,$sth)=@_;
  3290.   my $check=$self->{'check'};
  3291.   return 0 if (!($row=$sth->fetchrow_arrayref));
  3292.   if (defined($check))
  3293.   {
  3294.     return (defined($row->[0]) &&
  3295.         $row->[0] eq main::fix_query($check,$self->{'limit'})) ? 1 : 0;
  3296.   }
  3297.   return 1;
  3298. }
  3299.  
  3300.  
  3301. # Remove table before and after create table query
  3302.  
  3303. sub cleanup()
  3304. {
  3305.   my ($self)=@_;
  3306.   main::safe_query(main::fix_query($self->{'cleanup'},$self->{'limit'}));
  3307. }
  3308.  
  3309. #
  3310. # Package to do many queries with %d, and %s substitution
  3311. #
  3312.  
  3313. package query_many;
  3314.  
  3315. sub new
  3316. {
  3317.   my ($type,$query,$safe_query,$check_result,$cleanup,$max_limit,$offset,
  3318.       $safe_cleanup)=@_;
  3319.   my $self={};
  3320.   $self->{'query'}=$query;
  3321.   $self->{'safe_query'}=$safe_query;
  3322.   $self->{'check'}=$check_result;
  3323.   $self->{'cleanup'}=$cleanup;
  3324.   $self->{'max_limit'}=$max_limit;
  3325.   $self->{'offset'}=$offset;
  3326.   $self->{'safe_cleanup'}=$safe_cleanup;
  3327.   bless $self;
  3328. }
  3329.  
  3330.  
  3331. sub query
  3332. {
  3333.   my ($self,$limit)=@_;
  3334.   my ($queries,$query,@res);
  3335.   $self->{'limit'}=$limit;
  3336.   $self->cleanup() if (defined($self->{'safe_cleanup'}));
  3337.   $queries=$self->{'query'};
  3338.   foreach $query (@$queries)
  3339.   {
  3340.     push(@res,main::fix_query($query,$limit));
  3341.   }
  3342.   return \@res;
  3343. }
  3344.  
  3345. sub check_query
  3346. {
  3347.   my ($self)=@_;
  3348.   return main::fix_query($self->{'safe_query'},$self->{'limit'});
  3349. }
  3350.  
  3351. sub cleanup
  3352. {
  3353.   my ($self)=@_;
  3354.   my($tmp,$statement);
  3355.   return if (!defined($self->{'cleanup'}));
  3356.   $tmp=$self->{'cleanup'};
  3357.   foreach $statement (@$tmp)
  3358.   {
  3359.     if (defined($statement) && length($statement))
  3360.     {
  3361.       main::safe_query(main::fix_query($statement,$self->{'limit'}));
  3362.     }
  3363.   }
  3364. }
  3365.  
  3366.  
  3367. sub check
  3368. {
  3369.   my ($self,$sth)=@_;
  3370.   my ($check,$row);
  3371.   return 0 if (!($row=$sth->fetchrow_arrayref));
  3372.   $check=$self->{'check'};
  3373.   if (defined($check))
  3374.   {
  3375.     return (defined($row->[0]) &&
  3376.         $row->[0] eq main::fix_query($check,$self->{'limit'})) ? 1 : 0;
  3377.   }
  3378.   return 1;
  3379. }
  3380.  
  3381. sub max_limit
  3382. {
  3383.   my ($self)=@_;
  3384.   return $self->{'max_limit'};
  3385. }
  3386.  
  3387. #
  3388. # Used to find max supported row length
  3389. #
  3390.  
  3391. package query_row_length;
  3392.  
  3393. sub new
  3394. {
  3395.   my ($type,$create,$null,$drop,$max_limit)=@_;
  3396.   my $self={};
  3397.   $self->{'table_name'}=$create;
  3398.   $self->{'null'}=$null;
  3399.   $self->{'cleanup'}=$drop;
  3400.   $self->{'max_limit'}=$max_limit;
  3401.   bless $self;
  3402. }
  3403.  
  3404.  
  3405. sub query
  3406. {
  3407.   my ($self,$limit)=@_;
  3408.   my ($res,$values,$size,$length,$i);
  3409.   $self->{'limit'}=$limit;
  3410.  
  3411.   $res="";
  3412.   $size=main::min($main::limits{'max_char_size'},255);
  3413.   $size = 255 if (!$size); # Safety
  3414.   for ($length=$i=0; $length + $size <= $limit ; $length+=$size, $i++)
  3415.   {
  3416.     $res.= "q$i char($size) $self->{'null'},";
  3417.     $values.="'" . ('a' x $size) . "',";
  3418.   }
  3419.   if ($length < $limit)
  3420.   {
  3421.     $size=$limit-$length;
  3422.     $res.= "q$i char($size) $self->{'null'},";
  3423.     $values.="'" . ('a' x $size) . "',";
  3424.   }
  3425.   chop($res);
  3426.   chop($values);
  3427.   return ["create table " . $self->{'table_name'} . " ($res)",
  3428.       "insert into " . $self->{'table_name'} . " values ($values)"];
  3429. }
  3430.  
  3431. sub max_limit
  3432. {
  3433.   my ($self)=@_;
  3434.   return $self->{'max_limit'};
  3435. }
  3436.  
  3437. sub cleanup
  3438. {
  3439.   my ($self)=@_;
  3440.   main::safe_query($self->{'cleanup'});
  3441. }
  3442.  
  3443.  
  3444. sub check
  3445. {
  3446.   return 1;
  3447. }
  3448.  
  3449. sub check_query
  3450. {
  3451.   return undef;
  3452. }
  3453.  
  3454. #
  3455. # Used to find max supported index length
  3456. #
  3457.  
  3458. package query_index_length;
  3459.  
  3460. sub new
  3461. {
  3462.   my ($type,$create,$drop,$max_limit)=@_;
  3463.   my $self={};
  3464.   $self->{'create'}=$create;
  3465.   $self->{'cleanup'}=$drop;
  3466.   $self->{'max_limit'}=$max_limit;
  3467.   bless $self;
  3468. }
  3469.  
  3470.  
  3471. sub query
  3472. {
  3473.   my ($self,$limit)=@_;
  3474.   my ($res,$size,$length,$i,$parts,$values);
  3475.   $self->{'limit'}=$limit;
  3476.  
  3477.   $res=$parts=$values="";
  3478.   $size=main::min($main::limits{'max_index_part_length'},$main::limits{'max_char_size'});
  3479.   $size=1 if ($size == 0);    # Avoid infinite loop errors
  3480.   for ($length=$i=0; $length + $size <= $limit ; $length+=$size, $i++)
  3481.   {
  3482.     $res.= "q$i char($size) not null,";
  3483.     $parts.= "q$i,";
  3484.     $values.= "'" . ('a' x $size) . "',";
  3485.   }
  3486.   if ($length < $limit)
  3487.   {
  3488.     $size=$limit-$length;
  3489.     $res.= "q$i char($size) not null,";
  3490.     $parts.="q$i,";
  3491.     $values.= "'" . ('a' x $size) . "',";
  3492.   }
  3493.   chop($parts);
  3494.   chop($res);
  3495.   chop($values);
  3496.   if ($main::limits{'unique_in_create'} eq 'yes')
  3497.   {
  3498.     return [$self->{'create'} . "($res,unique ($parts))",
  3499.         "insert into crash_q values($values)"];
  3500.   }
  3501.   return [$self->{'create'} . "($res)",
  3502.       "create index crash_q_index on crash_q ($parts)",
  3503.       "insert into crash_q values($values)"];
  3504. }
  3505.  
  3506. sub max_limit
  3507. {
  3508.   my ($self)=@_;
  3509.   return $self->{'max_limit'};
  3510. }
  3511.  
  3512. sub cleanup
  3513. {
  3514.   my ($self)=@_;
  3515.   main::safe_query($self->{'cleanup'});
  3516. }
  3517.  
  3518.  
  3519. sub check
  3520. {
  3521.   return 1;
  3522. }
  3523.  
  3524. sub check_query
  3525. {
  3526.   return undef;
  3527. }
  3528.  
  3529.  
  3530. ### TODO:
  3531. # OID test instead of / in addition to _rowid
  3532.