添加对PostgreSQL中7种几何类型的支持

This commit is contained in:
johnechoo
2025-09-29 12:54:47 +08:00
parent ba9644f7d9
commit f4cd8179e5
5 changed files with 159 additions and 9 deletions

View File

@@ -29,6 +29,7 @@ namespace OrmTest
_a6_SqlPage.Init();
_a7_JsonType.Init();
_a8_SelectReturnType.Init();
_a9_GeometryTest.Init();
}
}
@@ -57,7 +58,6 @@ namespace OrmTest
DbType = DbType.PostgreSQL,
ConnectionString = Connection,
LanguageType=LanguageType.Default//Set language
},
it => {
// Logging SQL statements and parameters before execution

View File

@@ -0,0 +1,126 @@
using NpgsqlTypes;
using SqlSugar;
namespace OrmTest;
public class _a9_GeometryTest
{
public static void Init()
{
// Get a new database instance
// 获取新的数据库实例
var db = DbHelper.GetNewDb();
// Create the database if it doesn't exist
// 如果数据库不存在,则创建数据库
db.DbMaintenance.CreateDatabase();
// Initialize tables based on G1 entity class
// 根据 G1 实体类初始化表
db.CodeFirst.InitTables<Geometries>();
//Prepare data
//准备数据
var points = new NpgsqlPoint[]
{
new NpgsqlPoint(0,-10),
new NpgsqlPoint(7,-7),
new NpgsqlPoint(10,0),
new NpgsqlPoint(7,7),
new NpgsqlPoint(0,10),
new NpgsqlPoint(-7,7),
new NpgsqlPoint(-10,0),
new NpgsqlPoint(-7,-7),
};
//Insert
//插入
var id = db.Insertable(new Geometries
{
Box = new NpgsqlBox(5, 4, 0, 0),
Circle = new NpgsqlCircle(4, 5, 3),
Line = new NpgsqlLine(1, 2, 3),
Lseg = new NpgsqlLSeg(1, 2, 3, 4),
Path = new NpgsqlPath(points),
Point = new NpgsqlPoint(0, 1),
Polygon = new NpgsqlPolygon(points),
}).ExecuteReturnIdentity();
//Query
//查询
var geom = db.Queryable<Geometries>().InSingle(id);
var container = db.Queryable<Geometries>()
.Where($"@point <@ {nameof(Geometries.Polygon)}", new { point = new NpgsqlPoint(3, 4) })
.First();
var area = db.Queryable<Geometries>()
.Select<double>($"area({nameof(Geometries.Circle)})")
.First();
var length = db.Queryable<Geometries>()
.Select<double>($"@-@ {nameof(Geometries.Path)}")
.First();
var center = db.Queryable<Geometries>()
.Select<NpgsqlPoint>($"@@ {nameof(Geometries.Box)}")
.First();
//Update
//更新
db.Updateable(geom).ExecuteCommand();
//Delete
//删除
db.Deleteable(geom).ExecuteCommand();
}
}
/// <summary>
/// Geometry entity class
/// 几何实体类
/// </summary>
public class Geometries
{
/// <summary>
/// ID (Primary Key)
/// ID主键
/// </summary>
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
public int Id { get; set; }
/// <summary>
/// 矩形框
/// </summary>
public NpgsqlBox Box { get; set; }
/// <summary>
/// 圆
/// </summary>
public NpgsqlCircle Circle { get; set; }
/// <summary>
/// 线 Ax + By + C = 0
/// </summary>
public NpgsqlLine Line { get; set; }
/// <summary>
/// 线段
/// </summary>
public NpgsqlLSeg Lseg { get; set; }
/// <summary>
/// 路径
/// </summary>
public NpgsqlPath Path { get; set; }
/// <summary>
/// 坐标点
/// </summary>
public NpgsqlPoint Point { get; set; }
/// <summary>
/// 多边形
/// </summary>
public NpgsqlPolygon Polygon { get; set; }
}

View File

@@ -27,6 +27,13 @@ namespace SqlSugar
@DateTimeOffset,
@Single,
@TimeSpan,
@char
@char,
@NpgsqlBox,
@NpgsqlCircle,
@NpgsqlLine,
@NpgsqlLseg,
@NpgsqlPath,
@NpgsqlPoint,
@NpgsqlPolygon,
}
}

View File

@@ -126,13 +126,9 @@ namespace SqlSugar
new KeyValuePair<string, CSharpDataType>("double precision",CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("numeric",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("decimal",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("path",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("point",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("polygon",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("boolean",CSharpDataType.@bool),
new KeyValuePair<string, CSharpDataType>("bool",CSharpDataType.@bool),
new KeyValuePair<string, CSharpDataType>("box",CSharpDataType.@bool),
new KeyValuePair<string, CSharpDataType>("bytea",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.@string),
@@ -146,7 +142,6 @@ namespace SqlSugar
new KeyValuePair<string, CSharpDataType>("char",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("character",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("cidr",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("circle",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("tsquery",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("tsvector",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("txid_snapshot",CSharpDataType.@string),
@@ -155,7 +150,6 @@ namespace SqlSugar
new KeyValuePair<string, CSharpDataType>("json",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("interval",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("lseg",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("macaddr",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("money",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("timestamp",CSharpDataType.DateTime),
@@ -184,6 +178,15 @@ namespace SqlSugar
new KeyValuePair<string, CSharpDataType>("number",CSharpDataType.@long),
new KeyValuePair<string, CSharpDataType>("number",CSharpDataType.@bool),
new KeyValuePair<string, CSharpDataType>("number",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("box",CSharpDataType.@NpgsqlBox),
new KeyValuePair<string, CSharpDataType>("circle",CSharpDataType.@NpgsqlCircle),
new KeyValuePair<string, CSharpDataType>("lseg",CSharpDataType.@NpgsqlLseg),
new KeyValuePair<string, CSharpDataType>("line",CSharpDataType.@NpgsqlLine),
new KeyValuePair<string, CSharpDataType>("path",CSharpDataType.@NpgsqlPath),
new KeyValuePair<string, CSharpDataType>("point",CSharpDataType.@NpgsqlPoint),
new KeyValuePair<string, CSharpDataType>("polygon",CSharpDataType.@NpgsqlPolygon),
};
public override List<string> StringThrow
{

View File

@@ -147,6 +147,20 @@ namespace SqlSugar
{
sqlParameter.NpgsqlDbType =((NpgsqlDbType)parameter.CustomDbType);
}
else
{
switch (parameter.Value)
{
case NpgsqlBox b: sqlParameter.NpgsqlDbType = NpgsqlDbType.Box; break;
case NpgsqlCircle c: sqlParameter.NpgsqlDbType = NpgsqlDbType.Circle; break;
case NpgsqlLine l: sqlParameter.NpgsqlDbType = NpgsqlDbType.Line; break;
case NpgsqlLSeg s: sqlParameter.NpgsqlDbType = NpgsqlDbType.LSeg; break;
case NpgsqlPath p: sqlParameter.NpgsqlDbType = NpgsqlDbType.Path; break;
case NpgsqlPoint p: sqlParameter.NpgsqlDbType = NpgsqlDbType.Point; break;
case NpgsqlPolygon p: sqlParameter.NpgsqlDbType = NpgsqlDbType.Polygon; break;
default: break;
}
}
}
return result;
}