Search in sources :

Example 51 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project pinpoint by naver.

the class CassandraDatastaxITBase method test.

@Test
public void test() throws Exception {
    final int testId = 99;
    final String testValue = "testValue";
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    Session myKeyspaceSession = null;
    try {
        myKeyspaceSession = cluster.connect(TEST_KEYSPACE);
        // ===============================================
        // Insert Data (PreparedStatement, BoundStatement)
        PreparedStatement preparedStatement = myKeyspaceSession.prepare(CQL_INSERT);
        BoundStatement boundStatement = new BoundStatement(preparedStatement);
        boundStatement.bind(testId, testValue);
        myKeyspaceSession.execute(boundStatement);
        verifier.printCache();
        // Cluster#connect(String)
        Class<?> clusterClass = Class.forName("com.datastax.driver.core.Cluster");
        Method connect = clusterClass.getDeclaredMethod("connect", String.class);
        verifier.verifyTrace(event(CASSANDRA, connect, null, CASSANDRA_ADDRESS, TEST_KEYSPACE));
        // SessionManager#prepare(String) OR AbstractSession#prepare(String)
        Class<?> sessionClass;
        try {
            sessionClass = Class.forName("com.datastax.driver.core.AbstractSession");
        } catch (ClassNotFoundException e) {
            sessionClass = Class.forName("com.datastax.driver.core.SessionManager");
        }
        Method prepare = sessionClass.getDeclaredMethod("prepare", String.class);
        verifier.verifyTrace(event(CASSANDRA, prepare, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(CQL_INSERT, null)));
        // SessionManager#execute(Statement) OR AbstractSession#execute(Statement)
        Method execute = sessionClass.getDeclaredMethod("execute", Statement.class);
        verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(CQL_INSERT, null)));
        // ====================
        // Select Data (String)
        final String cqlSelect = String.format("SELECT %s, %s FROM %s WHERE %s = %d", TEST_COL_ID, TEST_COL_VALUE, TEST_TABLE, TEST_COL_ID, testId);
        verifySelect(myKeyspaceSession.execute(cqlSelect), testId, testValue);
        verifier.printCache();
        // SessionManager#execute(String) OR AbstractSession#execute(String)
        execute = sessionClass.getDeclaredMethod("execute", String.class);
        String normalizedSelectSql = SQL_PARSER.normalizedSql(cqlSelect).getNormalizedSql();
        verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(normalizedSelectSql, String.valueOf(testId))));
        // ====================
        // Delete Data (String)
        final String cqlDelete = String.format("DELETE FROM %s.%s WHERE %s = ?", TEST_KEYSPACE, TEST_TABLE, TEST_COL_ID);
        myKeyspaceSession.execute(cqlDelete, testId);
        verifier.printCache();
        // SessionManager#execute(String, Object[]) OR AbstractSession#execute(String, Object[])
        execute = sessionClass.getDeclaredMethod("execute", String.class, Object[].class);
        String normalizedDeleteSql = SQL_PARSER.normalizedSql(cqlDelete).getNormalizedSql();
        verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(normalizedDeleteSql, null)));
    } finally {
        closeSession(myKeyspaceSession);
    }
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) Method(java.lang.reflect.Method) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) BoundStatement(com.datastax.driver.core.BoundStatement) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 52 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project camel by apache.

the class CassandraProducer method executePreparedStatement.

/**
     * Execute CQL as PreparedStatement
     */
private ResultSet executePreparedStatement(Session session, Object messageCql, Object[] cqlParams) {
    ResultSet resultSet;
    PreparedStatement lPreparedStatement;
    if (messageCql == null) {
        // URI CQL
        lPreparedStatement = this.preparedStatement;
    } else if (messageCql instanceof String) {
        // Message CQL
        lPreparedStatement = getEndpoint().prepareStatement((String) messageCql);
    } else if (messageCql instanceof RegularStatement) {
        // Message Statement
        lPreparedStatement = getEndpoint().getSession().prepare((RegularStatement) messageCql);
    } else {
        throw new IllegalArgumentException("Invalid " + CassandraConstants.CQL_QUERY + " header");
    }
    if (isEmpty(cqlParams)) {
        resultSet = session.execute(lPreparedStatement.bind());
    } else {
        resultSet = session.execute(lPreparedStatement.bind(cqlParams));
    }
    return resultSet;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) RegularStatement(com.datastax.driver.core.RegularStatement)

Example 53 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project smscgateway by RestComm.

the class PersistenceRAInterfaceProxy method checkSmsExists.

public int checkSmsExists(long dueSlot, String targetId) throws PersistenceException {
    try {
        String s1 = "select \"ID\" from \"SLOT_MESSAGES_TABLE" + this.getTableName(dueSlot) + "\" where \"DUE_SLOT\"=? and \"TARGET_ID\"=?;";
        PreparedStatement ps = session.prepare(s1);
        BoundStatement boundStatement = new BoundStatement(ps);
        boundStatement.bind(dueSlot, targetId);
        ResultSet rs = session.execute(boundStatement);
        return rs.all().size();
    } catch (Exception e) {
        int ggg = 0;
        ggg = 0;
        return -1;
    }
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) IOException(java.io.IOException)

Example 54 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project smscgateway by RestComm.

the class PersistenceRAInterfaceProxy method testCassandraAccess.

public boolean testCassandraAccess() {
    try {
        Cluster cluster = Cluster.builder().addContactPoint(ip).build();
        try {
            Metadata metadata = cluster.getMetadata();
            for (Host host : metadata.getAllHosts()) {
                logger.info(String.format("Datacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(), host.getAddress(), host.getRack()));
            }
            Session session = cluster.connect();
            session.execute("USE \"" + keyspace + "\"");
            // testing if a keyspace is acceptable
            int tstRes = 0;
            PreparedStatement ps;
            BoundStatement boundStatement;
            try {
                ps = session.prepare("SELECT * from \"TEST_TABLE\";");
                boundStatement = new BoundStatement(ps);
                boundStatement.bind();
                session.execute(boundStatement);
                tstRes = 1;
            } catch (Exception e) {
                int g1 = 0;
                g1++;
            }
            ProtocolVersion protVersion = DBOperations.getProtocolVersion(cluster);
            if (protVersion == ProtocolVersion.V1) {
                throw new Exception("We do not support cassandra databse 1.2 more");
            // if (tstRes == 0) {
            // session.execute("CREATE TABLE \"TEST_TABLE\" (id uuid primary key);");
            // }
            // 
            // // deleting of current tables
            // try {
            // session.execute("TRUNCATE \"" + Schema.FAMILY_CURRENT_SLOT_TABLE + "\";");
            // } catch (Exception e) {
            // int g1 = 0;
            // g1++;
            // }
            } else {
                if (tstRes == 0) {
                    ps = session.prepare("CREATE TABLE \"TEST_TABLE\" (id uuid primary key);");
                    boundStatement = new BoundStatement(ps);
                    boundStatement.bind();
                    session.execute(boundStatement);
                }
                // deleting of current tables
                ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_CURRENT_SLOT_TABLE + "\";");
                boundStatement = new BoundStatement(ps);
                boundStatement.bind();
                try {
                    session.execute(boundStatement);
                } catch (Exception e) {
                    int g1 = 0;
                    g1++;
                }
            }
            // 1
            Date dt = new Date();
            Date dt2 = new Date(new Date().getTime() + 1000 * 60 * 60 * 24);
            Date dt3 = new Date(new Date().getTime() - 1000 * 60 * 60 * 24);
            String tName = this.getTableName(dt);
            doTrauncateTables(session, tName);
            // 2
            tName = this.getTableName(dt2);
            doTrauncateTables(session, tName);
            // 3
            tName = this.getTableName(dt3);
            doTrauncateTables(session, tName);
            return true;
        } finally {
            cluster.close();
        // cluster.shutdown();
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
Also used : Metadata(com.datastax.driver.core.Metadata) Cluster(com.datastax.driver.core.Cluster) Host(com.datastax.driver.core.Host) PreparedStatement(com.datastax.driver.core.PreparedStatement) ProtocolVersion(com.datastax.driver.core.ProtocolVersion) BoundStatement(com.datastax.driver.core.BoundStatement) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) IOException(java.io.IOException) Date(java.util.Date) Session(com.datastax.driver.core.Session)

Example 55 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project smscgateway by RestComm.

the class PersistenceRAInterfaceProxy method doTrauncateTables.

private void doTrauncateTables(Session session, String tName) {
    PreparedStatement ps;
    BoundStatement boundStatement;
    ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_DST_SLOT_TABLE + tName + "\";");
    boundStatement = new BoundStatement(ps);
    boundStatement.bind();
    try {
        session.execute(boundStatement);
    } catch (Exception e) {
        int g1 = 0;
        g1++;
    }
    ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_SLOT_MESSAGES_TABLE + tName + "\";");
    boundStatement = new BoundStatement(ps);
    boundStatement.bind();
    try {
        session.execute(boundStatement);
    } catch (Exception e) {
        int g1 = 0;
        g1++;
    }
    ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_MESSAGES + tName + "\";");
    boundStatement = new BoundStatement(ps);
    boundStatement.bind();
    try {
        session.execute(boundStatement);
    } catch (Exception e) {
        int g1 = 0;
        g1++;
    }
    ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_MES_ID + tName + "\";");
    boundStatement = new BoundStatement(ps);
    boundStatement.bind();
    try {
        session.execute(boundStatement);
    } catch (Exception e) {
        int g1 = 0;
        g1++;
    }
    ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_DLV_MES_ID + tName + "\";");
    boundStatement = new BoundStatement(ps);
    boundStatement.bind();
    try {
        session.execute(boundStatement);
    } catch (Exception e) {
        int g1 = 0;
        g1++;
    }
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) IOException(java.io.IOException)

Aggregations

PreparedStatement (com.datastax.driver.core.PreparedStatement)113 ResultSet (com.datastax.driver.core.ResultSet)60 BoundStatement (com.datastax.driver.core.BoundStatement)59 Session (com.datastax.driver.core.Session)39 Test (org.junit.Test)30 Row (com.datastax.driver.core.Row)27 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)27 XMLStreamException (javolution.xml.stream.XMLStreamException)25 PersistenceException (org.mobicents.smsc.cassandra.PersistenceException)15 Cluster (com.datastax.driver.core.Cluster)9 Date (java.util.Date)9 IInvokableInstance (org.apache.cassandra.distributed.api.IInvokableInstance)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)7 QueryProcessor (org.apache.cassandra.cql3.QueryProcessor)7 GOSSIP (org.apache.cassandra.distributed.api.Feature.GOSSIP)7 NATIVE_PROTOCOL (org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL)7 NETWORK (org.apache.cassandra.distributed.api.Feature.NETWORK)7 ICluster (org.apache.cassandra.distributed.api.ICluster)7