using System; using System.Collections.Generic; using System.Linq; namespace SqlSugar.HG { public class HGDbBind : DbBindProvider { public override string GetDbTypeName(string csharpTypeName) { if (csharpTypeName == UtilConstants.ByteArrayType.Name) return "bytea"; if (csharpTypeName.ToLower() == "int32") csharpTypeName = "int"; if (csharpTypeName.ToLower() == "int16") csharpTypeName = "short"; if (csharpTypeName.ToLower() == "int64") csharpTypeName = "long"; if (csharpTypeName.ToLower().IsIn("boolean", "bool")) csharpTypeName = "bool"; if (csharpTypeName == "DateTimeOffset") csharpTypeName = "DateTime"; var mappings = this.MappingTypes.Where(it => it.Value.ToString().Equals(csharpTypeName, StringComparison.CurrentCultureIgnoreCase)).ToList(); if (mappings != null && mappings.Count > 0) return mappings.First().Key; else return "varchar"; } public override string GetPropertyTypeName(string dbTypeName) { dbTypeName = dbTypeName.ToLower(); var propertyTypes = MappingTypes.Where(it => it.Value.ToString().ToLower() == dbTypeName || it.Key.ToLower() == dbTypeName); if (propertyTypes == null) { return "other"; } else if (dbTypeName == "xml" || dbTypeName == "string"|| dbTypeName == "jsonb"|| dbTypeName == "json") { return "string"; }else if (dbTypeName == "bpchar")//数据库char datatype 查询出来的时候是 bpchar { return "char"; } if (dbTypeName == "byte[]") { return "byte[]"; } else if (propertyTypes == null || propertyTypes.Count() == 0) { if (dbTypeName.StartsWith("_")) { var dbTypeName2 = dbTypeName.TrimStart('_'); return MappingTypes.Where(it => it.Value.ToString().ToLower() == dbTypeName2 || it.Key.ToLower() == dbTypeName2).Select(it => it.Value + "[]").First(); } Check.ThrowNotSupportedException(string.Format(" \"{0}\" Type NotSupported, DbBindProvider.GetPropertyTypeName error.", dbTypeName)); return null; } else if (propertyTypes.First().Value == CSharpDataType.byteArray) { return "byte[]"; } else { return propertyTypes.First().Value.ToString(); } } public override List> MappingTypes { get { var extService = this.Context.CurrentConnectionConfig.ConfigureExternalServices; if (extService != null && extService.AppendDataReaderTypeMappings.HasValue()) { return extService.AppendDataReaderTypeMappings.Union(MappingTypesConst).ToList(); } else { return MappingTypesConst; } } } public static List> MappingTypesConst = new List>(){ new KeyValuePair("int2",CSharpDataType.@short), new KeyValuePair("int1",CSharpDataType.@byte), new KeyValuePair("smallint",CSharpDataType.@short), new KeyValuePair("int4",CSharpDataType.@int), new KeyValuePair("serial",CSharpDataType.@int), new KeyValuePair("integer",CSharpDataType.@int), new KeyValuePair("int8",CSharpDataType.@long), new KeyValuePair("bigint",CSharpDataType.@long), new KeyValuePair("float4",CSharpDataType.@float), new KeyValuePair("float4",CSharpDataType.Single), new KeyValuePair("real",CSharpDataType.@float), new KeyValuePair("float8",CSharpDataType.@double), new KeyValuePair("double precision",CSharpDataType.@int), new KeyValuePair("numeric",CSharpDataType.@decimal), new KeyValuePair("decimal",CSharpDataType.@decimal), new KeyValuePair("path",CSharpDataType.@decimal), new KeyValuePair("point",CSharpDataType.@decimal), new KeyValuePair("polygon",CSharpDataType.@decimal), new KeyValuePair("boolean",CSharpDataType.@bool), new KeyValuePair("bool",CSharpDataType.@bool), new KeyValuePair("box",CSharpDataType.@bool), new KeyValuePair("bytea",CSharpDataType.byteArray), new KeyValuePair("varchar",CSharpDataType.@string), new KeyValuePair("character varying",CSharpDataType.@string), new KeyValuePair("geometry",CSharpDataType.@string), new KeyValuePair("name",CSharpDataType.@string), new KeyValuePair("text",CSharpDataType.@string), new KeyValuePair("char",CSharpDataType.@string), new KeyValuePair("character",CSharpDataType.@string), new KeyValuePair("cidr",CSharpDataType.@string), new KeyValuePair("circle",CSharpDataType.@string), new KeyValuePair("tsquery",CSharpDataType.@string), new KeyValuePair("tsvector",CSharpDataType.@string), new KeyValuePair("txid_snapshot",CSharpDataType.@string), new KeyValuePair("uuid",CSharpDataType.Guid), new KeyValuePair("xml",CSharpDataType.@string), new KeyValuePair("json",CSharpDataType.@string), new KeyValuePair("interval",CSharpDataType.@decimal), new KeyValuePair("lseg",CSharpDataType.@decimal), new KeyValuePair("macaddr",CSharpDataType.@decimal), new KeyValuePair("money",CSharpDataType.@decimal), new KeyValuePair("timestamp",CSharpDataType.DateTime), new KeyValuePair("timestamp with time zone",CSharpDataType.DateTime), new KeyValuePair("timestamptz",CSharpDataType.DateTime), new KeyValuePair("timestamp without time zone",CSharpDataType.DateTime), new KeyValuePair("date",CSharpDataType.DateTime), new KeyValuePair("time",CSharpDataType.DateTime), new KeyValuePair("time with time zone",CSharpDataType.DateTime), new KeyValuePair("timetz",CSharpDataType.DateTime), new KeyValuePair("time without time zone",CSharpDataType.DateTime), new KeyValuePair("bit",CSharpDataType.byteArray), new KeyValuePair("bit varying",CSharpDataType.byteArray), new KeyValuePair("varbit",CSharpDataType.@byte), new KeyValuePair("time",CSharpDataType.TimeSpan), new KeyValuePair("public.geometry",CSharpDataType.@object), new KeyValuePair("inet",CSharpDataType.@object) }; public override List StringThrow { get { return new List() { "int32", "datetime", "decimal", "double", "byte" }; } } } }