Esempio n. 1
0
        /// <summary>
        /// Creates a SprocMethod object that doesn't require to be tested against to have automatically generated code.
        /// </summary>
        /// <param name="section">The sproc section that defines the sproc configuration.</param>
        /// <returns>A SprocMethod object with the required information for automatic code generation.</returns>
        public SprocMethod ReadSectionAndGenerateSprocMethodWithoutTestParams(SprocSection section)
        {
            GlobalProgress.NotifyProgress(
                new GlobalProgressNotification()
            {
                Text  = section.Name,
                Color = ConsoleColor.Cyan
            }
                );

            SprocMethod method = new SprocMethod();

            method.SprocName  = section.Name;
            method.MethodName = section.Method;
            method.Comments   = section.Comments;

            if (section.Return == null)
            {
                method.ReturnType = SprocMethod.ReturnTypes.IntWithRecordsAffected;
            }
            else
            {
                method.ReturnType = SprocMethod.ReturnTypes.ScalarValue;
                method.Return     = SqlParameterResolver.GetTypeFromSqlServerFriendlyType(section.Return).ClrType;
            }
            return(method);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a SprocMethod object that requires to be tested against to define the results.
        /// </summary>
        /// <param name="section">The sproc section that defines the sproc configuration.</param>
        /// <returns>A SprocMethod object with the required information for automatic code generation.</returns>
        public SprocMethod ReadSectionAndGenerateSprocMethodUsingTestParams(SprocSection section)
        {
            GlobalProgress.NotifyProgress(
                new GlobalProgressNotification()
            {
                Text  = $"{section.Name} (testing...)",
                Color = ConsoleColor.Cyan
            }
                );

            SprocMethod method = new SprocMethod();

            method.MethodName = section.Method;
            method.SprocName  = section.Name;
            method.Comments   = section.Comments;

            /* Creates a new connection to test the sproc. */
            using (SqlConnection connection = new SqlConnection())
            {
                connection.ConnectionString = Configuration.ConnectionString;
                connection.Open();

                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.CommandText = section.Name;

                    foreach (var param in section.TestParams)
                    {
                        var sqlParamMapping = SqlParameterResolver.Resolve(param.Name, param.Value);
                        command.Parameters.Add(sqlParamMapping.Parameter);
                    }

                    GlobalProgress.NotifyProgress($"Testing sproc '{section.Name}' with {command.Parameters.Count} parameter(s) ...");
                    /* Tests the sproc. */
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.FieldCount > 1)
                        {
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                SprocResultProperty sprocProperty = new SprocResultProperty();
                                sprocProperty.ClrType      = reader.GetFieldType(i);
                                sprocProperty.PropertyName = reader.GetName(i);

                                method.ResultProperties.Add(sprocProperty);
                                method.ReturnType = SprocMethod.ReturnTypes.IEnumerableOf;
                            }
                            GlobalProgress.NotifyProgress($"'{section.Name}' returned multiple columns, generating result class.");
                        }

                        if (reader.FieldCount == 1)
                        {
                            GlobalProgress.NotifyProgress($"'{section.Name}' returned one column, generating method as scalar.");
                            method.Return     = reader.GetFieldType(0);
                            method.ReturnType = SprocMethod.ReturnTypes.ScalarValue;
                        }

                        if (reader.FieldCount == 0)
                        {
                            GlobalProgress.NotifyProgress($"'{section.Name}' didn't return any columns, returning System.Int32.");
                            method.ReturnType = SprocMethod.ReturnTypes.IntWithRecordsAffected;
                        }
                    }
                }
            }

            return(method);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a context section from a valid JObject containing a context section.
        /// </summary>
        /// <param name="jsonObject">A valid context JObject object.</param>
        /// <returns>Returns an object containing the data set in the dataflip.json file for a context.</returns>
        public static ContextSection FromJson(JObject jsonObject)
        {
            ContextSection result = new ContextSection();

            try
            {
                string connectionString    = jsonObject["connectionString"]?.Value <string>();
                string nspace              = jsonObject["namespace"]?.Value <string>();
                string name                = jsonObject["name"]?.Value <string>();
                string output              = jsonObject["output"]?.Value <string>();
                string typeScriptTypings   = jsonObject["typeScript"]?["output"]?.Value <string>();
                bool?  useCamelCasing      = jsonObject["typeScript"]?["useCamelCasing"]?.Value <bool>();
                string angularHtmlBindings = jsonObject["angular"]?["htmlBindings"]?.Value <string>();

                if (connectionString == null)
                {
                    throw new DataflipToolException("The '/contexts[]/connectionString' element in dataflip.json is required.");
                }
                if (nspace == null)
                {
                    throw new DataflipToolException("The '/contexts[]/namespace' element in dataflip.json is required.");
                }
                if (name == null)
                {
                    throw new DataflipToolException("The '/contexts[]/name' element in dataflip.json is required.");
                }
                if (output == null)
                {
                    throw new DataflipToolException("The '/contexts[]/output' element in dataflip.json is required.");
                }
                if (useCamelCasing == null)
                {
                    useCamelCasing = true;
                }

                result.ConnectionString         = connectionString;
                result.Namespace                = nspace;
                result.Name                     = name;
                result.Output                   = output;
                result.TypeScriptTypings        = typeScriptTypings;
                result.TypeScriptUseCamelCasing = useCamelCasing.Value;
                result.AngularHtmlBindings      = angularHtmlBindings;
            }
            catch (Exception ex)
            {
                throw new DataflipToolException("There was a problem parsing the configuration for at least one context, error details: " + ex.Message);
            }


            if (jsonObject["sprocs"] == null)
            {
                throw new DataflipToolException("The context does not contain a 'sprocs' element. The 'sprocs' element is required.");
            }

            foreach (var sproc in jsonObject["sprocs"].AsJEnumerable())
            {
                SprocSection sprocSection = new SprocSection(result)
                {
                    Name     = sproc["name"].Value <string>(),
                    Return   = sproc["return"]?.Value <string>(),
                    Method   = sproc["method"]?.Value <string>(),
                    Comments = sproc["comments"]?.Value <string>()
                };

                if (sprocSection.Name == null)
                {
                    throw new DataflipToolException("'contexts[]/sprocs[n]/name' section in dataflip.json is required.");
                }

                if (sprocSection.Method == null)
                {
                    sprocSection.Method = sprocSection.Name;
                }

                result.Sprocs.Add(sprocSection);

                if (sproc["testParams"] != null)
                {
                    foreach (var testParam in sproc["testParams"].AsJEnumerable())
                    {
                        var property = ((Newtonsoft.Json.Linq.JProperty)testParam.First);
                        if (property == null || property.Name == null || property.Value == null)
                        {
                            throw new DataflipToolException("There was a problem parsing the entry in 'testParams'.");
                        }

                        string paramName  = property.Name;
                        string paramValue = property.Value.ToString();

                        sprocSection.TestParams.Add(new TestParamSection()
                        {
                            Name  = paramName,
                            Value = paramValue
                        }
                                                    );
                    }
                }
            }

            return(result);
        }