Search in sources :

Example 1 with Database

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Database in project legend-engine by finos.

the class TestGeneration method testGenerationWithSnowflakeConnection.

@Test
public void testGenerationWithSnowflakeConnection() throws Exception {
    PureModelContextData contextData = PureGrammarParser.newInstance().parseModel("" + "Class p::A {name:String[1];}" + "function p::f():p::A[*]{p::A.all();}\n" + "###Relational\n" + "Database p::db(Table tb (name VARCHAR(200)))\n" + "###Mapping\n" + "Mapping p::M (p::A : Relational {name : [p::db]tb.name })\n" + "###Runtime\n" + "Runtime p::R" + "{" + "   mappings:[p::M];" + "   connections:[" + "       p::db : [" + "           c1: #{\n" + "                   RelationalDatabaseConnection\n" + "                   {\n" + "                       type: Snowflake;\n" + "                       specification: Snowflake\n" + "                                      {\n" + "                                           name: 'test';\n" + "                                           account: 'account';\n" + "                                           warehouse: 'warehouseName';\n" + "                                           region: 'us-east2';\n" + "                                           cloudType: 'aws';\n" + "                                           quotedIdentifiersIgnoreCase : false;\n" + "                                      };\n" + "                       auth: SnowflakePublic\n" + "                             {" + "                                 publicUserName: 'name';\n" + "                                 privateKeyVaultReference : 'key';\n" + "                                 passPhraseVaultReference: 'val';" + "                             };\n" + "                   }\n" + "               }#\n" + "           ]" + "   ];" + "}");
    PureModel pureModel = Compiler.compile(contextData, null, null);
    String plan = PlanGenerator.generateExecutionPlanAsString(HelperValueSpecificationBuilder.buildLambda(contextData.getElementsOfType(Function.class).get(0).body, Collections.emptyList(), new CompileContext.Builder(pureModel).build()), pureModel.getMapping("p::M"), pureModel.getRuntime("p::R"), null, pureModel, "vX_X_X", PlanPlatform.JAVA, null, core_relational_relational_router_router_extension.Root_meta_pure_router_extension_defaultRelationalExtensions__RouterExtension_MANY_(pureModel.getExecutionSupport()), LegendPlanTransformers.transformers);
    ObjectMapper mapper = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports().enable(SerializationFeature.INDENT_OUTPUT);
    ExecutionPlan executionPlan = mapper.readValue(plan, ExecutionPlan.class);
    StringWriter writer = new StringWriter();
    mapper.writeValue(writer, executionPlan);
    assertGeneratedStringEquals("{\n" + "  \"_type\" : \"simple\",\n" + "  \"authDependent\" : false,\n" + "  \"globalImplementationSupport\" : {\n" + "    \"_type\" : \"java\",\n" + "    \"classes\" : [ {\n" + "      \"name\" : \"A\",\n" + "      \"package\" : \"_pure.app.p\",\n" + "      \"source\" : \"package _pure.app.p;\\n\\nimport java.math.*;\\nimport java.util.*;\\nimport org.finos.legend.engine.plan.dependencies.domain.date.PureDate;\\n\\npublic interface A extends org.finos.legend.engine.plan.dependencies.store.shared.IReferencedObject\\n{\\n    String getName();\\n    String getAlloyStoreObjectReference$();\\n}\"\n" + "    }, {\n" + "      \"name\" : \"A_p_A_BaseImpl\",\n" + "      \"package\" : \"_pure.plan.root\",\n" + "      \"source\" : \"package _pure.plan.root;\\n\\nimport com.fasterxml.jackson.annotation.JsonIgnore;\\nimport com.fasterxml.jackson.annotation.JsonInclude;\\nimport com.fasterxml.jackson.annotation.JsonProperty;\\nimport com.fasterxml.jackson.core.JsonGenerator;\\nimport com.fasterxml.jackson.databind.JsonSerializer;\\nimport com.fasterxml.jackson.databind.ObjectMapper;\\nimport com.fasterxml.jackson.databind.SerializerProvider;\\nimport com.fasterxml.jackson.databind.module.SimpleModule;\\nimport java.io.IOException;\\nimport java.util.*;\\nimport org.finos.legend.engine.plan.dependencies.domain.date.PureDate;\\n\\npublic class A_p_A_BaseImpl implements _pure.app.p.A\\n{\\n    private String name;\\n    private String setId$;\\n    public static String databaseConnection$;\\n    private static final ObjectMapper objectMapper$ = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL).registerModule(new SimpleModule().addSerializer(PureDate.class, new JsonSerializer<PureDate>() { @Override public void serialize(PureDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeRawValue(\\\"\\\\\\\"\\\" + value.toString() + \\\"\\\\\\\"\\\"); } }));\\n    private String alloyStoreObjectReference$;\\n\\n    @JsonProperty(\\\"name\\\")\\n    public String getName()\\n    {\\n        return this.name;\\n    }\\n\\n    public void setName(String name)\\n    {\\n        this.name = name;\\n    }\\n\\n    @JsonIgnore\\n    public String getSetId$()\\n    {\\n        return this.setId$;\\n    }\\n\\n    public void setSetId$(String setId)\\n    {\\n        this.setId$ = setId;\\n    }\\n\\n    @JsonProperty(\\\"alloyStoreObjectReference$\\\")\\n    public String getAlloyStoreObjectReference$()\\n    {\\n        if (this.alloyStoreObjectReference$ == null)\\n        {\\n            try\\n            {\\n                StringBuilder referenceBuilder = new StringBuilder();\\n                referenceBuilder.append(\\\"001:\\\");\\n                referenceBuilder.append(\\\"010:\\\");\\n\\n                referenceBuilder.append(\\\"0000000010:\\\");\\n                referenceBuilder.append(\\\"Relational:\\\");\\n\\n                referenceBuilder.append(\\\"0000000004:\\\");\\n                referenceBuilder.append(\\\"p::M:\\\");\\n\\n                referenceBuilder.append(\\\"0000000003:\\\");\\n                referenceBuilder.append(\\\"p_A:\\\");\\n\\n                String setId = this.getSetId$();\\n                referenceBuilder.append(String.format(\\\"%010d\\\", setId.length()));\\n                referenceBuilder.append(\\\":\\\");\\n                referenceBuilder.append(setId);\\n                referenceBuilder.append(\\\":\\\");\\n\\n                String databaseConnectionString = _pure.plan.root.A_p_A_BaseImpl.databaseConnection$;\\n                referenceBuilder.append(String.format(\\\"%010d\\\", databaseConnectionString.length()));\\n                referenceBuilder.append(\\\":\\\");\\n                referenceBuilder.append(databaseConnectionString);\\n                referenceBuilder.append(\\\":\\\");\\n\\n                Map<String, Object> pkMap = new HashMap<>();\\n\\n                String pkMapString = objectMapper$.writeValueAsString(pkMap);\\n                referenceBuilder.append(String.format(\\\"%010d\\\", pkMapString.length()));\\n                referenceBuilder.append(\\\":\\\");\\n                referenceBuilder.append(pkMapString);\\n\\n                this.alloyStoreObjectReference$ = \\\"ASOR:\\\" + org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(referenceBuilder.toString().getBytes());\\n            }\\n            catch (Exception e)\\n            {\\n               throw new RuntimeException(e);\\n            }\\n        }\\n\\n        return this.alloyStoreObjectReference$;\\n    }\\n}\"\n" + "    }, {\n" + "      \"name\" : \"A_p_A_Impl\",\n" + "      \"package\" : \"_pure.plan.root\",\n" + "      \"source\" : \"package _pure.plan.root;\\n\\nimport com.fasterxml.jackson.annotation.JsonIgnore;\\nimport com.fasterxml.jackson.annotation.JsonProperty;\\nimport java.util.*;\\n\\npublic class A_p_A_Impl extends A_p_A_BaseImpl implements _pure.app.p.A\\n{\\n    public A_p_A_Impl()\\n    {\\n        this.setSetId$(\\\"p_A\\\");\\n    }\\n}\"\n" + "    }, {\n" + "      \"name\" : \"Execute\",\n" + "      \"package\" : \"_pure.plan.root\",\n" + "      \"source\" : \"package _pure.plan.root;\\n\\nimport java.sql.ResultSet;\\nimport java.util.*;\\nimport org.finos.legend.engine.plan.dependencies.store.relational.classResult.IRelationalClassInstantiationNodeExecutor;\\n\\npublic class Execute implements IRelationalClassInstantiationNodeExecutor\\n{\\n    private Helper helper;\\n\\n    public Execute()\\n    {\\n        this.helper = new Helper();\\n    }\\n\\n    public Object getObjectFromResultSet(ResultSet resultSet,\\n                                         String databaseTimeZone,\\n                                         String databaseConnection)\\n    {\\n        return this.helper.getObjectFromResultSet(resultSet, databaseTimeZone, databaseConnection);\\n    }\\n}\"\n" + "    }, {\n" + "      \"name\" : \"Helper\",\n" + "      \"package\" : \"_pure.plan.root\",\n" + "      \"source\" : \"package _pure.plan.root;\\n\\nimport java.sql.ResultSet;\\nimport java.sql.ResultSetMetaData;\\nimport java.sql.Types;\\nimport java.util.*;\\nimport java.util.function.*;\\nimport java.util.stream.*;\\nimport org.finos.legend.engine.plan.dependencies.domain.date.PureDate;\\n\\npublic class Helper\\n{\\n    private static final List<Integer> STRING_TYPES = Arrays.asList(Types.CHAR, Types.VARCHAR, Types.LONGVARCHAR, Types.NCHAR, Types.NVARCHAR, Types.LONGNVARCHAR, Types.OTHER, Types.NULL);\\n    private static final List<Integer> INT_TYPES = Arrays.asList(Types.TINYINT, Types.SMALLINT, Types.INTEGER, Types.BIGINT, Types.NULL);\\n    private static final List<Integer> FLOAT_TYPES = Arrays.asList(Types.REAL, Types.FLOAT, Types.DOUBLE, Types.DECIMAL, Types.NUMERIC, Types.NULL);\\n    private static final List<Integer> BOOL_TYPES = Arrays.asList(Types.BIT, Types.BOOLEAN, Types.NULL);\\n    private static final List<Integer> STRICT_DATE_TYPES = Arrays.asList(Types.DATE, Types.NULL);\\n    private static final List<Integer> DATE_TIME_TYPES = Arrays.asList(Types.TIMESTAMP, Types.NULL);\\n    private List<Integer> columnTypes;\\n    private List<List<Integer>> propertyIndices;\\n    private List<List<Supplier<Object>>> propertyGetters;\\n    private Calendar calendar;\\n\\n    private Supplier<Object> getResultSetPropertyGetterForStringProperty(ResultSet resultSet,\\n                                                                         int columnIndex,\\n                                                                         int columnType,\\n                                                                         String propertyName)\\n    {\\n        if (STRING_TYPES.contains(columnType))\\n        {\\n           return () -> {\\n                try\\n                {\\n                    return resultSet.getString(columnIndex);\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        else\\n        {\\n            throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type String from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n        }\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForIntegerProperty(ResultSet resultSet,\\n                                                                          int columnIndex,\\n                                                                          int columnType,\\n                                                                          String propertyName)\\n    {\\n        if (INT_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    Long res = null;\\n                    long r = resultSet.getLong(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = Long.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type Integer from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForFloatProperty(ResultSet resultSet,\\n                                                                        int columnIndex,\\n                                                                        int columnType,\\n                                                                        String propertyName)\\n    {\\n        if (FLOAT_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    Double res = null;\\n                    double r = resultSet.getDouble(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = Double.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (INT_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    Double res = null;\\n                    long r = resultSet.getLong(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = Double.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type Float from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForDecimalProperty(ResultSet resultSet,\\n                                                                          int columnIndex,\\n                                                                          int columnType,\\n                                                                          String propertyName)\\n    {\\n        if (FLOAT_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    java.math.BigDecimal res = null;\\n                    double r = resultSet.getDouble(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = java.math.BigDecimal.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (INT_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    java.math.BigDecimal res = null;\\n                    long r = resultSet.getLong(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = java.math.BigDecimal.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type Decimal from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForBooleanProperty(ResultSet resultSet,\\n                                                                          int columnIndex,\\n                                                                          int columnType,\\n                                                                          String propertyName)\\n    {\\n        if (BOOL_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    Boolean res = null;\\n                    boolean r = resultSet.getBoolean(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = Boolean.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (STRING_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    Boolean res = null;\\n                    String r = resultSet.getString(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = Boolean.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (INT_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    Boolean res = null;\\n                    long r = resultSet.getLong(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = Boolean.valueOf(r == 1);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type Boolean from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForStrictDateProperty(ResultSet resultSet,\\n                                                                             int columnIndex,\\n                                                                             int columnType,\\n                                                                             String propertyName)\\n    {\\n        if (STRICT_DATE_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    java.sql.Date r = resultSet.getDate(columnIndex);\\n                    if (r != null)\\n                    {\\n                        res = PureDate.fromSQLDate(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (STRING_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    String r = resultSet.getString(columnIndex);\\n                    if (r != null)\\n                    {\\n                        try\\n                        {\\n                            res = PureDate.parsePureDate(r);\\n                        }\\n                        catch (java.lang.IllegalArgumentException dateTimeParseException)\\n                        {\\n                            res = PureDate.fromSQLDate(java.sql.Date.valueOf(r));\\n                        }\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type StrictDate from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForDateTimeProperty(ResultSet resultSet,\\n                                                                           int columnIndex,\\n                                                                           int columnType,\\n                                                                           String propertyName)\\n    {\\n        if (DATE_TIME_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    java.sql.Timestamp r = resultSet.getTimestamp(columnIndex, this.calendar);\\n                    if (r != null)\\n                    {\\n                        res = PureDate.fromSQLTimestamp(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (STRING_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    String r = resultSet.getString(columnIndex);\\n                    if (r != null)\\n                    {\\n                        try\\n                        {\\n                            res = PureDate.parsePureDate(r);\\n                        }\\n                        catch (java.lang.IllegalArgumentException dateTimeParseException)\\n                        {\\n                            res = PureDate.fromSQLTimestamp(java.sql.Timestamp.valueOf(r));\\n                        }\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type DateTime from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForDateProperty(ResultSet resultSet,\\n                                                                       int columnIndex,\\n                                                                       int columnType,\\n                                                                       String propertyName)\\n    {\\n        if (STRICT_DATE_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    java.sql.Date r = resultSet.getDate(columnIndex);\\n                    if (r != null)\\n                    {\\n                        res = PureDate.fromSQLDate(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (DATE_TIME_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    java.sql.Timestamp r = resultSet.getTimestamp(columnIndex, this.calendar);\\n                    if (r != null)\\n                    {\\n                        res = PureDate.fromSQLTimestamp(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (STRING_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    String r = resultSet.getString(columnIndex);\\n                    if (r != null)\\n                    {\\n                        try\\n                        {\\n                            res = PureDate.parsePureDate(r);\\n                        }\\n                        catch (java.lang.IllegalArgumentException dateTimeParseException1)\\n                        {\\n                            try\\n                            {\\n                                res = PureDate.fromSQLTimestamp(java.sql.Timestamp.valueOf(r));\\n                            }\\n                            catch (java.time.format.DateTimeParseException dateTimeParseException2)\\n                            {\\n                                res = PureDate.fromSQLDate(java.sql.Date.valueOf(r));\\n                            }\\n                        }\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type Date from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Object getAlloyNativeValueFromResultSet(ResultSet resultSet,\\n                                                    int columnIndex,\\n                                                    int columnType)\\n    {\\n        try\\n        {\\n            Object result = null;\\n            switch (columnType)\\n            {\\n                case Types.DATE:\\n                {\\n                    java.sql.Date date = resultSet.getDate(columnIndex);\\n                    if (date != null)\\n                    {\\n                        result = PureDate.fromSQLDate(date);\\n                    }\\n                    break;\\n                }\\n                case Types.TIMESTAMP:\\n                {\\n                    java.sql.Timestamp timestamp = resultSet.getTimestamp(columnIndex, this.calendar);\\n                    if (timestamp != null)\\n                    {\\n                        result = PureDate.fromSQLTimestamp(timestamp);\\n                    }\\n                    break;\\n                }\\n                case Types.TINYINT:\\n                case Types.SMALLINT:\\n                case Types.INTEGER:\\n                case Types.BIGINT:\\n                {\\n                    long num = resultSet.getLong(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        result = Long.valueOf(num);\\n                    }\\n                    break;\\n                }\\n                case Types.REAL:\\n                case Types.FLOAT:\\n                case Types.DOUBLE:\\n                {\\n                    double num = resultSet.getDouble(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        result = Double.valueOf(num);\\n                    }\\n                    break;\\n                }\\n                case Types.DECIMAL:\\n                case Types.NUMERIC:\\n                {\\n                    result = resultSet.getBigDecimal(columnIndex);\\n                    break;\\n                }\\n                case Types.CHAR:\\n                case Types.VARCHAR:\\n                case Types.LONGVARCHAR:\\n                case Types.NCHAR:\\n                case Types.NVARCHAR:\\n                case Types.LONGNVARCHAR:\\n                case Types.OTHER:\\n                {\\n                    result = resultSet.getString(columnIndex);\\n                    break;\\n                }\\n                case Types.BIT:\\n                case Types.BOOLEAN:\\n                {\\n                    boolean bool = resultSet.getBoolean(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        result = Boolean.valueOf(bool);\\n                    }\\n                }\\n                case Types.BINARY:\\n                case Types.VARBINARY:\\n                case Types.LONGVARBINARY:\\n                {\\n                    byte[] bytes = resultSet.getBytes(columnIndex);\\n                    if (bytes != null)\\n                    {\\n                        result = this.encodeHex(bytes);\\n                    }\\n                    break;\\n                }\\n                case Types.NULL:\\n                {\\n                    // do nothing: value is already assigned to null\\n                    break;\\n                }\\n                default:\\n                {\\n                    result = resultSet.getObject(columnIndex);\\n                }\\n            }\\n            return result;}\\n        catch (Exception e)\\n        {\\n            throw new RuntimeException(e);\\n        }\\n    }\\n\\n    private String encodeHex(byte[] data)\\n    {\\n        final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};\\n        final int l = data.length;\\n        final char[] out = new char[l << 1];\\n        for (int i = 0, j = 0; i < l; i++)\\n        {\\n            out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];\\n            out[j++] = DIGITS_LOWER[0x0F & data[i]];\\n        }\\n        return new String(out);\\n    }\\n\\n    public Object getObjectFromResultSet(ResultSet resultSet,\\n                                         String databaseTimeZone,\\n                                         String databaseConnection)\\n    {\\n        if (this.propertyIndices == null)\\n        {\\n            this.doSetup(resultSet, databaseTimeZone, databaseConnection);\\n        }\\n        final _pure.plan.root.A_p_A_Impl object = new _pure.plan.root.A_p_A_Impl();\\n        object.setSetId$(\\\"p_A\\\");\\n        int pkColIndex;\\n\\n        int propertyIndex;\\n\\n        propertyIndex = this.propertyIndices.get(0).get(0);\\n        {\\n            String res = (String) this.propertyGetters.get(0).get(0).get();\\n            if (res == null)\\n            {\\n                throw new RuntimeException(\\\"Error reading in property 'name'. Property of multiplicity [1] can not be null\\\");\\n            }\\n            object.setName(res);\\n        }\\n\\n        return object;\\n    }\\n\\n    private void doSetup(ResultSet resultSet, String databaseTimeZone, String databaseConnection)\\n    {\\n        try\\n        {\\n            this.calendar = new GregorianCalendar(TimeZone.getTimeZone(databaseTimeZone));\\n            _pure.plan.root.A_p_A_BaseImpl.databaseConnection$ = databaseConnection;\\n            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();\\n            int columnCount = resultSetMetaData.getColumnCount();\\n            this.columnTypes = new ArrayList<>();\\n            List<String> columnNames = new ArrayList<>();\\n            for (int i = 1; i <= columnCount; i++)\\n            {\\n                String columnLabel = resultSetMetaData.getColumnLabel(i);\\n                columnNames.add(columnLabel.startsWith(\\\"\\\\\\\"\\\") && columnLabel.endsWith(\\\"\\\\\\\"\\\") ? columnLabel.substring(1, columnLabel.length() - 1).toUpperCase() : columnLabel.toUpperCase());\\n                this.columnTypes.add(resultSetMetaData.getColumnType(i));\\n            }\\n\\n            this.propertyIndices = new ArrayList<>();\\n            List<Integer> index_0 = new ArrayList<>();\\n            index_0.add(columnNames.indexOf(\\\"NAME\\\") + 1);\\n            this.propertyIndices.add(index_0);\\n\\n            this.propertyGetters = new ArrayList<>();\\n            int propertyIndex;\\n            Supplier<Object> propertyGetter = null;\\n\\n            List<Supplier<Object>> propertyGetter_0 = new ArrayList<>();\\n\\n            propertyIndex = this.propertyIndices.get(0).get(0);\\n            propertyGetter = this.getResultSetPropertyGetterForStringProperty(resultSet, propertyIndex, resultSetMetaData.getColumnType(propertyIndex), \\\"name\\\");\\n            propertyGetter_0.add(propertyGetter);\\n\\n            this.propertyGetters.add(propertyGetter_0);\\n        }\\n        catch (RuntimeException e)\\n        {\\n            throw e;\\n        }\\n        catch (Exception e)\\n        {\\n            throw new RuntimeException(e);\\n        }\\n    }\\n}\"\n" + "    } ]\n" + "  },\n" + "  \"rootExecutionNode\" : {\n" + "    \"_type\" : \"relationalClassInstantiation\",\n" + "    \"executionNodes\" : [ {\n" + "      \"_type\" : \"sql\",\n" + "      \"connection\" : {\n" + "        \"_type\" : \"RelationalDatabaseConnection\",\n" + "        \"authenticationStrategy\" : {\n" + "          \"_type\" : \"snowflakePublic\",\n" + "          \"passPhraseVaultReference\" : \"val\",\n" + "          \"privateKeyVaultReference\" : \"key\",\n" + "          \"publicUserName\" : \"name\"\n" + "        },\n" + "        \"datasourceSpecification\" : {\n" + "          \"_type\" : \"snowflake\",\n" + "          \"accountName\" : \"account\",\n" + "          \"cloudType\" : \"aws\",\n" + "          \"databaseName\" : \"test\",\n" + "          \"quotedIdentifiersIgnoreCase\" : false,\n" + "          \"region\" : \"us-east2\",\n" + "          \"warehouseName\" : \"warehouseName\"\n" + "        },\n" + "        \"element\" : \"p::db\",\n" + "        \"postProcessorWithParameter\" : [ ],\n" + "        \"postProcessors\" : [ ],\n" + "        \"type\" : \"Snowflake\"\n" + "      },\n" + "      \"executionNodes\" : [ ],\n" + "      \"resultColumns\" : [ {\n" + "        \"dataType\" : \"VARCHAR(200)\",\n" + "        \"label\" : \"\\\"name\\\"\"\n" + "      } ],\n" + "      \"resultType\" : {\n" + "        \"_type\" : \"dataType\",\n" + "        \"dataType\" : \"meta::pure::metamodel::type::Any\"\n" + "      },\n" + "      \"sqlQuery\" : \"select \\\"root\\\".name as \\\"name\\\" from tb as \\\"root\\\"\"\n" + "    } ],\n" + "    \"implementation\" : {\n" + "      \"_type\" : \"java\",\n" + "      \"executionClassFullName\" : \"_pure.plan.root.Execute\"\n" + "    },\n" + "    \"resultSizeRange\" : {\n" + "      \"lowerBound\" : 0\n" + "    },\n" + "    \"resultType\" : {\n" + "      \"_type\" : \"class\",\n" + "      \"class\" : \"p::A\",\n" + "      \"setImplementations\" : [ {\n" + "        \"class\" : \"p::A\",\n" + "        \"id\" : \"p_A\",\n" + "        \"mapping\" : \"p::M\",\n" + "        \"propertyMappings\" : [ {\n" + "          \"enumMapping\" : { },\n" + "          \"property\" : \"name\",\n" + "          \"type\" : \"String\"\n" + "        } ]\n" + "      } ]\n" + "    }\n" + "  },\n" + "  \"serializer\" : {\n" + "    \"name\" : \"pure\",\n" + "    \"version\" : \"vX_X_X\"\n" + "  },\n" + "  \"templateFunctions\" : [ \"<#function renderCollection collection separator prefix suffix defaultValue><#if collection?size == 0><#return defaultValue></#if><#return prefix + collection?join(suffix + separator + prefix) + suffix></#function>\", \"<#function collectionSize collection> <#return collection?size?c> </#function>\" ]\n" + "}", writer.toString());
}
Also used : ExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.ExecutionPlan) StringWriter(java.io.StringWriter) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) CompileContext(org.finos.legend.engine.language.pure.compiler.toPureGraph.CompileContext) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with Database

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Database in project legend-engine by finos.

the class TestGeneration method testGenerationWithBigQueryConnection.

@Test
public void testGenerationWithBigQueryConnection() throws Exception {
    PureModelContextData contextData = PureGrammarParser.newInstance().parseModel("" + "Class p::A {name:String[1];}" + "function p::f():p::A[*]{p::A.all();}\n" + "###Relational\n" + "Database p::db(Table tb (name VARCHAR(200)))\n" + "###Mapping\n" + "Mapping p::M (p::A : Relational {name : [p::db]tb.name })\n" + "###Runtime\n" + "Runtime p::R" + "{" + "   mappings:[p::M];" + "   connections:[" + "       p::db : [" + "           c1: #{\n" + "                   RelationalDatabaseConnection\n" + "                   {\n" + "                       type: BigQuery;\n" + "                       specification: BigQuery\n" + "                                      {\n" + "                                           projectId: 'proj1';\n" + "                                           defaultDataset: 'dataset1';\n" + "                                      };\n" + "                       auth: GCPApplicationDefaultCredentials;\n" + "                   }\n" + "               }#\n" + "           ]" + "   ];" + "}");
    PureModel pureModel = Compiler.compile(contextData, null, null);
    String plan = PlanGenerator.generateExecutionPlanAsString(HelperValueSpecificationBuilder.buildLambda(contextData.getElementsOfType(Function.class).get(0).body, Collections.emptyList(), new CompileContext.Builder(pureModel).build()), pureModel.getMapping("p::M"), pureModel.getRuntime("p::R"), null, pureModel, "vX_X_X", PlanPlatform.JAVA, null, core_relational_relational_router_router_extension.Root_meta_pure_router_extension_defaultRelationalExtensions__RouterExtension_MANY_(pureModel.getExecutionSupport()), LegendPlanTransformers.transformers);
    ObjectMapper mapper = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports().enable(SerializationFeature.INDENT_OUTPUT);
    ExecutionPlan executionPlan = mapper.readValue(plan, ExecutionPlan.class);
    StringWriter writer = new StringWriter();
    mapper.writeValue(writer, executionPlan);
    assertGeneratedStringEquals("{\n" + "  \"_type\" : \"simple\",\n" + "  \"authDependent\" : false,\n" + "  \"globalImplementationSupport\" : {\n" + "    \"_type\" : \"java\",\n" + "    \"classes\" : [ {\n" + "      \"name\" : \"A\",\n" + "      \"package\" : \"_pure.app.p\",\n" + "      \"source\" : \"package _pure.app.p;\\n\\nimport java.math.*;\\nimport java.util.*;\\nimport org.finos.legend.engine.plan.dependencies.domain.date.PureDate;\\n\\npublic interface A extends org.finos.legend.engine.plan.dependencies.store.shared.IReferencedObject\\n{\\n    String getName();\\n    String getAlloyStoreObjectReference$();\\n}\"\n" + "    }, {\n" + "      \"name\" : \"A_p_A_BaseImpl\",\n" + "      \"package\" : \"_pure.plan.root\",\n" + "      \"source\" : \"package _pure.plan.root;\\n\\nimport com.fasterxml.jackson.annotation.JsonIgnore;\\nimport com.fasterxml.jackson.annotation.JsonInclude;\\nimport com.fasterxml.jackson.annotation.JsonProperty;\\nimport com.fasterxml.jackson.core.JsonGenerator;\\nimport com.fasterxml.jackson.databind.JsonSerializer;\\nimport com.fasterxml.jackson.databind.ObjectMapper;\\nimport com.fasterxml.jackson.databind.SerializerProvider;\\nimport com.fasterxml.jackson.databind.module.SimpleModule;\\nimport java.io.IOException;\\nimport java.util.*;\\nimport org.finos.legend.engine.plan.dependencies.domain.date.PureDate;\\n\\npublic class A_p_A_BaseImpl implements _pure.app.p.A\\n{\\n    private String name;\\n    private String setId$;\\n    public static String databaseConnection$;\\n    private static final ObjectMapper objectMapper$ = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL).registerModule(new SimpleModule().addSerializer(PureDate.class, new JsonSerializer<PureDate>() { @Override public void serialize(PureDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeRawValue(\\\"\\\\\\\"\\\" + value.toString() + \\\"\\\\\\\"\\\"); } }));\\n    private String alloyStoreObjectReference$;\\n\\n    @JsonProperty(\\\"name\\\")\\n    public String getName()\\n    {\\n        return this.name;\\n    }\\n\\n    public void setName(String name)\\n    {\\n        this.name = name;\\n    }\\n\\n    @JsonIgnore\\n    public String getSetId$()\\n    {\\n        return this.setId$;\\n    }\\n\\n    public void setSetId$(String setId)\\n    {\\n        this.setId$ = setId;\\n    }\\n\\n    @JsonProperty(\\\"alloyStoreObjectReference$\\\")\\n    public String getAlloyStoreObjectReference$()\\n    {\\n        if (this.alloyStoreObjectReference$ == null)\\n        {\\n            try\\n            {\\n                StringBuilder referenceBuilder = new StringBuilder();\\n                referenceBuilder.append(\\\"001:\\\");\\n                referenceBuilder.append(\\\"010:\\\");\\n\\n                referenceBuilder.append(\\\"0000000010:\\\");\\n                referenceBuilder.append(\\\"Relational:\\\");\\n\\n                referenceBuilder.append(\\\"0000000004:\\\");\\n                referenceBuilder.append(\\\"p::M:\\\");\\n\\n                referenceBuilder.append(\\\"0000000003:\\\");\\n                referenceBuilder.append(\\\"p_A:\\\");\\n\\n                String setId = this.getSetId$();\\n                referenceBuilder.append(String.format(\\\"%010d\\\", setId.length()));\\n                referenceBuilder.append(\\\":\\\");\\n                referenceBuilder.append(setId);\\n                referenceBuilder.append(\\\":\\\");\\n\\n                String databaseConnectionString = _pure.plan.root.A_p_A_BaseImpl.databaseConnection$;\\n                referenceBuilder.append(String.format(\\\"%010d\\\", databaseConnectionString.length()));\\n                referenceBuilder.append(\\\":\\\");\\n                referenceBuilder.append(databaseConnectionString);\\n                referenceBuilder.append(\\\":\\\");\\n\\n                Map<String, Object> pkMap = new HashMap<>();\\n\\n                String pkMapString = objectMapper$.writeValueAsString(pkMap);\\n                referenceBuilder.append(String.format(\\\"%010d\\\", pkMapString.length()));\\n                referenceBuilder.append(\\\":\\\");\\n                referenceBuilder.append(pkMapString);\\n\\n                this.alloyStoreObjectReference$ = \\\"ASOR:\\\" + org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(referenceBuilder.toString().getBytes());\\n            }\\n            catch (Exception e)\\n            {\\n               throw new RuntimeException(e);\\n            }\\n        }\\n\\n        return this.alloyStoreObjectReference$;\\n    }\\n}\"\n" + "    }, {\n" + "      \"name\" : \"A_p_A_Impl\",\n" + "      \"package\" : \"_pure.plan.root\",\n" + "      \"source\" : \"package _pure.plan.root;\\n\\nimport com.fasterxml.jackson.annotation.JsonIgnore;\\nimport com.fasterxml.jackson.annotation.JsonProperty;\\nimport java.util.*;\\n\\npublic class A_p_A_Impl extends A_p_A_BaseImpl implements _pure.app.p.A\\n{\\n    public A_p_A_Impl()\\n    {\\n        this.setSetId$(\\\"p_A\\\");\\n    }\\n}\"\n" + "    }, {\n" + "      \"name\" : \"Execute\",\n" + "      \"package\" : \"_pure.plan.root\",\n" + "      \"source\" : \"package _pure.plan.root;\\n\\nimport java.sql.ResultSet;\\nimport java.util.*;\\nimport org.finos.legend.engine.plan.dependencies.store.relational.classResult.IRelationalClassInstantiationNodeExecutor;\\n\\npublic class Execute implements IRelationalClassInstantiationNodeExecutor\\n{\\n    private Helper helper;\\n\\n    public Execute()\\n    {\\n        this.helper = new Helper();\\n    }\\n\\n    public Object getObjectFromResultSet(ResultSet resultSet,\\n                                         String databaseTimeZone,\\n                                         String databaseConnection)\\n    {\\n        return this.helper.getObjectFromResultSet(resultSet, databaseTimeZone, databaseConnection);\\n    }\\n}\"\n" + "    }, {\n" + "      \"name\" : \"Helper\",\n" + "      \"package\" : \"_pure.plan.root\",\n" + "      \"source\" : \"package _pure.plan.root;\\n\\nimport java.sql.ResultSet;\\nimport java.sql.ResultSetMetaData;\\nimport java.sql.Types;\\nimport java.util.*;\\nimport java.util.function.*;\\nimport java.util.stream.*;\\nimport org.finos.legend.engine.plan.dependencies.domain.date.PureDate;\\n\\npublic class Helper\\n{\\n    private static final List<Integer> STRING_TYPES = Arrays.asList(Types.CHAR, Types.VARCHAR, Types.LONGVARCHAR, Types.NCHAR, Types.NVARCHAR, Types.LONGNVARCHAR, Types.OTHER, Types.NULL);\\n    private static final List<Integer> INT_TYPES = Arrays.asList(Types.TINYINT, Types.SMALLINT, Types.INTEGER, Types.BIGINT, Types.NULL);\\n    private static final List<Integer> FLOAT_TYPES = Arrays.asList(Types.REAL, Types.FLOAT, Types.DOUBLE, Types.DECIMAL, Types.NUMERIC, Types.NULL);\\n    private static final List<Integer> BOOL_TYPES = Arrays.asList(Types.BIT, Types.BOOLEAN, Types.NULL);\\n    private static final List<Integer> STRICT_DATE_TYPES = Arrays.asList(Types.DATE, Types.NULL);\\n    private static final List<Integer> DATE_TIME_TYPES = Arrays.asList(Types.TIMESTAMP, Types.NULL);\\n    private List<Integer> columnTypes;\\n    private List<List<Integer>> propertyIndices;\\n    private List<List<Supplier<Object>>> propertyGetters;\\n    private Calendar calendar;\\n\\n    private Supplier<Object> getResultSetPropertyGetterForStringProperty(ResultSet resultSet,\\n                                                                         int columnIndex,\\n                                                                         int columnType,\\n                                                                         String propertyName)\\n    {\\n        if (STRING_TYPES.contains(columnType))\\n        {\\n           return () -> {\\n                try\\n                {\\n                    return resultSet.getString(columnIndex);\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        else\\n        {\\n            throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type String from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n        }\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForIntegerProperty(ResultSet resultSet,\\n                                                                          int columnIndex,\\n                                                                          int columnType,\\n                                                                          String propertyName)\\n    {\\n        if (INT_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    Long res = null;\\n                    long r = resultSet.getLong(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = Long.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type Integer from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForFloatProperty(ResultSet resultSet,\\n                                                                        int columnIndex,\\n                                                                        int columnType,\\n                                                                        String propertyName)\\n    {\\n        if (FLOAT_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    Double res = null;\\n                    double r = resultSet.getDouble(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = Double.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (INT_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    Double res = null;\\n                    long r = resultSet.getLong(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = Double.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type Float from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForDecimalProperty(ResultSet resultSet,\\n                                                                          int columnIndex,\\n                                                                          int columnType,\\n                                                                          String propertyName)\\n    {\\n        if (FLOAT_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    java.math.BigDecimal res = null;\\n                    double r = resultSet.getDouble(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = java.math.BigDecimal.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (INT_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    java.math.BigDecimal res = null;\\n                    long r = resultSet.getLong(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = java.math.BigDecimal.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type Decimal from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForBooleanProperty(ResultSet resultSet,\\n                                                                          int columnIndex,\\n                                                                          int columnType,\\n                                                                          String propertyName)\\n    {\\n        if (BOOL_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    Boolean res = null;\\n                    boolean r = resultSet.getBoolean(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = Boolean.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (STRING_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    Boolean res = null;\\n                    String r = resultSet.getString(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = Boolean.valueOf(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (INT_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    Boolean res = null;\\n                    long r = resultSet.getLong(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        res = Boolean.valueOf(r == 1);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type Boolean from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForStrictDateProperty(ResultSet resultSet,\\n                                                                             int columnIndex,\\n                                                                             int columnType,\\n                                                                             String propertyName)\\n    {\\n        if (STRICT_DATE_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    java.sql.Date r = resultSet.getDate(columnIndex);\\n                    if (r != null)\\n                    {\\n                        res = PureDate.fromSQLDate(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (STRING_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    String r = resultSet.getString(columnIndex);\\n                    if (r != null)\\n                    {\\n                        try\\n                        {\\n                            res = PureDate.parsePureDate(r);\\n                        }\\n                        catch (java.lang.IllegalArgumentException dateTimeParseException)\\n                        {\\n                            res = PureDate.fromSQLDate(java.sql.Date.valueOf(r));\\n                        }\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type StrictDate from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForDateTimeProperty(ResultSet resultSet,\\n                                                                           int columnIndex,\\n                                                                           int columnType,\\n                                                                           String propertyName)\\n    {\\n        if (DATE_TIME_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    java.sql.Timestamp r = resultSet.getTimestamp(columnIndex, this.calendar);\\n                    if (r != null)\\n                    {\\n                        res = PureDate.fromSQLTimestamp(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (STRING_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    String r = resultSet.getString(columnIndex);\\n                    if (r != null)\\n                    {\\n                        try\\n                        {\\n                            res = PureDate.parsePureDate(r);\\n                        }\\n                        catch (java.lang.IllegalArgumentException dateTimeParseException)\\n                        {\\n                            res = PureDate.fromSQLTimestamp(java.sql.Timestamp.valueOf(r));\\n                        }\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type DateTime from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Supplier<Object> getResultSetPropertyGetterForDateProperty(ResultSet resultSet,\\n                                                                       int columnIndex,\\n                                                                       int columnType,\\n                                                                       String propertyName)\\n    {\\n        if (STRICT_DATE_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    java.sql.Date r = resultSet.getDate(columnIndex);\\n                    if (r != null)\\n                    {\\n                        res = PureDate.fromSQLDate(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (DATE_TIME_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    java.sql.Timestamp r = resultSet.getTimestamp(columnIndex, this.calendar);\\n                    if (r != null)\\n                    {\\n                        res = PureDate.fromSQLTimestamp(r);\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        if (STRING_TYPES.contains(columnType))\\n        {\\n            return () -> {\\n                try\\n                {\\n                    PureDate res = null;\\n                    String r = resultSet.getString(columnIndex);\\n                    if (r != null)\\n                    {\\n                        try\\n                        {\\n                            res = PureDate.parsePureDate(r);\\n                        }\\n                        catch (java.lang.IllegalArgumentException dateTimeParseException1)\\n                        {\\n                            try\\n                            {\\n                                res = PureDate.fromSQLTimestamp(java.sql.Timestamp.valueOf(r));\\n                            }\\n                            catch (java.time.format.DateTimeParseException dateTimeParseException2)\\n                            {\\n                                res = PureDate.fromSQLDate(java.sql.Date.valueOf(r));\\n                            }\\n                        }\\n                    }\\n                    return res;\\n                }\\n                catch (Exception e)\\n                {\\n                    throw new RuntimeException(e);\\n                }\\n            };\\n        }\\n        throw new RuntimeException(\\\"Error reading in property '\\\" + propertyName + \\\"' of type Date from SQL column of type '\\\" + columnType + \\\"'.\\\");\\n    }\\n\\n    private Object getAlloyNativeValueFromResultSet(ResultSet resultSet,\\n                                                    int columnIndex,\\n                                                    int columnType)\\n    {\\n        try\\n        {\\n            Object result = null;\\n            switch (columnType)\\n            {\\n                case Types.DATE:\\n                {\\n                    java.sql.Date date = resultSet.getDate(columnIndex);\\n                    if (date != null)\\n                    {\\n                        result = PureDate.fromSQLDate(date);\\n                    }\\n                    break;\\n                }\\n                case Types.TIMESTAMP:\\n                {\\n                    java.sql.Timestamp timestamp = resultSet.getTimestamp(columnIndex, this.calendar);\\n                    if (timestamp != null)\\n                    {\\n                        result = PureDate.fromSQLTimestamp(timestamp);\\n                    }\\n                    break;\\n                }\\n                case Types.TINYINT:\\n                case Types.SMALLINT:\\n                case Types.INTEGER:\\n                case Types.BIGINT:\\n                {\\n                    long num = resultSet.getLong(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        result = Long.valueOf(num);\\n                    }\\n                    break;\\n                }\\n                case Types.REAL:\\n                case Types.FLOAT:\\n                case Types.DOUBLE:\\n                {\\n                    double num = resultSet.getDouble(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        result = Double.valueOf(num);\\n                    }\\n                    break;\\n                }\\n                case Types.DECIMAL:\\n                case Types.NUMERIC:\\n                {\\n                    result = resultSet.getBigDecimal(columnIndex);\\n                    break;\\n                }\\n                case Types.CHAR:\\n                case Types.VARCHAR:\\n                case Types.LONGVARCHAR:\\n                case Types.NCHAR:\\n                case Types.NVARCHAR:\\n                case Types.LONGNVARCHAR:\\n                case Types.OTHER:\\n                {\\n                    result = resultSet.getString(columnIndex);\\n                    break;\\n                }\\n                case Types.BIT:\\n                case Types.BOOLEAN:\\n                {\\n                    boolean bool = resultSet.getBoolean(columnIndex);\\n                    if (!resultSet.wasNull())\\n                    {\\n                        result = Boolean.valueOf(bool);\\n                    }\\n                }\\n                case Types.BINARY:\\n                case Types.VARBINARY:\\n                case Types.LONGVARBINARY:\\n                {\\n                    byte[] bytes = resultSet.getBytes(columnIndex);\\n                    if (bytes != null)\\n                    {\\n                        result = this.encodeHex(bytes);\\n                    }\\n                    break;\\n                }\\n                case Types.NULL:\\n                {\\n                    // do nothing: value is already assigned to null\\n                    break;\\n                }\\n                default:\\n                {\\n                    result = resultSet.getObject(columnIndex);\\n                }\\n            }\\n            return result;}\\n        catch (Exception e)\\n        {\\n            throw new RuntimeException(e);\\n        }\\n    }\\n\\n    private String encodeHex(byte[] data)\\n    {\\n        final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};\\n        final int l = data.length;\\n        final char[] out = new char[l << 1];\\n        for (int i = 0, j = 0; i < l; i++)\\n        {\\n            out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];\\n            out[j++] = DIGITS_LOWER[0x0F & data[i]];\\n        }\\n        return new String(out);\\n    }\\n\\n    public Object getObjectFromResultSet(ResultSet resultSet,\\n                                         String databaseTimeZone,\\n                                         String databaseConnection)\\n    {\\n        if (this.propertyIndices == null)\\n        {\\n            this.doSetup(resultSet, databaseTimeZone, databaseConnection);\\n        }\\n        final _pure.plan.root.A_p_A_Impl object = new _pure.plan.root.A_p_A_Impl();\\n        object.setSetId$(\\\"p_A\\\");\\n        int pkColIndex;\\n\\n        int propertyIndex;\\n\\n        propertyIndex = this.propertyIndices.get(0).get(0);\\n        {\\n            String res = (String) this.propertyGetters.get(0).get(0).get();\\n            if (res == null)\\n            {\\n                throw new RuntimeException(\\\"Error reading in property 'name'. Property of multiplicity [1] can not be null\\\");\\n            }\\n            object.setName(res);\\n        }\\n\\n        return object;\\n    }\\n\\n    private void doSetup(ResultSet resultSet, String databaseTimeZone, String databaseConnection)\\n    {\\n        try\\n        {\\n            this.calendar = new GregorianCalendar(TimeZone.getTimeZone(databaseTimeZone));\\n            _pure.plan.root.A_p_A_BaseImpl.databaseConnection$ = databaseConnection;\\n            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();\\n            int columnCount = resultSetMetaData.getColumnCount();\\n            this.columnTypes = new ArrayList<>();\\n            List<String> columnNames = new ArrayList<>();\\n            for (int i = 1; i <= columnCount; i++)\\n            {\\n                String columnLabel = resultSetMetaData.getColumnLabel(i);\\n                columnNames.add(columnLabel.startsWith(\\\"\\\\\\\"\\\") && columnLabel.endsWith(\\\"\\\\\\\"\\\") ? columnLabel.substring(1, columnLabel.length() - 1).toUpperCase() : columnLabel.toUpperCase());\\n                this.columnTypes.add(resultSetMetaData.getColumnType(i));\\n            }\\n\\n            this.propertyIndices = new ArrayList<>();\\n            List<Integer> index_0 = new ArrayList<>();\\n            index_0.add(columnNames.indexOf(\\\"NAME\\\") + 1);\\n            this.propertyIndices.add(index_0);\\n\\n            this.propertyGetters = new ArrayList<>();\\n            int propertyIndex;\\n            Supplier<Object> propertyGetter = null;\\n\\n            List<Supplier<Object>> propertyGetter_0 = new ArrayList<>();\\n\\n            propertyIndex = this.propertyIndices.get(0).get(0);\\n            propertyGetter = this.getResultSetPropertyGetterForStringProperty(resultSet, propertyIndex, resultSetMetaData.getColumnType(propertyIndex), \\\"name\\\");\\n            propertyGetter_0.add(propertyGetter);\\n\\n            this.propertyGetters.add(propertyGetter_0);\\n        }\\n        catch (RuntimeException e)\\n        {\\n            throw e;\\n        }\\n        catch (Exception e)\\n        {\\n            throw new RuntimeException(e);\\n        }\\n    }\\n}\"\n" + "    } ]\n" + "  },\n" + "  \"rootExecutionNode\" : {\n" + "    \"_type\" : \"relationalClassInstantiation\",\n" + "    \"executionNodes\" : [ {\n" + "      \"_type\" : \"sql\",\n" + "      \"connection\" : {\n" + "        \"_type\" : \"RelationalDatabaseConnection\",\n" + "        \"authenticationStrategy\" : {\n" + "          \"_type\" : \"gcpApplicationDefaultCredentials\"\n" + "        },\n" + "        \"datasourceSpecification\" : {\n" + "          \"_type\" : \"bigQuery\",\n" + "          \"defaultDataset\" : \"dataset1\",\n" + "          \"projectId\" : \"proj1\"\n" + "        },\n" + "        \"element\" : \"p::db\",\n" + "        \"postProcessorWithParameter\" : [ ],\n" + "        \"postProcessors\" : [ ],\n" + "        \"type\" : \"BigQuery\"\n" + "      },\n" + "      \"executionNodes\" : [ ],\n" + "      \"resultColumns\" : [ {\n" + "        \"dataType\" : \"VARCHAR(200)\",\n" + "        \"label\" : \"\\\"name\\\"\"\n" + "      } ],\n" + "      \"resultType\" : {\n" + "        \"_type\" : \"dataType\",\n" + "        \"dataType\" : \"meta::pure::metamodel::type::Any\"\n" + "      },\n" + "      \"sqlQuery\" : \"select `root`.name as `name` from tb as `root`\"\n" + "    } ],\n" + "    \"implementation\" : {\n" + "      \"_type\" : \"java\",\n" + "      \"executionClassFullName\" : \"_pure.plan.root.Execute\"\n" + "    },\n" + "    \"resultSizeRange\" : {\n" + "      \"lowerBound\" : 0\n" + "    },\n" + "    \"resultType\" : {\n" + "      \"_type\" : \"class\",\n" + "      \"class\" : \"p::A\",\n" + "      \"setImplementations\" : [ {\n" + "        \"class\" : \"p::A\",\n" + "        \"id\" : \"p_A\",\n" + "        \"mapping\" : \"p::M\",\n" + "        \"propertyMappings\" : [ {\n" + "          \"enumMapping\" : { },\n" + "          \"property\" : \"name\",\n" + "          \"type\" : \"String\"\n" + "        } ]\n" + "      } ]\n" + "    }\n" + "  },\n" + "  \"serializer\" : {\n" + "    \"name\" : \"pure\",\n" + "    \"version\" : \"vX_X_X\"\n" + "  },\n" + "  \"templateFunctions\" : [ \"<#function renderCollection collection separator prefix suffix defaultValue><#if collection?size == 0><#return defaultValue></#if><#return prefix + collection?join(suffix + separator + prefix) + suffix></#function>\", \"<#function collectionSize collection> <#return collection?size?c> </#function>\" ]\n" + "}", writer.toString());
}
Also used : ExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.ExecutionPlan) StringWriter(java.io.StringWriter) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) CompileContext(org.finos.legend.engine.language.pure.compiler.toPureGraph.CompileContext) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with Database

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Database in project legend-engine by finos.

the class ServiceTestRunner method executeTestAsserts.

@Prometheus(name = "service test execute", doc = "Execution duration summary within service test execution")
private RichServiceTestResult executeTestAsserts(SingleExecutionPlan executionPlan, List<TestContainer> asserts, RichIterable<? extends String> sqlStatements, Scope scope) throws IOException {
    long start = System.currentTimeMillis();
    if (ExecutionNodeTDSResultHelper.isResultTDS(executionPlan.rootExecutionNode) || (executionPlan.rootExecutionNode.isResultPrimitiveType() && "String".equals(executionPlan.rootExecutionNode.getDataTypeResultType()))) {
        // Java
        String packageName = "org.finos.legend.tests.generated";
        String className = "TestSuite";
        String javaCode = ServiceTestGenerationHelper.generateJavaForAsserts(asserts, this.service, this.pureModel, packageName, className);
        Class<?> assertsClass;
        RichServiceTestResult testRun;
        try {
            assertsClass = compileJavaForAsserts(packageName, className, javaCode);
        } catch (JavaCompileException e) {
            MetricsHandler.incrementErrorCount("test_execute", 0);
            throw new RuntimeException("Error compiling test asserts for " + this.service.getPath(), e);
        }
        scope.span().log("Java asserts generated and compiled");
        TestExecutionScope execScope = null;
        try {
            // Setup test database if needed
            if (sqlStatements != null) {
                execScope = TestExecutionScope.setupTestServer(sqlStatements, scope);
            }
            // Run tests
            Map<String, TestResult> results = Maps.mutable.empty();
            Map<String, Exception> assertExceptions = Maps.mutable.empty();
            for (Pair<TestContainer, Integer> tc : LazyIterate.zipWithIndex(asserts)) {
                // Build Param Map
                Map<String, Result> parameters = Maps.mutable.empty();
                if (this.service.execution instanceof PureExecution) {
                    parameters = ListIterate.zip(((PureExecution) this.service.execution).func.parameters, tc.getOne().parametersValues).toMap(p -> p.getOne().name, p -> // Condition evoked in case of studio-flow
                    p.getTwo() instanceof Collection ? new ConstantResult(ListIterate.collect(((Collection) p.getTwo()).values, v -> v.accept(new ValueSpecificationToResultVisitor()).getValue())) : // Condition evoked in case of pureIDE-flow
                    p.getTwo() instanceof PureList ? new ConstantResult(ListIterate.collect(((PureList) p.getTwo()).values, v -> v.accept(new ValueSpecificationToResultVisitor()).getValue())) : p.getTwo().accept(new ValueSpecificationToResultVisitor()));
                }
                // Execute Plan
                ExecutionState testExecutionState = new ExecutionState(parameters, Lists.mutable.withAll(executionPlan.templateFunctions), Lists.mutable.with(new RelationalStoreExecutionState(new RelationalStoreState(execScope == null ? -1 : execScope.getPort())), new InMemoryStoreExecutionState(new InMemoryStoreState()), new ServiceStoreExecutionState(new ServiceStoreState())));
                Result result = this.executor.execute(executionPlan, testExecutionState, null, null);
                org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Result<Object> pureResult = result.accept(new ResultToPureResultVisitor());
                // Execute Assert
                String testName = ServiceTestGenerationHelper.getAssertMethodName(tc.getTwo());
                scope.span().setTag(testName, resultToString(pureResult, this.pureModel.getExecutionSupport()));
                TestResult testResult;
                try {
                    Boolean assertResult = (Boolean) assertsClass.getMethod(testName, org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Result.class, ExecutionSupport.class).invoke(null, pureResult, pureModel.getExecutionSupport());
                    testResult = assertResult ? TestResult.SUCCESS : TestResult.FAILURE;
                    scope.span().setTag(testName + "_assert", assertResult);
                } catch (Exception e) {
                    StringWriter out = new StringWriter();
                    PrintWriter writer = new PrintWriter(out);
                    e.printStackTrace(writer);
                    e.printStackTrace();
                    testResult = TestResult.ERROR;
                    assertExceptions.put(testName, e);
                    scope.span().setTag(testName + "_assert", out.toString());
                }
                results.put(testName, testResult);
            }
            testRun = new RichServiceTestResult(service.getPath(), results, assertExceptions, null, executionPlan, javaCode);
            scope.span().log("Finished running tests " + results);
            MetricsHandler.observeServerOperation("test_execute", metricsContext, start, System.currentTimeMillis());
        } catch (Exception e) {
            LOGGER.error("Error running tests", e);
            MetricsHandler.incrementErrorCount("test_execute", 0);
            throw (e instanceof RuntimeException) ? (RuntimeException) e : new RuntimeException(e);
        } finally {
            if (execScope != null) {
                execScope.close();
            }
            MetricsHandler.observe("service test execute", start, System.currentTimeMillis());
        }
        return testRun;
    } else {
        return new RichServiceTestResult(this.service.getPath(), Collections.emptyMap(), Collections.emptyMap(), null, executionPlan, "");
    }
}
Also used : ServiceStoreState(org.finos.legend.engine.plan.execution.stores.service.plugin.ServiceStoreState) KeyedSingleExecutionTest(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.KeyedSingleExecutionTest) MultiExecutionTest(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.MultiExecutionTest) ExecutionNodeTDSResultHelper(org.finos.legend.engine.plan.execution.nodes.helpers.ExecutionNodeTDSResultHelper) LoggerFactory(org.slf4j.LoggerFactory) TestResult(org.finos.legend.engine.test.runner.shared.TestResult) Runtime(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.runtime.Runtime) Root_meta_legend_service_metamodel_Service(org.finos.legend.pure.generated.Root_meta_legend_service_metamodel_Service) MutableList(org.eclipse.collections.api.list.MutableList) Maps(org.eclipse.collections.api.factory.Maps) EngineRuntime(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.runtime.EngineRuntime) RichIterable(org.eclipse.collections.api.RichIterable) PlanExecutor(org.finos.legend.engine.plan.execution.PlanExecutor) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) Map(java.util.Map) ConstantResult(org.finos.legend.engine.plan.execution.result.ConstantResult) LegacyRuntime(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.runtime.LegacyRuntime) Root_meta_legend_service_metamodel_SingleExecutionTest(org.finos.legend.pure.generated.Root_meta_legend_service_metamodel_SingleExecutionTest) ServiceStoreExecutionState(org.finos.legend.engine.plan.execution.stores.service.plugin.ServiceStoreExecutionState) PlanTransformer(org.finos.legend.engine.plan.generation.transformers.PlanTransformer) PrintWriter(java.io.PrintWriter) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) ServiceExecutionExtension(org.finos.legend.engine.language.pure.dsl.service.generation.extension.ServiceExecutionExtension) Prometheus(org.finos.legend.engine.shared.core.operational.prometheus.Prometheus) SingleExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan) ServiceLoader(java.util.ServiceLoader) Iterate(org.eclipse.collections.impl.utility.Iterate) InMemoryStoreExecutionState(org.finos.legend.engine.plan.execution.stores.inMemory.plugin.InMemoryStoreExecutionState) Root_meta_pure_router_extension_RouterExtension(org.finos.legend.pure.generated.Root_meta_pure_router_extension_RouterExtension) Objects(java.util.Objects) ServiceTest(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.ServiceTest) RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) List(java.util.List) JavaCompileException(org.finos.legend.engine.shared.javaCompiler.JavaCompileException) PackageableElement(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement) SingleExecutionTest(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.SingleExecutionTest) Service(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Service) ObjectMapperFactory(org.finos.legend.engine.shared.core.ObjectMapperFactory) TestContainer(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.TestContainer) Optional(java.util.Optional) Scope(io.opentracing.Scope) PureList(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.PureList) InMemoryStoreState(org.finos.legend.engine.plan.execution.stores.inMemory.plugin.InMemoryStoreState) Assert(org.finos.legend.engine.shared.core.operational.Assert) ExecutionState(org.finos.legend.engine.plan.execution.nodes.state.ExecutionState) KeyedExecutionParameter(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.KeyedExecutionParameter) Lists(org.eclipse.collections.api.factory.Lists) TestExecutionScope(org.finos.legend.engine.plan.execution.stores.relational.TestExecutionScope) PlanPlatform(org.finos.legend.engine.plan.platform.PlanPlatform) MutableMap(org.eclipse.collections.api.map.MutableMap) ServicePlanGenerator(org.finos.legend.engine.language.pure.dsl.service.generation.ServicePlanGenerator) JavaHelper(org.finos.legend.engine.plan.execution.nodes.helpers.platform.JavaHelper) Pair(org.eclipse.collections.api.tuple.Pair) MetricsHandler(org.finos.legend.engine.shared.core.operational.prometheus.MetricsHandler) Execution(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Execution) Result(org.finos.legend.engine.plan.execution.result.Result) RelationalStoreExecutionState(org.finos.legend.engine.plan.execution.stores.relational.plugin.RelationalStoreExecutionState) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EngineJavaCompiler(org.finos.legend.engine.shared.javaCompiler.EngineJavaCompiler) GlobalTracer(io.opentracing.util.GlobalTracer) IOException(java.io.IOException) RelationalStoreState(org.finos.legend.engine.plan.execution.stores.relational.plugin.RelationalStoreState) ListIterate(org.eclipse.collections.impl.utility.ListIterate) Collection(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Collection) PureMultiExecution(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.PureMultiExecution) ExecutionSupport(org.finos.legend.pure.m3.execution.ExecutionSupport) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) PureSingleExecution(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.PureSingleExecution) ExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.ExecutionPlan) LocalH2DatasourceSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.specification.LocalH2DatasourceSpecification) PureExecution(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.PureExecution) Class(java.lang.Class) StringJavaSource(org.finos.legend.engine.shared.javaCompiler.StringJavaSource) Collections(java.util.Collections) org.finos.legend.pure.generated.core_relational_relational_helperFunctions_helperFunctions(org.finos.legend.pure.generated.core_relational_relational_helperFunctions_helperFunctions) ServiceStoreExecutionState(org.finos.legend.engine.plan.execution.stores.service.plugin.ServiceStoreExecutionState) InMemoryStoreExecutionState(org.finos.legend.engine.plan.execution.stores.inMemory.plugin.InMemoryStoreExecutionState) ExecutionState(org.finos.legend.engine.plan.execution.nodes.state.ExecutionState) RelationalStoreExecutionState(org.finos.legend.engine.plan.execution.stores.relational.plugin.RelationalStoreExecutionState) RelationalStoreExecutionState(org.finos.legend.engine.plan.execution.stores.relational.plugin.RelationalStoreExecutionState) InMemoryStoreExecutionState(org.finos.legend.engine.plan.execution.stores.inMemory.plugin.InMemoryStoreExecutionState) PureList(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.PureList) TestResult(org.finos.legend.engine.test.runner.shared.TestResult) ConstantResult(org.finos.legend.engine.plan.execution.result.ConstantResult) Result(org.finos.legend.engine.plan.execution.result.Result) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) InMemoryStoreState(org.finos.legend.engine.plan.execution.stores.inMemory.plugin.InMemoryStoreState) TestContainer(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.TestContainer) TestExecutionScope(org.finos.legend.engine.plan.execution.stores.relational.TestExecutionScope) ConstantResult(org.finos.legend.engine.plan.execution.result.ConstantResult) JavaCompileException(org.finos.legend.engine.shared.javaCompiler.JavaCompileException) TestResult(org.finos.legend.engine.test.runner.shared.TestResult) JavaCompileException(org.finos.legend.engine.shared.javaCompiler.JavaCompileException) IOException(java.io.IOException) ServiceStoreExecutionState(org.finos.legend.engine.plan.execution.stores.service.plugin.ServiceStoreExecutionState) ServiceStoreState(org.finos.legend.engine.plan.execution.stores.service.plugin.ServiceStoreState) Collection(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Collection) RelationalStoreState(org.finos.legend.engine.plan.execution.stores.relational.plugin.RelationalStoreState) PureExecution(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.PureExecution) Prometheus(org.finos.legend.engine.shared.core.operational.prometheus.Prometheus)

Example 4 with Database

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Database in project legend-engine by finos.

the class TestRuntimeGenerationForServiceTests method testRuntimeGenerationForModelChainConnection.

@Test
public void testRuntimeGenerationForModelChainConnection() {
    String pureGrammarWithModelChainConnection = "###Relational\n" + "Database demo::modelChainConnection::relationalDB\n" + "(\n" + "  Table Person\n" + "  (\n" + "    fullname VARCHAR(1000) PRIMARY KEY,\n" + "    firm VARCHAR(200)\n" + "  )\n" + ")\n" + "\n" + "\n" + "###Service\n" + "Service demo::modelChainConnection::testModelChainConnectionService\n" + "{\n" + "  pattern: '/maheha/testModelChainConnection/fromStudio/';\n" + "  owners:\n" + "  [\n" + "    'maheha'\n" + "  ];\n" + "  documentation: '';\n" + "  autoActivateUpdates: false;\n" + "  execution: Single\n" + "  {\n" + "    query: |demo::modelChainConnection::dest::Person.all()->project([f|$f.firstName, f|$f.lastName], ['firstName', 'lastName']);\n" + "    mapping: demo::modelChainConnection::simpleModelMappingWithAssociation;\n" + "    runtime:\n" + "    #{\n" + "      mappings:\n" + "      [\n" + "        demo::modelChainConnection::simpleModelMappingWithAssociation\n" + "      ];\n" + "      connections:\n" + "      [\n" + "        ModelStore:\n" + "        [\n" + "          connection_1: demo::modelChainConnection::modelChainConnection\n" + "        ],\n" + "        demo::modelChainConnection::relationalDB:\n" + "        [\n" + "          connection_2: demo::modelChainConnection::mySimpleConnection\n" + "        ]\n" + "      ];\n" + "    }#;\n" + "  }\n" + "  test: Single\n" + "  {\n" + "    data: 'default\\nPerson\\nfullname,firm\\nPierre DeBelen,A\\nA. Only One,A\\n\\n\\n\\n';\n" + "    asserts:\n" + "    [\n" + "    ];\n" + "  }\n" + "}\n" + "\n" + "\n" + "###Pure\n" + "Class demo::modelChainConnection::dest::Address\n" + "{\n" + "  street: String[0..1];\n" + "  extension: demo::modelChainConnection::dest::AddressExtension[0..1];\n" + "}\n" + "\n" + "Class demo::modelChainConnection::dest::AddressExtension\n" + "{\n" + "  stuff: String[1];\n" + "}\n" + "\n" + "Class demo::modelChainConnection::dest::Firm\n" + "{\n" + "  legalName: String[1];\n" + "  employees: demo::modelChainConnection::dest::Person[*];\n" + "  addresses: demo::modelChainConnection::dest::Address[*];\n" + "  count: Integer[1];\n" + "}\n" + "\n" + "Class demo::modelChainConnection::dest::Person\n" + "{\n" + "  firstName: String[1];\n" + "  lastName: String[1];\n" + "  addresses: demo::modelChainConnection::dest::Address[*];\n" + "  firm: demo::modelChainConnection::dest::Firm[0..1];\n" + "  description: String[0..1];\n" + "  type: String[0..1];\n" + "}\n" + "\n" + "Class demo::modelChainConnection::src::_Person\n" + "{\n" + "  fullName: String[1];\n" + "  addresses: demo::modelChainConnection::dest::Address[*];\n" + "}\n" + "\n" + "\n" + "###Mapping\n" + "Mapping demo::modelChainConnection::relationalMapping\n" + "(\n" + "  demo::modelChainConnection::src::_Person: Relational\n" + "  {\n" + "    ~primaryKey\n" + "    (\n" + "      [demo::modelChainConnection::relationalDB]Person.fullname\n" + "    )\n" + "    ~mainTable [demo::modelChainConnection::relationalDB]Person\n" + "    fullName: [demo::modelChainConnection::relationalDB]Person.fullname\n" + "  }\n" + ")\n" + "\n" + "Mapping demo::modelChainConnection::simpleModelMappingWithAssociation\n" + "(\n" + "  demo::modelChainConnection::dest::Person: Pure\n" + "  {\n" + "    ~src demo::modelChainConnection::src::_Person\n" + "    firstName: $src.fullName->substring(0, $src.fullName->indexOf(' ')),\n" + "    lastName: $src.fullName->substring($src.fullName->indexOf(' ') + 1, $src.fullName->length())\n" + "  }\n" + ")\n" + "\n" + "\n" + "###Connection\n" + "ModelChainConnection demo::modelChainConnection::modelChainConnection\n" + "{\n" + "  mappings: [\n" + "    demo::modelChainConnection::relationalMapping\n" + "  ];\n" + "}\n" + "\n" + "RelationalDatabaseConnection demo::modelChainConnection::mySimpleConnection\n" + "{\n" + "  store: demo::modelChainConnection::relationalDB;\n" + "  type: H2;\n" + "  specification: LocalH2\n" + "  {\n" + "    testDataSetupCSV: '';\n" + "  };\n" + "  auth: DefaultH2;\n" + "}\n" + "\n" + "\n" + "###Runtime\n" + "Runtime demo::modelChainConnection\n" + "{\n" + "  mappings:\n" + "  [\n" + "    demo::modelChainConnection::simpleModelMappingWithAssociation\n" + "  ];\n" + "  connections:\n" + "  [\n" + "    ModelStore:\n" + "    [\n" + "      connection_1: demo::modelChainConnection::modelChainConnection\n" + "    ],\n" + "    demo::modelChainConnection::relationalDB:\n" + "    [\n" + "      connection_2: demo::modelChainConnection::mySimpleConnection\n" + "    ]\n" + "  ];\n" + "}\n";
    PureModelContextData contextData = PureGrammarParser.newInstance().parseModel(pureGrammarWithModelChainConnection);
    PureModel pureModel = new PureModel(contextData, null, DeploymentMode.TEST);
    Service service = contextData.getElementsOfType(Service.class).get(0);
    EngineRuntime testRuntime = (EngineRuntime) ServiceTestGenerationHelper.buildSingleExecutionTestRuntime((PureSingleExecution) service.execution, (SingleExecutionTest) service.test, contextData, pureModel);
    Assert.assertEquals(testRuntime.connections.size(), 2);
    Assert.assertNotNull(testRuntime.getStoreConnections("ModelStore"));
    Assert.assertEquals(testRuntime.getStoreConnections("ModelStore").storeConnections.size(), 1);
    Assert.assertFalse((testRuntime.getStoreConnections("ModelStore").storeConnections.get(0).connection instanceof RelationalDatabaseConnection));
}
Also used : PureSingleExecution(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.PureSingleExecution) SingleExecutionTest(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.SingleExecutionTest) RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) EngineRuntime(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.runtime.EngineRuntime) Service(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Service) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) SingleExecutionTest(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.SingleExecutionTest) Test(org.junit.Test)

Example 5 with Database

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Database in project legend-engine by finos.

the class TestRuntimeGenerationForServiceTests method testRuntimeGenerationForServiceWithRuntimePointer.

@Test
public void testRuntimeGenerationForServiceWithRuntimePointer() {
    String pureGrammarWithModelChainConnection = "###Relational\n" + "Database test::relationalDB\n" + "(\n" + "  Table Person\n" + "  (\n" + "    fullname VARCHAR(1000) PRIMARY KEY\n" + "  )\n" + ")\n" + "\n" + "\n" + "###Service\n" + "Service test::serviceWithRuntimePointer\n" + "{\n" + "  pattern: '/garprat/test/fromStudio/';\n" + "  owners:\n" + "  [\n" + "    'garprat'\n" + "  ];\n" + "  documentation: '';\n" + "  autoActivateUpdates: false;\n" + "  execution: Single\n" + "  {\n" + "    query: |test::Person.all()->project([f|$f.fullName], ['fullName']);\n" + "    mapping: test::relationalMapping;\n" + "    runtime: test::runtimePointer;\n" + "  }\n" + "  test: Single\n" + "  {\n" + "    data: 'default\\nPerson\\nfullname\\nPierre DeBelen\\n';\n" + "    asserts:\n" + "    [\n" + "    ];\n" + "  }\n" + "}\n" + "\n" + "\n" + "###Pure\n" + "Class test::Person\n" + "{\n" + "  fullName: String[1];\n" + "}\n" + "\n" + "\n" + "###Mapping\n" + "Mapping test::relationalMapping\n" + "(\n" + "  test::Person: Relational\n" + "  {\n" + "    ~primaryKey\n" + "    (\n" + "      [test::relationalDB]Person.fullname\n" + "    )\n" + "    ~mainTable [test::relationalDB]Person\n" + "    fullName: [test::relationalDB]Person.fullname\n" + "  }\n" + ")\n" + "\n" + "###Connection\n" + "RelationalDatabaseConnection test::mySimpleConnection\n" + "{\n" + "  store: test::relationalDB;\n" + "  type: MemSQL;\n" + "  specification: Static\n" + "  {\n" + "    name: 'memsql_person_data';\n" + "    host: 'myserver_url';\n" + "    port: 80;\n" + "  };\n" + "  auth: DefaultH2;\n" + "}\n" + "\n" + "\n" + "###Runtime\n" + "Runtime test::runtimePointer\n" + "{\n" + "  mappings:\n" + "  [\n" + "     test::relationalMapping\n" + "  ];\n" + "  connections:\n" + "  [\n" + "    test::relationalDB:\n" + "    [\n" + "      connection_1: test::mySimpleConnection\n" + "    ]\n" + "  ];\n" + "}\n";
    PureModelContextData contextData = PureGrammarParser.newInstance().parseModel(pureGrammarWithModelChainConnection);
    PureModel pureModel = new PureModel(contextData, null, DeploymentMode.TEST);
    Service service = contextData.getElementsOfType(Service.class).get(0);
    EngineRuntime testRuntime = (EngineRuntime) ServiceTestGenerationHelper.buildSingleExecutionTestRuntime((PureSingleExecution) service.execution, (SingleExecutionTest) service.test, contextData, pureModel);
    Assert.assertEquals(testRuntime.connections.size(), 1);
}
Also used : PureSingleExecution(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.PureSingleExecution) SingleExecutionTest(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.SingleExecutionTest) EngineRuntime(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.runtime.EngineRuntime) Service(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Service) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) SingleExecutionTest(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.SingleExecutionTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)20 Database (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Database)19 DatabaseBuilderInput (org.finos.legend.engine.plan.execution.stores.relational.connection.api.schema.model.DatabaseBuilderInput)11 MutableList (org.eclipse.collections.api.list.MutableList)9 PureModelContextData (org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)9 List (java.util.List)8 DatabasePattern (org.finos.legend.engine.plan.execution.stores.relational.connection.api.schema.model.DatabasePattern)8 FastList (org.eclipse.collections.impl.list.mutable.FastList)7 ListIterate (org.eclipse.collections.impl.utility.ListIterate)7 RelationalDatabaseConnection (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection)7 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Schema (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Schema)6 MutableMap (org.eclipse.collections.api.map.MutableMap)5 PureModel (org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel)5 RootRelationalClassMapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.RootRelationalClassMapping)5 TablePtr (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.TablePtr)5 Pair (org.eclipse.collections.api.tuple.Pair)4 StringWriter (java.io.StringWriter)3 Collections (java.util.Collections)3