コード例 #1
0
        /// <summary>
        /// Convierte una cláusula de INSERT a string.
        /// Si el INSERT devuelve valores.
        ///
        /// Devuelve ya sea un <see cref="InsertNoReturningStrResult"/> o un <see cref="InsertReturningToStr"/>
        /// </summary>
        public static StatementToStrResult InsertToString(InsertClause clause, ParamMode paramMode, SqlParamDic paramDic)
        {
            var b = new StringBuilder();

            b.Append("INSERT INTO ");
            b.Append($"\"{clause.Table}\" ");

            if ((clause.Value == null) == (clause.Query == null))
            {
                throw new ArgumentException("Query debe de ser null si value no es null");
            }

            if (clause.Value != null)
            {
                b.Append(InsertValueToString(clause, paramMode, paramDic));
            }
            else
            {
                //Query
                var sqlQuery = SqlSelect.SelectToStringScalar(clause.Query, paramMode, paramDic);

                //Texto de las columnas:
                b.Append("(");
                b.Append(string.Join(", ", sqlQuery.Columns));
                b.AppendLine(")");

                b.Append(sqlQuery.Sql);
            }

            if (clause.OnConflict != null)
            {
                b.AppendLine();
                b.Append(OnConflict(clause.OnConflict, paramMode, paramDic, clause.Table));
            }

            //Columnas del returning
            IReadOnlyList <string> columns = null;

            if (clause.Returning != null)
            {
                b.AppendLine();
                var returning = ReturningToString(clause.Returning, paramMode, paramDic, clause.Table);
                b.Append(returning.sql);
                columns = returning.cols;
            }

            var sql = b.ToString();

            if (columns != null)
            {
                return(new InsertReturningToStr(sql, columns));
            }
            return(new InsertNoReturningStrResult(sql));
        }