Esempio n. 1
0
 protected override void Generate(StatementCommand cmd)
 {
     this.TextRenderer.Write($"_Template.PushCaller({this.SourceExpression(cmd.Statement.Position)});", cmd.Statement.Position);
     this.TextRenderer.Write(cmd.Statement.Text, cmd.Statement.Position, EntryFeatures.ColumnInterpolation);
     this.TextRenderer.Write($"_Template.PopCaller();", cmd.End.Position);
     this.TextRenderer.WriteLine(cmd.End.Position);
 }
Esempio n. 2
0
        /// <summary>
        /// Serializes this object into command text that the
        /// <see cref="Provider"/> can process.
        /// </summary>
        /// <returns></returns>
        public virtual string GenerateCommandText()
        {
            if (!Inited)
            {
                InnerInit();
            }

            PrepareForCommandText();

            var cmd = new StringBuilder();

            string sprocName = null;

            switch (StatementCommand)
            {
            case SqlStatement.SELECT:
                sprocName = MaxRowCount == 1
                        ? ModelMap.TableMapping.SelectProcedure
                        : ModelMap.TableMapping.SelectManyProcedure;
                break;

            case SqlStatement.INSERT:
                sprocName = ModelMap.TableMapping.InsertProcedure;
                break;

            case SqlStatement.UPDATE:
                sprocName = ModelMap.TableMapping.UpdateProcedure;
                break;

            case SqlStatement.DELETE:
                sprocName = ModelMap.TableMapping.DeleteProcedure;
                break;
            }
            bool useSproc = !string.IsNullOrEmpty(sprocName);

            if (useSproc)
            {
                return(sprocName);
            }

            if (!string.IsNullOrEmpty(CommandTextPrefix))
            {
                cmd.Append(CommandTextPrefix);
            }

            cmd.Append(StatementCommand.ToString());
            cmd.Append("\t");

            if (!string.IsNullOrEmpty(StatementCommandSuffix))
            {
                cmd.Append(StatementCommandSuffix);
                cmd.Append(" ");
            }

            switch (StatementCommand)
            {
            case SqlStatement.SELECT:
                cmd.AppendLine(ColumnsListForSelect);
                if (!string.IsNullOrEmpty(SelectionItemsListSuffix))
                {
                    cmd.AppendLine("\t\t" + SelectionItemsListSuffix);
                }
                cmd.Append("  FROM\t");
                cmd.AppendLine(ModelMap.TableMapping.ToString(Provider.DbFactory));
                break;

            case SqlStatement.INSERT:
                cmd.AppendLine("INTO " + ModelMap.TableMapping.ToString(Provider.DbFactory));
                cmd.AppendLine("(");
                cmd.Append("\t");
                cmd.AppendLine(ColumnsListForInsert);
                cmd.AppendLine(") VALUES (");
                cmd.Append("\t");
                cmd.AppendLine(ValueParamsListForInsert);
                cmd.AppendLine(")");
                break;

            case SqlStatement.UPDATE:
                cmd.AppendLine(ModelMap.TableMapping.ToString(Provider.DbFactory));
                cmd.Append("   SET\t");
                cmd.AppendLine(ParamsAssignmentListForUpdate);
                break;

            case SqlStatement.DELETE:
                cmd.Append("  FROM ");
                cmd.AppendLine(ModelMap.TableMapping.ToString(Provider.DbFactory));
                break;
            }
            string where = GenerateWhereClause();
            if (!string.IsNullOrEmpty(WhereClauseAdditionalCondition))
            {
                if (string.IsNullOrEmpty(where))
                {
                    where = " WHERE\t";
                }
                else
                {
                    where += "  AND ";
                }
                where += WhereClauseAdditionalCondition;
            }
            if (!string.IsNullOrEmpty(where))
            {
                cmd.AppendLine(where);
            }
            if (StatementCommand == SqlStatement.SELECT)
            {
                string orderBy = GenerateOrderByClause();
                if (!string.IsNullOrEmpty(orderBy))
                {
                    cmd.AppendLine(GenerateOrderByClause());
                }
            }

            if (!string.IsNullOrEmpty(CommandTextSuffix))
            {
                cmd.AppendLine(CommandTextSuffix);
            }

            if (!string.IsNullOrEmpty(CommandTextSecondaryCommand))
            {
                cmd.AppendLine(CommandTextSecondaryCommand);
            }

            return(cmd.ToString());
        }