Example #1
0
        private static List <ForeignKey> GetForeignKey(Microsoft.SqlServer.Management.Smo.Table table)
        {
            var foreignKeys = new List <ForeignKey>();

            foreach (Microsoft.SqlServer.Management.Smo.ForeignKey item in table.ForeignKeys)
            {
                int action = -1;
                switch (item.DeleteAction)
                {
                case Microsoft.SqlServer.Management.Smo.ForeignKeyAction.NoAction:
                    action = 0;
                    break;

                case Microsoft.SqlServer.Management.Smo.ForeignKeyAction.SetNull:
                    action = 1;
                    break;

                case Microsoft.SqlServer.Management.Smo.ForeignKeyAction.Cascade:
                    action = 2;
                    break;

                case Microsoft.SqlServer.Management.Smo.ForeignKeyAction.SetDefault:
                    action = 3;
                    break;
                }
                foreignKeys.Add(new ForeignKey()
                {
                    PropertyName = item.Columns[0].Name,
                    FK_Name      = item.Name,
                    DeleteAction = action
                });
            }

            return(foreignKeys);
        }
        private static List <Index> GetIndexes(Microsoft.SqlServer.Management.Smo.Table table)
        {
            var indexes = new List <Index>();

            foreach (Microsoft.SqlServer.Management.Smo.Index item in table.Indexes)
            {
                if (item.IndexKeyType == Microsoft.SqlServer.Management.Smo.IndexKeyType.None)
                {
                    if (item.IsUnique == true)
                    {
                        indexes.Add(new Index()
                        {
                            PropertyName = item.IndexedColumns[0].Name,
                            IX_Name      = item.Name,
                            IndexType    = 2
                        });
                    }
                    else
                    {
                        indexes.Add(new Index()
                        {
                            PropertyName = item.IndexedColumns[0].Name,
                            IX_Name      = item.Name,
                            IndexType    = 0
                        });
                    }
                }
                else if (item.IndexKeyType == Microsoft.SqlServer.Management.Smo.IndexKeyType.DriPrimaryKey)
                {
                    indexes.Insert(0, new Index()
                    {
                        PropertyName = item.IndexedColumns[0].Name,
                        IX_Name      = item.Name,
                        IndexType    = 1
                    });
                }
            }

            return(indexes);
        }
Example #3
0
        public static List <EntityProperty> ListColumnsOfTable(string dbName, string tableName)
        {
            var result = new List <EntityProperty>();

            var server = new Microsoft.SqlServer.Management.Smo.Server(serverName);
            var db     = new Microsoft.SqlServer.Management.Smo.Database(server, dbName);
            var table  = new Microsoft.SqlServer.Management.Smo.Table(db, tableName);

            table.Refresh();
            var dic = new Dictionary <string, string>();

            foreach (Microsoft.SqlServer.Management.Smo.ForeignKey item in table.ForeignKeys)
            {
                dic.Add(item.Columns[0].Name, item.ReferencedTable);
            }
            foreach (Microsoft.SqlServer.Management.Smo.Column item in table.Columns)
            {
                var propertyType = _typeMapping[item.DataType.Name];
                if (item.Nullable == true && propertyType != "string")
                {
                    propertyType = propertyType + "?";
                }

                var entityProperty = new EntityProperty()
                {
                    PropertyType = propertyType,
                    PropertyName = item.Name,
                    IsForeignKey = item.IsForeignKey,
                    IsIdentity   = item.Identity
                };
                if (item.IsForeignKey == true)
                {
                    entityProperty.ForeignKeyTableName = UpperFirstLetter(dic[item.Name]);
                }
                result.Add(entityProperty);
            }
            return(result);
        }
Example #4
0
        internal string WriteApiStation(GenerationInfo GenInfo, Microsoft.SqlServer.Management.Smo.Database datab, Microsoft.SqlServer.Management.Smo.Table t, string RepositoryPath)
        {
            string module_folder_name = String.Format(@"{0}\Modules", RepositoryPath);
            string file_path          = string.Format(@"{0}\{1}Module.cs", module_folder_name, t.Name);

            #region [ Build Module Folder Name ]
            if (Directory.Exists(module_folder_name) == false)
            {
                Directory.CreateDirectory(module_folder_name);
            }
            #endregion



            return("");
        }
Example #5
0
        private Model.Table GetNewTable(Microsoft.SqlServer.Management.Smo.Table smoTable)
        {
            Model.Table table = new Model.Table(smoTable.Name, Script.GetSingluar(smoTable.Name), false);

            #region Columns
            int ordinalPosition = 0;
            List <Microsoft.SqlServer.Management.Smo.Column> smoColumns = new List <Microsoft.SqlServer.Management.Smo.Column>();

            foreach (Microsoft.SqlServer.Management.Smo.Column smoColumn in smoTable.Columns)
            {
                smoColumns.Add(smoColumn);
            }
            foreach (Microsoft.SqlServer.Management.Smo.Column smoColumn in smoColumns)
            {
                if (UnsupportedDataTypes.ToLower().IndexOf("'" + smoColumn.DataType.Name.ToLower() + "'") >= 0)
                {
                    continue;
                }

                // Some columns do not have default values
                string defaultValue = null;
                try
                {
                    defaultValue = smoColumn.Default;
                }
                catch { }

                Column column = new Column(smoColumn.Name, Script.GetSingluar(smoColumn.Name), false, smoColumn.Name, table, ordinalPosition, smoColumn.Nullable, smoColumn.DataType.Name,
                                           smoColumn.DataType.MaximumLength, smoColumn.InPrimaryKey, smoColumn.Identity, defaultValue, smoColumn.Computed);
                table.AddColumn(column);
                ordinalPosition++;
            }
            #endregion

            #region Indexes
            //foreach (Microsoft.SqlServer.Management.Smo.Index smoIndex in smoTable.Indexes)
            //{
            //    string indexType;
            //    string indexKeyType = smoIndex.IndexKeyType.ToString();
            //    if (indexKeyType == "DriPrimaryKey")
            //    {
            //        continue;
            //    }
            //    else if (indexKeyType == "DriUniqueKey")
            //    {
            //        continue;
            //    }
            //    else if (indexKeyType == "None")
            //    {
            //        continue;
            //        //indexType = DatabaseConstant.IndexType.None;
            //    }
            //    else
            //    {
            //        throw new Exception("IndexType " + indexKeyType + " Not Defined");
            //    }

            //    // Create Alias
            //    string indexAlias = indexType + "_";
            //    for (int i = 0; i <= smoIndex.IndexedColumns.Count - 1; i++)
            //    {
            //        Microsoft.SqlServer.Management.Smo.IndexedColumn smoIndexedColumn = smoIndex.IndexedColumns[i];

            //        indexAlias += smoIndexedColumn.Name;
            //        if (i < smoIndex.IndexedColumns.Count - 1)
            //        {
            //            indexAlias += "And";
            //        }
            //    }

            //    Index index = new Index(smoIndex.Name, Script.GetSingluar(indexAlias), false, indexType, table);

            //    // Fill Columns
            //    foreach (Microsoft.SqlServer.Management.Smo.IndexedColumn smoColumn in smoIndex.IndexedColumns)
            //    {
            //        Column indexColumn = new Column(smoColumn.Name, Script.GetSingluar(smoColumn.Name), false);
            //        index.AddColumn(indexColumn);
            //    }

            //    table.AddIndex(index);
            //}

            // Indexes -- that should be keys
            foreach (Microsoft.SqlServer.Management.Smo.Index smoIndex in smoTable.Indexes)
            {
                string keyType;
                string indexKeyType = smoIndex.IndexKeyType.ToString();
                if (indexKeyType == "DriPrimaryKey")
                {
                    keyType = DatabaseConstant.KeyType.Primary;
                }
                else if (indexKeyType == "DriUniqueKey")
                {
                    keyType = DatabaseConstant.KeyType.Unique;
                }
                else if (indexKeyType == "None")
                {
                    keyType = DatabaseConstant.KeyType.None;
                    continue;
                }
                else
                {
                    throw new Exception("KeyType " + indexKeyType + " Not Defined");
                }

                // Create Alias
                string keyAlias = keyType + "_";
                for (int i = 0; i <= smoIndex.IndexedColumns.Count - 1; i++)
                {
                    Microsoft.SqlServer.Management.Smo.Column smoColumn = smoTable.Columns[smoIndex.IndexedColumns[i].Name];

                    keyAlias += smoColumn.Name;
                    if (i < smoIndex.IndexedColumns.Count - 1)
                    {
                        keyAlias += "And";
                    }
                }

                Key key = new Key(smoIndex.Name, Script.GetSingluar(keyAlias), false, keyType, table);

                // Fill Columns
                foreach (Microsoft.SqlServer.Management.Smo.IndexedColumn smoColumn in smoIndex.IndexedColumns)
                {
                    Column keyColumn = new Column(smoColumn.Name, Script.GetSingluar(smoColumn.Name), false);
                    key.AddColumn(keyColumn);
                }

                table.AddKey(key);
            }
            #endregion

            #region Keys
            foreach (Microsoft.SqlServer.Management.Smo.ForeignKey smoForeignKey in smoTable.ForeignKeys)
            {
                string keyType = DatabaseConstant.KeyType.Foreign;

                // Create Alias
                string keyAlias = keyType + "_";
                for (int i = 0; i <= smoForeignKey.Columns.Count - 1; i++)
                {
                    Microsoft.SqlServer.Management.Smo.Column smoColumn = smoTable.Columns[smoForeignKey.Columns[i].Name];

                    keyAlias += smoColumn.Name;
                    if (i < smoForeignKey.Columns.Count - 1)
                    {
                        keyAlias += "And";
                    }
                }

                Key key = new Key(smoForeignKey.Name, Script.GetSingluar(keyAlias), false, keyType, table);

                // Fill Columns
                foreach (Microsoft.SqlServer.Management.Smo.ForeignKeyColumn smoColumn in smoForeignKey.Columns)
                {
                    Column keyColumn = new Column(smoColumn.Name, Script.GetSingluar(smoColumn.Name), false);
                    key.AddColumn(keyColumn);
                }

                // Fill References
                key.ReferencedTable = new Model.Table(smoForeignKey.ReferencedTable, Script.GetSingluar(smoForeignKey.ReferencedTable), false);
                key.ReferencedKey   = new Key(smoForeignKey.ReferencedKey, Script.GetSingluar(smoForeignKey.ReferencedKey), false);

                // Fill Referenced Columns
                foreach (Microsoft.SqlServer.Management.Smo.ForeignKeyColumn smoColumn in smoForeignKey.Columns)
                {
                    Column referencedKeyColumn = new Column(smoColumn.ReferencedColumn, Script.GetSingluar(smoColumn.ReferencedColumn), false);
                    key.AddReferencedColumn(referencedKeyColumn);
                }

                table.AddKey(key);
            }
            #endregion

            return(table);
        }