use of herddb.client.HDBClient in project herddb by diennea.
the class SimpleExpressionsTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client)) {
try (Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
statement.execute("CREATE TABLE mytable (k1 string primary key, n1 int, l1 long, t1 timestamp, nu string, b1 bool, d1 double)");
}
try (Connection con = dataSource.getConnection();
PreparedStatement statement = con.prepareStatement("INSERT INTO mytable(k1,n1,l1,t1,nu,b1,d1) values(?,?,?,?,?,?,?)")) {
int i = 1;
statement.setString(i++, "mykey");
statement.setInt(i++, 1);
statement.setLong(i++, 2);
statement.setTimestamp(i++, new java.sql.Timestamp(System.currentTimeMillis()));
statement.setString(i++, null);
statement.setBoolean(i++, true);
statement.setDouble(i++, 1.5);
int rows = statement.executeUpdate();
Assert.assertEquals(1, rows);
}
// Types
try (Connection con = dataSource.getConnection()) {
// String
try (PreparedStatement statement = con.prepareStatement("SELECT k1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals("mykey", rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT upper(k1) FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals("MYKEY", rs.getObject(1));
}
}
// Number
try (PreparedStatement statement = con.prepareStatement("SELECT n1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(1, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT n1*5 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(5L, rs.getObject(1));
assertEquals(5, rs.getLong(1));
assertEquals(5, rs.getInt(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT n1*5.0 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(5d, rs.getObject(1));
assertEquals(5, rs.getLong(1));
assertEquals(5, rs.getInt(1));
}
}
// Long
try (PreparedStatement statement = con.prepareStatement("SELECT l1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(2L, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT l1*5 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(10L, rs.getObject(1));
assertEquals(10, rs.getLong(1));
assertEquals(10, rs.getInt(1));
assertEquals(10f, rs.getFloat(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT l1*5.1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(10.2, rs.getObject(1));
assertEquals(10, rs.getLong(1));
assertEquals(10, rs.getInt(1));
assertEquals(10.2f, rs.getFloat(1), 0);
}
}
// Null
try (PreparedStatement statement = con.prepareStatement("SELECT nu FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(null, rs.getObject(1));
}
}
// Double
try (PreparedStatement statement = con.prepareStatement("SELECT 3/2 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(1.5, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT d1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(1.5, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT d1*5/2 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(3.75, rs.getObject(1));
assertEquals(3.75, rs.getDouble(1), 0);
assertEquals(3, rs.getLong(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT -n1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(-1, rs.getObject(1));
assertEquals(-1, rs.getDouble(1), 0);
assertEquals(-1, rs.getInt(1), 0);
assertEquals(-1, rs.getLong(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT -n1-n1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(-2L, rs.getObject(1));
assertEquals(-2, rs.getDouble(1), 0);
assertEquals(-2, rs.getInt(1), 0);
assertEquals(-2, rs.getLong(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT +n1+n1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(2L, rs.getObject(1));
assertEquals(2, rs.getDouble(1), 0);
assertEquals(2, rs.getInt(1), 0);
assertEquals(2, rs.getLong(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT n1*n1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(1L, rs.getObject(1));
assertEquals(1, rs.getDouble(1), 0);
assertEquals(1, rs.getInt(1), 0);
assertEquals(1, rs.getLong(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT -d1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(-1.5, rs.getObject(1));
assertEquals(-1.5, rs.getDouble(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT +d1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(1.5, rs.getObject(1));
assertEquals(1.5, rs.getDouble(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT +d1+d1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(3, rs.getDouble(1), 0);
assertEquals(3.0, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT +d1*d1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(2.25, rs.getDouble(1), 0);
assertEquals(2.25, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT -d1+d1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(0, rs.getDouble(1), 0);
assertEquals(0.0, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT +d1+4 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(5.5, rs.getDouble(1), 0);
assertEquals(5.5, rs.getObject(1));
}
}
// Boolean
try (PreparedStatement statement = con.prepareStatement("SELECT true FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(true, rs.getObject(1));
assertEquals(true, rs.getBoolean(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT false FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(false, rs.getObject(1));
assertEquals(false, rs.getBoolean(1));
}
}
}
}
}
}
}
use of herddb.client.HDBClient in project herddb by diennea.
the class SimpleScanTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(herddb.jdbc.TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client)) {
try (Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
statement.execute("CREATE TABLE mytable (k1 string primary key, n1 int, l1 long, t1 timestamp, nu string, b1 bool, d1 double, s1 smallint)");
}
try (Connection con = dataSource.getConnection();
PreparedStatement statement = con.prepareStatement("INSERT INTO mytable(k1,n1,l1,t1,nu,b1,d1,s1) values(?,?,?,?,?,?,?,?)")) {
for (int n = 0; n < 10; ++n) {
int i = 1;
statement.setString(i++, "mykey_" + n);
statement.setInt(i++, n);
statement.setLong(i++, n);
statement.setTimestamp(i++, new java.sql.Timestamp(System.currentTimeMillis()));
statement.setString(i++, null);
statement.setBoolean(i++, true);
statement.setDouble(i++, n + 0.5);
statement.setShort(i++, (short) n);
statement.addBatch();
}
int[] batches = statement.executeBatch();
Assert.assertEquals(10, batches.length);
for (int batch : batches) {
Assert.assertEquals(1, batch);
}
}
try (Connection con = dataSource.getConnection()) {
// Get and projection
try (PreparedStatement statement = con.prepareStatement("SELECT n1, k1 FROM mytable WHERE k1 = 'mykey_1'")) {
int count = 0;
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
assertEquals(1, rs.getInt(1));
assertNotNull(rs.getString(2));
++count;
}
}
assertEquals(1, count);
}
// Scan and projection
try (PreparedStatement statement = con.prepareStatement("SELECT n1, k1 FROM mytable WHERE n1 > 1")) {
int count = 0;
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
assertTrue(rs.getInt(1) > 1);
assertNotNull(rs.getString(2));
++count;
}
}
assertEquals(8, count);
}
// Scan, sort and projection
try (PreparedStatement statement = con.prepareStatement("SELECT n1, k1 FROM mytable WHERE n1 > 1 ORDER BY n1")) {
int count = 0;
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
assertTrue(rs.getInt(1) > 1);
assertNotNull(rs.getString(2));
++count;
}
}
assertEquals(8, count);
}
// verify all data types and table contents
try (PreparedStatement statement = con.prepareStatement("SELECT * FROM mytable ORDER BY n1")) {
try (ResultSet rs = statement.executeQuery()) {
for (int n = 0; n < 10; ++n) {
int i = 1;
assertTrue(rs.next());
assertEquals("mykey_" + n, rs.getString(i++));
assertEquals(n, rs.getInt(i++));
assertEquals(n, rs.getLong(i++));
assertNotNull(rs.getTimestamp(i++));
assertNull(rs.getString(i++));
assertTrue(rs.getBoolean(i++));
assertEquals(n + 0.5, rs.getDouble(i++), 0d);
assertEquals((short) n, rs.getShort(i++));
}
}
}
}
}
}
}
}
use of herddb.client.HDBClient in project herddb by diennea.
the class SimpleScanTest method testSingleColumnHeapTable.
@Test
public void testSingleColumnHeapTable() throws Exception {
try (Server server = new Server(herddb.jdbc.TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client)) {
try (Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
// no primary key
statement.execute("CREATE TABLE mytable (id string)");
}
try (Connection con = dataSource.getConnection();
PreparedStatement statement = con.prepareStatement("INSERT INTO mytable(id) values(?)")) {
for (int n = 0; n < 10; ++n) {
int i = 1;
statement.setString(i++, "mykey_" + n);
statement.addBatch();
}
int[] batches = statement.executeBatch();
Assert.assertEquals(10, batches.length);
for (int batch : batches) {
Assert.assertEquals(1, batch);
}
}
try (Connection con = dataSource.getConnection()) {
try (PreparedStatement statement = con.prepareStatement("SELECT * FROM mytable")) {
int count = 0;
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
assertNotNull(rs.getString(1));
++count;
}
}
assertEquals(10, count);
}
}
}
}
}
}
use of herddb.client.HDBClient in project herddb by diennea.
the class HerdDBResultSetTest method getStatement.
@Test
public void getStatement() throws Exception {
try (final Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
try (final HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
try (final BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client)) {
try (Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
statement.execute("CREATE TABLE mytable (c1 int primary key)");
}
try (final Connection con = dataSource.getConnection();
final PreparedStatement statement = con.prepareStatement("select * from mytable");
final ResultSet rows = statement.executeQuery()) {
Assert.assertEquals(statement, rows.getStatement());
}
}
}
}
}
use of herddb.client.HDBClient in project herddb by diennea.
the class MixedCaseIdentifiersTest method jdbcAliasList.
@Test
public void jdbcAliasList() throws Exception {
try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
statement.executeUpdate("CREATE TABLE IF NOT EXISTS roles (\n" + " role_id BIGINT PRIMARY KEY AUTO_INCREMENT,\n" + " role_name varchar(256) NOT NULL,\n" + " description varchar(128),\n" + " resource_type varchar(48),\n" + " resource_name varchar(48),\n" + " resource_verbs varchar(256))");
statement.executeUpdate("INSERT INTO roles(role_name,description, resource_type) values('theName', 'theDesc', 'theType')");
try (ResultSet rs = statement.executeQuery("SELECT * FROM roles")) {
assertTrue(rs.next());
assertEquals(1, rs.getInt("role_id"));
assertEquals("theName", rs.getString("role_name"));
assertEquals("theDesc", rs.getString("description"));
assertEquals("theType", rs.getString("resource_type"));
assertNull(rs.getString("resource_name"));
assertNull(rs.getString("resource_verbs"));
}
try (ResultSet rs = statement.executeQuery("SELECT role_name AS roleName, role_id AS roleId, description, resource_type AS resourceType,resource_name AS resourceName, resource_verbs AS resourceVerbs FROM roles")) {
assertTrue(rs.next());
assertEquals(1, rs.getInt("roleId"));
assertEquals("theName", rs.getString("roleName"));
assertEquals("theDesc", rs.getString("description"));
assertEquals("theType", rs.getString("resourceType"));
assertNull(rs.getString("resourceName"));
assertNull(rs.getString("resourceVerbs"));
}
try (ResultSet rs = statement.executeQuery("SELECT role_name AS roleName, role_id AS roleId, description, resource_type AS resourceType,resource_name AS resourceName, resource_verbs AS resourceVerbs FROM roles")) {
assertTrue(rs.next());
assertEquals(1, rs.getInt("roleId"));
assertEquals("theName", rs.getString("roleName"));
assertEquals("theDesc", rs.getString("description"));
assertEquals("theType", rs.getString("resourceType"));
assertNull(rs.getString("resourceName"));
assertNull(rs.getString("resourceVerbs"));
}
}
}
}
}
Aggregations