コード例 #1
0
        public string ToSQL(out SqlParam[] sqlArgs)
        {
            bool hasRange = StartVal > 0 || CountVal > 0;

            OrderByClause = OrderByClause.NullIfEmptyTrimmed();

            //if (OrderByVal.IsNulle()) // OLD <-- keep for moment
            //  OrderByVal = TabelDef?.GroupByClause;
            //  OrderByVal = TabelDef.GroupByClause; //was: tableDefqqq.TableInfo.IndexInfo.OrderByClause;

            if (TableNameFull.IsNulle())
            {
                throw new ArgumentNullException("tableName");
            }

            if (hasRange && OrderByClause.IsNulle())
            {
                throw new ArgumentNullException("OrderBy clause required in Range query.");
            }

            sqlArgs = GetSqlParameters.ToArray();
            if (sqlArgs.IsNulle())
            {
                sqlArgs = null;
            }

            // SELECT
            var sb = new StringBuilder();

            if (SelectVal.NotNulle())
            {
                sb.AppendFormat(
                    @"SELECT {0}
FROM {1}", SelectVal, TableNameFull);
            }
            else if (SetClauses.NotNulle())
            {
                sb.AppendFormat(
                    @"UPDATE {0}
SET {1}", TableNameFull, GetSetClause);
            }
            else
            {
                throw new ArgumentNullException("SQL action, such as SELECT or UPDATE, must be specified.");
            }

            sb.AppendLine();

            // WHERE
            string whr = GetWhereClause;

            if (whr.NotNulle())
            {
                sb.Append(
                    @"WHERE ");
                sb.AppendLine(whr);
            }

            // ORDERBY
            if (SelectVal.NotNulle())
            {
                if (hasRange && OrderByClause.IsNulle())
                {
                    throw new ArgumentNullException($"SQL range query requires {OrderByClause} to be set.");
                    // NEW, didn't throw on this prior to 2018-05-24, not sure why we didn't,
                    // but just in case this shouldn't throw... this note
                }

                if (OrderByClause.NotNulle())
                {
                    sb.Append(
                        @"ORDER BY ");
                    sb.AppendLine(OrderByClause);
                }
            }

            // RANGE
            if (hasRange)
            {
                sb.AppendLine("OFFSET @Offset ROWS FETCH FIRST @Count ROWS ONLY");
            }

            if (NextQueryVal != null)
            {
                sb.TrimEnd();
                sb.AppendLine(";\r\n");

                sb.AppendLine(NextQueryVal.ToSQL(out SqlParam[] sqlArgs2));
                sqlArgs = sqlArgs.JoinSequences(sqlArgs2).ToArray();

                sb.TrimEnd();
                if (sb[sb.Length - 1] != ';')
                {
                    sb.AppendLine(";\r\n");
                }
            }

            string result = sb.ToString();

            return(result);
        }