source include/not_valgrind.inc;
source include/have_memcached_plugin.inc;
source include/not_windows.inc;
source include/have_innodb.inc;

--disable_query_log
CALL mtr.add_suppression("daemon-memcached-w-batch-size': unsigned");
CALL mtr.add_suppression("Could not obtain server's UPN to be used as target service name");
CALL mtr.add_suppression("InnoDB: Warning: MySQL is trying to drop");
--enable_query_log

--enable_connect_log
SET @tx_isolation= @@global.tx_isolation;
SET GLOBAL TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

# Create the memcached tables
--disable_query_log
source include/memcache_config.inc;
--enable_query_log

INSERT INTO cache_policies VALUES("cache_policy", "innodb_only",
				  "innodb_only", "innodb_only", "innodb_only");

INSERT INTO config_options VALUES("separator", "|");

# describe table for memcache
INSERT INTO containers VALUES ("desc_t1", "test", "t1",
			       "c1", "c2,c21", "c3", "c4", "c5", "PRIMARY");
INSERT INTO containers VALUES ("int_test", "test", "int_test",
			       "k", "v",  "", "", "", "PRIMARY");
USE test;

--disable_warnings
DROP TABLE IF EXISTS int_test;
DROP TABLE IF EXISTS t1;
--enable_warnings
CREATE TABLE t1        (c1 VARCHAR(32),
			c2 INT,
			c21 VARCHAR(1024),
			c3 INT, c4 BIGINT UNSIGNED, c5 INT, primary key(c1))
ENGINE = INNODB;

CREATE TABLE int_test  (k varchar(32),
			v int unsigned NOT NULL, PRIMARY KEY (k)) ENGINE=InnoDB;

INSERT INTO t1 VALUES ('D', 1, 'City', 0, 0, 0);
INSERT INTO t1 VALUES ('B', 2, 'Mitte', 0, 0, 0);
INSERT INTO t1 VALUES ('C', 3, 'West', 0, 0 ,0);
INSERT INTO t1 VALUES ('H', 4, 'Norderstedt', 0, 0, 0);

# Tables must exist before plugin can be started!
INSTALL PLUGIN daemon_memcached SONAME 'libmemcached.so';

# The following select is writing all to  memcache otherwisw memcache is empty.
--sorted_result
SELECT c1,c2,c21 FROM t1;

SELECT SLEEP(2);

perl;
use DBI;
use Cache::Memcached;
my $memd = new Cache::Memcached {
  'servers' => [ "127.0.0.1:11213" ],
  'connect_timeout' => 20,
  'select_timeout' => 20
};
print "Here the memcached results with D,B,H,C:\n";
$val = $memd->get("D");
if ($val) { print "$val\n"; }
$val = $memd->get("B");
if ($val) { print "$val\n"; }
$val = $memd->get("H");
if ($val) { print "$val\n"; }
$val = $memd->get("C");
if ($val) { print "$val\n"; }
$memd->disconnect_all;
EOF

--sorted_result
SELECT c1,c2,c21 FROM t1;

perl;
use DBI;
use Cache::Memcached;
my $memd = new Cache::Memcached {
  'servers' => [ "127.0.0.1:11213" ],
  'connect_timeout' => 20,
  'select_timeout' => 20
};
print "Here the memcached results after set:\n";
$val = $memd->set("E","5|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set min border:\n";
$val = $memd->set("E","-2147483648|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set max border:\n";
$val = $memd->set("E","2147483647|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set over max border:\n";
$val = $memd->set("E","2147483648|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
$memd->disconnect_all;
EOF

--sorted_result
SELECT c1,c2,c21 FROM t1;

#Test incr/decr command
perl;
use DBI;
use Cache::Memcached;
my $memd = new Cache::Memcached {
  'servers' => [ "127.0.0.1:11213" ],
  'connect_timeout' => 20,
  'select_timeout' => 20
};
$val = $memd->set("F","6|Sofia");
print "Here the memcached results after set:\n";
$val = $memd->get("F");
if ($val) { print "$val\n"; }
print "Here the memcached results after calling incr 10:\n";
$val = $memd->incr("F",10);
$val = $memd->get("F");
if ($val) { print "$val\n"; }
print "Here the memcached results after calling decr 4:\n";
$val = $memd->decr("F",4);
$val = $memd->get("F");
if ($val) { print "$val\n"; }
$val = $memd->delete("F",0);
$memd->disconnect_all;
EOF

--sorted_result
SELECT c1,c2,c21 FROM t1;

ALTER TABLE t1 MODIFY c2 varchar(20);

perl;
use DBI;
use Cache::Memcached;
my $memd = new Cache::Memcached {
  'servers' => [ "127.0.0.1:11213" ],
  'connect_timeout' => 20,
  'select_timeout' => 20
};
print "Here the memcached results with D,B,H,C:\n";
$val = $memd->get("D");
if ($val) { print "$val\n"; }
$val = $memd->get("B");
if ($val) { print "$val\n"; }
$val = $memd->get("H");
if ($val) { print "$val\n"; }
$val = $memd->get("C");
if ($val) { print "$val\n"; }
print "Here the memcached results after set:\n";
$val = $memd->set("E","5|Ost-VARCHAR");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
$memd->disconnect_all;
EOF

--sorted_result
SELECT c1,c2,c21 FROM t1;

# Test BIGINT
ALTER TABLE t1 MODIFY c2 BIGINT;

perl;
use DBI;
use Cache::Memcached;
my $memd = new Cache::Memcached {
  'servers' => [ "127.0.0.1:11213" ],
  'connect_timeout' => 20,
  'select_timeout' => 20
};
print "Here the memcached results with D,B,H,C:\n";
$val = $memd->get("D");
if ($val) { print "$val\n"; }
$val = $memd->get("B");
if ($val) { print "$val\n"; }
$val = $memd->get("H");
if ($val) { print "$val\n"; }
$val = $memd->get("C");
if ($val) { print "$val\n"; }
print "Here the memcached results after set:\n";
$val = $memd->set("E","5|Ost-BIGINT");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set min border:\n";
$val = $memd->set("E","-9223372036854775808|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set max border:\n";
$val = $memd->set("E","9223372036854775807|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set over max border:\n";
$val = $memd->set("E","9223372036854775808|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after add:\n";
$val = $memd->set("F","-1|Beijing-Signed-BIGINT");
$val = $memd->get("F");
if ($val) { print "$val\n"; }
$memd->disconnect_all;
EOF

--sorted_result
SELECT c1,c2,c21 FROM t1;

SET sql_mode='NO_ENGINE_SUBSTITUTION';
# Test SMALLINT
ALTER TABLE t1 MODIFY c2 SMALLINT;

perl;
use DBI;
use Cache::Memcached;
my $memd = new Cache::Memcached {
  'servers' => [ "127.0.0.1:11213" ],
  'connect_timeout' => 20,
  'select_timeout' => 20
};
print "Here the memcached results with D,B,H,C:\n";
$val = $memd->get("D");
if ($val) { print "$val\n"; }
$val = $memd->get("B");
if ($val) { print "$val\n"; }
$val = $memd->get("H");
if ($val) { print "$val\n"; }
$val = $memd->get("C");
if ($val) { print "$val\n"; }
print "Here the memcached results after set:\n";
$val = $memd->set("E","5|Ost-SMALLINT");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set min border:\n";
$val = $memd->set("E","-32768|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set max border:\n";
$val = $memd->set("E","32767|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set over max border:\n";
$val = $memd->set("E","32768|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
$memd->disconnect_all;
EOF

--sorted_result
SELECT c1,c2,c21 FROM t1;

# Test TINYINT
ALTER TABLE t1 MODIFY c2 TINYINT;

perl;
use DBI;
use Cache::Memcached;
my $memd = new Cache::Memcached {
  'servers' => [ "127.0.0.1:11213" ],
  'connect_timeout' => 20,
  'select_timeout' => 20
};
print "Here the memcached results with D,B,H,C:\n";
$val = $memd->get("D");
if ($val) { print "$val\n"; }
$val = $memd->get("B");
if ($val) { print "$val\n"; }
$val = $memd->get("H");
if ($val) { print "$val\n"; }
$val = $memd->get("C");
if ($val) { print "$val\n"; }
print "Here the memcached results after set:\n";
$val = $memd->set("E","5|Ost-TINYINT");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set min border:\n";
$val = $memd->set("E","-128|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set max border:\n";
$val = $memd->set("E","127|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set over max border:\n";
$val = $memd->set("E","129|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
$memd->disconnect_all;
EOF

--sorted_result
SELECT c1,c2,c21 FROM t1;

# Test BIGINT UNSIGNED
ALTER TABLE t1 MODIFY c2 BIGINT UNSIGNED;

perl;
use DBI;
use Cache::Memcached;
my $memd = new Cache::Memcached {
  'servers' => [ "127.0.0.1:11213" ],
  'connect_timeout' => 20,
  'select_timeout' => 20
};
print "Here the memcached results with D,B,H,C:\n";
$val = $memd->get("D");
if ($val) { print "$val\n"; }
$val = $memd->get("B");
if ($val) { print "$val\n"; }
$val = $memd->get("H");
if ($val) { print "$val\n"; }
$val = $memd->get("C");
if ($val) { print "$val\n"; }
print "Here the memcached results after set:\n";
$val = $memd->set("E","5|Ost-BIGINT-UNSIGNED");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set max border:\n";
$val = $memd->set("E","18446744073709551615|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set over max border:\n";
$val = $memd->set("E","18446744073709551616|Ost");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
$memd->disconnect_all;
EOF

# Test MULTI INT COLUMNS
ALTER TABLE t1 MODIFY c21 BIGINT UNSIGNED;

perl;
use DBI;
use Cache::Memcached;
my $memd = new Cache::Memcached {
  'servers' => [ "127.0.0.1:11213" ],
  'connect_timeout' => 20,
  'select_timeout' => 20
};
print "Here the memcached results with D,B,H,C:\n";
$val = $memd->get("D");
if ($val) { print "$val\n"; }
$val = $memd->get("B");
if ($val) { print "$val\n"; }
$val = $memd->get("H");
if ($val) { print "$val\n"; }
$val = $memd->get("C");
if ($val) { print "$val\n"; }
print "Here the memcached results after set:\n";
$val = $memd->set("E","5|5");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set max border:\n";
$val = $memd->set("E","18446744073709551615|18446744073709551615");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after set over max border:\n";
$val = $memd->set("E","18446744073709551616|18446744073709551616");
$val = $memd->get("E");
if ($val) { print "$val\n"; }
print "Here the memcached results after append:\n";
$val = $memd->append("E"," abcde");
if ($val) { print "$val\n"; } else { print "Append to integer value is not supported.\n"; }
$val = $memd->get("E");
if ($val) { print "$val\n"; }
$memd->disconnect_all;
EOF

--sorted_result
SELECT c1,c2,c21 FROM t1;

--sorted_result
SELECT c1,c2,c21 FROM t1;

# Test INTEGER KEY
DELETE FROM t1;
ALTER TABLE t1 MODIFY c1 INT;
ALTER TABLE t1 MODIFY c21 VARCHAR(1024);
INSERT INTO t1 VALUES (1, 1, 'City', 0, 0, 0);
INSERT INTO t1 VALUES (2, 2, 'Mitte', 0, 0, 0);
INSERT INTO t1 VALUES (3, 3, 'West', 0, 0 ,0);
INSERT INTO t1 VALUES (4, 4, 'Norderstedt', 0, 0, 0);

--sorted_result
SELECT c1,c2,c21 FROM t1;

perl;
use DBI;
use Cache::Memcached;
my $memd = new Cache::Memcached {
  'servers' => [ "127.0.0.1:11213" ],
  'connect_timeout' => 20,
  'select_timeout' => 20
};
print "Here the memcached results with 1,2,3,4:\n";
$val = $memd->get("1");
if ($val) { print "$val\n"; }
$val = $memd->get("2");
if ($val) { print "$val\n"; }
$val = $memd->get("3");
if ($val) { print "$val\n"; }
$val = $memd->get("4");
if ($val) { print "$val\n"; }
print "Here the memcached results after set:\n";
$val = $memd->set("5","5|Ost-INT-KEY");
$val = $memd->get("5");
if ($val) { print "$val\n"; }
print "Here the memcached results after add key with min border:\n";
$val = $memd->add("-2147483648", "6|Ost");
$val = $memd->get("-2147483648");
if ($val) { print "$val\n"; }
print "Here the memcached results after add key with max border:\n";
$val = $memd->add("2147483647","7|Ost");
$val = $memd->get("2147483647");
if ($val) { print "$val\n"; }
print "Here the memcached results after add key with over max border:\n";
$val = $memd->add("2147483648","8|Ost");
$val = $memd->get("2147483648");
if ($val) { print "$val\n"; }
$memd->disconnect_all;
EOF

DROP TABLE t1;
SET sql_mode=default;

--echo #
--echo # Bug#20535517	INCORRECT HANDLING OF UNSIGNED NOT NULL INTEGERS IN INNODB_MEMCACHED
--echo #

perl;
use DBI;
use Cache::Memcached;
my $memd = new Cache::Memcached {
  'servers' => [ "127.0.0.1:11213" ],
  'connect_timeout' => 20,
  'select_timeout' => 20
};

$memd->get('@@int_test');
$memd->set("E","543");

print "Here the memcached results with E:\n";
$val = $memd->get("E");
if ($val) { print "$val\n"; }
$memd->disconnect_all;
EOF

SELECT * FROM int_test;
DROP TABLE int_test;

UNINSTALL PLUGIN daemon_memcached;
DROP DATABASE innodb_memcache;

SET @@global.tx_isolation= @tx_isolation;
