Example #1
0
        /// <summary>
        /// Calculate & persist metatable stats for tables belonging to broker
        /// </summary>
        /// <returns></returns>

        public int UpdateMetaTableStatistics()
        {
            Dictionary <string, MetaTableStats> stats = new Dictionary <string, MetaTableStats>();

            //return stats.Count; // debug

            foreach (MetaTable mt in MetaTableCollection.TableMap.Values)
            {
                if (String.IsNullOrEmpty(mt.RowCountSql) && String.IsNullOrEmpty(mt.UpdateDateTimeSql))
                {
                    continue;                     // if no sql defined for stats then skip
                }
                string mtName = mt.Name.ToUpper();

                // Get row count

                string rowCountSql = null;

                if (Lex.Eq(mt.RowCountSql, "TableMap"))                 // use native source
                {
                    rowCountSql = "select count(*) from " + mt.TableMap;
                }

                else if (mt.RowCountSql != "")                 // use table-specific sql
                {
                    rowCountSql = mt.RowCountSql;
                }

                if (rowCountSql != null)
                {
                    try
                    {
                        int rowCount = SelectSingleValueDao.SelectInt(rowCountSql);
                        if (!stats.ContainsKey(mtName))
                        {
                            stats[mtName] = new MetaTableStats();
                        }
                        stats[mtName].RowCount = rowCount;
                    }
                    catch (Exception ex) { continue; }
                }

                // Get date

                string dateSql = null;

                if (Lex.Eq(mt.UpdateDateTimeSql, "TableMap"))                 // use native source
                {
                    for (int mci = mt.MetaColumns.Count - 1; mci >= 0; mci--)
                    {                     // search backwards for date col
                        MetaColumn mc = mt.MetaColumns[mci];
                        if (mc.DataType == MetaColumnType.Date)
                        {
                            dateSql = "select max(" + mc.Name + ") from " + mt.TableMap +
                                      " where " + mc.Name + " <= current_date";
                            break;
                        }
                    }
                }

                else if (mt.UpdateDateTimeSql != "")                 // use table-specific sql
                {
                    dateSql = mt.UpdateDateTimeSql;
                }


                if (dateSql != null)
                {
                    try
                    {
                        if (!stats.ContainsKey(mtName))
                        {
                            stats[mtName] = new MetaTableStats();
                        }
                        DateTime dt = SelectSingleValueDao.SelectDateTime(dateSql);
                        stats[mtName].UpdateDateTime = dt;
                    }
                    catch (Exception ex) { continue; }
                }
            }

            string fileName = MetaTableXmlFolder + @"\GenericMetaTableStats";

            WriteMetaTableStats(stats, fileName);
            return(stats.Count);
        }