a695e54dbd5418933371d717d99f767dbc513c2f
[packages/trusty/mysql-wsrep-5.6.git] /
1 /*
2    Copyright 2010 Sun Microsystems, Inc.
3    All rights reserved. Use is subject to license terms.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; version 2 of the License.
8
9    This program 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
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
17 */
18
19 package testsuite.clusterj;
20
21 import testsuite.clusterj.model.DatetimeAsUtilDateTypes;
22 import java.util.Date;
23
24 import testsuite.clusterj.model.IdBase;
25
26 public class QueryDatetimeAsUtilDateTypesTest extends AbstractQueryTest {
27
28     @Override
29     public Class<DatetimeAsUtilDateTypes> getInstanceType() {
30         return DatetimeAsUtilDateTypes.class;
31     }
32
33     @Override
34     void createInstances(int number) {
35         createAllDateTimeTypesInstances(number);
36     }
37
38     /** Test all single- and double-predicate queries using DateTimeTypes.
39 drop table if exists datetimetypes;
40 create table datetimetypes (
41  id int not null primary key,
42
43  datetime_null_hash datetime,
44  datetime_null_btree datetime,
45  datetime_null_both datetime,
46  datetime_null_none datetime,
47
48  datetime_not_null_hash datetime,
49  datetime_not_null_btree datetime,
50  datetime_not_null_both datetime,
51  datetime_not_null_none datetime
52
53 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
54
55 create unique index idx_datetime_null_hash using hash on datetimetypes(datetime_null_hash);
56 create index idx_datetime_null_btree on datetimetypes(datetime_null_btree);
57 create unique index idx_datetime_null_both on datetimetypes(datetime_null_both);
58
59 create unique index idx_datetime_not_null_hash using hash on datetimetypes(datetime_not_null_hash);
60 create index idx_datetime_not_null_btree on datetimetypes(datetime_not_null_btree);
61 create unique index idx_datetime_not_null_both on datetimetypes(datetime_not_null_both);
62
63      */
64
65     public void test() {
66         btreeIndexScanDateTime();
67         hashIndexScanDateTime();
68         bothIndexScanDateTime();
69         noneIndexScanDateTime();
70         failOnError();
71     }
72
73     public void btreeIndexScanDateTime() {
74         equalQuery("datetime_not_null_btree", "idx_datetime_not_null_btree", getDateTimeFor(8), 8);
75         greaterEqualQuery("datetime_not_null_btree", "idx_datetime_not_null_btree", getDateTimeFor(7), 7, 8, 9);
76         greaterThanQuery("datetime_not_null_btree", "idx_datetime_not_null_btree", getDateTimeFor(6), 7, 8, 9);
77         lessEqualQuery("datetime_not_null_btree", "idx_datetime_not_null_btree", getDateTimeFor(4), 4, 3, 2, 1, 0);
78         lessThanQuery("datetime_not_null_btree", "idx_datetime_not_null_btree", getDateTimeFor(4), 3, 2, 1, 0);
79         betweenQuery("datetime_not_null_btree", "idx_datetime_not_null_btree", getDateTimeFor(4), getDateTimeFor(6), 4, 5, 6);
80         greaterEqualAndLessEqualQuery("datetime_not_null_btree", "idx_datetime_not_null_btree", getDateTimeFor(4), getDateTimeFor(6), 4, 5, 6);
81         greaterThanAndLessEqualQuery("datetime_not_null_btree", "idx_datetime_not_null_btree", getDateTimeFor(4), getDateTimeFor(6), 5, 6);
82         greaterEqualAndLessThanQuery("datetime_not_null_btree", "idx_datetime_not_null_btree", getDateTimeFor(4), getDateTimeFor(6), 4, 5);
83         greaterThanAndLessThanQuery("datetime_not_null_btree", "idx_datetime_not_null_btree", getDateTimeFor(4), getDateTimeFor(6), 5);
84     }
85
86     public void hashIndexScanDateTime() {
87         equalQuery("datetime_not_null_hash", "idx_datetime_not_null_hash", getDateTimeFor(8), 8);
88         greaterEqualQuery("datetime_not_null_hash", "none", getDateTimeFor(7), 7, 8, 9);
89         greaterThanQuery("datetime_not_null_hash", "none", getDateTimeFor(6), 7, 8, 9);
90         lessEqualQuery("datetime_not_null_hash", "none", getDateTimeFor(4), 4, 3, 2, 1, 0);
91         lessThanQuery("datetime_not_null_hash", "none", getDateTimeFor(4), 3, 2, 1, 0);
92         betweenQuery("datetime_not_null_hash", "none", getDateTimeFor(4), getDateTimeFor(6), 4, 5, 6);
93         greaterEqualAndLessEqualQuery("datetime_not_null_hash", "none", getDateTimeFor(4), getDateTimeFor(6), 4, 5, 6);
94         greaterThanAndLessEqualQuery("datetime_not_null_hash", "none", getDateTimeFor(4), getDateTimeFor(6), 5, 6);
95         greaterEqualAndLessThanQuery("datetime_not_null_hash", "none", getDateTimeFor(4), getDateTimeFor(6), 4, 5);
96         greaterThanAndLessThanQuery("datetime_not_null_hash", "none", getDateTimeFor(4), getDateTimeFor(6), 5);
97     }
98
99     public void bothIndexScanDateTime() {
100         equalQuery("datetime_not_null_both", "idx_datetime_not_null_both", getDateTimeFor(8), 8);
101         greaterEqualQuery("datetime_not_null_both", "idx_datetime_not_null_both", getDateTimeFor(7), 7, 8, 9);
102         greaterThanQuery("datetime_not_null_both", "idx_datetime_not_null_both", getDateTimeFor(6), 7, 8, 9);
103         lessEqualQuery("datetime_not_null_both", "idx_datetime_not_null_both", getDateTimeFor(4), 4, 3, 2, 1, 0);
104         lessThanQuery("datetime_not_null_both", "idx_datetime_not_null_both", getDateTimeFor(4), 3, 2, 1, 0);
105         betweenQuery("datetime_not_null_both", "idx_datetime_not_null_both", getDateTimeFor(4), getDateTimeFor(6), 4, 5, 6);
106         greaterEqualAndLessEqualQuery("datetime_not_null_both", "idx_datetime_not_null_both", getDateTimeFor(4), getDateTimeFor(6), 4, 5, 6);
107         greaterThanAndLessEqualQuery("datetime_not_null_both", "idx_datetime_not_null_both", getDateTimeFor(4), getDateTimeFor(6), 5, 6);
108         greaterEqualAndLessThanQuery("datetime_not_null_both", "idx_datetime_not_null_both", getDateTimeFor(4), getDateTimeFor(6), 4, 5);
109         greaterThanAndLessThanQuery("datetime_not_null_both", "idx_datetime_not_null_both", getDateTimeFor(4), getDateTimeFor(6), 5);
110     }
111
112     public void noneIndexScanDateTime() {
113         equalQuery("datetime_not_null_none", "none", getDateTimeFor(8), 8);
114         greaterEqualQuery("datetime_not_null_none", "none", getDateTimeFor(7), 7, 8, 9);
115         greaterThanQuery("datetime_not_null_none", "none", getDateTimeFor(6), 7, 8, 9);
116         lessEqualQuery("datetime_not_null_none", "none", getDateTimeFor(4), 4, 3, 2, 1, 0);
117         lessThanQuery("datetime_not_null_none", "none", getDateTimeFor(4), 3, 2, 1, 0);
118         betweenQuery("datetime_not_null_none", "none", getDateTimeFor(4), getDateTimeFor(6), 4, 5, 6);
119         greaterEqualAndLessEqualQuery("datetime_not_null_none", "none", getDateTimeFor(4), getDateTimeFor(6), 4, 5, 6);
120         greaterThanAndLessEqualQuery("datetime_not_null_none", "none", getDateTimeFor(4), getDateTimeFor(6), 5, 6);
121         greaterEqualAndLessThanQuery("datetime_not_null_none", "none", getDateTimeFor(4), getDateTimeFor(6), 4, 5);
122         greaterThanAndLessThanQuery("datetime_not_null_none", "none", getDateTimeFor(4), getDateTimeFor(6), 5);
123     }
124
125
126     private void createAllDateTimeTypesInstances(int number) {
127         for (int i = 0; i < number; ++i) {
128             DatetimeAsUtilDateTypes instance = session.newInstance(DatetimeAsUtilDateTypes.class);
129             instance.setId(i);
130             instance.setDatetime_not_null_hash(getDateTimeFor(i));
131             instance.setDatetime_not_null_btree(getDateTimeFor(i));
132             instance.setDatetime_not_null_both(getDateTimeFor(i));
133             instance.setDatetime_not_null_none(getDateTimeFor(i));
134             instances.add(instance);
135         }
136     }
137
138     protected Date getDateTimeFor(int i) {
139         return new Date(getMillisFor(1980, 0, 1, 0, 0, i));
140     }
141
142     public static String toString(IdBase instance) {
143         DatetimeAsUtilDateTypes timetype = (DatetimeAsUtilDateTypes)instance;
144         StringBuffer buffer = new StringBuffer("DateTimeTypes id: ");
145         buffer.append(timetype.getId());
146         buffer.append("; datetime_not_null_both: ");
147         buffer.append(timetype.getDatetime_not_null_both().toString());
148         buffer.append("; datetime_not_null_btree: ");
149         buffer.append(timetype.getDatetime_not_null_btree().toString());
150         buffer.append("; datetime_not_null_hash: ");
151         buffer.append(timetype.getDatetime_not_null_hash().toString());
152         buffer.append("; datetime_not_null_none: ");
153         buffer.append(timetype.getDatetime_not_null_none().toString());
154         return buffer.toString();
155     }
156 }