File: big-test.hs

package info (click to toggle)
haskelldb 0.9.cvs.601-13
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 680 kB
  • ctags: 33
  • sloc: haskell: 4,392; sh: 1,900; makefile: 130
file content (289 lines) | stat: -rw-r--r-- 7,619 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
{-
  A very disorganized set of tests using almost all SQL92 data types.
-}

import System.Time
import System.Locale
import System.IO.Unsafe

import Database.HaskellDB
import Database.HaskellDB.Database
import Database.HaskellDB.DBSpec
import Database.HaskellDB.FieldType

import TestConnect

import Dp037.Hdb_test_t1
import Dp037.Hdb_test_t2

now = unsafePerformIO (getClockTime >>= toCalendarTime)


t1 = TInfo {tname = "hdb_test_t1",
	    cols = [CInfo {cname = "t1f01",
		      descr = (StringT, True)},
		    CInfo {cname = "t1f02",
			   descr = (StringT, False)},
		    CInfo {cname = "t1f03",
			   descr = (StringT, True)},
		    CInfo {cname = "t1f04",
			   descr = (StringT, False)},
		    CInfo {cname = "t1f05",
			   descr = (IntT, True)},
		    CInfo {cname = "t1f06",
			   descr = (IntT, False)},
		    CInfo {cname = "t1f07",
			   descr = (DoubleT, True)},
		    CInfo {cname = "t1f08",
			   descr = (DoubleT, False)},
		    CInfo {cname = "t1f09",
			   descr = (DoubleT, True)},
		    CInfo {cname = "t1f10",
			   descr = (DoubleT, False)},
		    CInfo {cname = "t1f11",
			   descr = (IntT, True)},
		    CInfo {cname = "t1f12",
			   descr = (IntT, False)},
		    CInfo {cname = "t1f13",
			   descr = (DoubleT, True)},
		    CInfo {cname = "t1f14",
			   descr = (DoubleT, False)},
		    CInfo {cname = "t1f15",
			   descr = (DoubleT, True)},
		    CInfo {cname = "t1f16",
			   descr = (DoubleT, False)},
		    CInfo {cname = "t1f17",
			   descr = (DoubleT, True)},
		    CInfo {cname = "t1f18",
			   descr = (DoubleT, False)},
		    CInfo {cname = "t1f19",
			   descr = (CalendarTimeT, True)},
		    CInfo {cname = "t1f20",
			   descr = (CalendarTimeT, False)}]}

t2 = TInfo {tname = "hdb_test_t2",
	    cols = [CInfo {cname = "t2f01",
			   descr = (StringT, True)},
		    CInfo {cname = "t2f02",
			   descr = (StringT, False)},
		    CInfo {cname = "t2f03",
			   descr = (StringT, True)},
		    CInfo {cname = "t2f04",
			   descr = (StringT, False)},
		    CInfo {cname = "t2f05",
			   descr = (IntT, True)},
		    CInfo {cname = "t2f06",
			   descr = (IntT, False)},
		    CInfo {cname = "t2f07",
			   descr = (DoubleT, True)},
		    CInfo {cname = "t2f08",
			   descr = (DoubleT, False)},
		    CInfo {cname = "t2f09",
			   descr = (DoubleT, True)},
		    CInfo {cname = "t2f10",
			   descr = (DoubleT, False)},
		    CInfo {cname = "t2f11",
			   descr = (IntT, True)},
		    CInfo {cname = "t2f12",
			   descr = (IntT, False)},
		    CInfo {cname = "t2f13",
			   descr = (DoubleT, True)},
		    CInfo {cname = "t2f14",
			   descr = (DoubleT, False)},
		    CInfo {cname = "t2f15",
			   descr = (DoubleT, True)},
		    CInfo {cname = "t2f16",
			   descr = (DoubleT, False)},
		    CInfo {cname = "t2f17",
			   descr = (DoubleT, True)},
		    CInfo {cname = "t2f18",
			   descr = (DoubleT, False)},
		    CInfo {cname = "t2f19",
			   descr = (CalendarTimeT, True)},
		    CInfo {cname = "t2f20",
			   descr = (CalendarTimeT, False)}]}

dbinfo :: DBInfo 
dbinfo = DBInfo {dbname = "BigTestDB", 
		 opts = DBOptions {useBString = False}, 
		 tbls = [t1, t2]}

data1 = [(
	 t1f01 << constant (Just "bepa") #
	 t1f02 << constant "apa" #
	 t1f03 << constant (Just "bar")	#
	 t1f04 << constant "foo" #
	 t1f05 << constant (Just 2) #
	 t1f06 << constant 1 #
	 t1f07 << constant (Just 4.0) #
	 t1f08 << constant 3.0 #
	 t1f09 << constant (Just 5.0) #
	 t1f10 << constant 6.0 #
	 t1f11 << constant (Just 7) #
	 t1f12 << constant (-13) #
	 t1f13 << constant (Just 3.1415) #
	 t1f14 << constant (-3456.8) #
	 t1f15 << constant (Just (-67.5)) #
	 t1f16 << constant 12.0 #
	 t1f17 << constant (Just 17.17) #
	 t1f18 << constant 18.18 #
	 t1f19 << constant (Just now) #
	 t1f20 << constant now 
-- Insertion doesn't work in Postgre
--	 t1f21 << constant (Just now) #
--	 t1f22 << constant now #
-- MySQL makes this NOT NULL
--	 t1f23 << constant now #
-- Treated as strings in Postgre
--	 t1f23 << constant (Just now) #
--	 t1f24 << constant now
	)
	]

data2 =  [(
	 t2f01 << constant (Just "bepa") #
	 t2f02 << constant "mepa" #
	 t2f03 << constant (Just "sdfjhc")	#
	 t2f04 << constant "quasar" #
	 t2f05 << constant (Just 2) #
	 t2f06 << constant 1 #
	 t2f07 << constant (Just 4.0) #
	 t2f08 << constant 3.0 #
	 t2f09 << constant (Just 5.0) #
	 t2f10 << constant 6.0 #
	 t2f11 << constant (Just 7) #
	 t2f12 << constant (-13) #
	 t2f13 << constant (Just 3.1415) #
	 t2f14 << constant (-3456.8) #
	 t2f15 << constant (Just (-67.5)) #
	 t2f16 << constant 12.0 #
	 t2f17 << constant (Just 17.17) #
	 t2f18 << constant 18.18 #
	 t2f19 << constant (Just now) #
	 t2f20 << constant now 
-- Insertion doesn't work in Postgre
--	 t2f21 << constant (Just now) #
--	 t2f22 << constant now #
-- MySQL makes this NOT NULL
--	 t2f23 << constant now #
-- Treated as strings in Postgre
--	 t2f23 << constant (Just now) 
--	 t2f24 << constant now
	)]

insertData db = do
		mapM (insert db hdb_test_t1) data1
		mapM (insert db hdb_test_t2) data2


deleteData db = do
		putStrLn "Deleting data from hdb_test_t1..."
		delete db hdb_test_t1 (\r -> constant True)
		putStrLn "Deleting data from hdb_test_t2..."
		delete db hdb_test_t2 (\r -> constant True)

mkJoinOnQuery f1 f2 = 
    do
    t1 <- table hdb_test_t1
    t2 <- table hdb_test_t2
    restrict (t1!f1 .==. t2!f2)
    project (f1 << t1!f1 # f2 << t2!f2 # 
	     t1f01 << t1!t1f01 # t2f01 << t2!t2f01)

joinOn db f1 f2 t1 t2 = 
    do
    rs <- query db (mkJoinOnQuery f1 f2)
    if and [r!t1 == r!t2 | r <- rs] then
       putStrLn "Join ok"
     else
       putStrLn "Join equality check FAILED"

formatDate = formatCalendarTime defaultTimeLocale fmt
	where fmt = iso8601DateFormat (Just "%H:%M:%S")

showAllFields1 r = [
		    show $ r!t1f01,
		    show $ r!t1f02,
		    show $ r!t1f03,
		    show $ r!t1f04,
		    show $ r!t1f05,
		    show $ r!t1f06,
		    show $ r!t1f07,
		    show $ r!t1f08,
		    show $ r!t1f09,
		    show $ r!t1f10,
		    show $ r!t1f11,
		    show $ r!t1f12,
		    show $ r!t1f13,
		    show $ r!t1f14,
		    show $ r!t1f15,
		    show $ r!t1f16,
		    show $ r!t1f17,
		    show $ r!t1f18,
		    show $ r!t1f19,
		    show $ r!t1f20
-- Insertion doesn't work in Postgre
--		    show $ r!t1f21,
--		    show $ r!t1f22,
-- Treated as strings in Postgre
--		    show $ r!t1f23,
--		    show $ r!t1f24
		   ]

showAll db = 
    do
    rs <- query db (table hdb_test_t1)
    mapM_ (putStrLn . unwords . showAllFields1) rs

q1 = do
     t <- table hdb_test_t1
     restrict (t!t1f02 .==. constant "apa")
     return t

q2 = do
     t <- table hdb_test_t1
     restrict (t!t1f10 .==. constant 6.0)
     return t

printQuery = putStrLn . show . showSql

doQuery db q =
    do
    --printQuery q
    query db q

testOps db =
    do
    putStrLn "Testing UNION..."
    let uq = union q1 q2
    putStrLn $ show $ showOpt $ uq
    putStrLn $ show $ showSql $ uq
    query db uq
-- These don't work in MySQL:
--    query db (intersect q1 q2)
--    query db (divide q1 q2)
--    query db (minus q1 q2)

runTests db =
    do
    dropTable db "hdb_test_t1"
    dropTable db "hdb_test_t2"
    dbSpecToDatabase db dbinfo
    insertData db
    putStrLn "After INSERT:"
    showAll db
    joinOn db t1f01 t2f01 t1f01 t2f01
    joinOn db t1f02 t2f02 t1f02 t2f02
    joinOn db t1f03 t2f03 t1f03 t2f03
    joinOn db t1f04 t2f04 t1f04 t2f04
    joinOn db t1f05 t2f05 t1f05 t2f05
    joinOn db t1f06 t2f06 t1f06 t2f06
    testOps db    
    deleteData db
    putStrLn "After DELETE:"

startsWith x y = take (length y) x == y

main = do
       argConnect runTests
--       putStrLn $ unlines $ [ v "" | (_,v) <- showRecRow data0]