public void Query(int queryId)
        {
            var doQuery = new DoQuery.Builder(Application.Client.Ticket, Application.Token, Application.Client.AccountDomain, TableId)
                          .SetQid(queryId)
                          .SetFmt(true)
                          .Build();
            var xml = doQuery.Post().CreateNavigator();

            LoadColumns(xml);
            LoadRecords(xml);
        }
        public void Query(Query query, int[] clist)
        {
            var colList = GetColumnList(clist);

            var doQuery = new DoQuery.Builder(Application.Client.Ticket, Application.Token, Application.Client.AccountDomain, TableId)
                          .SetQuery(query.ToString())
                          .SetCList(colList)
                          .SetFmt(true)
                          .Build();
            var xml = doQuery.Post().CreateNavigator();

            LoadColumns(xml);
            LoadRecords(xml);
        }
Exemple #3
0
 private void _doQuery(DoQuery qry, bool isClearOldRecords = true)
 {
     if (isClearOldRecords)
     {
         Records.Clear();
     }
     try
     {
         XPathNavigator xml = qry.Post().CreateNavigator();
         LoadColumns(xml); //In case the schema changes due to another user, or from a previous query that has a differing subset of columns TODO: remove this requirement
         LoadRecords(xml);
     }
     catch (TooManyCriteriaInQueryException)
     {
         //If and only if all elements of a query are OR operations, we can split the query in 99 element chunks
         string query = qry.Query;
         if (string.IsNullOrEmpty(query) || QueryCheckRegex.IsMatch(query))
         {
             throw;
         }
         string[] args   = query.Split(QuerySeparator, StringSplitOptions.None);
         int      argCnt = args.Length;
         if (argCnt < 100) //We've no idea how to split this, apparently...
         {
             throw;
         }
         if (args[0].StartsWith("{"))
         {
             args[0] = args[0].Substring(1);                          //remove leading {
         }
         if (args[argCnt = 1].EndsWith("}"))
         {
             args[argCnt - 1] = args[argCnt - 1].Substring(0, args[argCnt - 1].Length - 1);                                 // remove trailing }
         }
         int sentArgs = 0;
         while (sentArgs < argCnt)
         {
             int      useArgs    = Math.Min(99, argCnt - sentArgs);
             string[] argsToSend = args.Skip(sentArgs).Take(useArgs).ToArray();
             string   sendQuery  = "{" + string.Join("}OR{", argsToSend) + "}";
             DoQuery  dqry       = new DoQuery.Builder(Application.Client.Ticket, Application.Token, Application.Client.AccountDomain, TableId)
                                   .SetQuery(sendQuery)
                                   .SetCList(qry.Collist)
                                   .SetOptions(qry.Options)
                                   .SetFmt(true)
                                   .Build();
             XPathNavigator xml = dqry.Post().CreateNavigator();
             if (sentArgs == 0)
             {
                 LoadColumns(xml);
             }
             LoadRecords(xml);
             sentArgs += useArgs;
         }
     }
     catch (ViewTooLargeException)
     {
         //split into smaller queries automagically
         List <string> optionsList = new List <string>();
         string        query       = qry.Query;
         string        collist     = qry.Collist;
         int           maxCount    = 0;
         int           baseSkip    = 0;
         if (!string.IsNullOrEmpty(qry.Options))
         {
             string[] optArry = qry.Options.Split('.');
             foreach (string opt in optArry)
             {
                 if (opt.StartsWith("num-"))
                 {
                     maxCount = int.Parse(opt.Substring(4));
                 }
                 else if (opt.StartsWith("skp-"))
                 {
                     baseSkip = int.Parse(opt.Substring(4));
                 }
                 else
                 {
                     optionsList.Add(opt);
                 }
             }
         }
         if (maxCount == 0)
         {
             DoQueryCount dqryCnt;
             if (string.IsNullOrEmpty(query))
             {
                 dqryCnt = new DoQueryCount.Builder(Application.Client.Ticket, Application.Token, Application.Client.AccountDomain, TableId)
                           .Build();
             }
             else
             {
                 dqryCnt = new DoQueryCount.Builder(Application.Client.Ticket, Application.Token, Application.Client.AccountDomain, TableId)
                           .SetQuery(query)
                           .Build();
             }
             var cntXml = dqryCnt.Post().CreateNavigator();
             maxCount = int.Parse(cntXml.SelectSingleNode("/qdbapi/numMatches").Value);
         }
         int stride  = maxCount / 2;
         int fetched = 0;
         while (fetched < maxCount)
         {
             List <string> optLst = new List <string>();
             optLst.AddRange(optionsList);
             optLst.Add("skp-" + (fetched + baseSkip));
             optLst.Add("num-" + stride);
             string  options = string.Join(".", optLst);
             DoQuery dqry;
             if (string.IsNullOrEmpty(query))
             {
                 dqry = new DoQuery.Builder(Application.Client.Ticket, Application.Token, Application.Client.AccountDomain, TableId)
                        .SetCList(collist)
                        .SetOptions(options)
                        .SetFmt(true)
                        .Build();
             }
             else
             {
                 dqry = new DoQuery.Builder(Application.Client.Ticket, Application.Token, Application.Client.AccountDomain, TableId)
                        .SetQuery(query)
                        .SetCList(collist)
                        .SetOptions(options)
                        .SetFmt(true)
                        .Build();
             }
             try
             {
                 XPathNavigator xml = dqry.Post().CreateNavigator();
                 if (fetched == 0)
                 {
                     LoadColumns(xml);
                 }
                 LoadRecords(xml);
                 fetched += stride;
             }
             catch (ViewTooLargeException)
             {
                 stride = stride / 2;
             }
             catch (ApiRequestLimitExceededException ex)
             {
                 TimeSpan waitTime = ex.WaitUntil - DateTime.Now;
                 System.Threading.Thread.Sleep(waitTime);
             }
         }
     }
 }