Mercurial > hg > monetdb-perl
comparison DBD/t/14rows.t @ 0:cedccb7e0143
Copy of clients/perl directory without Tests from MonetDB changeset 4d2d4532228a.
author | Sjoerd Mullender <sjoerd@acm.org> |
---|---|
date | Mon, 19 Sep 2016 15:15:52 +0200 (2016-09-19) |
parents | |
children | a0ec9e080a5b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:cedccb7e0143 |
---|---|
1 #!perl -I./t | |
2 | |
3 # This Source Code Form is subject to the terms of the Mozilla Public | |
4 # License, v. 2.0. If a copy of the MPL was not distributed with this | |
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
6 # | |
7 # Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V. | |
8 | |
9 $| = 1; | |
10 | |
11 use strict; | |
12 use warnings; | |
13 use DBI(); | |
14 use DBD_TEST(); | |
15 use Time::HiRes qw(gettimeofday tv_interval); | |
16 | |
17 use Test::More; | |
18 | |
19 if (defined $ENV{DBI_DSN}) { | |
20 plan tests => 18; | |
21 } else { | |
22 plan skip_all => 'Cannot test without DB info'; | |
23 } | |
24 | |
25 pass('Insert tests'); | |
26 | |
27 my $tbl1 = $DBD_TEST::table_name; | |
28 my $tbl2 = $tbl1 . '_2'; | |
29 | |
30 my $MAX_ROWS = 200; | |
31 | |
32 my $dbh = DBI->connect or die "Connect failed: $DBI::errstr\n"; | |
33 pass('Database connection created'); | |
34 | |
35 ok( DBD_TEST::tab_create( $dbh, $tbl1 ),"Create table $tbl1"); | |
36 ok( DBD_TEST::tab_create( $dbh, $tbl2 ),"Create table $tbl2"); | |
37 | |
38 # for my $ac ( 0, 1 ) { | |
39 # pass("Testing with AutoCommit $ac"); | |
40 # $dbh->{AutoCommit} = $ac; | |
41 # | |
42 # # Time how long it takes to run the insert test. | |
43 # my $t_beg = [gettimeofday]; | |
44 # run_insert_test( $dbh ); | |
45 # | |
46 # my $elapsed = tv_interval( $t_beg, [gettimeofday] ); | |
47 # | |
48 # pass("Run insert test: MAX_ROWS elapsed: $elapsed"); | |
49 # | |
50 # ok( $dbh->do( "DROP TABLE $tbl1"),"Drop table $tbl1"); | |
51 # } | |
52 | |
53 # Time how long it takes to run the insert test. | |
54 $dbh->{AutoCommit} = 0; | |
55 my $t_beg = [gettimeofday]; | |
56 run_insert_test( $dbh, $tbl1 ); | |
57 | |
58 my $elapsed = tv_interval( $t_beg, [gettimeofday] ); | |
59 pass("Run insert test: MAX_ROWS elapsed: $elapsed"); | |
60 | |
61 # Test the number of rows returned by an execute. | |
62 my $sql = <<"SQL"; | |
63 INSERT | |
64 INTO $tbl2( A, B ) | |
65 SELECT A, B | |
66 FROM $tbl1 | |
67 SQL | |
68 | |
69 my $sth = $dbh->prepare( $sql ); | |
70 ok( defined $sth,'Prepared insert select statement'); | |
71 my $rc = $sth->execute; | |
72 ok( !ref $rc,"Not a ref?"); | |
73 is( $rc, $MAX_ROWS,"Execute returned $MAX_ROWS rows"); | |
74 is( $sth->rows, $rc,"Execute sth->rows returned $rc"); | |
75 | |
76 $sth->finish; $sth = undef; | |
77 | |
78 # Test the number of rows returned by a do. | |
79 $rc = $dbh->do( $sql ); | |
80 is( $rc, $MAX_ROWS,"Do returned $MAX_ROWS rows"); | |
81 | |
82 $dbh->rollback; | |
83 | |
84 $dbh->{AutoCommit} = 1; | |
85 | |
86 ok( $dbh->do("DROP TABLE $tbl1"),"Drop table $tbl1"); | |
87 ok( $dbh->do("DROP TABLE $tbl2"),"Drop table $tbl2"); | |
88 | |
89 ok( $dbh->disconnect,'Disconnect'); | |
90 | |
91 | |
92 sub run_insert_test { | |
93 my $dbh = shift; | |
94 my $tbl = shift; | |
95 | |
96 my $sth = $dbh->prepare("INSERT INTO $tbl( B ) VALUES( ? )"); | |
97 ok( defined $sth,'Insert statement prepared'); | |
98 ok( !$dbh->err,'No error on prepare.'); | |
99 | |
100 pass("Loading rows into table: $tbl"); | |
101 | |
102 my $cnt = 0; my $added = 0; | |
103 my $ac = $dbh->{AutoCommit}; | |
104 while( $cnt < $MAX_ROWS ) { | |
105 $added += ( $sth->execute("Just a text message for $cnt") || 0 ); | |
106 } continue { | |
107 $cnt++; | |
108 $dbh->commit if $ac == 0 && $cnt % 1000 == 0; | |
109 print "# Checkpoint: $cnt\n" if $cnt % 1000 == 0; | |
110 } | |
111 $dbh->commit if $ac == 0; | |
112 | |
113 ok( $added > 0,"Added $added rows to test using count of $cnt"); | |
114 ok( $added == $MAX_ROWS,"Added MAX $MAX_ROWS rows to test using count of $cnt"); | |
115 | |
116 $sth->finish; $sth = undef; | |
117 return; | |
118 } |