Example #1
0
        /// <summary>
        /// Initializes an EnumColumn.  The <paramref name="column"/> that the enumeration should represent is passed
        /// in.  This column should contain an extended property called codal_enumeration that contains details
        /// on the enumeration values.  These details are parsed and used to complete the initialization of the
        /// EnumColumn.
        /// </summary>
        /// <param name="column">The column that this enumeration will represent.</param>
        internal EnumColumn(Column column)
        {
            // Assert that this column is a proper enum field.
            if (column.DataType != SqlDbType.TinyInt || column.Properties["codal_enumeration"] == null)
            {
                throw new ApplicationException("Enumeration columns must be TinyInt types with a codal_enumeration extended property");
            }

            // Set the name of the enumeration based on the column name.
            this._Name = column.Name + "Enum";

            // Extract values from string.
            this._EnumColumnValues = new List <EnumColumnValue>();

            string[] EnumStrings = column.Properties["codal_enumeration"].Value.Split(new char[] { ',' });

            if (EnumStrings.Length == 0)
            {
                throw new ApplicationException("At least one enumeration value must be defined for " + column.Table.Name + "." + column.Name);
            }

            for (int n = 0; n < EnumStrings.Length; n++)
            {
                string[] StringParts = EnumStrings[n].Split(":".ToCharArray());

                if (StringParts.Length < 2)
                {
                    throw new ApplicationException("At least a value and a name must be specified for each enumeration value");
                }

                // Required value and name.
                byte   EnumValue = System.Convert.ToByte(StringParts[0].Trim());
                string EnumName  = StringParts[1].Trim();

                // Create the value object and add it to the collection.
                EnumColumnValue objEnumColumnValue = new EnumColumnValue(this, EnumValue, EnumName);
                this._EnumColumnValues.Add(objEnumColumnValue);
            }
        }
Example #2
0
        /// <summary>
        /// Generates a pair of user defined functions for each enumeration column.
        /// The functions are used to convert enumeration column name/value pairs.
        /// </summary>
        /// <param name="table">The table that may contain enumeration columns.</param>
        internal void GenerateEnumerationFunctions(Table table)
        {
            foreach (Column col in table.Columns)
            {
                if (col.DataType == SqlDbType.TinyInt && col.Properties.Contains("codal_enumeration"))
                {
                    CodeBuilder cb;
                    EnumColumn  objEnum;

                    // Name to value function.
                    cb = new CodeBuilder();
                    SqlGenerator.AddComment(cb);
                    cb.WriteLine("");

                    cb.WriteLine("CREATE FUNCTION dbo.CoDAL_" + table.Name + "_" + col.Name + "_NameToValue (@Param AS VarChar(30)) RETURNS TinyInt AS");
                    cb.WriteLine("");
                    cb.WriteLine("BEGIN");
                    cb.WriteLine(1, "DECLARE @RetVal TinyInt");
                    cb.WriteLine("");
                    cb.WriteLine(1, "SET @RetVal =");
                    cb.WriteLine(2, "CASE");

                    objEnum = new EnumColumn(col);

                    for (int n = 0; n < objEnum.EnumColumnValues.Count; n++)
                    {
                        EnumColumnValue objEnumValue = objEnum.EnumColumnValues[n];
                        cb.WriteLine(2, "WHEN @Param = '" + objEnumValue.Name + "' THEN " + objEnumValue.Value);
                    }

                    cb.WriteLine(2, "ELSE NULL");
                    cb.WriteLine(1, "END");
                    cb.WriteLine("");
                    cb.WriteLine(1, "RETURN(@RetVal)");
                    cb.WriteLine("END");

                    this.AddProcedure("CoDAL_" + table.Name + "_" + col.Name + "_NameToValue", cb.CodeText, false);

                    // Value to name function.
                    cb = new CodeBuilder();
                    SqlGenerator.AddComment(cb);
                    cb.WriteLine("");

                    cb.WriteLine("CREATE FUNCTION dbo.CoDAL_" + table.Name + "_" + col.Name + "_ValueToName (@Param AS TinyInt) RETURNS VarChar(30) AS");
                    cb.WriteLine("");
                    cb.WriteLine("BEGIN");
                    cb.WriteLine(1, "DECLARE @RetVal VarChar(30)");
                    cb.WriteLine("");
                    cb.WriteLine(1, "SET @RetVal =");
                    cb.WriteLine(2, "CASE");

                    objEnum = new EnumColumn(col);

                    for (int n = 0; n < objEnum.EnumColumnValues.Count; n++)
                    {
                        EnumColumnValue objEnumValue = objEnum.EnumColumnValues[n];
                        cb.WriteLine(2, "WHEN @Param = " + objEnumValue.Value + " THEN '" + objEnumValue.Name + "'");
                    }

                    cb.WriteLine(2, "ELSE NULL");
                    cb.WriteLine(1, "END");
                    cb.WriteLine("");
                    cb.WriteLine(1, "RETURN(@RetVal)");
                    cb.WriteLine("END");

                    this.AddProcedure("CoDAL_" + table.Name + "_" + col.Name + "_ValueToName", cb.CodeText, false);
                }
            }
            return;
        }
Example #3
0
        /// <summary>
        /// Builds a region containing enumerations.  The enumerations consist of a Columns enumeration
        /// which contains the oridinal positions of columns in the table, as well as an enuemration for
        /// each TinyInt column that has a codegen_enumration extended property.
        /// </summary>
        /// <param name="cb">The CodeBuilder that this region should be added to.</param>
        /// <param name="table">The table that code is being built for.</param>
        private static void BuildEnumerationsRegion(CodeBuilder cb, Table table)
        {
            // Start region.
            cb.WriteLine(1, "#region Enumerations");
            cb.WriteLine("");

            // Columns enumeration.
            cb.WriteLine(2, "public enum Columns : byte");
            cb.WriteLine(2, "{");

            for (int n = 0; n < table.Columns.Count; n++)
            {
                Column objColumn = table.Columns[n];
                cb.Write(3, objColumn.Name + " = " + n.ToString());

                // Add a column for all but the last value.
                if (n == table.Columns.Count - 1)
                {
                    cb.WriteLine("");
                }
                else
                {
                    cb.WriteLine(",");
                }
            }

            cb.WriteLine(2, "}");

            // TinyInt enumeration columns.
            foreach (Column objColumn in table.Columns)
            {
                if (objColumn.DataType == SqlDbType.TinyInt && objColumn.Properties.Contains("codal_enumeration"))
                {
                    EnumColumn objEnum = new EnumColumn(objColumn);

                    cb.WriteLine("");
                    cb.WriteLine(2, "public enum " + objEnum.Name + " : byte");
                    cb.WriteLine(2, "{");

                    for (int n = 0; n < objEnum.EnumColumnValues.Count; n++)
                    {
                        EnumColumnValue objEnumValue = objEnum.EnumColumnValues[n];
                        cb.Write(3, objEnumValue.Name + " = " + objEnumValue.Value.ToString());

                        // Add a column for all but the last value.
                        if (n == objEnum.EnumColumnValues.Count - 1)
                        {
                            cb.WriteLine("");
                        }
                        else
                        {
                            cb.WriteLine(",");
                        }
                    }

                    cb.WriteLine(2, "}");
                }
            }

            // End region.
            cb.WriteLine("");
            cb.WriteLine(1, "#endregion");
            cb.WriteLine("");
        }