Пример #1
0
        /// <summary>
        /// Get dictionary of UserObject tables
        /// </summary>
        /// <returns></returns>

        public static Dictionary <string, object> GetUserObjectTables()
        {
            if (UserObjectTables != null)
            {
                return(UserObjectTables);
            }

            IUserObjectTree iuot = InterfaceRefs.IUserObjectTree;

            if (iuot != null)             // be sure the user object tree is built
            {
                iuot.AssureTreeIsBuilt();
            }

            int t0 = TimeOfDay.Milliseconds();

            UserObjectTables = new Dictionary <string, object>();
            if (ContentsRoot == null)
            {
                return(null);                                  // Can't find root
            }
            AddNodeChildrenToUserObjectTables(ContentsRoot);
            t0 = TimeOfDay.Milliseconds() - t0;
            return(UserObjectTables);
        }
Пример #2
0
        /// <summary>
        /// Transform basic query to select all data for a compound number
        /// </summary>
        /// <param name="keyMt">Key metatable. If null then try to determine from key value</param>
        /// <param name="cn"></param>
        /// <returns></returns>

        public static Query TransformSelectAllDataQuery(
            Query originalQuery,
            QueryTable qt0,
            Query newQuery)
        {
            Query        q2 = null;
            MetaTable    mt;
            MetaColumn   mc;
            QueryTable   qt;
            QueryColumn  qc;
            MetaTreeNode mtn, tn;

            qc = qt0.GetQueryColumnByNameWithException("root_table");
            ParsedSingleCriteria psc = MqlUtil.ParseQueryColumnCriteria(qc);

            if (psc == null || Lex.IsUndefined(psc.Value))
            {
                throw new UserQueryException("Root table not defined");
            }
            string keyMtName = psc.Value;

            psc = MqlUtil.ParseSingleCriteria("cid " + originalQuery.KeyCriteria);
            if (psc == null || Lex.IsUndefined(psc.Value))
            {
                throw new UserQueryException("Compound Id not defined");
            }
            string cn = psc.Value;

            MetaTable keyMt = null;

            if (Lex.IsDefined(keyMtName))
            {
                keyMt = MetaTableCollection.Get(keyMtName);
            }

            if (keyMt != null && keyMt.Root.IsUserDatabaseStructureTable) // if root metatable is user database then normalize based on key
            {
                keyMt = keyMt.Root;                                       // be sure we have root
                cn    = CompoundId.Normalize(cn, keyMt);
            }

            else
            {
                cn    = CompoundId.Normalize(cn);
                keyMt = CompoundId.GetRootMetaTableFromCid(cn, keyMt);
                keyMt = keyMt.Root;                 // be sure we have root (may not be structure table)
            }

            if (keyMt == null)
            {
                throw new Exception("Failed to identify key MetaTable");
            }

            string allTableName = keyMt.Name + "_AllData";             // see if specific all-data tree node

            mtn = MetaTree.GetNode(allTableName);
            if (mtn == null)             // no special "_AllData" node, lookup in menu
            {
                foreach (MetaTreeNode parent in MetaTree.Nodes.Values)
                {
                    foreach (MetaTreeNode child in parent.Nodes)
                    {
                        if (Lex.Eq(child.Target, keyMt.Name))
                        {
                            mtn = parent;
                            break;
                        }
                    }
                }

                IUserObjectTree iuot = InterfaceRefs.IUserObjectTree;
                if (mtn == null && keyMt.IsUserDatabaseStructureTable && iuot != null)                 // see if user structure table & db
                {
                    int          userObjId  = UserObject.ParseObjectIdFromInternalName(keyMt.Name);
                    string       nodeItemId = "ANNOTATION_" + userObjId;
                    MetaTreeNode childMtn   = iuot.GetUserObjectNodeBytarget(nodeItemId);
                    if (childMtn != null && childMtn.Parent.Type == MetaTreeNodeType.Database)
                    {
                        mtn = childMtn.Parent;
                    }
                }
            }

            if (mtn == null)
            {
                return(null);
            }

            Query q = newQuery;

            for (int i1 = 0; i1 < mtn.Nodes.Count; i1++)
            {
                tn = (MetaTreeNode)mtn.Nodes[i1];
                if (!tn.IsDataTableType)
                {
                    continue;
                }

                mt = MetaTableCollection.Get(tn.Target);
                if (mt == null)
                {
                    continue;
                }
                if (mt.Root.Name != keyMt.Name)
                {
                    continue;                                             // must have same root
                }
                if (mt.MultiPivot && !mt.UseSummarizedData && mt.SummarizedExists)
                {
                    MetaTable mt2 = MetaTableCollection.Get(mt.Name + MetaTable.SummarySuffix);
                    if (mt2 != null)
                    {
                        mt = mt2;
                    }
                }

                //if (mt.RemapForRetrieval && mt.SummarizedExists) mt.UseSummarizedData = true; // get summarized multipivot data (not good, permanently changes the metatable)

                qt = new QueryTable(mt);
                //				if (Lex.Eq(mt.Name, "all_star_pivoted") || Lex.Eq(mt.Name, "all_annotation_pivoted")) mt = mt // debug;

                if (qt.SelectedCount > 0)                 // be sure something is selected
                {
                    q.AddQueryTable(qt);
                }
            }

            // See if a model query exists & use it or append to what we have already

            string fileName = ServicesDirs.ModelQueriesDir + @"\" + allTableName + ".qry";

            if (!ServerFile.GetLastWriteTime(fileName).Equals(DateTime.MinValue))             // model query file exist?
            {
                try
                {
                    string query2String = FileUtil.ReadFile(fileName);
                    q2 = Query.Deserialize(query2String);
                    q.MergeSubqueries(q2);                     // just use subquery
                }
                catch (Exception ex) { ex = ex; }
            }

            q.SetupQueryPagesAndViews(ResultsViewType.HtmlTable);             // be sure we have a default page & HTML view

            // Set key criteria

            q.KeyCriteria = " = " + cn;

            // Review tables (debug)

            //int tCnt = q.Tables.Count;
            //string tls = q.TableListString;
            //q.Tables.RemoveRange(23, 1);
            //q.Tables.RemoveRange(27, q.Tables.Count - 27);
            //q.Tables.RemoveRange(1, 25);

            // Get list of any inaccessible tables & remove from query

            q.InaccessableData = CheckDataSourceAccessibility(q);

            if (q.InaccessableData != null)
            {
                foreach (string schema in q.InaccessableData.Keys)
                {
                    foreach (string tName in q.InaccessableData[schema])
                    {
                        qt = q.GetQueryTableByName(tName);
                        if (qt != null && !qt.MetaTable.IsRootTable && q.Tables.Contains(qt))
                        {
                            q.RemoveQueryTable(qt);
                        }
                    }
                }

                //ShowUnavailableDataMessage(q);
                q.InaccessableData = null;
            }

            UsageDao.LogEvent("QueryAllData", "");

            //string mql = MqlUtil.ConvertQueryToMql(q); // debug

            return(q);
        }