OmniSciDB  f632821e96
OmniSciPrepareTest.java
Go to the documentation of this file.
1 package com.omnisci.jdbc;
2 
3 import static org.junit.Assert.*;
4 
5 import org.junit.After;
6 import org.junit.Before;
7 import org.junit.Ignore;
8 import org.junit.Test;
9 
10 import java.sql.*;
11 import java.util.Properties;
12 
13 public class OmniSciPrepareTest {
14  static Properties PROPERTIES = new Property_loader("prepare_test.properties");
15  static final String url = PROPERTIES.getProperty("default_db_connection_url");
16  static final String user = PROPERTIES.getProperty("default_super_user");
17  static final String password = PROPERTIES.getProperty("default_user_password");
18 
19  private Connection m_conn = null;
20 
21  @Before
22  public void setUp() throws Exception {
23  Properties pt = new Properties();
24  pt.setProperty("user", user);
25  pt.setProperty("password", password);
26  m_conn = DriverManager.getConnection(url, pt);
27  }
28 
29  @After
30  public void tearDown() throws Exception {
31  m_conn.close();
32  }
33 
34  @Test
35  public void prepare_test() throws Exception {
36  Statement statement = m_conn.createStatement();
37  {
38  statement.executeUpdate(PROPERTIES.getProperty("drop_base_t1"));
39  statement.executeUpdate(PROPERTIES.getProperty("create_base_t1"));
40  String prepare_insert_statement =
41  "insert into test_prepare_table1 (cc, bb, aa) values (?,?,?)";
42  PreparedStatement pr1 = m_conn.prepareStatement(prepare_insert_statement);
43  int aa_i = 100;
44  int bb_i = 1000;
45  int cc_i = 10000;
46  pr1.setInt(1, cc_i);
47  pr1.setInt(2, bb_i);
48  pr1.setInt(3, aa_i);
49  pr1.executeUpdate();
50  ResultSet rs = statement.executeQuery("select cc, bb, aa from test_prepare_table1");
51 
52  while (rs.next()) {
53  int returned_cc = rs.getInt("cc");
54  assertEquals(cc_i, returned_cc);
55  int returned_bb = rs.getInt("bb");
56  assertEquals(bb_i, returned_bb);
57  int returned_aa = rs.getInt("aa");
58  assertEquals(aa_i, returned_aa);
59  }
60  statement.executeUpdate(PROPERTIES.getProperty("drop_base_t1"));
61  }
62  {
63  statement.executeUpdate(PROPERTIES.getProperty("drop_base_t2"));
64  statement.executeUpdate(PROPERTIES.getProperty("create_base_t2"));
65  String prepare_insert_statement2 =
66  "insert into test_prepare_table2 (cc, bb, aa) values (?,?,?)";
67  PreparedStatement pr2 = m_conn.prepareStatement(prepare_insert_statement2);
68  String aa_s = "100";
69  String bb_s = "1000";
70  String cc_s = "10000";
71  pr2.setString(1, cc_s);
72  pr2.setString(2, bb_s);
73  pr2.setString(3, aa_s);
74  pr2.executeUpdate();
75  ResultSet rs2 =
76  statement.executeQuery("select cc, bb, aa from test_prepare_table2");
77 
78  while (rs2.next()) {
79  String returned_cc = rs2.getString("cc");
80  assertEquals(cc_s, returned_cc);
81  String returned_bb = rs2.getString("bb");
82  assertEquals(bb_s, returned_bb);
83  String returned_aa = rs2.getString("aa");
84  assertEquals(aa_s, returned_aa);
85  }
86  statement.executeUpdate(PROPERTIES.getProperty("drop_base_t2"));
87  }
88  }
89 
90  @Test
91  public void get_metadata() throws Exception {
92  Statement statement = m_conn.createStatement();
93  statement.executeUpdate(PROPERTIES.getProperty("drop_base_t3"));
94  statement.executeUpdate(PROPERTIES.getProperty("create_base_t3"));
95  statement.executeQuery("insert into test_prepare_table3 values(1, 1.1, 'one')");
96  ResultSetMetaData md = null;
97 
98  PreparedStatement pr_select_no_params =
99  m_conn.prepareStatement("select aa, bb, cc from test_prepare_table3");
100  md = pr_select_no_params.getMetaData();
101  assertNotNull(md);
102  assertEquals(md.getColumnCount(), 3);
103  assertEquals(md.getColumnName(1), "aa");
104  assertEquals(md.getColumnType(1), Types.INTEGER);
105  assertEquals(md.getColumnType(2), Types.DOUBLE);
106 
107  PreparedStatement pr_select_with_params = m_conn.prepareStatement(
108  "select bb, aa from test_prepare_table3 where cc <> ? and aa > ?");
109  md = pr_select_with_params.getMetaData();
110  assertNotNull(md);
111  assertEquals(md.getColumnCount(), 2);
112  assertEquals(md.getColumnName(1), "bb");
113  assertEquals(md.getColumnType(1), Types.DOUBLE);
114  assertEquals(md.getColumnType(2), Types.INTEGER);
115 
116  String commented_sql_statement = " \n \n"
117  + "-- comment\n"
118  + "\n\n"
119  + "/*some\n"
120  + "multiline\n"
121  + "comment\n"
122  + "-- comment inside comment\n"
123  + "*/ \n"
124  + "-- another /*tricky edge case/*\n"
125  + " select bb, aa from test_prepare_table3 where cc <> ? and aa > ?";
126  PreparedStatement pr_select_with_params_and_comments =
127  m_conn.prepareStatement(commented_sql_statement);
128  md = pr_select_with_params_and_comments.getMetaData();
129  assertNotNull(md);
130  assertEquals(md.getColumnCount(), 2);
131 
132  PreparedStatement pr_insert = m_conn.prepareStatement(
133  "insert into test_prepare_table3(aa, bb, cc) values (?, ?, ?)");
134  md = pr_insert.getMetaData();
135  assertNull(md);
136 
137  PreparedStatement pr_insert_from_select = m_conn.prepareStatement(
138  "insert into test_prepare_table3(aa, bb, cc) select aa, bb, cc from test_prepare_table3 where cc <> ?");
139  md = pr_insert_from_select.getMetaData();
140  assertNull(md);
141 
142  statement.executeUpdate(PROPERTIES.getProperty("drop_base_t3"));
143  }
144 
145  private void formBatch(int start, int end, PreparedStatement ps, Integer[][] ia)
146  throws Exception {
147  for (int i = start; i < end; ++i) {
148  ps.setInt(1, i);
149  ps.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
150  if (ia[i] != null) {
151  ps.setArray(3, m_conn.createArrayOf("INT", ia[i]));
152  } else {
153  ps.setNull(3, Types.ARRAY);
154  }
155  ps.addBatch();
156  }
157  }
158 
159  @Test
160  public void batchTest() throws Exception {
161  Statement stmt = m_conn.createStatement();
162  stmt.executeUpdate("DROP TABLE IF EXISTS batch_tbl");
163  stmt.executeUpdate("CREATE TABLE batch_tbl ("
164  + "i INTEGER,"
165  + "t TIMESTAMP,"
166  + "ia INTEGER[])");
167  Integer[][] ia = {{1, 10, 100}, {null}, null, {543, null, null, 123, 543}, {17}};
168  Integer[][] ia2 = {{12345, 12, null, 1234, null}, {1, -1, -2, 2, 3, -3, -4, 4, -5}};
169  PreparedStatement ps =
170  m_conn.prepareStatement("INSERT INTO batch_tbl VALUES(?, ?, ?)");
171  formBatch(0, 4, ps, ia);
172  int[] result = ps.executeBatch();
173  for (int i : result) {
174  assertEquals(i, 1);
175  }
176  formBatch(0, 2, ps, ia2);
177  ps.clearBatch();
178  formBatch(4, 5, ps, ia);
179  result = ps.executeBatch();
180  assertEquals(result.length, 1);
181  assertEquals(result[0], 1);
182  ps.close();
183 
184  ResultSet rs = stmt.executeQuery("SELECT i, ia FROM batch_tbl");
185  int i = 0;
186  while (rs.next()) {
187  assertEquals(rs.getInt("i"), i);
188  if (ia[i] == null) {
189  assertNull(rs.getArray("ia"));
190  } else {
191  assertArrayEquals((Integer[]) rs.getArray("ia").getArray(), ia[i]);
192  }
193  i++;
194  }
195  assertEquals(i, 5);
196  }
197 }
void formBatch(int start, int end, PreparedStatement ps, Integer[][] ia)