public string GetQualifiedTableNameOrToString()
            {
                if (string.IsNullOrEmpty(this.WhereCommand))
                {
                    if (string.IsNullOrEmpty(this.OrderByCommand))
                    {
                        if (string.IsNullOrEmpty(this.LimitCommand))
                        {
                            if (string.IsNullOrEmpty(this.GroupByCommand))
                            {
                                if (this.FromCommand == "from `" + Strategy.GetDescriptor().GetQualifiedTableName() + "`")
                                {
                                    if (this.SelectCommand == Strategy.GetDescriptor().GetSelectAllColumnsText())
                                    {
                                        return("`" + Strategy.GetDescriptor().GetQualifiedTableName() + "`");
                                    }
                                }
                            }
                        }
                    }
                }


                return("(\n" + this.ToString() + ")");
            }
        public static double Sum <TElement>(this IQueryStrategy <TElement> Strategy, Expression <Func <TElement, double> > selector)
        {
            // http://stackoverflow.com/questions/3785995/sqlite-accumulator-sum-column-in-a-select-statement
            // http://www.tutorialspoint.com/sqlite/sqlite_useful_functions.htm
            //throw new NotImplementedException("sqlite does not have it yet");
            // http://sqlite.1065341.n5.nabble.com/SUM-and-NULL-values-td2257.html

            var body = ((MemberExpression)((LambdaExpression)selector).Body);

            // do we need to check our db schema or is reflection the schema for us?
            #region ColumnName
            var ColumnName = "";

            ColumnName = body.Member.Name;
            #endregion



            return(((Task <double>)Strategy.GetDescriptor().GetWithConnection()(
                        c =>
            {
                var state = QueryStrategyExtensions.AsCommandBuilder(Strategy);

                // override
                state.SelectCommand = "select sum(`" + ColumnName + "`) ";

                //var cmd = new SQLiteCommand(state.ToString(), c);
                var cmd = c.CreateCommand(state.ToString());

                foreach (var item in state.ApplyParameter)
                {
                    item(cmd);
                }

                var s = new TaskCompletionSource <double>();


                s.SetResult(
                    (double)cmd.ExecuteScalar()
                    );

                //var r = cmd.ExecuteReader();

                //if (r.NextResult())
                //{
                //    //ex = {"No current row"}
                //    s.SetResult(
                //        r.GetInt64(0)
                //    );
                //}

                return s.Task;
            }
                        )).Result);
        }
        public static TResult Min <TElement, TResult>(this IQueryStrategy <TElement> Strategy, Expression <Func <TElement, TResult> > selector)
        {
            // X:\jsc.svn\examples\javascript\LINQ\MinMaxAverageExperiment\MinMaxAverageExperiment\ApplicationWebService.cs

            return(((Task <TResult>)Strategy.GetDescriptor().GetWithConnection()(
                        c =>
            {
                // http://stackoverflow.com/questions/3785995/sqlite-accumulator-sum-column-in-a-select-statement
                // http://www.tutorialspoint.com/sqlite/sqlite_useful_functions.htm
                // http://sqlite.1065341.n5.nabble.com/SUM-and-NULL-values-td2257.html

                var body = ((MemberExpression)((LambdaExpression)selector).Body);

                // do we need to check our db schema or is reflection the schema for us?
                #region ColumnName
                var ColumnName = "";

                ColumnName = body.Member.Name;
                #endregion

                var state = QueryStrategyExtensions.AsCommandBuilder(Strategy);

                // override
                state.SelectCommand = "select min(`" + ColumnName + "`) ";

                //var cmd = new SQLiteCommand(state.ToString(), c);
                var cmd = c.CreateCommand(state.ToString());

                foreach (var item in state.ApplyParameter)
                {
                    item(cmd);
                }

                var s = new TaskCompletionSource <TResult>();

                // will it compile to java???
                s.SetResult(
                    (TResult)cmd.ExecuteScalar()
                    );

                //var r = cmd.ExecuteReader();

                //if (r.NextResult())
                //{
                //    //ex = {"No current row"}
                //    s.SetResult(
                //        r.GetInt64(0)
                //    );
                //}

                return s.Task;
            }
                        )).Result);
        }
        public static IQueryStrategy <TElement> Where <TElement>(this IQueryStrategy <TElement> source, Expression <Func <TElement, bool> > filter)
        {
            // which thread are we running on?
            // X:\jsc.svn\examples\javascript\appengine\AppEngineUserAgentLoggerWithXSLXAsset\AppEngineUserAgentLoggerWithXSLXAsset\ApplicationWebService.cs
            Console.WriteLine("enter Where");


            var xINestedQueryStrategy = source is INestedQueryStrategy;

            if (!xINestedQueryStrategy)
            {
                // lets mark it as nestable, this allows where to know how to aviod name clashes.
                // X:\jsc.svn\examples\javascript\linq\test\TestGroupByCountViaScalarWhere\TestGroupByCountViaScalarWhere\ApplicationWebService.cs
                // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2014/201406/20140607


                // first step being immputable for where clauses
                var that = new WhereQueryStrategy <TElement>
                {
                    source = source,
                    filter = filter,


                    InternalGetElementType =
                        delegate
                    {
                        return(source.GetElementType());
                    },

                    InternalGetDescriptor =
                        () =>
                    {
                        // inherit the connection/context from above
                        var StrategyDescriptor = source.GetDescriptor();

                        return(StrategyDescriptor);
                    }
                };

                MutableWhere(
                    that, filter
                    );

                return(that);
            }

            MutableWhere(
                source, filter
                );

            return(source);
        }
        // X:\jsc.svn\examples\javascript\LINQ\MashableVelocityGraph\MashableVelocityGraph\ApplicationWebService.cs
        //[Obsolete("this is the first method we made generic. would it work for a group now?")]
        //public static long Count<TElement>(this IQueryStrategy<TElement> Strategy)
        public static long Count(this IQueryStrategy Strategy)
        {
            return(((Task <long>)Strategy.GetDescriptor().GetWithConnection()(
                        c =>
            {
                var state = QueryStrategyExtensions.AsCommandBuilder(Strategy);

                // override
                state.SelectCommand = "select count(*)";

                var cmd = c.CreateCommand(state.ToString());

                foreach (var item in state.ApplyParameter)
                {
                    item(cmd);
                }

                var s = new TaskCompletionSource <long>();
                s.SetResult((long)cmd.ExecuteScalar());

                return s.Task;
            }
                        )).Result);
        }
예제 #6
0
        static DataTable InternalAsDataTable(IQueryStrategy Strategy)
        {
            // X:\jsc.svn\examples\javascript\forms\Test\TestSQLiteGroupBy\TestSQLiteGroupBy\ApplicationWebService.cs

            // will it work for jvm?
            var st = new StackTrace(0, true);
            var sw = Stopwatch.StartNew();

            Console.WriteLine("AsDataTable " + new { Strategy });

            if (Strategy == null)
            {
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }

                throw new ArgumentNullException("Strategy");
            }

            //System.Diagnostics.Contracts.Contract.Assume

            var value = ((Task <DataTable>)Strategy.GetDescriptor().GetWithConnection()(
                             c =>
            {
                var state = QueryStrategyExtensions.AsCommandBuilder(Strategy);

                //var cmd = new SQLiteCommand(state.ToString(), c);

                var cmd = (DbCommand)c.CreateCommand();


                //ex = {"no such column: dealer.Key"}
                cmd.CommandText = state.ToString();

                // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2014/201405/20140501

                foreach (var item in state.ApplyParameter)
                {
                    item(cmd);
                }



                // X:\jsc.svn\core\ScriptCoreLib\Shared\BCLImplementation\System\Data\Common\DbDataAdapter.cs
                // will this work under CLR too?

                // http://stackoverflow.com/questions/12608025/how-to-construct-a-sqlite-query-to-group-by-order
                // http://www.devart.com/dotconnect/sqlite/docs/Devart.Data.SQLite~Devart.Data.SQLite.SQLiteDataReader~NextResult.html
                // http://www.maplesoft.com/support/help/Maple/view.aspx?path=Database/Statement/NextResult
                // To issue a multi-statement SQL string, the Execute command must be used.
                //  Some databases may require that the processing of the current result be completed before the next result is returned by NextResult.
                // http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/ExecutingaQueryThatReturnsMultipleResultSetswithSqlDataReader.htm
                // http://amitchandnz.wordpress.com/2011/09/28/issues-with-idatareaderdatareader-multiple-results-sets-and-datatables/
                // http://stuff.mit.edu/afs/athena/software/mono_v3.0/arch/i386_linux26/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteDataReader.cs
                // http://zetcode.com/db/sqlitecsharp/read/
                // http://stackoverflow.com/questions/18493169/sqlite-query-combining-two-result-sets-that-use-and
                // http://www.sqlite.org/queryplanner.html
                // One possible solution is to fetch all events, to a ToList() and do the grouping in-memory.
                // http://blog.csainty.com/2008/01/linq-to-sql-groupby.html
                // http://msdn.microsoft.com/en-us/library/vstudio/bb386922(v=vs.100).aspx

                //var reader = cmd.ExecuteReader();
                ////var reader = cmd.ExecuteReader();

                ////Console.WriteLine(
                ////    new
                ////    {
                ////        reader.Depth,
                ////        reader.FieldCount
                ////        //reader.NextResult

                ////    }
                ////);

                //var a = new SQLiteDataAdapter(cmd);

                // http://msdn.microsoft.com/en-us/library/bh8kx08z(v=vs.110).aspx

                var a = new __DbDataAdapter {
                    SelectCommand = cmd
                };
                //var a = new SQLiteDataAdapter { SelectCommand = cmd };

                //System.Data.XSQLite.dll!Community.CsharpSqlite.Sqlite3.fetchPayload(Community.CsharpSqlite.Sqlite3.BtCursor pCur, ref int pAmt, ref int outOffset, bool skipKey)	Unknown
                //System.Data.XSQLite.dll!Community.CsharpSqlite.Sqlite3.sqlite3BtreeKeyFetch(Community.CsharpSqlite.Sqlite3.BtCursor pCur, ref int pAmt, ref int outOffset)	Unknown
                //System.Data.XSQLite.dll!Community.CsharpSqlite.Sqlite3.sqlite3VdbeExec(Community.CsharpSqlite.Sqlite3.Vdbe p)	Unknown
                //System.Data.XSQLite.dll!Community.CsharpSqlite.Sqlite3.sqlite3Step(Community.CsharpSqlite.Sqlite3.Vdbe p)	Unknown
                //System.Data.XSQLite.dll!Community.CsharpSqlite.Sqlite3.sqlite3_step(Community.CsharpSqlite.Sqlite3.Vdbe pStmt)	Unknown
                //System.Data.XSQLite.dll!System.Data.SQLite.SQLiteCommand.ExecuteStatement(Community.CsharpSqlite.Sqlite3.Vdbe pStmt, out int cols, out System.IntPtr pazValue, out System.IntPtr pazColName)	Unknown
                //System.Data.XSQLite.dll!System.Data.SQLite.SQLiteDataReader.ReadpVm(Community.CsharpSqlite.Sqlite3.Vdbe pVm, int version, System.Data.SQLite.SQLiteCommand cmd)	Unknown
                //System.Data.XSQLite.dll!System.Data.SQLite.SQLiteDataReader.SQLiteDataReader(System.Data.SQLite.SQLiteCommand cmd, Community.CsharpSqlite.Sqlite3.Vdbe pVm, int version)	Unknown
                //System.Data.XSQLite.dll!System.Data.SQLite.SQLiteCommand.ExecuteReader(System.Data.CommandBehavior behavior, bool want_results, out int rows_affected)	Unknown
                //System.Data.XSQLite.dll!System.Data.SQLite.SQLiteCommand.ExecuteReader(System.Data.CommandBehavior behavior)	Unknown
                //System.Data.XSQLite.dll!System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(System.Data.CommandBehavior behavior)	Unknown
                //System.Data.dll!System.Data.Common.DbCommand.ExecuteReader()	Unknown
                //ScriptCoreLib.dll!ScriptCoreLib.Shared.BCLImplementation.System.Data.Common.__DbDataAdapter.Fill(System.Data.DataTable dataTable)	Unknown

                var ss = Stopwatch.StartNew();

                Console.WriteLine("before Fill");
                var t = new DataTable();
                //var ds = new DataSet();
                a.Fill(t);
                // is SQLIte Fill handicapped or what?

                //a.Fill(ds);
                //Console.WriteLine("after Fill " + new { ss.ElapsedMilliseconds, t.Rows.Count });
                Console.WriteLine("after Fill " + new { ss.ElapsedMilliseconds });


                var s = new TaskCompletionSource <DataTable>();
                //s.SetResult(ds.Tables[0]);
                s.SetResult(t);

                return(s.Task);
            }
                             )).Result;

            var caller = st.GetFrame(1);

            Console.WriteLine(
                Process.GetCurrentProcess().Id.ToString("x4") + ":"
                + Thread.CurrentThread.ManagedThreadId.ToString("x4")
                + " AsDataTable " + new { sw.ElapsedMilliseconds, Debugger.IsAttached, caller = caller.ToString() });

            return(value);
        }