use of herddb.client.ClientConfiguration in project herddb by diennea.
the class MysqlCompatilityTest method test2.
@Test
public void test2() throws Exception {
try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
assumeTrue(server.getManager().getPlanner() instanceof SQLPlanner);
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();
Connection con2 = dataSource.getConnection();
Statement statement = con.createStatement()) {
con.setAutoCommit(false);
statement.execute("CREATE TABLE `queuebouncecategory_history` (\n" + " `queueid` int(11) NOT NULL,\n" + " `idbouncecategory` smallint(6) NOT NULL,\n" + " `refdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n" + " `messagecount` bigint(20) NOT NULL,\n" + " PRIMARY KEY (`queueid`,`refdate`,`idbouncecategory`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
statement.executeUpdate("INSERT INTO `queuebouncecategory_history` VALUES (1,3,'2015-03-29 01:00:00',1)");
statement.executeUpdate("INSERT INTO `queuebouncecategory_history` VALUES (1,3,'2015-03-29 02:00:00',1)");
statement.executeUpdate("INSERT INTO `queuebouncecategory_history` VALUES (1,3,'2015-03-29 03:00:00',1)");
}
}
}
}
use of herddb.client.ClientConfiguration in project herddb by diennea.
the class PreparedStatemetParametersTest method missingParameter.
/**
* Execute a prepared statement without a needed parameter
*/
@Test(expected = SQLException.class)
public void missingParameter() throws Exception {
try (Server server = new Server(new ServerConfiguration(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 (c1 int primary key)");
}
try (Connection con = dataSource.getConnection();
PreparedStatement statement = con.prepareStatement("INSERT INTO mytable values (?)")) {
int rows = statement.executeUpdate();
Assert.assertEquals(1, rows);
}
}
}
}
}
use of herddb.client.ClientConfiguration in project herddb by diennea.
the class AdvancedInsertSyntaxTest method testInsertMultiValues.
@Test
public void testInsertMultiValues() throws Exception {
try (Server server = new Server(new ServerConfiguration(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 create = con.createStatement();
PreparedStatement statement = con.prepareStatement("INSERT INTO mytable (name) values (?),(?)");
PreparedStatement statement_return_keys = con.prepareStatement("INSERT INTO mytable (name) values (?),(?)", Statement.RETURN_GENERATED_KEYS)) {
create.execute("CREATE TABLE mytable (n1 int primary key auto_increment, name string)");
statement.setString(1, "v1");
statement.setString(2, "v2");
statement.executeUpdate();
try {
statement_return_keys.setString(1, "v1");
statement_return_keys.setString(2, "v2");
statement_return_keys.executeUpdate();
fail();
} catch (SQLException err) {
assertTrue(err.getMessage().contains("cannot 'return values' on multi-values insert"));
}
}
}
}
}
use of herddb.client.ClientConfiguration in project herddb by diennea.
the class BatchTest method testBatch.
@Test
public void testBatch() throws Exception {
try (Server server = new Server(new ServerConfiguration(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 create = con.createStatement();
PreparedStatement statement = con.prepareStatement("INSERT INTO mytable (name) values(?)")) {
create.execute("CREATE TABLE mytable (n1 int primary key auto_increment, name string)");
{
for (int i = 0; i < 100; i++) {
statement.setString(1, "v" + i);
statement.addBatch();
}
int[] results = statement.executeBatch();
for (int i = 0; i < 100; i++) {
assertEquals(1, results[i]);
}
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable ORDER BY n1")) {
int count = 0;
while (rs.next()) {
assertEquals("v" + count, rs.getString("name"));
assertEquals(count + 1, rs.getInt("n1"));
count++;
}
assertEquals(100, count);
}
}
int next_id;
try (ResultSet rs = statement.executeQuery("SELECT MAX(n1) FROM mytable")) {
assertTrue(rs.next());
next_id = rs.getInt(1) + 1;
assertEquals(101, next_id);
}
statement.executeUpdate("DELETE FROM mytable");
try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM mytable")) {
assertTrue(rs.next());
assertEquals(0, rs.getInt(1));
}
// transactions
con.setAutoCommit(false);
{
for (int i = 0; i < 100; i++) {
statement.setString(1, "v" + i);
statement.addBatch();
}
int[] results = statement.executeBatch();
for (int i = 0; i < 100; i++) {
assertEquals(1, results[i]);
}
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable ORDER BY n1")) {
int count = 0;
while (rs.next()) {
assertEquals("v" + count, rs.getString("name"));
assertEquals(count + next_id, rs.getInt("n1"));
count++;
}
assertEquals(100, count);
}
}
con.commit();
}
}
}
}
use of herddb.client.ClientConfiguration in project herddb by diennea.
the class GeneratedKeysTest method testStatementInt.
@Test
public void testStatementInt() throws Exception {
try (Server server = new Server(new ServerConfiguration(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.execute("CREATE TABLE mytable (n1 int primary key auto_increment, name string)");
assertEquals(1, statement.executeUpdate("INSERT INTO mytable (name) values('name1')", Statement.RETURN_GENERATED_KEYS));
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(1, count);
}
Object key = null;
try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
if (generatedKeys.next()) {
key = generatedKeys.getObject(1);
}
}
assertNotNull(key);
assertEquals(Integer.valueOf(1), key);
}
}
}
}
Aggregations