Search in sources :

Example 11 with PreparedStatement

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

the class DBOperations method doGetStatementCollection.

private synchronized PreparedStatementCollection doGetStatementCollection(String tName) throws PersistenceException {
    PreparedStatementCollection psc = dataTableRead.get(tName);
    if (psc != null)
        return psc;
    try {
        try {
            // checking if a datatable exists
            String s1 = "SELECT * FROM \"" + Schema.FAMILY_DST_SLOT_TABLE + tName + "\";";
            PreparedStatement ps = session.prepare(s1);
        } catch (InvalidQueryException e) {
            // datatable does not exist
            // DST_SLOT_TABLE
            StringBuilder sb = new StringBuilder();
            sb.append("CREATE TABLE \"" + Schema.FAMILY_DST_SLOT_TABLE);
            sb.append(tName);
            sb.append("\" (");
            appendField(sb, Schema.COLUMN_TARGET_ID, "ascii");
            appendField(sb, Schema.COLUMN_DUE_SLOT, "bigint");
            sb.append("PRIMARY KEY (\"");
            sb.append(Schema.COLUMN_TARGET_ID);
            sb.append("\"");
            sb.append("));");
            String s2 = sb.toString();
            PreparedStatement ps = session.prepare(s2);
            BoundStatement boundStatement = new BoundStatement(ps);
            ResultSet res = session.execute(boundStatement);
            // SLOT_MESSAGES_TABLE
            sb = new StringBuilder();
            sb.append("CREATE TABLE \"" + Schema.FAMILY_SLOT_MESSAGES_TABLE);
            sb.append(tName);
            sb.append("\" (");
            addSmsFields(sb);
            sb.append("PRIMARY KEY ((\"");
            sb.append(Schema.COLUMN_DUE_SLOT);
            sb.append("\"), \"");
            sb.append(Schema.COLUMN_TARGET_ID);
            sb.append("\", \"");
            sb.append(Schema.COLUMN_ID);
            sb.append("\"");
            sb.append("));");
            s2 = sb.toString();
            ps = session.prepare(s2);
            boundStatement = new BoundStatement(ps);
            res = session.execute(boundStatement);
            // MESSAGES
            sb = new StringBuilder();
            sb.append("CREATE TABLE \"" + Schema.FAMILY_MESSAGES);
            sb.append(tName);
            sb.append("\" (");
            addSmsFields(sb);
            sb.append("PRIMARY KEY ((\"");
            sb.append(Schema.COLUMN_ADDR_DST_DIGITS);
            sb.append("\"), \"");
            sb.append(Schema.COLUMN_ID);
            sb.append("\"");
            sb.append("));");
            s2 = sb.toString();
            ps = session.prepare(s2);
            boundStatement = new BoundStatement(ps);
            res = session.execute(boundStatement);
        }
    } catch (Exception e1) {
        String msg = "Failed to access or create table " + tName + "!";
        throw new PersistenceException(msg, e1);
    }
    try {
        try {
            // checking if a datatable MES_ID_YYYY_MM_DD exists
            String s1 = "SELECT * FROM \"" + Schema.FAMILY_MES_ID + tName + "\";";
            PreparedStatement ps = session.prepare(s1);
        } catch (InvalidQueryException e) {
            // datatable does not exist
            // FAMILY_MES_ID
            StringBuilder sb = new StringBuilder();
            sb.append("CREATE TABLE \"" + Schema.FAMILY_MES_ID);
            sb.append(tName);
            sb.append("\" (");
            appendField(sb, Schema.COLUMN_MESSAGE_ID, "bigint");
            appendField(sb, Schema.COLUMN_ADDR_DST_DIGITS, "ascii");
            appendField(sb, Schema.COLUMN_ID, "uuid");
            sb.append("PRIMARY KEY (\"");
            sb.append(Schema.COLUMN_MESSAGE_ID);
            sb.append("\"");
            sb.append("));");
            String s2 = sb.toString();
            PreparedStatement ps = session.prepare(s2);
            BoundStatement boundStatement = new BoundStatement(ps);
            ResultSet res = session.execute(boundStatement);
        }
    } catch (Exception e1) {
        String msg = "Failed to access or create table " + tName + "!";
        throw new PersistenceException(msg, e1);
    }
    try {
        try {
            // checking if a datatable MES_ID_YYYY_MM_DD exists
            String s1 = "SELECT * FROM \"" + Schema.FAMILY_DLV_MES_ID + tName + "\";";
            PreparedStatement ps = session.prepare(s1);
        } catch (InvalidQueryException e) {
            // datatable does not exist
            // FAMILY_MES_ID
            StringBuilder sb = new StringBuilder();
            sb.append("CREATE TABLE \"" + Schema.FAMILY_DLV_MES_ID);
            sb.append(tName);
            sb.append("\" (");
            appendField(sb, Schema.COLUMN_REMOTE_MESSAGE_ID, "ascii");
            appendField(sb, Schema.COLUMN_DEST_ID, "ascii");
            appendField(sb, Schema.COLUMN_MESSAGE_ID, "bigint");
            sb.append("PRIMARY KEY (\"");
            sb.append(Schema.COLUMN_REMOTE_MESSAGE_ID);
            sb.append("\", \"");
            sb.append(Schema.COLUMN_DEST_ID);
            sb.append("\"");
            sb.append("));");
            String s2 = sb.toString();
            PreparedStatement ps = session.prepare(s2);
            BoundStatement boundStatement = new BoundStatement(ps);
            ResultSet res = session.execute(boundStatement);
        }
    } catch (Exception e1) {
        String msg = "Failed to access or create table " + tName + "!";
        throw new PersistenceException(msg, e1);
    }
    psc = new PreparedStatementCollection(this, tName, ttlCurrent, ttlArchive);
    dataTableRead.putEntry(tName, psc);
    return psc;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) XMLStreamException(javolution.xml.stream.XMLStreamException) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException)

Example 12 with PreparedStatement

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

the class PersistenceRAInterfaceProxy method obtainArchiveSms.

public SmsProxy obtainArchiveSms(long dueSlot, String dstDigits, UUID dbId) throws PersistenceException, IOException {
    PreparedStatement ps = session.prepare("select * from \"" + Schema.FAMILY_MESSAGES + this.getTableName(dueSlot) + "\" where \"" + Schema.COLUMN_ADDR_DST_DIGITS + "\"=? and \"" + Schema.COLUMN_ID + "\"=?;");
    BoundStatement boundStatement = new BoundStatement(ps);
    boundStatement.bind(dstDigits, dbId);
    ResultSet result = session.execute(boundStatement);
    Row row = result.one();
    SmsSet smsSet = createSms(row, null, true, true, true, true, true, false);
    if (smsSet == null)
        return null;
    SmsProxy res = new SmsProxy();
    res.sms = smsSet.getSms(0);
    res.addrDstDigits = row.getString(Schema.COLUMN_ADDR_DST_DIGITS);
    res.addrDstTon = row.getInt(Schema.COLUMN_ADDR_DST_TON);
    res.addrDstNpi = row.getInt(Schema.COLUMN_ADDR_DST_NPI);
    res.destClusterName = row.getString(Schema.COLUMN_DEST_CLUSTER_NAME);
    res.destEsmeName = row.getString(Schema.COLUMN_DEST_ESME_NAME);
    res.destSystemId = row.getString(Schema.COLUMN_DEST_SYSTEM_ID);
    res.imsi = row.getString(Schema.COLUMN_IMSI);
    res.corrId = row.getString(Schema.COLUMN_CORR_ID);
    res.networkId = row.getInt(Schema.COLUMN_NETWORK_ID);
    res.nnnDigits = row.getString(Schema.COLUMN_NNN_DIGITS);
    res.smStatus = row.getInt(Schema.COLUMN_SM_STATUS);
    res.smType = row.getInt(Schema.COLUMN_SM_TYPE);
    res.deliveryCount = row.getInt(Schema.COLUMN_DELIVERY_COUNT);
    res.deliveryDate = DBOperations.getRowDate(row, Schema.COLUMN_DELIVERY_DATE);
    return res;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) Row(com.datastax.driver.core.Row) BoundStatement(com.datastax.driver.core.BoundStatement) SmsSet(org.mobicents.smsc.library.SmsSet)

Example 13 with PreparedStatement

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

the class PersistenceRAInterfaceProxy method obtainLiveSms.

public Sms obtainLiveSms(long dueSlot, String targetId, UUID id) throws PersistenceException {
    try {
        String s1 = "select * from \"SLOT_MESSAGES_TABLE" + this.getTableName(dueSlot) + "\" where \"DUE_SLOT\"=? and \"TARGET_ID\"=? and \"ID\"=?;";
        PreparedStatement ps = session.prepare(s1);
        BoundStatement boundStatement = new BoundStatement(ps);
        boundStatement.bind(dueSlot, targetId, id);
        ResultSet rs = session.execute(boundStatement);
        SmsSet smsSet = null;
        Row row2 = null;
        for (Row row : rs) {
            smsSet = this.createSms(row, null, true, true, true, true, true, false);
            row2 = row;
            break;
        }
        if (smsSet == null || smsSet.getSmsCount() == 0)
            return null;
        else {
            smsSet.setAlertingSupported(row2.getBool(Schema.COLUMN_ALERTING_SUPPORTED));
            smsSet.setStatus(ErrorCode.fromInt(row2.getInt(Schema.COLUMN_SM_STATUS)));
            return smsSet.getSms(0);
        }
    } catch (Exception e) {
        int ggg = 0;
        ggg = 0;
        return null;
    }
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) Row(com.datastax.driver.core.Row) BoundStatement(com.datastax.driver.core.BoundStatement) SmsSet(org.mobicents.smsc.library.SmsSet) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) IOException(java.io.IOException)

Example 14 with PreparedStatement

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

the class PersistenceProxy method testCassandraAccess.

// public void setKeyspace(Keyspace val) {
// this.keyspace = val;
// }
// 
// public Keyspace getKeyspace() {
// return this.keyspace;
// }
public boolean testCassandraAccess() {
    String ip = "127.0.0.1";
    String keyspace = "RestCommSMSC";
    try {
        // dbOperations = DBOperations.getInstance();
        Cluster cluster = Cluster.builder().addContactPoint(ip).build();
        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 + "\"");
        PreparedStatement ps = session.prepare("select * from \"" + Schema.FAMILY_SMPP_SMS_ROUTING_RULE + "\" limit 1;");
        BoundStatement boundStatement = new BoundStatement(ps);
        boundStatement.bind();
        session.execute(boundStatement);
        return true;
    } catch (Exception e) {
        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) BoundStatement(com.datastax.driver.core.BoundStatement) Session(com.datastax.driver.core.Session)

Example 15 with PreparedStatement

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

the class NN_DBOper method createDueSlotForTargetId.

private void createDueSlotForTargetId(PreparedStatementCollection3 psc, String targetId, long newDueSlot) throws PersistenceException {
    try {
        PreparedStatement ps = psc.createDueSlotForTargetId;
        BoundStatement boundStatement = new BoundStatement(ps);
        boundStatement.bind(targetId, newDueSlot);
        ResultSet res = session.execute(boundStatement);
    } catch (Exception e1) {
        String msg = "Failed to execute createDueSlotForTargetId() !";
        throw new PersistenceException(msg, e1);
    }
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) XMLStreamException(javolution.xml.stream.XMLStreamException) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException)

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