Example #1
0
        private FunctionResult Execute(ConnectionTypeSelection connectionType, Transaction transaction, string sqlStatement,
                                       ExecuteSQLShared.ReturnModeType returnMode, ResultType resultType, bool connectionStringAsExpression,
                                       params ParameterValue[] sqlFieldParameters)
        {
            string connectionString = connectionType == ConnectionTypeSelection.UseTransaction ? string.Empty : Helpers.DatabaseHelpers.GetDefaultConnectionString(connectionType.ToConnectionType().Value);

            object dynamicConnectionString;

            if (connectionStringAsExpression)
            {
                dynamicConnectionString = new MockExpression("\"" + connectionString + "\"");
            }
            else
            {
                dynamicConnectionString = connectionString;
            }

            var functionProperties = new PropertyValue[] {
                new PropertyValue(ExecuteSQLShared.TimeoutPropertyName, 5),
                new PropertyValue(ExecuteSQLShared.ReturnOptionsPropertyName, returnMode),
                new PropertyValue(ExecuteSQLShared.ResultTypePropertyName, resultType),
                new PropertyValue(DbShared.ConnectionTypePropertyName, connectionType),
                new PropertyValue(ExecuteSQLShared.SqlStatementPropertyName, sqlStatement)
            };

            var executeParameters = new ParameterValue[]
            {
                new ParameterValue(ExecuteSQLShared.SqlStatementPropertyName, sqlStatement)
            };

            if (connectionType == ConnectionTypeSelection.UseTransaction)
            {
                executeParameters = executeParameters.Concat(new ParameterValue[] { new ParameterValue(DbShared.TransactionPropertyName, transaction) }).ToArray();
            }
            else
            {
                functionProperties = functionProperties.Concat(new PropertyValue[] { new PropertyValue(DbShared.ConnectionStringPropertyName, dynamicConnectionString) }).ToArray();
                executeParameters  = executeParameters.Concat(new ParameterValue[] { new ParameterValue(DbShared.ConnectionStringPropertyName, connectionString) }).ToArray();
            }

            var tester = (new FunctionTester <ExecuteSQL.ExecuteSQL>()).Compile(functionProperties);

            return(tester.Execute(executeParameters
                                  .Concat <ParameterValue>(sqlFieldParameters).ToArray <ParameterValue>()));
        }
Example #2
0
        public override void GenerateCode(IFunctionBuilder functionBuilder)
        {
            ExecuteSQL_Gen generator = new ExecuteSQL_Gen();

            generator.Session = new Dictionary <string, object>();
            generator.Session.Add("FunctionContextProperty", functionBuilder.ContextParamName);
            generator.Session.Add("GetParamName", new Func <string, string>(functionBuilder.GetParamName));

            // Function properties
            generator.Session.Add("Timeout", FunctionData.Properties[ExecuteSQLShared.TimeoutPropertyName].GetValue <int>());
            ConnectionTypeSelection connectionType = FunctionData.Properties[DbShared.ConnectionTypePropertyName].GetValue <ConnectionTypeSelection>();
            bool useTransaction = connectionType == ConnectionTypeSelection.UseTransaction;

            generator.Session.Add("UseTransaction", useTransaction);
            if (useTransaction)
            {
                generator.Session.Add("TransactionProperty", functionBuilder.GetParamName(DbShared.TransactionPropertyName));
            }
            else
            {
                generator.Session.Add("ConnectionType", connectionType.ToConnectionType());
                generator.Session.Add("ConnectionStringProperty", functionBuilder.GetParamName(DbShared.ConnectionStringPropertyName));
            }
            generator.Session.Add("Sql", functionBuilder.GetParamName(ExecuteSQLShared.SqlStatementPropertyName));
            ExecuteSQLShared.ReturnModeType returnMode = FunctionData.Properties[ExecuteSQLShared.ReturnOptionsPropertyName].GetValue <ExecuteSQLShared.ReturnModeType>();
            generator.Session.Add("ReturnMode", returnMode);

            // SQL placeholders
            SqlStringHandler sqlStringHandler = SqlStringHandler.GetSqlStringHandler(StaticSqlStatementValue);

            generator.Session.Add("SqlIdentifiers", CSharpUtilities.ArrayAsString(sqlStringHandler.Expressions.Select(ex => SqlStringHandler.CreateSqlExpression(ex.ExpressionText))));
            int parameterIndex = 0;

            generator.Session.Add("SqlValues", "new object[] { " + string.Join(", ", sqlStringHandler.Expressions.Select(ex => functionBuilder.GetParamName(ExecuteSQLShared.SqlValuePropertyPrefix + (++parameterIndex)))) + " }");

            // Output columns
            var resultType = FunctionData.Properties[ExecuteSQLShared.ResultTypePropertyName].GetValue <ResultType>();

            generator.Session.Add("ResultTypeFields", resultType.Fields);

            ITypeReference rowTypeReference = null;

            if (resultType.Fields.Count != 0)
            {
                switch (returnMode)
                {
                case ExecuteSQLShared.ReturnModeType.RowByRow:
                    rowTypeReference = FunctionData.ExecutionPaths[ExecuteSQLShared.ExecutionPathName].Output;
                    break;

                case ExecuteSQLShared.ReturnModeType.ListOfRows:
                    rowTypeReference = FunctionData.Output.GetEnumerableContentType();
                    break;

                case ExecuteSQLShared.ReturnModeType.FirstRow:
                case ExecuteSQLShared.ReturnModeType.FirstRowElseEmptyRow:
                    rowTypeReference = FunctionData.Output;
                    break;
                }
            }
            generator.Session.Add("RowTypeName", rowTypeReference == null ? null : functionBuilder.GetTypeName(rowTypeReference));
            generator.Session.Add("RowType", rowTypeReference);
            generator.Session.Add("ExecutionPathName", ExecuteSQLShared.ExecutionPathName);
            generator.Session.Add("ExecutionPathOutputName", functionBuilder.ExecutionPathOutParamName);

            generator.Initialize();
            functionBuilder.AddCode(generator.TransformText());
            functionBuilder.AddAssemblyReference(typeof(ExecuteSQL));
            functionBuilder.AddAssemblyReference(typeof(System.Data.IDataReader));
        }
Example #3
0
        public override void GenerateCode(IFunctionBuilder functionBuilder)
        {
            ExecuteStoredProcedureX_Gen generator = new ExecuteStoredProcedureX_Gen();

            generator.Session = new Dictionary <string, object>();
            generator.Session.Add("FunctionContextProperty", functionBuilder.ContextParamName);
            ConnectionTypeSelection connectionType = FunctionData.Properties[DbShared.ConnectionTypePropertyName].GetValue <ConnectionTypeSelection>();
            bool useTransaction = connectionType == ConnectionTypeSelection.UseTransaction;

            generator.Session.Add("UseTransaction", useTransaction);
            if (useTransaction)
            {
                generator.Session.Add("TransactionProperty", functionBuilder.GetParamName(DbShared.TransactionPropertyName));
            }
            else
            {
                generator.Session.Add("ConnectionType", connectionType.ToConnectionType());
                generator.Session.Add("ConnectionStringProperty", functionBuilder.GetParamName(DbShared.ConnectionStringPropertyName));
            }
            generator.Session.Add("StoredProcedureProperty", functionBuilder.GetParamName(ExecuteStoredProcedureShared.StoredProcedurePropertyName));
            var parameters = FunctionData.Properties[ExecuteStoredProcedureShared.ParametersPropertyName].GetValue <DatabaseModel.ProcedureParameters>();

            generator.Session.Add("Parameters", parameters);
            generator.Session.Add("GetParamName", new Func <string, string>(functionBuilder.GetParamName));

            generator.Session.Add("OutputTypeName", FunctionData.Output == null ? null : functionBuilder.GetTypeName(FunctionData.Output));
            bool hasOutParameters = parameters.Any(p => (p.Direction != DatabaseModel.ParameterDirection.In) && (p.DataType != DatabaseModel.DataType.RefCursor));

            generator.Session.Add("OutParametersOutputPropertyName", hasOutParameters ? ExecuteStoredProcedureShared.OutParametersOutputPropertyName : null);
            generator.Session.Add("OutParametersOutputTypeName", hasOutParameters ? functionBuilder.GetTypeName(FunctionData.Output.GetProperty(ExecuteStoredProcedureShared.OutParametersOutputPropertyName).TypeReference) : null);

            OutputOption outputOption = FunctionData.Properties[ExecuteStoredProcedureShared.OutputOptionPropertyName].GetValue <OutputOption>();

            generator.Session.Add("OutputOption", outputOption);
            int resultSetCount = FunctionData.Properties[ExecuteStoredProcedureShared.ResultSetCountPropertyName].GetValue <int>();

            generator.Session.Add("ResultSets", Enumerable.Range(0, resultSetCount).Select(i => FunctionData.Properties[string.Format(ExecuteStoredProcedureShared.ResultSetPropertyNameFormat, i + 1)].GetValue <DatabaseModel.ResultSet>()).ToArray());
            if (resultSetCount != 0)
            {
                if (outputOption == OutputOption.RowByRow)
                {
                    string[] executionPathKeys  = Enumerable.Range(0, resultSetCount).Select(i => string.Format(ExecuteStoredProcedureShared.ResultSetExecutionPathNameFormat, i + 1)).ToArray();
                    string[] executionPathNames = resultSetCount == 1 ? new string[] { string.Format(ExecuteStoredProcedureShared.ResultSetExecutionPathNameFormat, string.Empty) } : executionPathKeys;
                    generator.Session.Add("RowTypeNames", executionPathKeys.Select(p => functionBuilder.GetTypeName(FunctionData.ExecutionPaths[p].Output)).ToArray());
                    generator.Session.Add("ExecutionPathOutputName", functionBuilder.ExecutionPathOutParamName);
                    generator.Session.Add("ExecutionPathNames", executionPathNames);
                    generator.Session.Add("ResultSetRowOutputPropertyNames", null);
                }
                else if (outputOption == OutputOption.ListOfRows)
                {
                    string[] resultSetRowOutputPropertyNames = resultSetCount == 1 ? new string[] { string.Format(ExecuteStoredProcedureShared.ResultSetRowsOutputPropertyNameFormat, string.Empty) } : Enumerable.Range(0, resultSetCount).Select(i => string.Format(ExecuteStoredProcedureShared.ResultSetRowsOutputPropertyNameFormat, i + 1)).ToArray();
                    generator.Session.Add("RowTypeNames", resultSetRowOutputPropertyNames.Select(p => functionBuilder.GetTypeName(FunctionData.Output.GetProperty(p).TypeReference.GetEnumerableContentType())).ToArray());
                    generator.Session.Add("ResultSetRowOutputPropertyNames", resultSetRowOutputPropertyNames);
                    generator.Session.Add("ExecutionPathOutputName", null);
                    generator.Session.Add("ExecutionPathNames", null);
                }
                else
                {
                    string[] resultSetRowOutputPropertyNames = resultSetCount == 1 ? new string[] { string.Format(ExecuteStoredProcedureShared.ResultSetRowOutputPropertyNameFormat, string.Empty) } : Enumerable.Range(0, resultSetCount).Select(i => string.Format(ExecuteStoredProcedureShared.ResultSetRowOutputPropertyNameFormat, i + 1)).ToArray();
                    generator.Session.Add("RowTypeNames", resultSetRowOutputPropertyNames.Select(p => functionBuilder.GetTypeName(FunctionData.Output.GetProperty(p).TypeReference)).ToArray());
                    generator.Session.Add("ResultSetRowOutputPropertyNames", resultSetRowOutputPropertyNames);
                    generator.Session.Add("ExecutionPathOutputName", null);
                    generator.Session.Add("ExecutionPathNames", null);
                }
            }

            generator.Initialize();
            functionBuilder.AddCode(generator.TransformText());

            functionBuilder.AddAssemblyReference(typeof(ExecuteStoredProcedure));
            functionBuilder.AddAssemblyReference(typeof(IDbCommand));
        }