internal MySqlParameter GetMySqlParameter(SyncColumn column)
        {
            var mySqlDbMetadata = new MySqlDbMetadata();

            var parameterName = ParserName.Parse(column, "`").Unquoted().Normalized().ToString();

            var sqlParameter = new MySqlParameter
            {
                ParameterName = $"{MySqlBuilderProcedure.MYSQL_PREFIX_PARAMETER}{parameterName}",
                DbType        = column.GetDbType(),
                IsNullable    = column.AllowDBNull
            };

#if MARIADB
            (byte precision, byte scale) = mySqlDbMetadata.TryGetOwnerPrecisionAndScale(column.OriginalDbType, column.GetDbType(), false, false, column.MaxLength, column.Precision, column.Scale, this.tableDescription.OriginalProvider, MariaDB.MariaDBSyncProvider.ProviderType);
#elif MYSQL
            (byte precision, byte scale) = mySqlDbMetadata.TryGetOwnerPrecisionAndScale(column.OriginalDbType, column.GetDbType(), false, false, column.MaxLength, column.Precision, column.Scale, this.tableDescription.OriginalProvider, MySqlSyncProvider.ProviderType);
#endif

            if ((sqlParameter.DbType == DbType.Decimal || sqlParameter.DbType == DbType.Double ||
                 sqlParameter.DbType == DbType.Single || sqlParameter.DbType == DbType.VarNumeric) && precision > 0)
            {
                sqlParameter.Precision = precision;
                if (scale > 0)
                {
                    sqlParameter.Scale = scale;
                }
            }
            else if (column.MaxLength > 0)
            {
                sqlParameter.Size = (int)column.MaxLength;
            }
            else if (sqlParameter.DbType == DbType.Guid)
            {
                sqlParameter.Size = 36;
            }
            else
            {
                sqlParameter.Size = -1;
            }

            return(sqlParameter);
        }
Beispiel #2
0
        public override DbType GetDbType(SyncColumn columnDefinition)
        {
            DbType stringDbType;

            if (columnDefinition.IsUnicode)
            {
                stringDbType = columnDefinition.MaxLength <= 0 ? DbType.String : DbType.StringFixedLength;
            }
            else
            {
                stringDbType = columnDefinition.MaxLength <= 0 ? DbType.AnsiString : DbType.AnsiStringFixedLength;
            }

            return(columnDefinition.OriginalTypeName.ToLowerInvariant() switch
            {
                "int" or "integer" or "mediumint" => columnDefinition.IsUnsigned ? DbType.UInt32 : DbType.Int32,
                "int16" => DbType.Int16,
                "int24" or "int32" => DbType.Int32,
                "int64" => DbType.Int64,
                "uint16" => DbType.UInt16,
                "uint24" or "uint32" => DbType.UInt32,
                "uint64" => DbType.UInt64,
                "bit" => DbType.Boolean,
                "numeric" => DbType.VarNumeric,
                "decimal" or "dec" or "fixed" or "real" or "double" or "float" => DbType.Decimal,
                "tinyint" => columnDefinition.IsUnsigned ? DbType.Byte : DbType.SByte,
                "bigint" => columnDefinition.IsUnsigned ? DbType.UInt64 : DbType.Int64,
                "serial" => DbType.UInt64,
                "smallint" => columnDefinition.IsUnsigned ? DbType.UInt16 : DbType.Int16,
                "mediumtext" or "longtext" or "json" or "tinytext" => columnDefinition.IsUnicode ? DbType.String : DbType.AnsiString,
                "date" => DbType.Date,
                "datetime" or "newdate" => DbType.DateTime,
                "blob" or "longblob" or "tinyblob" or "mediumblob" or "binary" or "varbinary" => DbType.Binary,
                "year" => DbType.Int32,
                "time" => DbType.Time,
                "timestamp" => DbType.DateTime,
                "text" or "set" or "enum" or "nchar" or "nvarchar" or "varchar" or "json" => stringDbType,
                "char" => columnDefinition.MaxLength == 36 ? DbType.Guid : stringDbType,

                _ => throw new Exception($"this db type {columnDefinition.GetDbType()} for column {columnDefinition.ColumnName} is not supported"),
            });
Beispiel #3
0
        private SqlMetaData GetSqlMetadaFromType(SyncColumn column)
        {
            long maxLength = column.MaxLength;
            var  dataType  = column.GetDataType();
            var  dbType    = column.GetDbType();

            var sqlDbType = (SqlDbType)this.sqlMetadata.TryGetOwnerDbType(column.OriginalDbType, dbType, false, false, column.MaxLength, this.TableDescription.OriginalProvider, SqlSyncProvider.ProviderType);

            // Since we validate length before, it's not mandatory here.
            // let's say.. just in case..
            if (sqlDbType == SqlDbType.VarChar || sqlDbType == SqlDbType.NVarChar)
            {
                // set value for (MAX)
                maxLength = maxLength < 0 ? SqlMetaData.Max : maxLength;

                // If max length is specified (not (MAX) )
                if (maxLength > 0)
                {
                    maxLength = sqlDbType == SqlDbType.NVarChar ? Math.Min(maxLength, 4000) : Math.Min(maxLength, 8000);
                }

                return(new SqlMetaData(column.ColumnName, sqlDbType, maxLength));
            }


            if (dataType == typeof(char))
            {
                return(new SqlMetaData(column.ColumnName, sqlDbType, 1));
            }

            if (sqlDbType == SqlDbType.Char || sqlDbType == SqlDbType.NChar)
            {
                maxLength = maxLength <= 0 ? (sqlDbType == SqlDbType.NChar ? 4000 : 8000) : maxLength;
                return(new SqlMetaData(column.ColumnName, sqlDbType, maxLength));
            }

            if (sqlDbType == SqlDbType.Binary)
            {
                maxLength = maxLength <= 0 ? 8000 : maxLength;
                return(new SqlMetaData(column.ColumnName, sqlDbType, maxLength));
            }

            if (sqlDbType == SqlDbType.VarBinary)
            {
                // set value for (MAX)
                maxLength = maxLength <= 0 ? SqlMetaData.Max : maxLength;

                return(new SqlMetaData(column.ColumnName, sqlDbType, maxLength));
            }

            if (sqlDbType == SqlDbType.Decimal)
            {
                if (column.PrecisionSpecified && column.ScaleSpecified)
                {
                    var(p, s) = this.sqlMetadata.ValidatePrecisionAndScale(column);
                    return(new SqlMetaData(column.ColumnName, sqlDbType, p, s));
                }

                if (column.PrecisionSpecified)
                {
                    var p = this.sqlMetadata.ValidatePrecision(column);
                    return(new SqlMetaData(column.ColumnName, sqlDbType, p));
                }
            }

            return(new SqlMetaData(column.ColumnName, sqlDbType));
        }
 /// <summary>
 /// Gets the SqlDbType issued from the downgraded DbType
 /// </summary>
 public SqlDbType GetOwnerDbTypeFromDbType(SyncColumn column) => column.GetDbType() switch
 {
        private SqlMetaData GetSqlMetadaFromType(SyncColumn column)
        {
            long maxLength = column.MaxLength;
            var  dataType  = column.GetDataType();
            var  dbType    = column.GetDbType();

            var sqlDbType = this.TableDescription.OriginalProvider == SqlSyncProvider.ProviderType ?
                            this.SqlMetadata.GetSqlDbType(column) : this.SqlMetadata.GetOwnerDbTypeFromDbType(column);

            // Since we validate length before, it's not mandatory here.
            // let's say.. just in case..
            if (sqlDbType == SqlDbType.VarChar || sqlDbType == SqlDbType.NVarChar)
            {
                // set value for (MAX)
                maxLength = maxLength <= 0 ? SqlMetaData.Max : maxLength;

                // If max length is specified (not (MAX) )
                if (maxLength > 0)
                {
                    maxLength = sqlDbType == SqlDbType.NVarChar ? Math.Min(maxLength, 4000) : Math.Min(maxLength, 8000);
                }

                return(new SqlMetaData(column.ColumnName, sqlDbType, maxLength));
            }


            if (dataType == typeof(char))
            {
                return(new SqlMetaData(column.ColumnName, sqlDbType, 1));
            }

            if (sqlDbType == SqlDbType.Char || sqlDbType == SqlDbType.NChar)
            {
                maxLength = maxLength <= 0 ? (sqlDbType == SqlDbType.NChar ? 4000 : 8000) : maxLength;
                return(new SqlMetaData(column.ColumnName, sqlDbType, maxLength));
            }

            if (sqlDbType == SqlDbType.Binary)
            {
                maxLength = maxLength <= 0 ? 8000 : maxLength;
                return(new SqlMetaData(column.ColumnName, sqlDbType, maxLength));
            }

            if (sqlDbType == SqlDbType.VarBinary)
            {
                // set value for (MAX)
                maxLength = maxLength <= 0 ? SqlMetaData.Max : maxLength;

                return(new SqlMetaData(column.ColumnName, sqlDbType, maxLength));
            }

            if (sqlDbType == SqlDbType.Decimal)
            {
                var(p, s) = this.SqlMetadata.GetPrecisionAndScale(column);
                if (p > 0 && p > s)
                {
                    return(new SqlMetaData(column.ColumnName, sqlDbType, p, s));
                }
                else
                {
                    if (p == 0)
                    {
                        p = 18;
                    }
                    if (s == 0)
                    {
                        s = Math.Min((byte)(p - 1), (byte)6);
                    }
                    return(new SqlMetaData(column.ColumnName, sqlDbType, p, s));
                }
            }

            return(new SqlMetaData(column.ColumnName, sqlDbType));
        }