示例#1
0
        protected string CreateColumn(ISqlGenerator generator, IProvider provider, string columnName, Type type, bool autoIncrement)
        {
            string        name       = provider.GetQuotedName(columnName);
            string        columnType = null;
            string        width      = null;
            StringBuilder sb         = new StringBuilder();

            columnType = generator.DbTypeFromType(type);

            int dl = provider.GetDefaultLength(type);

            if (dl != 0)
            {
                width = dl.ToString();
            }

            if (!generator.LengthAllowed(type))
            {
                width = null;
            }

            if (autoIncrement && generator.HasSpecialAutoIncrementColumnFormat)
            {
                sb.Append(generator.AutoIncrementColumn(name, type, columnType, width));
            }
            else if (generator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InColumn)
            {
                sb.Append(generator.PrimaryKeyColumn(name, type, columnType, width));
            }
            else if (width != null)
            {
                sb.Append(name + " " + columnType + "(" + width + ")");
            }
            else
            {
                sb.Append(name + " " + columnType);
            }

            sb.Append(" ");

            return(sb.ToString());
        }
        protected string CreateColumn(XElement columnElement, Class cl, IProvider provider, bool isPrimary)
        {
            string rawName    = columnElement.Attribute("name").Value;
            Type   dcDataType = Type.GetType(columnElement.Attribute("type").Value);
            string name       = provider.GetQuotedName(rawName);
            string columnType = columnElement.Attribute("dbtype")?.Value;

            if (columnType == String.Empty)              // Make sure the column type will be infered, if it is an empty string
            {
                columnType = null;
            }
            string        width         = columnElement.Attribute("size") == null ? null : columnElement.Attribute("size").Value;
            string        precision     = null;
            bool          autoIncrement = false;
            StringBuilder sb            = new StringBuilder();
            bool          allowNull     = true;
            string        defaultValue  = columnElement.Attribute("default")?.Value;

            if (cl != null)
            {
                Field field = FindField(rawName, cl);

                if (null != field)
                {
                    if (!String.IsNullOrEmpty(columnType) && null != field.Column.DbType)
                    {
                        columnType = field.Column.DbType;
                    }
                    if (0 != field.Column.Size && !field.Column.IgnoreColumnSizeInDDL)
                    {
                        int dl = field.Column.Size;
                        if (dl == -1)
                        {
                            width = "max";
                        }
                        else
                        {
                            width = dl.ToString();
                        }
                    }
                    if (0 != field.Column.Precision && !field.Column.IgnoreColumnSizeInDDL)
                    {
                        precision = field.Column.Precision.ToString();
                    }
                    allowNull = field.Column.AllowDbNull;
                }
                else if (cl.TimeStampColumn == rawName)
                {
                    if (!provider.SupportsNativeGuidType)
                    {
                        width = "36";
                    }
                }
                else if (isPrimary && columnElement.Attribute("autoIncrement") != null && String.Compare(columnElement.Attribute("autoIncrement").Value, "true", true) == 0)
                {
                    autoIncrement = true;
                }
            }
            if (null == columnType)
            {
                try
                {
                    columnType = concreteGenerator.DbTypeFromType(dcDataType);
                }
                catch
                {
                    System.Diagnostics.Debug.Write("");
                }
            }

            if (null == width)
            {
                int dl = provider.GetDefaultLength(dcDataType);
                if (dl != 0)
                {
                    if (dl == -1)
                    {
                        width = "max";
                    }
                    else
                    {
                        width = dl.ToString();
                    }
                }
            }

            // Because there is no GetDefaultPrecision in the provider...
            // We assume the field to represent currency data
            if (precision == null && dcDataType == typeof(decimal))
            {
                precision = "2";
            }

            if (columnType != null)
            {
                if (!concreteGenerator.LengthAllowed(columnType))
                {
                    width = null;
                }
            }
            else
            {
                if (!concreteGenerator.LengthAllowed(dcDataType))
                {
                    width = null;
                }
            }


            if (autoIncrement && concreteGenerator.HasSpecialAutoIncrementColumnFormat)
            {
                sb.Append(concreteGenerator.AutoIncrementColumn(name, dcDataType, columnType, width));
            }
            else if (isPrimary && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InColumn)
            {
                sb.Append(concreteGenerator.PrimaryKeyColumn(name, dcDataType, columnType, width));
            }
            else if (width != null && precision != null)
            {
                sb.Append(name + " " + columnType + "(" + width + "," + precision + ")");
            }
            else if (width != null)
            {
                sb.Append(name + " " + columnType + "(" + width + ")");
            }
            else
            {
                sb.Append(name + " " + columnType);
            }

            sb.Append(" ");

            if (defaultValue != null)
            {
                sb.Append("DEFAULT ");
                if (IsNumeric(dcDataType))
                {
                    sb.Append(defaultValue);
                }
                else
                {
                    sb.Append('\'');
                    sb.Append(defaultValue.Replace("'", "''"));
                    sb.Append('\'');
                }
                sb.Append(' ');
            }

            sb.Append(concreteGenerator.NullExpression(allowNull && columnElement.Attribute("allowNull") != null && String.Compare(columnElement.Attribute("allowNull").Value, "true", true) == 0));
            return(sb.ToString());
        }
示例#3
0
        protected string CreateColumn(DataColumn dc, Class cl, IProvider provider, bool isPrimary)
        {
            string        name          = provider.GetQuotedName(dc.ColumnName);
            string        columnType    = null;
            string        width         = null;
            string        precision     = null;
            bool          autoIncrement = false;
            StringBuilder sb            = new StringBuilder();
            bool          allowNull     = true;
            int           size          = 0;

            if (cl != null)
            {
                Field field = FindField(dc.ColumnName, cl);
                if (null != field)
                {
                    if (null != field.Column.DbType)
                    {
                        columnType = field.Column.DbType;
                    }
                    size = field.Column.Size;
                    var  defaultDbType    = concreteGenerator.DbTypeFromType(dc.DataType, size);
                    bool ignoreColumnSize = field.Column.IgnoreColumnSizeInDDL;
                    if (0 != size && !ignoreColumnSize)
                    {
                        int dl = field.Column.Size;
                        if (dl == -1 && String.Compare(defaultDbType, "nvarchar", true) == 0 && concreteGenerator.ProviderName == "SqlServer")
                        {
                            width = "max";
                        }
                        else
                        {
                            width = dl.ToString();
                        }
                    }
                    if (0 != field.Column.Precision && !ignoreColumnSize)
                    {
                        precision = field.Column.Precision.ToString();
                    }
                    allowNull = field.Column.AllowDbNull;
                }
                else if (cl.TimeStampColumn == dc.ColumnName)
                {
                    if (!provider.SupportsNativeGuidType)
                    {
                        width = "36";
                    }
                }
                else if (isPrimary && dc.AutoIncrement)
                {
                    autoIncrement = true;
                }
            }
            if (null == columnType)
            {
                try
                {
                    columnType = concreteGenerator.DbTypeFromType(dc.DataType, size);
                }
                catch
                {
                    System.Diagnostics.Debug.Write("");
                }
            }

            if (null == width)
            {
                int dl = provider.GetDefaultLength(dc.DataType);
                if (dl != 0)
                {
                    width = dl.ToString();
                }
            }

            // Because there is no GetDefaultPrecision in the provider...
            // We assume the field to represent currency data
            if (precision == null && dc.DataType == typeof(decimal))
            {
                precision = "2";
            }

            if (columnType != null)
            {
                if (!concreteGenerator.LengthAllowed(columnType))
                {
                    width = null;
                }
            }
            else
            {
                if (!concreteGenerator.LengthAllowed(dc.DataType))
                {
                    width = null;
                }
            }


            if (autoIncrement && concreteGenerator.HasSpecialAutoIncrementColumnFormat)
            {
                sb.Append(concreteGenerator.AutoIncrementColumn(name, dc.DataType, columnType, width));
            }
            else if (isPrimary && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InColumn)
            {
                sb.Append(concreteGenerator.PrimaryKeyColumn(name, dc.DataType, columnType, width));
            }
            else if (width != null && precision != null)
            {
                sb.Append(name + " " + columnType + "(" + width + "," + precision + ")");
            }
            else if (width != null)
            {
                sb.Append(name + " " + columnType + "(" + width + ")");
            }
            else
            {
                sb.Append(name + " " + columnType);
            }

            sb.Append(" ");

            sb.Append(concreteGenerator.NullExpression(allowNull && dc.AllowDBNull));
            return(sb.ToString());
        }
示例#4
0
        private void CreateColumn(DataColumn dc, System.IO.StreamWriter stream, Class cl, IProvider provider, bool isPrimary)
        {
            string name          = provider.GetQuotedName(dc.ColumnName);
            string columnType    = null;
            string width         = null;
            string precision     = null;
            bool   autoIncrement = false;

            if (cl != null)
            {
                Field field = FindField(dc.ColumnName, cl);

                if (null != field)
                {
                    if (null != field.ColumnType)
                    {
                        columnType = field.ColumnType;
                    }
                    if (0 != field.ColumnLength && !field.IgnoreLengthInDDL)
                    {
                        width = field.ColumnLength.ToString();
                    }
                    if (0 != field.ColumnPrecision && !field.IgnoreLengthInDDL)
                    {
                        precision = field.ColumnPrecision.ToString();
                    }
                }
                else if (cl.TimeStampColumn == dc.ColumnName)
                {
                    if (!provider.SupportsNativeGuidType)
                    {
                        width = "36";
                    }
                }
                else if (isPrimary && dc.AutoIncrement)
                {
                    autoIncrement = true;
                }
            }
            if (null == columnType)
            {
                columnType = concreteGenerator.DbTypeFromType(dc.DataType);
            }

            if (null == width)
            {
                int dl = provider.GetDefaultLength(dc.DataType);
                if (dl != 0)
                {
                    width = dl.ToString();
                }
            }

            // Because there is no GetDefaultPrecision in the provider...
            // We assume the field to represent currency data
            if (precision == null && dc.DataType == typeof(decimal))
            {
                precision = "2";
            }

            if (columnType != null)
            {
                if (!concreteGenerator.LengthAllowed(columnType))
                {
                    width = null;
                }
            }
            else
            {
                if (!concreteGenerator.LengthAllowed(dc.DataType))
                {
                    width = null;
                }
            }


            if (autoIncrement && concreteGenerator.HasSpecialAutoIncrementColumnFormat)
            {
                stream.Write(concreteGenerator.AutoIncrementColumn(name, dc.DataType, columnType, width));
            }
            else if (isPrimary && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InColumn)
            {
                stream.Write(concreteGenerator.PrimaryKeyColumn(name, dc.DataType, columnType, width));
            }
            else if (width != null && precision != null)
            {
                stream.Write(name + " " + columnType + "(" + width + "," + precision + ")");
            }
            else if (width != null)
            {
                stream.Write(name + " " + columnType + "(" + width + ")");
            }
            else
            {
                stream.Write(name + " " + columnType);
            }

            stream.Write(" ");

            stream.Write(concreteGenerator.NullExpression(dc.AllowDBNull));
        }