コード例 #1
0
        public void TestTryGetLongFromValueImplicit()
        {
            long l;

            Assert.IsTrue(ExtendedEnum <Colours> .TryGetLong(Colours.Red | Colours.Green | Colours.Blue, out l, true));
            Assert.AreEqual(19, l);
        }
コード例 #2
0
        public void TestTryGetLongFromNameImplicit()
        {
            long l;

            Assert.IsTrue(ExtendedEnum <Colours> .TryGetLong("Red | Green | Blue", out l, true));
            Assert.AreEqual(19, l);
        }
コード例 #3
0
        private void MaskedEnable_FireAction(DynamicFormActionTrigger trigger, DynamicFormAction action, object sender, ModelPropertyChangedEventArgs e)
        {
            // find all controls and set enabled / disabled state using proper mask
            Type enumType = trigger.Param as Type;

            if (enumType == null)
            {
                // wrong settings
                _logger.Error(string.Format("MaskedEnable_FireAction: Wrong mask enum type!"));
                return;
            }

            int mask = 0;

            try
            {
                mask = ExtendedEnum.ToInt(enumType, e.Value); //e.Value != null && e.Value.ToString() != "" ? (int)System.Enum.Parse(enumType, e.Value.ToString(), true) : 0;
            }
            catch (Exception)
            {
                //_logger.Error(string.Format("MaskedEnable_FireAction: Wrong mask enum value [{0}]!", e.Value.ToString()));
            }


            // get control
            if (ReferenceEquals(null, action.Control))
            {
                action.Control = FindControlByPropertyName(action.FieldName);
            }

            if (!ReferenceEquals(null, action.Control))
            {
                // reset data using binding source of this action
                FilterObjectbase fo = action.BindingSource?.Current as FilterObjectbase;
                if (!ReferenceEquals(fo, null))
                {
                    action.Control.DataBindings[0].DataSourceUpdateMode = DataSourceUpdateMode.Never;
                    // reset field
                    fo.ResetFieldByName(action.FieldName);
                    // turn back DS update mode
                    action.Control.DataBindings[0].DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
                }
                // confront field bits with enable mask arrived from trigger
                // control remain enable only if all mask (trigger) bits are present allow bitset of field (action target)
                if ((((int)(object)action.Param & (int)(object)mask) > 0))
                {
                    action.Control.Enabled = true;
                }
                else
                {
                    action.Control.Enabled = false;
                }
                // set back default style
                if (action.Control is TextEdit)
                {
                    (action.Control as TextEdit).StyleController = DefaultStyles.ContainsKey((action.Control as TextEdit)) ? DefaultStyles[(action.Control as TextEdit)] : null;
                }
            }
        }
コード例 #4
0
        public void TestCheck()
        {
            ExtendedEnum <NotAFlag> .Check();

            ExtendedEnum <NotAFlag> .Check(false);

            ExtendedEnum <Colours> .Check();

            ExtendedEnum <Colours> .Check(TriState.True);
        }
コード例 #5
0
 public void TestGetLongFromValueExplicit()
 {
     long l = ExtendedEnum <Colours> .GetLong(Colours.Red | Colours.Green | Colours.Blue);
 }
コード例 #6
0
        public void TestTryGetLongFromNameImplicitFail()
        {
            long l;

            Assert.IsFalse(ExtendedEnum <Colours> .TryGetLong("Red | Green | Blue | Dog", out l, true));
        }
コード例 #7
0
        public void TestTryGetLongFromNameExplicit()
        {
            long l;

            Assert.IsFalse(ExtendedEnum <Colours> .TryGetLong("Red | Green | Blue", out l));
        }
コード例 #8
0
 public void TestGetLongFromNameImplicitFail()
 {
     long l = ExtendedEnum <Colours> .GetLong("Red | Green | Blue | Dog", true);
 }
コード例 #9
0
 public void TestGetLongFromNameExplicit()
 {
     long l = ExtendedEnum <Colours> .GetLong("Red | Green | Blue");
 }
コード例 #10
0
 public void TestIsNameDefinedExplicit()
 {
     Assert.IsTrue(ExtendedEnum <Colours> .IsDefined("Red"));
 }
コード例 #11
0
 public void TestIsDefinedImplicit()
 {
     Assert.IsTrue(ExtendedEnum <Colours> .IsDefined(32, true));
 }
コード例 #12
0
 public void TestIsDefinedExplicit()
 {
     Assert.IsTrue(ExtendedEnum <Colours> .IsDefined(Colours.Red));
 }
コード例 #13
0
 public void TestCheckFailFlag()
 {
     ExtendedEnum <NotAFlag> .Check(true);
 }
コード例 #14
0
 public void TestCheckFail()
 {
     ExtendedEnum <int> .Check();
 }
コード例 #15
0
        public void TestValueDetail()
        {
            /*
             * Simple example
             */
            ExtendedEnum <Colours> .ValueDetail valueDetail = Colours.Red.GetValueDetail();
            Assert.AreEqual("Red", valueDetail.Name);
            Assert.AreEqual(Colours.Red, valueDetail.Value);
            Assert.AreEqual(1L, valueDetail.RawValue);
            Assert.AreEqual(1, valueDetail.Flags);

            // We have duplicate names
            Assert.AreEqual(2, valueDetail.AllNames.Count());
            Assert.IsTrue(valueDetail.AllNames.Contains("Red"));
            Assert.IsTrue(valueDetail.AllNames.Contains("Rouge"));

            Assert.IsTrue(valueDetail.IsExplicit);
            Assert.IsFalse(valueDetail.IsCombination);
            Assert.AreEqual(string.Empty, valueDetail.Description);

            /*
             * The 'None' Value is special, in that is always exits (even if implicit)
             * And it's Flags count is 0.
             */
            valueDetail = Colours.DuplicateNoneName.GetValueDetail();
            Assert.AreEqual(ExtendedEnum <Colours> .NoneValueDetail, valueDetail);
            Assert.AreEqual("None", valueDetail.Name);
            Assert.AreEqual(Colours.None, valueDetail.Value);
            Assert.AreEqual(Colours.DuplicateNoneName, valueDetail.Value);
            Assert.AreEqual(0L, valueDetail.RawValue);
            Assert.AreEqual(0, valueDetail.Flags);

            // We have duplicate names
            Assert.AreEqual(2, valueDetail.AllNames.Count());
            Assert.IsTrue(valueDetail.AllNames.Contains("None"));
            Assert.IsTrue(valueDetail.AllNames.Contains("DuplicateNoneName"));

            // We are explicitly defined.
            Assert.IsTrue(valueDetail.IsExplicit);
            Assert.IsFalse(valueDetail.IsCombination);

            // Because we have duplicate definitions, the descriptions are concatenated
            Assert.AreEqual(
                string.Format(
                    "{0}{1}{2}",
                    "Descriptions are retrieved.",
                    Environment.NewLine,
                    "And concatenated."),
                valueDetail.Description);

            /*
             * The 'Mauve' Value is a combination value.
             */
            valueDetail = Colours.Mauve.GetValueDetail();
            Assert.AreEqual("Mauve", valueDetail.Name);
            Assert.AreEqual(Colours.Mauve, valueDetail.Value);
            Assert.AreEqual(48L, valueDetail.RawValue);
            // This is a combination flag so it's made up of two flags
            Assert.AreEqual(2, valueDetail.Flags);
            Assert.IsTrue(valueDetail.AllNames.Contains("Mauve"));
            Assert.IsTrue(valueDetail.IsExplicit);
            Assert.IsTrue(valueDetail.IsCombination);
            Assert.AreEqual(string.Empty, valueDetail.Description);

            /*
             * The '32' Value is implicit - it is defined as part of Mauve.
             * As such we have to allow implicits when we grab it's details.
             */
            valueDetail = ((Colours)32).GetValueDetail(true);
            Assert.AreEqual("32", valueDetail.Name);
            Assert.AreEqual(((Colours)32), valueDetail.Value);
            Assert.AreEqual(32L, valueDetail.RawValue);
            Assert.AreEqual(1, valueDetail.Flags);
            Assert.IsTrue(valueDetail.AllNames.Contains("32"));
            Assert.IsFalse(valueDetail.IsExplicit);
            Assert.IsFalse(valueDetail.IsCombination);
            Assert.AreEqual(string.Empty, valueDetail.Description);
        }
コード例 #16
0
        public void TestGetLongFromValueImplicit()
        {
            long l = ExtendedEnum <Colours> .GetLong(Colours.Red | Colours.Green | Colours.Blue, true);

            Assert.AreEqual(19, l);
        }
コード例 #17
0
        public void TestGetLongFromValueImplicitFail()
        {
            long l = ExtendedEnum <Colours> .GetLong((Colours)15, true);

            Assert.AreEqual(19, l);
        }
コード例 #18
0
 public void TestIsNameDefinedExplicitFail()
 {
     Assert.IsFalse(ExtendedEnum <Colours> .IsDefined("32"));
 }
コード例 #19
0
        public void TestGetLongFromNameImplicit()
        {
            long l = ExtendedEnum <Colours> .GetLong("Red | Green | Blue", true);

            Assert.AreEqual(19, l);
        }
コード例 #20
0
 public void TestIsNameDefinedImplicit()
 {
     Assert.IsTrue(ExtendedEnum <Colours> .IsDefined("32", true));
     Assert.IsTrue(ExtendedEnum <Colours> .IsDefined("Red | Blue", true));
 }
コード例 #21
0
        public void TestTryGetLongFromValueExplicit()
        {
            long l;

            Assert.IsFalse(ExtendedEnum <Colours> .TryGetLong(Colours.Red | Colours.Green | Colours.Blue, out l));
        }
コード例 #22
0
 public void TestIsNameDefinedImplicitFail()
 {
     Assert.IsFalse(ExtendedEnum <Colours> .IsDefined("8", true));
 }
コード例 #23
0
        public void TestTryGetLongFromValueImplicitFail()
        {
            long l;

            Assert.IsFalse(ExtendedEnum <Colours> .TryGetLong((Colours)90, out l, true));
        }
コード例 #24
0
        public void TestGetValueFromName()
        {
            Colours c = ExtendedEnum <Colours> .GetValue("Yellow | Blue", true);

            Assert.AreEqual(Colours.Yellow | Colours.Blue, c);
        }
コード例 #25
0
        public void TestValueFromName2()
        {
            Colours c = ExtendedEnum <Colours> .GetValue("Green | Red", true);

            Assert.AreEqual(Colours.Yellow, c);
        }
コード例 #26
0
        public void TestNumericalGetValue()
        {
            Colours c = ExtendedEnum <Colours> .GetValue("32 | Red", true);

            Assert.AreEqual((Colours)32 | Colours.Red, c);
        }
コード例 #27
0
 public void TestGetNameImplicitDirect()
 {
     Assert.AreEqual("32", ExtendedEnum <Colours> .GetName(32, true));
 }
コード例 #28
0
        private async Task <DatabaseSchema> Load(bool forceReload, CancellationToken cancellationToken)
        {
            Instant requested = TimeHelpers.Clock.Now;

            using (await _lock.LockAsync(cancellationToken).ConfigureAwait(false))
            {
                // Check to see if the currently loaded schema is acceptable.
                CurrentSchema current = _current;

                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                if ((current != null) &&
                    (!forceReload || (current.Loaded > requested)))
                {
                    // Rethrow load errors.
                    if (current.ExceptionDispatchInfo != null)
                    {
                        current.ExceptionDispatchInfo.Throw();
                    }

                    Debug.Assert(current.Schema != null);
                    return(this);
                }

                // Create dictionaries
                Dictionary <int, SqlSchema>  sqlSchemas  = new Dictionary <int, SqlSchema>();
                Dictionary <int, SqlType>    typesByID   = new Dictionary <int, SqlType>();
                Dictionary <string, SqlType> typesByName =
                    new Dictionary <string, SqlType>(StringComparer.InvariantCultureIgnoreCase);
                Dictionary <string, SqlProgramDefinition> programDefinitions =
                    new Dictionary <string, SqlProgramDefinition>(StringComparer.InvariantCultureIgnoreCase);
                Dictionary <string, SqlTableDefinition> tables =
                    new Dictionary <string, SqlTableDefinition>(StringComparer.InvariantCultureIgnoreCase);

                try
                {
                    // Open a connection
                    using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        await sqlConnection.OpenAsync(cancellationToken).ConfigureAwait(false);

                        Version version;
                        if (!Version.TryParse(sqlConnection.ServerVersion, out version))
                        {
                            throw new DatabaseSchemaException(
                                      () => Resources.DatabaseSchema_Load_CouldNotParseVersionInformation);
                        }
                        Debug.Assert(version != null);

                        if (version.Major < 9)
                        {
                            throw new DatabaseSchemaException(
                                      () => Resources.DatabaseSchema_Load_VersionNotSupported,
                                      version);
                        }

                        string sql = version.Major == 9 ? SQLResources.RetrieveSchema9 : SQLResources.RetrieveSchema10;

                        // Create the command first, as we will reuse on each connection.
                        using (
                            SqlCommand command = new SqlCommand(sql, sqlConnection)
                        {
                            CommandType = CommandType.Text
                        })
                            // Execute command
                            using (SqlDataReader reader =
                                       // ReSharper disable once PossibleNullReferenceException
                                       await
                                       command.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken)
                                       .ConfigureAwait(false))
                            {
                                /*
                                 * Load SQL Schemas
                                 */
                                while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
                                {
                                    SqlSchema sqlSchema = new SqlSchema(reader.GetInt32(0), reader.GetString(1));
                                    sqlSchemas.Add(sqlSchema.ID, sqlSchema);
                                }

                                if (sqlSchemas.Count < 1)
                                {
                                    throw new DatabaseSchemaException(
                                              () => Resources.DatabaseSchema_Load_CouldNotRetrieveSchemas);
                                }

                                /*
                                 * Load types
                                 */
                                if (!(await reader.NextResultAsync(cancellationToken).ConfigureAwait(false)))
                                {
                                    throw new DatabaseSchemaException(
                                              () => Resources.DatabaseSchema_Load_RanOutOfResultsRetrievingTypes);
                                }

                                while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
                                {
                                    int       schemaId = reader.GetInt32(0);
                                    SqlSchema sqlSchema;
                                    if (!sqlSchemas.TryGetValue(schemaId, out sqlSchema) ||
                                        (sqlSchema == null))
                                    {
                                        throw new DatabaseSchemaException(
                                                  () => Resources.DatabaseSchema_Load_CouldNotFindSchema,
                                                  schemaId);
                                    }
                                    int     id   = reader.GetInt32(1);
                                    string  name = reader.GetString(2).ToLower();
                                    SqlType baseType;
                                    if (reader.IsDBNull(3))
                                    {
                                        baseType = null;
                                    }
                                    else
                                    {
                                        // NB SQL returns types in dependency order
                                        // i.e. base types are always seen first, so this code is much easier.
                                        int baseId = reader.GetInt32(3);
                                        typesByID.TryGetValue(baseId, out baseType);
                                    }

                                    short maxLength     = reader.GetInt16(4);
                                    byte  precision     = reader.GetByte(5);
                                    byte  scale         = reader.GetByte(6);
                                    bool  isNullable    = reader.GetBoolean(7);
                                    bool  isUserDefined = reader.GetBoolean(8);
                                    bool  isCLR         = reader.GetBoolean(9);
                                    bool  isTable       = reader.GetBoolean(10);

                                    // Create type
                                    SqlType type = isTable
                                    ? new SqlTableType(
                                        baseType,
                                        sqlSchema,
                                        name,
                                        new SqlTypeSize(maxLength, precision, scale),
                                        isNullable,
                                        isUserDefined,
                                        isCLR)
                                    : new SqlType(
                                        baseType,
                                        sqlSchema,
                                        name,
                                        new SqlTypeSize(maxLength, precision, scale),
                                        isNullable,
                                        isUserDefined,
                                        isCLR);

                                    // Add to dictionary
                                    typesByName.Add(type.FullName, type);
                                    if (!typesByName.ContainsKey(type.Name))
                                    {
                                        typesByName.Add(type.Name, type);
                                    }
                                    typesByID.Add(id, type);
                                }

                                if (typesByName.Count < 1)
                                {
                                    throw new DatabaseSchemaException(
                                              () => Resources.DatabaseSchema_Load_CouldNotRetrieveTypes);
                                }

                                /*
                                 * Load program definitions
                                 */
                                if (!(await reader.NextResultAsync(cancellationToken).ConfigureAwait(false)))
                                {
                                    throw new DatabaseSchemaException(
                                              () => Resources.DatabaseSchema_Load_RanOutOfResultsRetrievingPrograms);
                                }

                                List <ProgramDefinitionData> programDefinitionData = new List <ProgramDefinitionData>();
                                while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
                                {
                                    SqlObjectType type;
                                    string        typeString = reader.GetString(0) ?? string.Empty;
                                    if (!ExtendedEnum <SqlObjectType> .TryParse(typeString, true, out type))
                                    {
                                        throw new DatabaseSchemaException(
                                                  () => Resources.DatabaseSchema_Load_CouldNotFindTypeWhenLoadingPrograms,
                                                  typeString);
                                    }

                                    int       schemaId = reader.GetInt32(1);
                                    SqlSchema sqlSchema;
                                    if (!sqlSchemas.TryGetValue(schemaId, out sqlSchema))
                                    {
                                        throw new DatabaseSchemaException(
                                                  () => Resources.DatabaseSchema_Load_CouldNotFindSchemaWhenLoadingPrograms,
                                                  schemaId);
                                    }
                                    string name = reader.GetString(2).ToLower();

                                    // If we have a null ordinal, we have no parameters.
                                    if (reader.IsDBNull(3))
                                    {
                                        programDefinitionData.Add(new ProgramDefinitionData(type, schemaId, name));
                                        continue;
                                    }

                                    int     ordinal       = reader.GetInt32(3);
                                    string  parameterName = reader.GetString(4).ToLower();
                                    int     typeId        = reader.GetInt32(5);
                                    SqlType parameterType;
                                    if (!typesByID.TryGetValue(typeId, out parameterType) ||
                                        (parameterType == null))
                                    {
                                        throw new DatabaseSchemaException(
                                                  () => Resources.DatabaseSchema_Load_ParameterTypeNotFound,
                                                  parameterName,
                                                  typeId,
                                                  name);
                                    }

                                    short       maxLength     = reader.GetInt16(6);
                                    byte        precision     = reader.GetByte(7);
                                    byte        scale         = reader.GetByte(8);
                                    SqlTypeSize parameterSize = new SqlTypeSize(maxLength, precision, scale);

                                    bool isOutput = reader.GetBoolean(9);
                                    ParameterDirection parameterDirection;
                                    if (!isOutput)
                                    {
                                        parameterDirection = ParameterDirection.Input;
                                    }
                                    else if (parameterName == string.Empty)
                                    {
                                        parameterDirection = ParameterDirection.ReturnValue;
                                    }
                                    else
                                    {
                                        parameterDirection = ParameterDirection.InputOutput;
                                    }

                                    bool parameterIsReadOnly = reader.GetBoolean(10);
                                    programDefinitionData.Add(
                                        new ProgramDefinitionData(
                                            type,
                                            schemaId,
                                            name,
                                            ordinal,
                                            parameterName,
                                            parameterType,
                                            parameterSize,
                                            parameterDirection,
                                            parameterIsReadOnly));
                                }

                                // Create unique program definitions.
                                foreach (SqlProgramDefinition program in programDefinitionData
                                         // ReSharper disable once PossibleNullReferenceException
                                         .GroupBy(d => d.ToString())
                                         .Select(
                                             g =>
                                {
                                    Debug.Assert(g != null);

                                    // Get columns ordered by ordinal.
                                    SqlProgramParameter[] parameters = g
                                                                       // ReSharper disable once PossibleNullReferenceException
                                                                       .Select(d => d.Parameter)
                                                                       .Where(p => p != null)
                                                                       .OrderBy(p => p.Ordinal)
                                                                       .ToArray();

                                    ProgramDefinitionData first = g.First();
                                    Debug.Assert(first != null);
                                    Debug.Assert(first.Name != null);

                                    Debug.Assert(sqlSchemas != null);

                                    SqlSchema sqlSchema;
                                    if (!sqlSchemas.TryGetValue(first.SchemaID, out sqlSchema))
                                    {
                                        throw new DatabaseSchemaException(
                                            () =>
                                            Resources
                                            .DatabaseSchema_Load_CouldNotFindSchemaLoadingTablesAndViews,
                                            first.SchemaID);
                                    }
                                    Debug.Assert(sqlSchema != null);

                                    return(new SqlProgramDefinition(
                                               first.Type,
                                               sqlSchema,
                                               first.Name,
                                               parameters));
                                }))
                                {
                                    Debug.Assert(program != null);
                                    programDefinitions[program.FullName] = program;

                                    if (!programDefinitions.ContainsKey(program.Name))
                                    {
                                        programDefinitions.Add(program.Name, program);
                                    }
                                }

                                /*
                                 * Load tables and views
                                 */
                                if (!(await reader.NextResultAsync(cancellationToken).ConfigureAwait(false)))
                                {
                                    throw new DatabaseSchemaException(
                                              () => Resources.DatabaseSchema_Load_RanOutOfTablesAndViews);
                                }

                                // Read raw data in.
                                List <TableDefinitionData> tableDefinitionData = new List <TableDefinitionData>();
                                while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
                                {
                                    SqlObjectType type;
                                    string        typeString = reader.GetString(0) ?? string.Empty;
                                    if (!ExtendedEnum <SqlObjectType> .TryParse(typeString, true, out type))
                                    {
                                        throw new DatabaseSchemaException(
                                                  () => Resources.DatabaseSchema_Load_CouldNotFindObjectType,
                                                  typeString);
                                    }
                                    int     schemaId   = reader.GetInt32(1);
                                    string  name       = reader.GetString(2).ToLower();
                                    int     ordinal    = reader.GetInt32(3);
                                    string  columnName = reader.GetString(4).ToLower();
                                    int     typeId     = reader.GetInt32(5);
                                    SqlType sqlType;
                                    if (!typesByID.TryGetValue(typeId, out sqlType) ||
                                        (sqlType == null))
                                    {
                                        throw new DatabaseSchemaException(
                                                  () => Resources.DatabaseSchema_Load_ColumnTypeNotFound,
                                                  columnName,
                                                  typeId,
                                                  name);
                                    }

                                    short       maxLength   = reader.GetInt16(6);
                                    byte        precision   = reader.GetByte(7);
                                    byte        scale       = reader.GetByte(8);
                                    SqlTypeSize sqlTypeSize = new SqlTypeSize(maxLength, precision, scale);

                                    bool isNullable = reader.GetBoolean(9);

                                    int?tableType = reader.IsDBNull(10) ? null : (int?)reader.GetInt32(10);

                                    tableDefinitionData.Add(
                                        new TableDefinitionData(
                                            type,
                                            schemaId,
                                            name,
                                            ordinal,
                                            columnName,
                                            sqlType,
                                            sqlTypeSize,
                                            isNullable,
                                            tableType));
                                }

                                // Create unique table definitions.
                                foreach (SqlTableDefinition table in tableDefinitionData
                                         // ReSharper disable once PossibleNullReferenceException
                                         .GroupBy(d => d.ToString())
                                         .Select(
                                             g =>
                                {
                                    Debug.Assert(g != null);

                                    // Get columns ordered by ordinal.
                                    SqlColumn[] columns = g
                                                          // ReSharper disable PossibleNullReferenceException
                                                          .Select(d => d.Column)
                                                          .OrderBy(c => c.Ordinal)
                                                          // ReSharper restore PossibleNullReferenceException
                                                          .ToArray();
                                    Debug.Assert(columns.Length > 0);

                                    TableDefinitionData first = g.First();
                                    Debug.Assert(first != null);
                                    Debug.Assert(first.Name != null);
                                    Debug.Assert(sqlSchemas != null);

                                    SqlSchema sqlSchema;
                                    if (!sqlSchemas.TryGetValue(first.SchemaID, out sqlSchema))
                                    {
                                        throw new DatabaseSchemaException(
                                            () =>
                                            Resources
                                            .DatabaseSchema_Load_CouldNotFindSchemaLoadingTablesAndViews,
                                            first.SchemaID);
                                    }
                                    Debug.Assert(sqlSchema != null);

                                    SqlTableType tableType;
                                    if (first.TableTypeID != null)
                                    {
                                        Debug.Assert(typesByID != null);

                                        SqlType tType;
                                        if (!typesByID.TryGetValue(first.TableTypeID.Value, out tType))
                                        {
                                            throw new DatabaseSchemaException(
                                                () => Resources.DatabaseSchema_Load_TableTypeNotFound,
                                                first.TableTypeID.Value,
                                                first.Name);
                                        }
                                        tableType = tType as SqlTableType;
                                        if (tableType == null)
                                        {
                                            throw new DatabaseSchemaException(
                                                () => Resources.DatabaseSchema_Load_TypeNotTableType,
                                                first.TableTypeID.Value,
                                                first.Name);
                                        }
                                    }
                                    else
                                    {
                                        tableType = null;
                                    }

                                    return(new SqlTableDefinition(
                                               first.Type,
                                               sqlSchema,
                                               first.Name,
                                               columns,
                                               tableType));
                                }))
                                {
                                    Debug.Assert(table != null);
                                    tables[table.FullName] = table;

                                    if (!tables.ContainsKey(table.Name))
                                    {
                                        tables.Add(table.Name, table);
                                    }
                                }
                            }
                    }

                    // Update the current schema.
                    _current = new CurrentSchema(Schema.GetOrAdd(sqlSchemas, programDefinitions, tables, typesByName));

                    // Always return this
                    return(this);
                }
                // In the event of an error we don't set the loaded flag - this allows retries.
                catch (DatabaseSchemaException databaseSchemaException)
                {
                    // Capture the exception in the current schema.
                    _current = new CurrentSchema(ExceptionDispatchInfo.Capture(databaseSchemaException));
                    throw;
                }
                catch (Exception exception)
                {
                    // Wrap exception in Database exception.
                    DatabaseSchemaException databaseSchemaException = new DatabaseSchemaException(
                        exception,
                        LoggingLevel.Critical,
                        () => Resources.DatabaseSchema_Load_ErrorOccurred);
                    // Capture the exception in the current schema.
                    _current = new CurrentSchema(ExceptionDispatchInfo.Capture(databaseSchemaException));
                    throw databaseSchemaException;
                }
            }
        }