Search in sources :

Example 1 with SQLDatasource

use of org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource in project ballerina by ballerina-lang.

the class BatchUpdate method execute.

@Override
public void execute(Context context) {
    try {
        BStruct bConnector = (BStruct) context.getRefArgument(0);
        String query = context.getStringArgument(0);
        BRefValueArray parameters = (BRefValueArray) context.getNullableRefArgument(1);
        SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
        Map<String, String> tags = new HashMap<>();
        tags.put(TAG_KEY_DB_STATEMENT, query);
        tags.put(TAG_KEY_DB_TYPE, TAG_DB_TYPE_SQL);
        TraceUtil.getTracer(context.getParentWorkerExecutionContext()).addTags(tags);
        executeBatchUpdate(context, datasource, query, parameters);
    } catch (Throwable e) {
        context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
        SQLDatasourceUtils.handleErrorOnTransaction(context);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HashMap(java.util.HashMap) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) SQLDatasource(org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)

Example 2 with SQLDatasource

use of org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource in project ballerina by ballerina-lang.

the class Update method execute.

@Override
public void execute(Context context) {
    try {
        BStruct bConnector = (BStruct) context.getRefArgument(0);
        String query = context.getStringArgument(0);
        BRefValueArray parameters = (BRefValueArray) context.getNullableRefArgument(1);
        SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
        Map<String, String> tags = new HashMap<>();
        tags.put(TAG_KEY_DB_STATEMENT, query);
        tags.put(TAG_KEY_DB_TYPE, TAG_DB_TYPE_SQL);
        TraceUtil.getTracer(context.getParentWorkerExecutionContext()).addTags(tags);
        executeUpdate(context, datasource, query, parameters);
    } catch (Throwable e) {
        context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
        SQLDatasourceUtils.handleErrorOnTransaction(context);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HashMap(java.util.HashMap) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) SQLDatasource(org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)

Example 3 with SQLDatasource

use of org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource in project ballerina by ballerina-lang.

the class InitEndpoint method execute.

@Override
public void execute(Context context) {
    Struct clientEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
    Struct clientEndpointConfig = clientEndpoint.getStructField(Constants.CLIENT_ENDPOINT_CONFIG);
    // Extract parameters from the endpoint config
    String database = clientEndpointConfig.getEnumField(Constants.EndpointConfig.DATABASE);
    String host = clientEndpointConfig.getStringField(Constants.EndpointConfig.HOST);
    int port = (int) clientEndpointConfig.getIntField(Constants.EndpointConfig.PORT);
    String name = clientEndpointConfig.getStringField(Constants.EndpointConfig.NAME);
    String username = clientEndpointConfig.getStringField(Constants.EndpointConfig.USERNAME);
    String password = clientEndpointConfig.getStringField(Constants.EndpointConfig.PASSWORD);
    Struct options = clientEndpointConfig.getStructField(Constants.EndpointConfig.OPTIONS);
    SQLDatasource datasource = new SQLDatasource();
    datasource.init(options, database, host, port, username, password, name);
    BStruct ballerinaClientConnector;
    if (clientEndpoint.getNativeData(Constants.B_CONNECTOR) != null) {
        ballerinaClientConnector = (BStruct) clientEndpoint.getNativeData(Constants.B_CONNECTOR);
    } else {
        ballerinaClientConnector = BLangConnectorSPIUtil.createBStruct(context.getProgramFile(), Constants.SQL_PACKAGE_PATH, Constants.CLIENT_CONNECTOR, database, host, port, name, username, password, options, clientEndpointConfig);
        clientEndpoint.addNativeData(Constants.B_CONNECTOR, ballerinaClientConnector);
    }
    ballerinaClientConnector.addNativeData(Constants.CLIENT_CONNECTOR, datasource);
    context.setReturnValues();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) SQLDatasource(org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct)

Example 4 with SQLDatasource

use of org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource in project ballerina by ballerina-lang.

the class Call method execute.

@Override
public void execute(Context context) {
    try {
        BStruct bConnector = (BStruct) context.getRefArgument(0);
        String query = context.getStringArgument(0);
        BRefValueArray parameters = (BRefValueArray) context.getNullableRefArgument(1);
        BStructType structType = getStructType(context);
        SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
        Map<String, String> tags = new HashMap<>();
        tags.put(TAG_KEY_DB_STATEMENT, query);
        tags.put(TAG_KEY_DB_TYPE, TAG_DB_TYPE_SQL);
        TraceUtil.getTracer(context.getParentWorkerExecutionContext()).addTags(tags);
        executeProcedure(context, datasource, query, parameters, structType);
    } catch (Throwable e) {
        context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
        SQLDatasourceUtils.handleErrorOnTransaction(context);
    }
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) HashMap(java.util.HashMap) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) SQLDatasource(org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)

Example 5 with SQLDatasource

use of org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource in project ballerina by ballerina-lang.

the class Close method execute.

@Override
public void execute(Context context) {
    try {
        BStruct bConnector = (BStruct) context.getRefArgument(0);
        SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
        closeConnections(datasource);
        context.setReturnValues(null);
    } catch (Throwable e) {
        context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
        SQLDatasourceUtils.handleErrorOnTransaction(context);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) SQLDatasource(org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)7 SQLDatasource (org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)7 HashMap (java.util.HashMap)5 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)5 BStructType (org.ballerinalang.model.types.BStructType)2 Struct (org.ballerinalang.connector.api.Struct)1 BStringArray (org.ballerinalang.model.values.BStringArray)1