Ejemplo n.º 1
0
        /// <summary>
        /// Loads database schema
        /// </summary>
        /// <param name="databaseName"></param>
        /// <param name="nameAliases"></param>
        /// <param name="nameFormat"></param>
        /// <param name="loadStoredProcedures"></param>
        /// <param name="contextNamespace"></param>
        /// <param name="entityNamespace"></param>
        /// <returns></returns>
        public virtual Database Load(string databaseName, INameAliases nameAliases, NameFormat nameFormat,
            bool loadStoredProcedures, string contextNamespace, string entityNamespace)
        {
            // check if connection is open. Note: we may use something more flexible
            if (Connection.State != ConnectionState.Open)
                Connection.Open();

            // get the database name. If we don't have one, take it from connection string...
            if (string.IsNullOrEmpty(databaseName))
                databaseName = Connection.Database;
            // ... and if connection string doesn't provide a name, then throw an error
            if (string.IsNullOrEmpty(databaseName))
                throw new ArgumentException("A database name is required. Please specify /database=<databaseName>");

            databaseName = GetDatabaseNameAliased(databaseName, nameAliases);

            var schemaName = NameFormatter.GetSchemaName(databaseName, GetExtraction(databaseName), nameFormat);
            var names = new Names();
            var schema = new Database
                             {
                                 Name = schemaName.DbName,
                                 Class = GetRuntimeClassName(schemaName.ClassName, nameAliases),
                                 BaseType = typeof(DataContext).FullName,
                                 ContextNamespace = contextNamespace,
                                 EntityNamespace = entityNamespace,
                             };

            // order is important, we must have:
            // 1. tables
            // 2. columns
            // 3. constraints
            LoadTables(schema, schemaName, Connection, nameAliases, nameFormat, names);
            LoadColumns(schema, schemaName, Connection, nameAliases, nameFormat, names);
            CheckColumnsName(schema);
            LoadConstraints(schema, schemaName, Connection, nameFormat, names);
            CheckConstraintsName(schema);
            if (loadStoredProcedures)
                LoadStoredProcedures(schema, schemaName, Connection, nameFormat);
            // names aren't checked here anymore, because this confuses DBML editor.
            // they will (for now) be checked before .cs generation
            // in the end, when probably will end up in mapping source (or somewhere around)
            //CheckNamesSafety(schema);

            // generate backing fields name (since we have here correct names)
            GenerateStorageAndMemberFields(schema);

            return schema;
        }
Ejemplo n.º 2
0
        public virtual Database Load(string databaseName, INameAliases nameAliases, NameFormat nameFormat,
            bool loadStoredProcedures, string contextNamespace, string entityNamespace)
        {
            // check if connection is open. Note: we may use something more flexible
            if (Connection.State != ConnectionState.Open)
                Connection.Open();

            // get the database name. If we don't have one, take it from connection string...
            if (string.IsNullOrEmpty(databaseName))
                databaseName = Connection.Database;
            // ... and if connection string doesn't provide a name, then throw an error
            if (string.IsNullOrEmpty(databaseName))
                throw new ArgumentException("A database name is required. Please specify /database=<databaseName>");

            var schemaName = NameFormatter.GetSchemaName(databaseName, GetExtraction(databaseName), nameFormat);
            var names = new Names();
            var schema = new Database
                             {
                                 Name = schemaName.DbName,
                                 Class = schemaName.ClassName,
                                 BaseType = typeof(DataContext).FullName,
                                 ContextNamespace = contextNamespace,
                                 EntityNamespace = entityNamespace,
                             };

            // order is important, we must have:
            // 1. tables
            // 2. columns
            // 3. constraints
            LoadTables(schema, schemaName, Connection, nameAliases, nameFormat, names);
            LoadColumns(schema, schemaName, Connection, nameAliases, nameFormat, names);
            LoadConstraints(schema, schemaName, Connection, nameFormat, names);
            if (loadStoredProcedures)
                LoadStoredProcedures(schema, schemaName, Connection, nameFormat);
            CheckNamesCaseSafety(schema);

            // check for duplicate names between properties
            CheckNames(schema);
            // generate backing fields name (since we have here correct names)
            GenerateStorageFields(schema);

            return schema;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads the columns.
        /// </summary>
        /// <param name="schema">The schema.</param>
        /// <param name="schemaName">Name of the schema.</param>
        /// <param name="conn">The conn.</param>
        /// <param name="nameAliases">The name aliases.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <param name="names">The names.</param>
        protected void LoadColumns(Database schema, SchemaName schemaName, IDbConnection conn, INameAliases nameAliases, NameFormat nameFormat, Names names)
        {
            var columnRows = ReadColumns(conn, schemaName.DbName);
            foreach (var columnRow in columnRows)
            {
                var columnName = CreateColumnName(columnRow.ColumnName, columnRow.TableName, columnRow.TableSchema, nameAliases, nameFormat);
                names.AddColumn(columnRow.TableName, columnName);

                //find which table this column belongs to
                string fullColumnDbName = GetFullDbName(columnRow.TableName, columnRow.TableSchema);
                DbLinq.Schema.Dbml.Table tableSchema = schema.Tables.FirstOrDefault(tblSchema => fullColumnDbName == tblSchema.Name);
                if (tableSchema == null)
                {
                    WriteErrorLine("ERROR L46: Table '" + columnRow.TableName + "' not found for column " + columnRow.ColumnName);
                    continue;
                }
                var column = new Column();
                column.Name = columnName.DbName;
                column.Member = columnName.PropertyName;
                column.DbType = columnRow.FullType;

                if (columnRow.PrimaryKey.HasValue)
                    column.IsPrimaryKey = columnRow.PrimaryKey.Value;

                bool? generated = (nameAliases != null) ? nameAliases.GetColumnGenerated(columnRow.ColumnName, columnRow.TableName, columnRow.TableSchema) : null;
                if (!generated.HasValue)
                    generated = columnRow.Generated;
                if (generated.HasValue)
                    column.IsDbGenerated = generated.Value;

                AutoSync? autoSync = (nameAliases != null) ? nameAliases.GetColumnAutoSync(columnRow.ColumnName, columnRow.TableName, columnRow.TableSchema) : null;
                if (autoSync.HasValue)
                    column.AutoSync = autoSync.Value;

                // the Expression can originate from two sources:
                // 1. DefaultValue
                // 2. Expression
                // we use any valid source (we can't have both)
                if (column.IsDbGenerated && columnRow.DefaultValue != null)
                    column.Expression = columnRow.DefaultValue;

                column.CanBeNull = columnRow.Nullable;

                string columnTypeAlias = nameAliases != null ? nameAliases.GetColumnForcedType(columnRow.ColumnName, columnRow.TableName, columnRow.TableSchema) : null;
                var columnType = MapDbType(columnName.DbName, columnRow);

                var columnEnumType = columnType as EnumType;
                if (columnEnumType != null)
                {
                    var enumType = column.SetExtendedTypeAsEnumType();
                    enumType.Name = columnEnumType.Name;
                    foreach (KeyValuePair<string, int> enumValue in columnEnumType.EnumValues)
                    {
                        enumType[enumValue.Key] = enumValue.Value;
                    }
                }
                else if (columnTypeAlias != null)
                    column.Type = columnTypeAlias;
                else
                    column.Type = columnType.ToString();

                tableSchema.Type.Columns.Add(column);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Loads the tables in the given schema.
        /// </summary>
        /// <param name="schema">The schema.</param>
        /// <param name="schemaName">Name of the schema.</param>
        /// <param name="conn">The conn.</param>
        /// <param name="nameAliases">The name aliases.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <param name="names">The names.</param>
        protected virtual void LoadTables(Database schema, SchemaName schemaName, IDbConnection conn, INameAliases nameAliases, NameFormat nameFormat, Names names)
        {
            var tables = ReadTables(conn, schemaName.DbName);
            foreach (var row in tables)
            {
                var tableName = CreateTableName(row.Name, row.Schema, nameAliases, nameFormat);
                names.TablesNames[tableName.DbName] = tableName;

                var table = new Table();
                table.Name = tableName.DbName;
                table.Member = tableName.MemberName;
                table.Type.Name = tableName.ClassName;
                schema.Tables.Add(table);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Creates the name of the column.
 /// </summary>
 /// <param name="dbColumnName">Name of the db column.</param>
 /// <param name="dbTableName">Name of the db table.</param>
 /// <param name="dbSchema">The db schema.</param>
 /// <param name="nameAliases">The name aliases.</param>
 /// <param name="nameFormat">The name format.</param>
 /// <returns></returns>
 protected virtual ColumnName CreateColumnName(string dbColumnName, string dbTableName, string dbSchema, INameAliases nameAliases, NameFormat nameFormat)
 {
     var columnNameAlias = nameAliases != null ? nameAliases.GetColumnMemberAlias(dbColumnName, dbTableName, dbSchema) : null;
     WordsExtraction extraction;
     if (columnNameAlias != null)
     {
         extraction = WordsExtraction.None;
     }
     else
     {
         extraction = GetExtraction(dbColumnName);
         columnNameAlias = dbColumnName;
     }
     var columnName = NameFormatter.GetColumnName(columnNameAlias, extraction, nameFormat);
     // The member name can not be the same as the class
     // we add a "1" (just like SqlMetal does)
     var tableName = CreateTableName(dbTableName, dbSchema, nameAliases, nameFormat);
     if (columnName.PropertyName == tableName.ClassName)
         columnName.PropertyName = columnName.PropertyName + "1";
     columnName.DbName = dbColumnName;
     return columnName;
 }
Ejemplo n.º 6
0
 protected virtual TableName CreateTableName(string dbTableName, string dbSchema, INameAliases nameAliases, NameFormat nameFormat)
 {
     return CreateTableName(dbTableName, dbSchema, nameAliases, nameFormat, GetExtraction(dbTableName));
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates the name of the table given a name and schema
        /// </summary>
        /// <param name="dbTableName">Name of the db table.</param>
        /// <param name="dbSchema">The db schema.</param>
        /// <param name="nameAliases">The name aliases.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <param name="extraction">The extraction.</param>
        /// <returns></returns>
        protected virtual TableName CreateTableName(string dbTableName, string dbSchema, INameAliases nameAliases, NameFormat nameFormat, WordsExtraction extraction)
        {
            // if we have an alias, use it, and don't try to analyze it (a human probably already did the job)
            var tableTypeAlias = nameAliases != null ? nameAliases.GetTableTypeAlias(dbTableName, dbSchema) : null;
            if (tableTypeAlias != null)
                extraction = WordsExtraction.None;
            else
                tableTypeAlias = dbTableName;

            var tableName = NameFormatter.GetTableName(tableTypeAlias, extraction, nameFormat);

            // alias for member
            var tableMemberAlias = nameAliases != null ? nameAliases.GetTableMemberAlias(dbTableName, dbSchema) : null;
            if (tableMemberAlias != null)
                tableName.MemberName = tableMemberAlias;

            tableName.DbName = GetFullDbName(dbTableName, dbSchema);
            return tableName;
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Gets a usable name for the database class.
 /// </summary>
 /// <param name="databaseName">Name of the clas.</param>
 /// <returns></returns>
 protected virtual string GetRuntimeClassName(string className, INameAliases nameAliases)
 {
     string classNameAliased = nameAliases != null ? nameAliases.GetClassNameAlias(className) : null;
     return (classNameAliased != null) ? classNameAliased : className;
 }
        /// <summary>
        /// Gets a usable name for the database class.
        /// </summary>
        /// <param name="databaseName">Name of the clas.</param>
        /// <returns></returns>
        protected virtual string GetRuntimeClassName(string className, INameAliases nameAliases)
        {
            string classNameAliased = nameAliases != null?nameAliases.GetClassNameAlias(className) : null;

            return((classNameAliased != null) ? classNameAliased : className);
        }
Ejemplo n.º 10
0
 protected virtual ColumnName CreateColumnName(string dbColumnName, string dbTableName, string dbSchema, INameAliases nameAliases, NameFormat nameFormat)
 {
     var columnNameAlias = nameAliases != null ? nameAliases.GetColumnMemberAlias(dbColumnName, dbTableName, dbSchema) : null;
     WordsExtraction extraction;
     if (columnNameAlias != null)
     {
         extraction = WordsExtraction.None;
     }
     else
     {
         extraction = GetExtraction(dbColumnName);
         columnNameAlias = dbColumnName;
     }
     var columnName = NameFormatter.GetColumnName(columnNameAlias, extraction, nameFormat);
     columnName.DbName = dbColumnName;
     return columnName;
 }
        /// <summary>
        /// Loads the columns.
        /// </summary>
        /// <param name="schema">The schema.</param>
        /// <param name="schemaName">Name of the schema.</param>
        /// <param name="conn">The conn.</param>
        /// <param name="nameAliases">The name aliases.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <param name="names">The names.</param>
        protected void LoadColumns(Database schema, SchemaName schemaName, IDbConnection conn, INameAliases nameAliases, NameFormat nameFormat, Names names)
        {
            var columnRows = ReadColumns(conn, schemaName.DbName);

            foreach (var columnRow in columnRows)
            {
                var columnName = CreateColumnName(columnRow.ColumnName, columnRow.TableName, columnRow.TableSchema, nameAliases, nameFormat);
                names.AddColumn(columnRow.TableName, columnName);

                //find which table this column belongs to
                string fullColumnDbName = GetFullDbName(columnRow.TableName, columnRow.TableSchema);
                DbLinq.Schema.Dbml.Table tableSchema = schema.Tables.FirstOrDefault(tblSchema => fullColumnDbName == tblSchema.Name);
                if (tableSchema == null)
                {
                    WriteErrorLine("ERROR L46: Table '" + columnRow.TableName + "' not found for column " + columnRow.ColumnName);
                    continue;
                }
                var column = new Column();
                column.Name   = columnName.DbName;
                column.Member = columnName.PropertyName;
                column.DbType = columnRow.FullType;

                if (columnRow.PrimaryKey.HasValue)
                {
                    column.IsPrimaryKey = columnRow.PrimaryKey.Value;
                }

                bool?generated = (nameAliases != null) ? nameAliases.GetColumnGenerated(columnRow.ColumnName, columnRow.TableName, columnRow.TableSchema) : null;
                if (!generated.HasValue)
                {
                    generated = columnRow.Generated;
                }
                if (generated.HasValue)
                {
                    column.IsDbGenerated = generated.Value;
                }

                AutoSync?autoSync = (nameAliases != null) ? nameAliases.GetColumnAutoSync(columnRow.ColumnName, columnRow.TableName, columnRow.TableSchema) : null;
                if (autoSync.HasValue)
                {
                    column.AutoSync = autoSync.Value;
                }

                // the Expression can originate from two sources:
                // 1. DefaultValue
                // 2. Expression
                // we use any valid source (we can't have both)
                if (column.IsDbGenerated && columnRow.DefaultValue != null)
                {
                    column.Expression = columnRow.DefaultValue;
                }

                column.CanBeNull = columnRow.Nullable;

                string columnTypeAlias = nameAliases != null?nameAliases.GetColumnForcedType(columnRow.ColumnName, columnRow.TableName, columnRow.TableSchema) : null;

                var columnType = MapDbType(columnName.DbName, columnRow);

                var columnEnumType = columnType as EnumType;
                if (columnEnumType != null)
                {
                    var enumType = column.SetExtendedTypeAsEnumType();
                    enumType.Name = columnEnumType.Name;
                    foreach (KeyValuePair <string, int> enumValue in columnEnumType.EnumValues)
                    {
                        enumType[enumValue.Key] = enumValue.Value;
                    }
                }
                else if (columnTypeAlias != null)
                {
                    column.Type = columnTypeAlias;
                }
                else
                {
                    column.Type = columnType.ToString();
                }

                tableSchema.Type.Columns.Add(column);
            }
        }
        /// <summary>
        /// Loads the tables in the given schema.
        /// </summary>
        /// <param name="schema">The schema.</param>
        /// <param name="schemaName">Name of the schema.</param>
        /// <param name="conn">The conn.</param>
        /// <param name="nameAliases">The name aliases.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <param name="names">The names.</param>
        protected virtual void LoadTables(Database schema, SchemaName schemaName, IDbConnection conn, INameAliases nameAliases, NameFormat nameFormat, Names names)
        {
            var tables = ReadTables(conn, schemaName.DbName);

            foreach (var row in tables)
            {
                var tableName = CreateTableName(row.Name, row.Schema, nameAliases, nameFormat);
                names.TablesNames[tableName.DbName] = tableName;

                var table = new Table();
                table.Name      = tableName.DbName;
                table.Member    = tableName.MemberName;
                table.Type.Name = tableName.ClassName;
                schema.Tables.Add(table);
            }
        }
        /// <summary>
        /// Creates the name of the column.
        /// </summary>
        /// <param name="dbColumnName">Name of the db column.</param>
        /// <param name="dbTableName">Name of the db table.</param>
        /// <param name="dbSchema">The db schema.</param>
        /// <param name="nameAliases">The name aliases.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <returns></returns>
        protected virtual ColumnName CreateColumnName(string dbColumnName, string dbTableName, string dbSchema, INameAliases nameAliases, NameFormat nameFormat)
        {
            var columnNameAlias = nameAliases != null?nameAliases.GetColumnMemberAlias(dbColumnName, dbTableName, dbSchema) : null;

            WordsExtraction extraction;

            if (columnNameAlias != null)
            {
                extraction = WordsExtraction.None;
            }
            else
            {
                extraction      = GetExtraction(dbColumnName);
                columnNameAlias = dbColumnName;
            }
            var columnName = NameFormatter.GetColumnName(columnNameAlias, extraction, nameFormat);
            // The member name can not be the same as the class
            // we add a "1" (just like SqlMetal does)
            var tableName = CreateTableName(dbTableName, dbSchema, nameAliases, nameFormat);

            if (columnName.PropertyName == tableName.ClassName)
            {
                columnName.PropertyName = columnName.PropertyName + "1";
            }

            if (startsWithNumber.IsMatch(columnName.PropertyName))
            {
                columnName.PropertyName = "_" + columnName.PropertyName;
            }

            columnName.DbName = dbColumnName;
            return(columnName);
        }
 protected virtual TableName CreateTableName(string dbTableName, string dbSchema, INameAliases nameAliases, NameFormat nameFormat)
 {
     return(CreateTableName(dbTableName, dbSchema, nameAliases, nameFormat, GetExtraction(dbTableName)));
 }
        /// <summary>
        /// Creates the name of the table given a name and schema
        /// </summary>
        /// <param name="dbTableName">Name of the db table.</param>
        /// <param name="dbSchema">The db schema.</param>
        /// <param name="nameAliases">The name aliases.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <param name="extraction">The extraction.</param>
        /// <returns></returns>
        protected virtual TableName CreateTableName(string dbTableName, string dbSchema, INameAliases nameAliases, NameFormat nameFormat, WordsExtraction extraction)
        {
            // if we have an alias, use it, and don't try to analyze it (a human probably already did the job)
            var tableTypeAlias = nameAliases != null?nameAliases.GetTableTypeAlias(dbTableName, dbSchema) : null;

            if (tableTypeAlias != null)
            {
                extraction = WordsExtraction.None;
            }
            else
            {
                tableTypeAlias = dbTableName;
            }

            var tableName = NameFormatter.GetTableName(tableTypeAlias, extraction, nameFormat);

            // alias for member
            var tableMemberAlias = nameAliases != null?nameAliases.GetTableMemberAlias(dbTableName, dbSchema) : null;

            if (tableMemberAlias != null)
            {
                tableName.MemberName = tableMemberAlias;
            }

            tableName.DbName = GetFullDbName(dbTableName, dbSchema);
            return(tableName);
        }
Ejemplo n.º 16
0
 protected virtual string GetDatabaseNameAliased(string databaseName, INameAliases nameAliases)
 {
     string databaseNameAliased = nameAliases != null ? nameAliases.GetDatabaseNameAlias(databaseName) : null;
     return (databaseNameAliased != null) ? databaseNameAliased : GetDatabaseName(databaseName);
 }
Ejemplo n.º 17
0
 protected override TableName CreateTableName(string dbTableName, string dbSchema, INameAliases nameAliases, NameFormat nameFormat)
 {
     return CreateTableName(dbTableName, dbSchema, nameAliases, nameFormat, WordsExtraction.FromDictionary);
 }
        protected virtual string GetDatabaseNameAliased(string databaseName, INameAliases nameAliases)
        {
            string databaseNameAliased = nameAliases != null?nameAliases.GetDatabaseNameAlias(databaseName) : null;

            return((databaseNameAliased != null) ? databaseNameAliased : GetDatabaseName(databaseName));
        }