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 new Context object based on the current context configuration.
        /// </summary>
        /// <returns>A new Context object.</returns>
        public Context BuildContext()
        {
            Context context = new Context(Configuration);

            context.Namespace = Configuration.Namespace;
            context.Name      = Configuration.Name;

            /* Resolves all the sprocs set in the context configuration. */
            foreach (var sproc in Configuration.Sprocs)
            {
                SprocMethod method = null;

                if (sproc.TestParams.Count > 0)
                {
                    method = ReadSectionAndGenerateSprocMethodUsingTestParams(sproc);
                }
                else
                {
                    method = ReadSectionAndGenerateSprocMethodWithoutTestParams(sproc);
                }

                GlobalProgress.NotifyProgress($"Running sp_help '{sproc.Name}' to derive the parameters.");
                method.Parameters = new SprocParameterResolver(Configuration).Resolve(method.SprocName);
                GlobalProgress.NotifyProgress($"Found {method.Parameters.Count()} parameter(s) for '{sproc.Name}'.");

                context.Methods.Add(method);
                GlobalProgress.NotifyProgress("Done.");
            }

            return(context);
        }
Esempio n. 3
0
        /// <summary>
        /// Resolves the parameters for a given sproc name.
        /// </summary>
        /// <param name="sprocName">The sproc name.</param>
        /// <returns>An IEnumerable of SproParameters that contains all the parameter information about the sproc.</returns>
        public IEnumerable <SprocParameter> Resolve(string sprocName)
        {
            try
            {
                using (SqlConnection connection = new SqlConnection())
                {
                    connection.ConnectionString = Configuration.ConnectionString;
                    connection.Open();

                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandText = $"sp_help {sprocName}";

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            List <SprocParameter> parameters = new List <SprocParameter>();

                            reader.NextResult();

                            while (reader.Read())
                            {
                                SprocParameter param = new SprocParameter();

                                param.ClrType       = GetClrTypeFromSqlServerType(reader["Type"] as string);
                                param.ParameterName = reader["Parameter_name"] as string;

                                parameters.Add(param);
                            }

                            return(parameters);
                        }
                    }
                }
            }
            catch
            {
                GlobalProgress.NotifyProgress("There was a problem running sp_test to get the parameter information.");
                throw;
            }
        }
Esempio n. 4
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);
        }