From e605ff71a1c101a8afd30c873e7eccf7d2e24ef7 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 20 Apr 2025 13:33:41 +0800 Subject: [PATCH] Update DuckDb --- .../DuckDB/DbBind/DuckDBDbBind.cs | 110 +++++++++--------- .../DbMaintenance/DuckDBDbMaintenance.cs | 56 ++------- 2 files changed, 63 insertions(+), 103 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar.DuckDBCore/DuckDB/DbBind/DuckDBDbBind.cs b/Src/Asp.NetCore2/SqlSugar.DuckDBCore/DuckDB/DbBind/DuckDBDbBind.cs index c3b997334..193e6e4a2 100644 --- a/Src/Asp.NetCore2/SqlSugar.DuckDBCore/DuckDB/DbBind/DuckDBDbBind.cs +++ b/Src/Asp.NetCore2/SqlSugar.DuckDBCore/DuckDB/DbBind/DuckDBDbBind.cs @@ -81,67 +81,63 @@ namespace SqlSugar.DuckDB } 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("TINYINT", CSharpDataType.@byte), // 1字节 + new KeyValuePair("SMALLINT", CSharpDataType.@short), // 2字节 + new KeyValuePair("INTEGER", CSharpDataType.@int), // 4字节 + new KeyValuePair("BIGINT", CSharpDataType.@long), // 8字节 + new KeyValuePair("UTINYINT", CSharpDataType.@byte), // 无符号 + new KeyValuePair("USMALLINT", CSharpDataType.@short), // 无符号 + new KeyValuePair("UINTEGER", CSharpDataType.@int), // 无符号 + new KeyValuePair("UBIGINT", CSharpDataType.@long), // 无符号 - new KeyValuePair("boolean",CSharpDataType.@bool), - new KeyValuePair("bool",CSharpDataType.@bool), - new KeyValuePair("box",CSharpDataType.@bool), - new KeyValuePair("bytea",CSharpDataType.byteArray), + // 浮点数类型 + new KeyValuePair("FLOAT", CSharpDataType.@float), // 4字节 + new KeyValuePair("DOUBLE", CSharpDataType.@double), // 8字节 + new KeyValuePair("REAL", CSharpDataType.@float), // 别名 FLOAT + new KeyValuePair("DECIMAL", CSharpDataType.@decimal), // 精确小数 + new KeyValuePair("NUMERIC", CSharpDataType.@decimal), // 别名 DECIMAL - 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("BOOLEAN", CSharpDataType.@bool), + new KeyValuePair("BOOL", CSharpDataType.@bool), // 别名 - 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("VARCHAR", CSharpDataType.@string), + new KeyValuePair("CHAR", CSharpDataType.@string), + new KeyValuePair("TEXT", CSharpDataType.@string), // 不限长度 + new KeyValuePair("STRING", CSharpDataType.@string), // 别名 - 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("public.geography",CSharpDataType.@object), - new KeyValuePair("inet",CSharpDataType.@object) + // 二进制类型 + new KeyValuePair("BLOB", CSharpDataType.byteArray), + new KeyValuePair("BYTEA", CSharpDataType.byteArray), // PostgreSQL风格 + + // 日期时间类型 + new KeyValuePair("TIMESTAMP", CSharpDataType.DateTime), // 无时区 + new KeyValuePair("DATE", CSharpDataType.DateTime), + new KeyValuePair("TIME", CSharpDataType.TimeSpan), + new KeyValuePair("TIMESTAMP_S", CSharpDataType.DateTime), // 秒级精度 + new KeyValuePair("TIMESTAMP_MS", CSharpDataType.DateTime), // 毫秒级 + new KeyValuePair("TIMESTAMP_NS", CSharpDataType.DateTime), // 纳秒级 + + // 特殊类型 + new KeyValuePair("UUID", CSharpDataType.Guid), + new KeyValuePair("JSON", CSharpDataType.@string), // 存储为字符串 + new KeyValuePair("ENUM", CSharpDataType.@string), // 枚举实际存储为字符串 + + // 几何类型(DuckDB支持简单几何类型) + new KeyValuePair("POINT", CSharpDataType.@object), // 需自定义解析 + new KeyValuePair("LINESTRING", CSharpDataType.@object), + new KeyValuePair("POLYGON", CSharpDataType.@object), + + // 数组类型(需特殊处理) + new KeyValuePair("INTEGER[]", CSharpDataType.@object), // 实际为int[] + new KeyValuePair("VARCHAR[]", CSharpDataType.@object), // 实际为string[] + new KeyValuePair("DOUBLE[]", CSharpDataType.@object), // 实际为double[] + + // 结构体/Map类型(DuckDB 1.0+支持) + new KeyValuePair("STRUCT", CSharpDataType.@object), // 需动态解析 + new KeyValuePair("MAP", CSharpDataType.@object) // 键值对 }; public override List StringThrow { diff --git a/Src/Asp.NetCore2/SqlSugar.DuckDBCore/DuckDB/DbMaintenance/DuckDBDbMaintenance.cs b/Src/Asp.NetCore2/SqlSugar.DuckDBCore/DuckDB/DbMaintenance/DuckDBDbMaintenance.cs index 1d30e093e..f90bff962 100644 --- a/Src/Asp.NetCore2/SqlSugar.DuckDBCore/DuckDB/DbMaintenance/DuckDBDbMaintenance.cs +++ b/Src/Asp.NetCore2/SqlSugar.DuckDBCore/DuckDB/DbMaintenance/DuckDBDbMaintenance.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Metrics; using System.Linq; using System.Text; @@ -310,48 +311,9 @@ namespace SqlSugar.DuckDB /// public override bool CreateDatabase(string databaseName, string databaseDirectory = null) { - if (databaseDirectory != null) - { - if (!FileHelper.IsExistDirectory(databaseDirectory)) - { - FileHelper.CreateDirectory(databaseDirectory); - } - } - // var oldDatabaseName = this.Context.Ado.Connection.Database; - //var connection = this.Context.CurrentConnectionConfig.ConnectionString; - //connection = connection.Replace(oldDatabaseName, ""); - if (this.Context.Ado.IsValidConnection()) - { - return true; - } - var newDb = this.Context.CopyNew(); - newDb.Ado.Connection.ChangeDatabase("highgo"); - newDb.Open(); - if (!GetDataBaseList(newDb).Any(it => it.Equals(databaseName, StringComparison.CurrentCultureIgnoreCase))) - { - newDb.Ado.ExecuteCommand(string.Format(CreateDataBaseSql, this.SqlBuilder.SqlTranslationLeft+databaseName+this.SqlBuilder.SqlTranslationRight, databaseDirectory)); - } - newDb.Close(); - return true; - } - public override bool AddRemark(EntityInfo entity) - { - var db = this.Context; - var columns = entity.Columns.Where(it => it.IsIgnore == false).ToList(); - - foreach (var item in columns) - { - if (item.ColumnDescription != null) - { - db.DbMaintenance.AddColumnRemark(item.DbColumnName, item.DbTableName, item.ColumnDescription); - - } - } - //table remak - if (entity.TableDescription != null) - { - db.DbMaintenance.AddTableRemark(entity.DbTableName, entity.TableDescription); - } + var newdb = this.Context.CopyNew(); + newdb.Open(); + newdb.Close(); return true; } public override bool CreateTable(string tableName, List columns, bool isCreatePrimaryKey = true) @@ -404,9 +366,11 @@ namespace SqlSugar.DuckDB string addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName.ToLower(isAutoToLowerCodeFirst)), dataType, dataSize, nullType, primaryKey, ""); if (item.IsIdentity) { - string length = dataType.Substring(dataType.Length - 1); - string identityDataType = "serial" + length; - addItem = addItem.Replace(dataType, identityDataType); + var seqName =GetSchema()+tableName + "_" + item.DbColumnName+ "_sequence"; + var seqSql=$@" + CREATE SEQUENCE IF NOT EXISTS ""{seqName}"""; + this.Context.Ado.ExecuteCommand(seqSql); + addItem += $" DEFAULT NEXTVAL('{seqName}') "; } columnArray.Add(addItem); } @@ -490,7 +454,7 @@ namespace SqlSugar.DuckDB } private string GetSchema() { - var schema = "public"; + var schema = "main"; if (System.Text.RegularExpressions.Regex.IsMatch(this.Context.CurrentConnectionConfig.ConnectionString.ToLower(), "searchpath=")) { var regValue = System.Text.RegularExpressions.Regex.Match(this.Context.CurrentConnectionConfig.ConnectionString.ToLower(), @"searchpath\=(\w+)").Groups[1].Value;