Obtaining a SQL/parameter values without executing it

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for Oracle
Post Reply
motuzko
Posts: 54
Joined: Tue 08 Sep 2009 18:02
Contact:

Obtaining a SQL/parameter values without executing it

Post by motuzko » Tue 21 Nov 2017 19:39

In LinqConnect, is there a way to obtain a SQL with all the parameter values without executing it?

motuzko
Posts: 54
Joined: Tue 08 Sep 2009 18:02
Contact:

Re: Obtaining a SQL/parameter values without executing it

Post by motuzko » Tue 21 Nov 2017 19:41

2 Scenarios:

1) IQueryable<T> to SQL + parameter values
2) a way to get a SQL for Min/Max(IQueryable<T> => int)

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Obtaining a SQL/parameter values without executing it

Post by Shalex » Mon 27 Nov 2017 15:30

Unfortunately, there is no way to do that with a current implementation.

motuzko
Posts: 54
Joined: Tue 08 Sep 2009 18:02
Contact:

Re: Obtaining a SQL/parameter values without executing it

Post by motuzko » Tue 28 Nov 2017 17:51

would it be possible to implement this functionality in the future?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Obtaining a SQL/parameter values without executing it

Post by Shalex » Thu 07 Dec 2017 19:40

We are processing your request.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Obtaining a SQL/parameter values without executing it

Post by Shalex » Fri 20 Apr 2018 10:04

Please try this workaround:

Code: Select all

using System;
using System.IO;
using System.Text;
using MyContext;

namespace MyConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new MyDataContext())
            {
                var log = new MyTextWriter();
                context.Log = log;

                string name = "abcd";
                var dept = new DEPT() { DEPTNO = 6, DNAME = name };
                context.DEPTs.InsertOnSubmit(dept);

                try
                {
                    context.SubmitChanges();
                }
                catch (Exception e)
                {
                    if (e.Message != "Getting SQL without execution")
                        throw e;
                }

                Console.WriteLine(log.GetSql());
                Console.ReadKey();
            }
        }
    }

    public class MyTextWriter : TextWriter
    {
        StringBuilder SQL;
        public MyTextWriter()
        {
            SQL = new StringBuilder();
        }
        public override void Write(string value)
        {
            SQL.Append(value);
        }
        public override void WriteLine(string value)
        {
            SQL.AppendLine(value);
        }

        public override void WriteLine()
        {
            throw new Exception("Getting SQL without execution");
        }

        public override Encoding Encoding {
            get { return Encoding.Default; }
        }

        public string GetSql()
        {
            string query = SQL.ToString();
            SQL.Clear();
            return query;
        }
    }
}

motuzko
Posts: 54
Joined: Tue 08 Sep 2009 18:02
Contact:

Re: Obtaining a SQL/parameter values without executing it

Post by motuzko » Mon 23 Apr 2018 13:58

*without executing it*

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Obtaining a SQL/parameter values without executing it

Post by Shalex » Mon 23 Apr 2018 17:24

The code doesn't send any CUD statements to the server.

Post Reply