Пример #1
0
        public IMetadataRetrievable ImportSource(Type poDataStructType, object poDataStructure = null)
        {
            WonkaImportSource NewImportSource = new WonkaImportSource();

            PropertyInfo[] Props = poDataStructType.GetProperties();

            foreach (PropertyInfo TmpProperty in Props)
            {
                Type   AttrType  = TmpProperty.PropertyType;
                string sAttrName = TmpProperty.Name;

                WonkaRefAttr TmpWonkaAttr = new WonkaRefAttr();

                TmpWonkaAttr.AttrId   = GenerateNewAttrId();
                TmpWonkaAttr.AttrName = sAttrName;
                TmpWonkaAttr.ColName  = sAttrName;
                TmpWonkaAttr.TabName  = poDataStructType.FullName;

                if (poDataStructure != null)
                {
                    object oTmpValue = TmpProperty.GetValue(poDataStructure);

                    TmpWonkaAttr.DefaultValue = Convert.ToString(oTmpValue);
                }

                TmpWonkaAttr.Description = "";

                TmpWonkaAttr.IsDate    = IsTypeDate(AttrType.Name);
                TmpWonkaAttr.IsNumeric = IsTypeNumeric(AttrType.Name);
                TmpWonkaAttr.IsDecimal = IsTypeDecimal(AttrType.Name);

                // NOTE: These values are simply defaults and have no real meaning
                if (TmpWonkaAttr.IsNumeric)
                {
                    TmpWonkaAttr.Precision = 9;
                    TmpWonkaAttr.Scale     = 0;
                }
                else if (TmpWonkaAttr.IsDecimal)
                {
                    TmpWonkaAttr.Precision = 9;
                    TmpWonkaAttr.Scale     = 9;
                }

                // TmpWonkaAttr.MaxLength = ?;

                TmpWonkaAttr.FieldId   = TmpWonkaAttr.AttrId + 1000;
                TmpWonkaAttr.GroupId   = CONST_DEFAULT_GROUP_ID;
                TmpWonkaAttr.IsAudited = true;

                TmpWonkaAttr.IsKey = (TmpWonkaAttr.AttrName.EndsWith("ID") || TmpWonkaAttr.AttrName.EndsWith("Id"));

                NewImportSource.AddAttribute(TmpWonkaAttr);
            }

            return(NewImportSource);
        }
Пример #2
0
        public IMetadataRetrievable ImportSource(HashSet <Type> poDataStructList)
        {
            WonkaImportSource NewAggregateSource = new WonkaImportSource();

            if (poDataStructList != null)
            {
                foreach (Type TempType in poDataStructList)
                {
                    IMetadataRetrievable TempSource = ImportSource(TempType);

                    TempSource.GetAttrCache().ForEach(x => NewAggregateSource.AddAttribute(x));
                    TempSource.GetGroupCache().ForEach(x => NewAggregateSource.AddGroup(x));
                }
            }

            return(NewAggregateSource);
        }
Пример #3
0
        public IMetadataRetrievable ImportSource(string psDatabaseTable, SqlConnection poDbConn)
        {
            bool bTableFound = false;

            WonkaImportSource NewImportSource = new WonkaImportSource();

            if ((poDbConn == null) || (poDbConn.State != ConnectionState.Open))
            {
                throw new WonkaImportException("ERROR!  Provided connection is either null or is not open.");
            }

            using (SqlCommand ValidateTableCmd = new SqlCommand(CONST_VALIDATE_TABLE_SQL, poDbConn))
            {
                ValidateTableCmd.CommandType = CommandType.Text;

                SqlParameter TableParam = new SqlParameter("@tname", SqlDbType.NVarChar, 128);
                TableParam.Value = psDatabaseTable;

                ValidateTableCmd.Parameters.Add(TableParam);

                using (SqlDataReader ValidateTableReader = ValidateTableCmd.ExecuteReader())
                {
                    if (ValidateTableReader.Read())
                    {
                        bTableFound = true;
                    }
                }
            }

            if (!bTableFound)
            {
                throw new WonkaImportException("ERROR!  Table (" + psDatabaseTable + ") not found within the schema.");
            }

            DataTable        TableSchema = null;
            HashSet <string> KeyAttrList = new HashSet <string>();

            // NOTE: Possible issues with ingesting the schema of a large table
            using (SqlCommand QueryTableCmd = poDbConn.CreateCommand())
            {
                QueryTableCmd.CommandText = "select * from " + psDatabaseTable;
                QueryTableCmd.CommandType = CommandType.Text;

                using (SqlDataReader QueryTableReader = QueryTableCmd.ExecuteReader(CommandBehavior.KeyInfo))
                {
                    TableSchema = QueryTableReader.GetSchemaTable();
                }
            }

            int nColumnCount = TableSchema.Columns.Count;

            foreach (DataRow TmpColInfoRow in TableSchema.Rows)
            {
                string sTmpColName = TmpColInfoRow["ColumnName"].ToString();
                string sTmpColType = TmpColInfoRow["DataType"].ToString();

                if (sTmpColType.Contains("System."))
                {
                    sTmpColType = sTmpColType.Replace("System.", "");
                }

                WonkaRefAttr TmpWonkaAttr = new WonkaRefAttr();

                TmpWonkaAttr.AttrId   = GenerateNewAttrId();
                TmpWonkaAttr.AttrName = sTmpColName;
                TmpWonkaAttr.ColName  = sTmpColName;
                TmpWonkaAttr.TabName  = psDatabaseTable;

                // TmpWonkaAttr.DefaultValue = TmpColInfoRow["DefaultValue"].ToString();
                // TmpWonkaAttr.Description  = (TmpCol.doc != null) ? TmpCol.doc.LongDescription : "";

                TmpWonkaAttr.IsDate    = IsTypeDate(sTmpColType);
                TmpWonkaAttr.IsNumeric = IsTypeNumeric(sTmpColType);
                TmpWonkaAttr.IsDecimal = IsTypeDecimal(sTmpColType);

                if (TmpWonkaAttr.IsNumeric || TmpWonkaAttr.IsDecimal)
                {
                    TmpWonkaAttr.Precision = Int32.Parse(TmpColInfoRow["NumericPrecision"].ToString());
                    TmpWonkaAttr.Scale     = Int32.Parse(TmpColInfoRow["NumericScale"].ToString());
                }
                else
                {
                    TmpWonkaAttr.MaxLength = Int32.Parse(TmpColInfoRow["ColumnSize"].ToString());
                }

                TmpWonkaAttr.FieldId   = TmpWonkaAttr.AttrId + 1000;
                TmpWonkaAttr.GroupId   = CONST_DEFAULT_GROUP_ID;
                TmpWonkaAttr.IsAudited = true;

                TmpWonkaAttr.IsKey = Boolean.Parse(TmpColInfoRow["IsKey"].ToString());
                if (TmpWonkaAttr.IsKey)
                {
                    KeyAttrList.Add(sTmpColName);
                }

                NewImportSource.AddAttribute(TmpWonkaAttr);
            }

            if (NewImportSource.GetAttrCache().Count <= 0)
            {
                throw new WonkaBizRuleException(0, 0, "ERROR!  Could not import the schema because the Reader's field count was zero.");
            }

            WonkaRefGroup NewImportGroup = new WonkaRefGroup();

            NewImportGroup.GroupId        = CONST_DEFAULT_GROUP_ID;
            NewImportGroup.GroupName      = psDatabaseTable;
            NewImportGroup.KeyTabCols     = KeyAttrList;
            NewImportGroup.ProductTabName = psDatabaseTable;
            NewImportSource.AddGroup(NewImportGroup);

            return(NewImportSource);
        }
Пример #4
0
        public IMetadataRetrievable ImportSource(string psDatabaseTable, IModel poDbContext)
        {
            WonkaImportSource NewImportSource = new WonkaImportSource();
            HashSet <string>  KeyColNames     = new HashSet <string>();

            IEntityType FoundTable = null;

            if (!String.IsNullOrEmpty(psDatabaseTable) && (poDbContext != null))
            {
                if (moCachedImports.ContainsKey(psDatabaseTable))
                {
                    return(moCachedImports[psDatabaseTable]);
                }

                var tables = poDbContext.GetEntityTypes(psDatabaseTable);

                foreach (var TmpTable in tables)
                {
                    if (TmpTable.Name == psDatabaseTable)
                    {
                        FoundTable = TmpTable;

                        var KeyCols = TmpTable.GetDeclaredKeys();
                        foreach (var KeyCol in KeyCols)
                        {
                            KeyColNames.Add(KeyCol.GetName());
                        }

                        break;
                    }
                }

                if (FoundTable == null)
                {
                    throw new WonkaImportException("ERROR!  Table (" + psDatabaseTable + ") was not found in the provided DbContext.");
                }

                var columns =
                    from p in FoundTable.GetProperties()
                    select new
                {
                    colName   = p.Name,
                    colType   = p.GetColumnType(),
                    maxLength = p.GetMaxLength(),
                    precision = 0,
                    scale     = 0,
                    defValue  = p.GetDefaultValue()
                };

                /*
                 * var columns =
                 *  from meta in poDbContext.MetadataWorkspace.GetItems(DataSpace.CSpace).Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
                 *  from p in (meta as EntityType).Properties.Where(p => p.DeclaringType.Name == psDatabaseTable)
                 *  select new
                 *  {
                 *      colName   = p.Name,
                 *      colType   = p.TypeUsage.EdmType,
                 *      doc       = p.Documentation,
                 *      maxLength = p.MaxLength,
                 *      precision = p.Precision,
                 *      scale     = p.Scale,
                 *      defValue  = p.DefaultValue,
                 *      props     = p.MetadataProperties
                 *  };
                 */

                foreach (var TmpCol in columns)
                {
                    string sTmpColName = TmpCol.colName;

                    WonkaRefAttr TmpWonkaAttr = new WonkaRefAttr();

                    TmpWonkaAttr.AttrId   = GenerateNewAttrId();
                    TmpWonkaAttr.AttrName = sTmpColName;
                    TmpWonkaAttr.ColName  = sTmpColName;
                    TmpWonkaAttr.TabName  = psDatabaseTable;

                    TmpWonkaAttr.DefaultValue = Convert.ToString(TmpCol.defValue);
                    // TmpWonkaAttr.Description  = (TmpCol.doc != null) ? TmpCol.doc.LongDescription : "";

                    TmpWonkaAttr.IsDate    = IsTypeDate(TmpCol.colType);
                    TmpWonkaAttr.IsNumeric = IsTypeNumeric(TmpCol.colType);
                    TmpWonkaAttr.IsDecimal = IsTypeDecimal(TmpCol.colType);

                    if (TmpWonkaAttr.IsNumeric || TmpWonkaAttr.IsDecimal)
                    {
                        // TmpWonkaAttr.Precision = (int) ((TmpCol.precision != null) ? TmpCol.precision : 0);
                        // TmpWonkaAttr.Scale     = (int) ((TmpCol.scale != null) ? TmpCol.scale : 0);
                        TmpWonkaAttr.Precision = TmpCol.precision;
                        TmpWonkaAttr.Scale     = TmpCol.scale;
                    }

                    TmpWonkaAttr.MaxLength = (TmpCol.maxLength != null) ? (int)TmpCol.maxLength : 0;

                    TmpWonkaAttr.FieldId   = TmpWonkaAttr.AttrId + 1000;
                    TmpWonkaAttr.GroupId   = CONST_DEFAULT_GROUP_ID;
                    TmpWonkaAttr.IsAudited = true;

                    TmpWonkaAttr.IsKey = KeyColNames.Contains(TmpWonkaAttr.AttrName);

                    NewImportSource.AddAttribute(TmpWonkaAttr);
                }

                if (NewImportSource.GetAttrCache().Count <= 0)
                {
                    throw new WonkaBizRuleException(0, 0, "ERROR!  Could not import the schema because the Reader's field count was zero.");
                }

                WonkaRefGroup NewImportGroup = new WonkaRefGroup();

                NewImportGroup.GroupId        = CONST_DEFAULT_GROUP_ID;
                NewImportGroup.GroupName      = psDatabaseTable;
                NewImportGroup.KeyTabCols     = KeyColNames;
                NewImportGroup.ProductTabName = psDatabaseTable;
                NewImportSource.AddGroup(NewImportGroup);

                WonkaRefSource GuestSource = new WonkaRefSource();

                GuestSource.SourceId   = 1;
                GuestSource.SourceName = "Guest";
                GuestSource.Status     = "Active";
                NewImportSource.AddSource(GuestSource);
            }
            else
            {
                throw new WonkaBizRuleException(0, 0, "ERROR!  Could not import the schema for the database table.");
            }

            PopulateDefaults();

            return(NewImportSource);
        }