Search in sources :

Example 6 with BTable

use of org.ballerinalang.model.values.BTable in project ballerina by ballerina-lang.

the class Close method execute.

public void execute(Context ctx) {
    BTable table = (BTable) ctx.getRefArgument(0);
    table.close(ctx.isInTransaction());
    ctx.setReturnValues();
}
Also used : BTable(org.ballerinalang.model.values.BTable)

Example 7 with BTable

use of org.ballerinalang.model.values.BTable in project ballerina by ballerina-lang.

the class Delete method execute.

@Override
public void execute(Context context) {
    BTable table = (BTable) context.getRefArgument(0);
    BStruct data = (BStruct) context.getRefArgument(1);
    table.removeData(data);
    context.setReturnValues();
}
Also used : BTable(org.ballerinalang.model.values.BTable) BStruct(org.ballerinalang.model.values.BStruct)

Example 8 with BTable

use of org.ballerinalang.model.values.BTable in project ballerina by ballerina-lang.

the class CPU method execTypeConversionOpcodes.

@SuppressWarnings("rawtypes")
private static void execTypeConversionOpcodes(WorkerExecutionContext ctx, WorkerData sf, int opcode, int[] operands) {
    int i;
    int j;
    int k;
    BRefType bRefType;
    String str;
    switch(opcode) {
        case InstructionCodes.I2F:
            i = operands[0];
            j = operands[1];
            sf.doubleRegs[j] = (double) sf.longRegs[i];
            break;
        case InstructionCodes.I2S:
            i = operands[0];
            j = operands[1];
            sf.stringRegs[j] = Long.toString(sf.longRegs[i]);
            break;
        case InstructionCodes.I2B:
            i = operands[0];
            j = operands[1];
            sf.intRegs[j] = sf.longRegs[i] != 0 ? 1 : 0;
            break;
        case InstructionCodes.I2JSON:
            i = operands[0];
            j = operands[1];
            sf.refRegs[j] = new BJSON(Long.toString(sf.longRegs[i]));
            break;
        case InstructionCodes.F2I:
            i = operands[0];
            j = operands[1];
            sf.longRegs[j] = (long) sf.doubleRegs[i];
            break;
        case InstructionCodes.F2S:
            i = operands[0];
            j = operands[1];
            sf.stringRegs[j] = Double.toString(sf.doubleRegs[i]);
            break;
        case InstructionCodes.F2B:
            i = operands[0];
            j = operands[1];
            sf.intRegs[j] = sf.doubleRegs[i] != 0.0 ? 1 : 0;
            break;
        case InstructionCodes.F2JSON:
            i = operands[0];
            j = operands[1];
            sf.refRegs[j] = new BJSON(Double.toString(sf.doubleRegs[i]));
            break;
        case InstructionCodes.S2I:
            i = operands[0];
            j = operands[1];
            str = sf.stringRegs[i];
            if (str == null) {
                handleTypeConversionError(ctx, sf, j, null, TypeConstants.INT_TNAME);
                break;
            }
            try {
                sf.refRegs[j] = new BInteger(Long.parseLong(str));
            } catch (NumberFormatException e) {
                handleTypeConversionError(ctx, sf, j, TypeConstants.STRING_TNAME, TypeConstants.INT_TNAME);
            }
            break;
        case InstructionCodes.S2F:
            i = operands[0];
            j = operands[1];
            str = sf.stringRegs[i];
            if (str == null) {
                handleTypeConversionError(ctx, sf, j, null, TypeConstants.FLOAT_TNAME);
                break;
            }
            try {
                sf.refRegs[j] = new BFloat(Double.parseDouble(str));
            } catch (NumberFormatException e) {
                handleTypeConversionError(ctx, sf, j, TypeConstants.STRING_TNAME, TypeConstants.FLOAT_TNAME);
            }
            break;
        case InstructionCodes.S2B:
            i = operands[0];
            j = operands[1];
            sf.intRegs[j] = Boolean.parseBoolean(sf.stringRegs[i]) ? 1 : 0;
            break;
        case InstructionCodes.S2JSON:
            i = operands[0];
            j = operands[1];
            str = StringEscapeUtils.escapeJson(sf.stringRegs[i]);
            sf.refRegs[j] = str == null ? null : new BJSON("\"" + str + "\"");
            break;
        case InstructionCodes.B2I:
            i = operands[0];
            j = operands[1];
            sf.longRegs[j] = sf.intRegs[i];
            break;
        case InstructionCodes.B2F:
            i = operands[0];
            j = operands[1];
            sf.doubleRegs[j] = sf.intRegs[i];
            break;
        case InstructionCodes.B2S:
            i = operands[0];
            j = operands[1];
            sf.stringRegs[j] = sf.intRegs[i] == 1 ? "true" : "false";
            break;
        case InstructionCodes.DT2XML:
            i = operands[0];
            j = operands[1];
            bRefType = sf.refRegs[i];
            if (bRefType == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                sf.refRegs[j] = XMLUtils.tableToXML((BTable) bRefType, ctx.isInTransaction());
            } catch (Exception e) {
                sf.refRegs[j] = null;
                handleTypeConversionError(ctx, sf, j, TypeConstants.TABLE_TNAME, TypeConstants.XML_TNAME);
            }
            break;
        case InstructionCodes.DT2JSON:
            i = operands[0];
            j = operands[1];
            bRefType = sf.refRegs[i];
            if (bRefType == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                sf.refRegs[j] = JSONUtils.toJSON((BTable) bRefType, ctx.isInTransaction());
            } catch (Exception e) {
                handleTypeConversionError(ctx, sf, j, TypeConstants.TABLE_TNAME, TypeConstants.XML_TNAME);
            }
            break;
        case InstructionCodes.T2MAP:
            convertStructToMap(ctx, operands, sf);
            break;
        case InstructionCodes.T2JSON:
            convertStructToJSON(ctx, operands, sf);
            break;
        case InstructionCodes.MAP2T:
            convertMapToStruct(ctx, operands, sf);
            break;
        case InstructionCodes.JSON2T:
            convertJSONToStruct(ctx, operands, sf);
            break;
        case InstructionCodes.XMLATTRS2MAP:
            i = operands[0];
            j = operands[1];
            bRefType = sf.refRegs[i];
            if (bRefType == null) {
                sf.refRegs[j] = null;
                break;
            }
            sf.refRegs[j] = ((BXMLAttributes) sf.refRegs[i]).value();
            break;
        case InstructionCodes.S2XML:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            str = sf.stringRegs[i];
            if (str == null) {
                sf.refRegs[j] = null;
                sf.refRegs[k] = null;
                break;
            }
            try {
                sf.refRegs[j] = XMLUtils.parse(str);
                sf.refRegs[k] = null;
            } catch (BallerinaException e) {
                sf.refRegs[j] = null;
                handleTypeConversionError(ctx, sf, k, e.getMessage());
            }
            break;
        case InstructionCodes.S2JSONX:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            str = sf.stringRegs[i];
            try {
                sf.refRegs[j] = str == null ? null : new BJSON(str);
                sf.refRegs[k] = null;
            } catch (BallerinaException e) {
                sf.refRegs[j] = null;
                handleTypeConversionError(ctx, sf, k, e.getMessage());
            }
            break;
        case InstructionCodes.XML2S:
            i = operands[0];
            j = operands[1];
            sf.stringRegs[j] = sf.refRegs[i].stringValue();
            break;
        case InstructionCodes.ANY2SCONV:
            i = operands[0];
            j = operands[1];
            bRefType = sf.refRegs[i];
            if (bRefType == null) {
                sf.stringRegs[j] = STRING_NULL_VALUE;
            } else {
                sf.stringRegs[j] = bRefType.stringValue();
            }
            break;
        default:
            throw new UnsupportedOperationException();
    }
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BTable(org.ballerinalang.model.values.BTable) BInteger(org.ballerinalang.model.values.BInteger) BFloat(org.ballerinalang.model.values.BFloat) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BJSON(org.ballerinalang.model.values.BJSON) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 9 with BTable

use of org.ballerinalang.model.values.BTable in project ballerina by ballerina-lang.

the class BAnyTypeSuccessScenariosTest method testInputAnyAsTable.

@Test(description = "Test any type as a return value with actual table returning")
public void testInputAnyAsTable() {
    BValue[] returns = BRunUtil.invoke(result, "inputAnyAsTableTest");
    Assert.assertEquals(returns.length, 1);
    Assert.assertSame(returns[0].getClass(), BTable.class);
    BTable table = (BTable) returns[0];
    Assert.assertEquals(table.stringValue(), "{data: [{id:1, name:\"Jane\"}, {id:2, name:\"Anne\"}]}");
}
Also used : BTable(org.ballerinalang.model.values.BTable) BValue(org.ballerinalang.model.values.BValue) Test(org.testng.annotations.Test)

Example 10 with BTable

use of org.ballerinalang.model.values.BTable in project ballerina by ballerina-lang.

the class BAnyTypeSuccessScenariosTest method testAnyReturnWithTable.

@Test(description = "Test any type as a return value with actual table returning")
public void testAnyReturnWithTable() {
    BValue[] returns = BRunUtil.invoke(result, "tableReturnTestAsAny");
    Assert.assertEquals(returns.length, 1);
    Assert.assertSame(returns[0].getClass(), BTable.class);
    BTable table = (BTable) returns[0];
    Assert.assertEquals(table.stringValue(), "{data: [{id:1, name:\"Jane\"}, {id:2, name:\"Anne\"}]}");
}
Also used : BTable(org.ballerinalang.model.values.BTable) BValue(org.ballerinalang.model.values.BValue) Test(org.testng.annotations.Test)

Aggregations

BTable (org.ballerinalang.model.values.BTable)13 BStruct (org.ballerinalang.model.values.BStruct)7 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)3 BValue (org.ballerinalang.model.values.BValue)3 BJSON (org.ballerinalang.model.values.BJSON)2 BRefType (org.ballerinalang.model.values.BRefType)2 BString (org.ballerinalang.model.values.BString)2 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)2 Test (org.testng.annotations.Test)2 List (java.util.List)1 StringJoiner (java.util.StringJoiner)1 Context (org.ballerinalang.bre.Context)1 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)1 BMapType (org.ballerinalang.model.types.BMapType)1 BStructType (org.ballerinalang.model.types.BStructType)1 BTableType (org.ballerinalang.model.types.BTableType)1 BBlobArray (org.ballerinalang.model.values.BBlobArray)1 BBoolean (org.ballerinalang.model.values.BBoolean)1 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)1 BFloat (org.ballerinalang.model.values.BFloat)1