Beispiel #1
0
        public int ExecuteQuery(string sql, IResultWriter sw, Action <Int64> setResults, string context = "", Globals.ResultWriterDestination dest = Globals.ResultWriterDestination.stdOut)
        {
            Int64 numResults = 0;

            if (sql.Contains("ROW_NUMBER("))
            {
                SQLitePCL.Functions.core.CoreFn.RowNumDictionary = new Dictionary <string, int>();
            }
            if (sql.Contains("JSON("))
            {
                var match = Regex.Match(sql, @"JSON\((?<key>.*?)\)");
                sql    = Regex.Replace(sql, @"JSON\(\w*\)", "", RegexOptions.IgnoreCase);
                sw.Key = match.Groups["key"].Value;
            }
            int resp = (int)ErrorCode.Ok;

            try
            {
                var stmts = DbConnection.PrepareAll(CleanSql(sql));
                foreach (var stmt in stmts)
                {
                    if (stmt.SQL != null && stmt.MoveNext())
                    {
                        var headers = stmt.Columns.Select(col => col.Name).ToList <string>();

                        sw
                        .BeginContext(context, dest)
                        .WriteHeaders(headers, dest)
                        .WriteResult(stmt.Current, dest);

                        // Write the rest...
                        while (stmt.MoveNext())
                        {
                            numResults++;
                            sw.WriteResult(stmt.Current, dest);
                        }
                        sw.EndContext(dest);
                    }
                }
            }
            catch (SQLiteException e)
            {
                resp      = (int)e.ErrorCode;
                LastError = e.Message + " -- " + raw.sqlite3_errmsg(RawDbConnection) + " -- on query: " + sql;
            }
            finally
            {
                sw.Flush();
                Int64 changes = DbConnection.Changes;
                // if we didn't make any db changes, then we performed a query, so let's return the number of results in that query
                if (changes == 0 && numResults != 0)
                {
                    changes = numResults;
                }
                setResults(changes);
            }

            return(resp);
        }