protected override string GetDefaultValue(IMemberMapping f, SqlType sqlType) { switch (sqlType.DbType) { case DBType.DateTime: break; case DBType.Guid: return " DEFAULT NEWID()"; case DBType.Int16: case DBType.Int32: case DBType.Int64: if (f.IsPrimaryKey) return " AUTO_INCREMENT PRIMARY KEY"; break; } return null; }
string GetVariableDeclaration(SqlType sqlType, bool suppressSize, int? length) { string result = null; if (typeNames == null) typeNames = new NLite.Data.Schema.Script.Generator.AccessScriptGenerator().typeNames; if (!suppressSize) result = typeNames.Get(sqlType.DbType); else if (sqlType.Length > 0 || sqlType.Precision > 0 || sqlType.Scale > 0) result = typeNames.Get(sqlType.DbType, sqlType.Length, sqlType.Precision, sqlType.Scale); else result = typeNames.Get(sqlType.DbType); if (result == null) throw new InvalidCastException(string.Format(Res.CastTypeInvalid, sqlType)); return result; }
internal static SqlType Get(Type memberType, ColumnAttribute col) { SqlType result = null; DBType dbType = col.DbType; string key = null; if (col.DbType == DBType.Unkonw) { var typeKey = memberType.TypeHandle.Value.GetHashCode(); if (col.Length == 0 && col.IsNullable && col.Precision == 0 && col.Scale == 0) { if (defaultTypes.TryGetValue(typeKey, out result)) return result; } var underlyingType = memberType; if (underlyingType.IsNullable()) underlyingType = Nullable.GetUnderlyingType(underlyingType); if (underlyingType.IsEnum) underlyingType = Enum.GetUnderlyingType(underlyingType); //if (!TypeMap.TryGetValue(underlyingType.TypeHandle.Value.GetHashCode(), out dbType)) // throw new NotSupportedException("Not support '" + underlyingType.FullName + "' for field or property type."); typeKey = underlyingType.TypeHandle.Value.GetHashCode(); if (TypeMap.TryGetValue(typeKey, out dbType)) { key = string.Concat(typeKey, col.Length, col.IsNullable, col.Precision, col.Scale); if (SqlTypes.TryGetValue(key, out result)) return result; defaultTypes.TryGetValue(typeKey, out result); } } else { key = string.Concat(col.DbType, col.Length, col.IsNullable, col.Precision, col.Scale); if (SqlTypes.TryGetValue(key, out result)) return result; result = new SqlType(col.DbType); } if (result != null) dbType = result.DbType; result = new SqlType(dbType, col.Precision, col.Scale) { Length = col.Length, Required = !col.IsNullable }; lock (SqlTypes) SqlTypes.Add(key, result); return result; }
internal static SqlType Get(DBType dbType, int length) { string key = GetKeyForLengthBased(dbType.ToString(), length); SqlType result; if (!SqlTypes.TryGetValue(key, out result)) { result = new SqlType(dbType, length); lock (SqlTypes) SqlTypes.Add(key, result); } return result; }
public bool Equals(SqlType other) { if (other == null) return false; return hashCode == other.hashCode; }
internal static SqlType Get(Type type) { var key = type.TypeHandle.Value.GetHashCode(); SqlType sqlType; if (defaultTypes.TryGetValue(key, out sqlType)) return sqlType; var strKey = key.ToString(); if (SqlTypes.TryGetValue(strKey, out sqlType)) return sqlType; bool isNotNull = type.IsValueType && !TypeHelper.IsNullable(type); type = TypeHelper.GetNonNullableType(type); if (type.IsEnum) type = Enum.GetUnderlyingType(type); DBType dbType = DBType.Unkonw; key = type.TypeHandle.Value.GetHashCode(); //if (!TypeMap.TryGetValue(key, out dbType)) // throw new NotSupportedException("Not support '" + type.FullName + "' for sql type."); TypeMap.TryGetValue(key, out dbType); sqlType = new SqlType(dbType); //if (type == Types.Char) // sqlType.Length = 1; sqlType.Required = isNotNull; lock (SqlTypes) SqlTypes[strKey] = sqlType; return sqlType; }
private static void InitializeParameterLengthWhenZero(IDbDataParameter p, object value, SqlType sqlType) { switch (sqlType.DbType) { case DBType.NChar: case DBType.NVarChar: //{ // var str = value as string; // if (string.IsNullOrEmpty(str)) // p.Size = 2; // else // p.Size = str.Length * 2; // break; //} case DBType.Char: case DBType.VarChar: //{ // var str = value as string; // if (string.IsNullOrEmpty(str)) // p.Size = 1; // else // p.Size = str.Length * 1; // break; //} p.Size = int.MaxValue; break; } }
private static void InitializeParameterLengthWhenZero(IDbDataParameter p, object value, SqlType sqlType) { switch (sqlType.DbType) { case DBType.NChar: case DBType.NVarChar: { var str = value as string; if (string.IsNullOrEmpty(str)) p.Size = 2; else p.Size = str.Length * 2; break; } case DBType.Char: case DBType.VarChar: { var str = value as string; if (string.IsNullOrEmpty(str)) p.Size = 1; else p.Size = str.Length * 1; break; } } }
public NamedParameter(string name, Type type, SqlType sqlType) { this.name = name; this.type = type; this.sqlType = sqlType; }
protected override string GetDefaultValue(IMemberMapping f, SqlType sqlType) { switch (sqlType.DbType) { case DBType.DateTime: break; case DBType.Guid: return " DEFAULT NEWID()"; } return null; }
private void PopulateEFDataAnnotitions() { var attr = member.GetCustomAttributes(EFDataAnnotiationAdapter.ColumndAttributeType, false).FirstOrDefault(); if (attr != null) { var cn = EFDataAnnotiationAdapter.Instance.Column.Name(attr) as string; if (cn.HasValue()) columnName = cn; } attr = member.GetCustomAttributes(EFDataAnnotiationAdapter.DatabaseGeneratedAttributeType, false); if (attr != null) { if ((int)Converter.Convert(EFDataAnnotiationAdapter.Instance.DatabaseGenerated.DatabaseGeneratedOption(attr), Types.Int32) == 1) isGenerated = true; } attr = member.GetCustomAttributes(EFDataAnnotiationAdapter.MaxLengthAttributeType, false); if (attr != null) { var length = (int)Converter.Convert(EFDataAnnotiationAdapter.Instance.MaxLength.Length(attr), Types.Int32); if (length > 0) sqlType = SqlType.Get(sqlType.DbType, length); } }
private void IntiailizeColumnAttribute(ColumnAttribute column) { alias = column.Alias; columnName = column.Name.HasValue() ? column.Name : member.Name; isComputed = column is ComputedColumnAttribute; sqlType = SqlType.Get(memberType, column); var id = column as IdAttribute; isPrimaryKey = id != null; if (id != null) { isGenerated = isPrimaryKey && id.IsDbGenerated; sequenceName = id.SequenceName; } isUpdatable = !isPrimaryKey && !isComputed; var version = column as VersionAttribute; if (version != null) { switch (sqlType.DbType) { case DBType.Int16: case DBType.Int32: case DBType.Int64: isVersion = true; break; default: throw new MappingException(string.Format(Res.VersionMemberTypeInvalid, sqlType.DbType.ToString(), this.entity.entityType.Name + "." + member.Name)); } } }
/// <summary> /// 初始化约定映射 /// </summary> /// <param name="isEnumerableType"></param> private void InitializeConversionMapping(bool isEnumerableType) { if (!isEnumerableType) { var underlyingType = memberType; if (underlyingType.IsNullable()) underlyingType = Nullable.GetUnderlyingType(underlyingType); if (underlyingType.IsEnum) underlyingType = Enum.GetUnderlyingType(underlyingType); if (Converter.IsPrimitiveType(underlyingType) || memberType == typeof(byte[])) { isColumn = true; columnName = MappingConversion.Current.ColumnName(member.Name); bool required = false; int length = 0; sqlType = SqlType.Get(underlyingType); isUpdatable = true; if (EFDataAnnotiationAdapter.Instance != null) PopulateEFDataAnnotitions(); if (DataAnnotationMappingAdapter.Instance != null) { object attr = null; #if !SDK35 attr = member.GetCustomAttributes(DataAnnotationMappingAdapter.KeyAttributeType, false).FirstOrDefault(); isPrimaryKey = attr != null; #endif attr = member.GetCustomAttributes(DataAnnotationMappingAdapter.RequiredAttributeType, false).FirstOrDefault(); required = attr != null; attr = member.GetCustomAttributes(DataAnnotationMappingAdapter.StringLengthAttributeType, false).FirstOrDefault(); if (attr != null) length =(int) DataAnnotationMappingAdapter.Instance.StringLength.Length(attr); } sqlType = SqlType.Get(underlyingType, new ColumnAttribute { IsNullable = !required, Length = length }); } else//manyToOne { isRelationship = true; relatedEntityType = underlyingType; isManyToOne = isRelationship && !isEnumerableType; if (relatedEntityType == entity.entityType) relatedEntity = entity; if (DataAnnotationMappingAdapter.Instance != null) { #if !SDK35 var assAtt = member.GetCustomAttributes(DataAnnotationMappingAdapter.AssociationAttributeType, false).FirstOrDefault(); if (assAtt != null) { thisKey = DataAnnotationMappingAdapter.Instance.Association.ThisKey(assAtt) as string; otherKey = DataAnnotationMappingAdapter.Instance.Association.OtherKey(assAtt) as string; } #endif } } } else { isRelationship = true; relatedEntityType = ReflectionHelper.GetElementType(memberType); isManyToOne = isRelationship && !isEnumerableType; if (relatedEntityType == entity.entityType) relatedEntity = entity; if (DataAnnotationMappingAdapter.Instance != null) { #if !SDK35 var assAtt = member.GetCustomAttributes(DataAnnotationMappingAdapter.AssociationAttributeType, false).FirstOrDefault(); if (assAtt != null) { thisKey = DataAnnotationMappingAdapter.Instance.Association.ThisKey(assAtt) as string; otherKey = DataAnnotationMappingAdapter.Instance.Association.OtherKey(assAtt) as string; } #endif } } }
/// <summary> /// 得到缺省值 /// </summary> /// <param name="sb"></param> /// <param name="f"></param> /// <param name="sqlType"></param> protected virtual string GetDefaultValue(IMemberMapping f, SqlType sqlType) { switch (sqlType.DbType) { case DBType.DateTime: break; case DBType.Guid: return " DEFAULT NEWID()"; case DBType.Int16: case DBType.Int32: case DBType.Int64: if (f.IsPrimaryKey) return " IDENTITY"; break; } return null; }
/// <summary> /// 得到对应的数据库类型 /// </summary> /// <param name="sqlType"></param> /// <returns></returns> protected string GetDbType(SqlType sqlType) { return typeNames.Get(sqlType.DbType, sqlType.Length, sqlType.Precision, sqlType.Scale); }