Esempio n. 1
0
        public override IEnumerable <string> SqlCommands(IDbConnection connection)
        {
            if (!connection.TableExists(_attribute.Schema, _attribute.TableName))
            {
                yield return(Syntax.CreateEnumTableStatement(_enumType));
            }

            var    values    = Enum.GetValues(_enumType);
            int    index     = 0;
            string tableName = _attribute.FullTableName();

            foreach (var name in Enum.GetNames(_enumType))
            {
                string formattedName = FormatEnumValueName(name);
                bool   valueExists   = false;
                if (connection.TableExists(_attribute.Schema, _attribute.TableName))
                {
                    valueExists = connection.Exists(Syntax.CheckEnumValueExistsStatement(tableName), new { name = formattedName });
                }

                if (!valueExists)
                {
                    yield return(Syntax.InsertEnumValueStatement(tableName, formattedName, (int)values.GetValue(index)));
                }

                index++;
            }
        }
Esempio n. 2
0
        public override string CreateEnumTableStatement(Type enumType)
        {
            EnumTableAttribute attr =
                enumType.GetAttribute <EnumTableAttribute>() ??
                Nullable.GetUnderlyingType(enumType).GetAttribute <EnumTableAttribute>() ??
                throw new Exception($"Enum type {enumType.Name} is missing an [EnumTable] attribute");

            return($"CREATE TABLE {ApplyDelimiter(attr.FullTableName())} (\r\n\t[Name] nvarchar(50) NOT NULL,\r\n\t[Value] int NOT NULL PRIMARY KEY\r\n)");
        }