Beispiel #1
0
        /// <summary>
        /// 转换源表达式的数据类型。
        /// </summary>
        /// <param name="sourceExp">要转换的源表达式。</param>
        /// <param name="dbType">要转换的类型。</param>
        /// <returns></returns>
        public virtual string Convert(object sourceExp, DbType dbType)
        {
            switch (dbType)
            {
            case DbType.AnsiString:
                return(string.Format("CAST({0} AS VARCHAR)", sourceExp));

            case DbType.AnsiStringFixedLength:
                return(string.Format("CAST({0} AS CHAR)", sourceExp));

            case DbType.Binary:
                return(string.Format("CAST({0} AS VARBINARY)", sourceExp));

            case DbType.Boolean:
                return(string.Format("CAST({0} AS BIT)", sourceExp));

            case DbType.Byte:
                return(string.Format("CAST({0} AS TINYINT)", sourceExp));

            case DbType.Currency:
                return(string.Format("CAST({0} AS MONEY)", sourceExp));

            case DbType.Date:
                return(string.Format("CAST({0} AS DATETIME)", sourceExp));

            case DbType.DateTime:
                return(string.Format("CAST({0} AS DATETIME)", sourceExp));

            case DbType.DateTime2:
                return(string.Format("CAST({0} AS DATETIME2)", sourceExp));

            case DbType.DateTimeOffset:
                return(string.Format("CAST({0} AS DATETIMEOFFSET)", sourceExp));

            case DbType.Decimal:
                return(string.Format("CAST({0} AS DECIMAL)", sourceExp));

            case DbType.Double:
                return(string.Format("CAST({0} AS DOUBLE PRECISION)", sourceExp));

            case DbType.Guid:
                return(string.Format("CAST({0} AS UNIQUEIDENTIFIER)", sourceExp));

            case DbType.Int16:
                return(string.Format("CAST({0} AS SMALLINT)", sourceExp));

            case DbType.Int32:
                return(string.Format("CAST({0} AS INT)", sourceExp));

            case DbType.Int64:
                return(string.Format("CAST({0} AS BIGINT)", sourceExp));

            case DbType.SByte:
                break;

            case DbType.Single:
                return(string.Format("CAST({0} AS REAL)", sourceExp));

            case DbType.String:
                return(string.Format("CAST({0} AS NVARCHAR)", sourceExp));

            case DbType.StringFixedLength:
                return(string.Format("CAST({0} AS NCHAR)", sourceExp));

            case DbType.Time:
                break;

            case DbType.UInt16:
                break;

            case DbType.UInt32:
                break;

            case DbType.UInt64:
                break;

            case DbType.VarNumeric:
                break;

            case DbType.Xml:
                return(string.Format("CAST({0} AS XML)", sourceExp));
            }
            return(ExceptionHelper.ThrowSyntaxConvertException(dbType));
        }
Beispiel #2
0
        /// <summary>
        /// 根据数据类型生成相应的列。
        /// </summary>
        /// <param name="dbType">数据类型。</param>
        /// <param name="length">数据长度。</param>
        /// <param name="precision">数值的精度。</param>
        /// <param name="scale">数值的小数位。</param>
        /// <returns></returns>
        public string Column(DbType dbType, int?length, int?precision, int?scale = new int?())
        {
            switch (dbType)
            {
            case DbType.AnsiString:
                if (length == null)
                {
                    return("VARCHAR(255)");
                }
                if (length > 8000)
                {
                    return("NTEXT");
                }
                return(string.Format("VARCHAR({0})", length));

            case DbType.AnsiStringFixedLength:
                return(length == null ? "CHAR(255)" : string.Format("CHAR({0})", length));

            case DbType.Binary:
                if (length == null)
                {
                    return("VARBINARY(8000)");
                }
                if (length > 8000)
                {
                    return("IMAGE");
                }
                return(string.Format("VARBINARY({0})", length));

            case DbType.Boolean:
                return("BIT");

            case DbType.Byte:
                return("TINYINT");

            case DbType.Currency:
                return("MONEY");

            case DbType.Date:
                return("DATETIME");

            case DbType.DateTime:
                return("DATETIME");

            case DbType.DateTime2:
                return("DATETIME2");

            case DbType.DateTimeOffset:
                return("DATETIMEOFFSET");

            case DbType.Decimal:
                if (precision == null && scale == null)
                {
                    return("DECIMAL(19, 5)");
                }
                if (precision == null)
                {
                    return(string.Format("DECIMAL(19, {0})", scale));
                }
                if (scale == null)
                {
                    return(string.Format("DECIMAL({0}, 5)", precision));
                }
                return(string.Format("DECIMAL({0}, {1})", precision, scale));

            case DbType.Double:
                return("DOUBLE PRECISION");

            case DbType.Guid:
                return("UNIQUEIDENTIFIER");

            case DbType.Int16:
                return("SMALLINT");

            case DbType.Int32:
                return("INT");

            case DbType.Int64:
                return("BIGINT");

            case DbType.SByte:
                return("TINYINT");

            case DbType.Single:
                return("REAL");

            case DbType.String:
                if (length == null)
                {
                    return("VARCHAR(255)");
                }
                if (length > 8000)
                {
                    return("NTEXT");
                }
                return(string.Format("VARCHAR({0})", length));

            case DbType.StringFixedLength:
                if (length == null)
                {
                    return("NCHAR(255)");
                }
                return(string.Format("NCHAR({0})", length));

            case DbType.Time:
                return("DATETIME");

            case DbType.UInt16:
                break;

            case DbType.UInt32:
                break;

            case DbType.UInt64:
                break;

            case DbType.VarNumeric:
                break;

            case DbType.Xml:
                return("XML");
            }
            return(ExceptionHelper.ThrowSyntaxCreteException(dbType));
        }
Beispiel #3
0
        /// <summary>
        /// 根据数据类型生成相应的列。
        /// </summary>
        /// <param name="dbType">数据类型。</param>
        /// <param name="length">数据长度。</param>
        /// <param name="precision">数值的精度。</param>
        /// <param name="scale">数值的小数位。</param>
        /// <returns></returns>
        public string Column(DbType dbType, int?length, int?precision, int?scale = new int?())
        {
            switch (dbType)
            {
            case DbType.String:
            case DbType.AnsiString:
                if (length == null || length <= 255)
                {
                    return(string.Format("VARCHAR({0})", length ?? 255));
                }
                if (length > 255 && length <= 65535)
                {
                    return("TEXT");
                }
                //length > 65535 && length <= 16777215
                return("MEDIUMTEXT");

            case DbType.StringFixedLength:
            case DbType.AnsiStringFixedLength:
                if (length == null || length <= 255)
                {
                    return(string.Format("CHAR({0})", length ?? 255));
                }
                if (length > 255 && length <= 65535)
                {
                    return("TEXT");
                }
                //length > 65535 && length <= 16777215
                return("MEDIUMTEXT");

            case DbType.Guid:
                return("VARCHAR(40)");

            case DbType.Binary:
                if (length == null || length <= 127)
                {
                    return("LONGBLOB");
                }
                if (length > 127 && length <= 65535)
                {
                    return("BLOB");
                }
                //length > 65535 && length <= 16777215
                return("MEDIUMBLOB");

            case DbType.Decimal:
                if (precision == null && scale == null)
                {
                    return("DECIMAL(19, 5)");
                }
                if (precision == null)
                {
                    return(string.Format("DECIMAL(19, {0})", scale));
                }
                if (scale == null)
                {
                    return(string.Format("DECIMAL({0}, 5)", precision));
                }
                return(string.Format("DECIMAL({0}, {1})", precision, scale));

            case DbType.Double:
                return("DOUBLE");

            case DbType.Single:
                return("FLOAT");

            case DbType.Boolean:
                return("TINYINT(1)");

            case DbType.Byte:
                return("TINY INT");

            case DbType.Currency:
                return("MONEY");

            case DbType.Int16:
                return("SMALLINT");

            case DbType.Int32:
                return("INT");

            case DbType.Int64:
                return("BIGINT");

            case DbType.SByte:
                return("TINYINT");

            case DbType.UInt16:
                return("SMALLINT");

            case DbType.UInt32:
                return("MEDIUMINT");

            case DbType.UInt64:
                return("BIT");

            case DbType.Date:
                return("DATE");

            case DbType.DateTime:
                return("DATETIME");

            case DbType.Time:
                return("TIME");
            }
            return(ExceptionHelper.ThrowSyntaxCreteException(dbType));
        }