comparison tests/Test_Int128.java @ 0:a5a898f6886c

Copy of MonetDB java directory changeset e6e32756ad31.
author Sjoerd Mullender <sjoerd@acm.org>
date Wed, 21 Sep 2016 09:34:48 +0200 (2016-09-21)
parents
children 04fbf3655452
comparison
equal deleted inserted replaced
-1:000000000000 0:a5a898f6886c
1 /*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
7 */
8
9 import java.math.BigDecimal;
10 import java.math.BigInteger;
11 import java.sql.Connection;
12 import java.sql.DriverManager;
13 import java.sql.PreparedStatement;
14 import java.sql.ResultSet;
15 import java.sql.SQLException;
16 import java.sql.Statement;
17
18 /* Test whether we can represent a full-size int128 as JDBC results */
19 public class Test_Int128 {
20 public static void main(String[] args) throws Exception {
21 Class.forName("nl.cwi.monetdb.jdbc.MonetDriver");
22 Connection con = DriverManager.getConnection(args[0]);
23 BigInteger bi = new BigInteger(
24 "123456789012345678909876543210987654321");
25 BigDecimal bd = new BigDecimal(
26 "1234567890123456789.9876543210987654321");
27 try {
28 con.setAutoCommit(false);
29 Statement s = con.createStatement();
30 s.executeUpdate("CREATE TABLE HUGEINTT (I HUGEINT)");
31 s.executeUpdate("CREATE TABLE HUGEDECT (I DECIMAL(38,19))");
32
33 PreparedStatement insertStatement = con
34 .prepareStatement("INSERT INTO HUGEINTT VALUES (?)");
35 insertStatement.setBigDecimal(1, new BigDecimal(bi));
36 insertStatement.executeUpdate();
37 insertStatement.close();
38
39 s.executeUpdate("INSERT INTO HUGEDECT VALUES ("+bd+");");
40
41 ResultSet rs = s.executeQuery("SELECT I FROM HUGEINTT");
42 rs.next();
43 BigInteger biRes = rs.getBigDecimal(1).toBigInteger();
44 rs.close();
45 rs = s.executeQuery("SELECT I FROM HUGEDECT");
46 rs.next();
47 BigDecimal bdRes = rs.getBigDecimal(1);
48 rs.close();
49 s.close();
50
51 System.out.println("Expecting " + bi + ", got " + biRes);
52 if (!bi.equals(biRes)) {
53 throw new RuntimeException();
54 }
55
56 System.out.println("Expecting " + bd + ", got " + bdRes);
57 if (!bd.equals(bdRes)) {
58 throw new RuntimeException();
59 }
60 System.out.println("SUCCESS");
61
62 } catch (SQLException e) {
63 System.out.println("FAILED :( " + e.getMessage());
64 System.out.println("ABORTING TEST!!!");
65 }
66
67 con.close();
68 }
69 }