mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Update TDengine CodeFirst
This commit is contained in:
parent
deeed0b65b
commit
b348aa9af5
12
Src/Asp.NetCore2/SqlSugar.TDengineCore/STable.cs
Normal file
12
Src/Asp.NetCore2/SqlSugar.TDengineCore/STable.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace SqlSugar.TDengine
|
||||||
|
{
|
||||||
|
public class STable
|
||||||
|
{
|
||||||
|
[SugarColumn(IsOnlyIgnoreInsert =true ,IsOnlyIgnoreUpdate =true)]
|
||||||
|
public string TagsTypeId { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@ -7,9 +8,175 @@ namespace SqlSugar.TDengine
|
|||||||
{
|
{
|
||||||
public class TDengineCodeFirst : CodeFirstProvider
|
public class TDengineCodeFirst : CodeFirstProvider
|
||||||
{
|
{
|
||||||
|
public override void ExistLogic(EntityInfo entityInfo)
|
||||||
|
{
|
||||||
|
if (entityInfo.Columns.HasValue() && entityInfo.IsDisabledUpdateAll == false)
|
||||||
|
{
|
||||||
|
//Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Multiple primary keys do not support modifications");
|
||||||
|
|
||||||
|
var tableName = GetTableName(entityInfo);
|
||||||
|
var dbColumns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableName, false);
|
||||||
|
ConvertColumns(dbColumns);
|
||||||
|
var entityColumns = entityInfo.Columns.Where(it => it.IsIgnore == false).ToList();
|
||||||
|
var dropColumns = dbColumns
|
||||||
|
.Where(dc => !entityColumns.Any(ec => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase)))
|
||||||
|
.Where(dc => !entityColumns.Any(ec => dc.DbColumnName.Equals(ec.DbColumnName, StringComparison.CurrentCultureIgnoreCase)))
|
||||||
|
.ToList();
|
||||||
|
var addColumns = entityColumns
|
||||||
|
.Where(ec => ec.OldDbColumnName.IsNullOrEmpty() || !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase)))
|
||||||
|
.Where(ec => !dbColumns.Any(dc => ec.DbColumnName.Equals(dc.DbColumnName, StringComparison.CurrentCultureIgnoreCase))).ToList();
|
||||||
|
|
||||||
|
var renameColumns = entityColumns
|
||||||
|
.Where(it => !string.IsNullOrEmpty(it.OldDbColumnName))
|
||||||
|
.Where(entityColumn => dbColumns.Any(dbColumn => entityColumn.OldDbColumnName.Equals(dbColumn.DbColumnName, StringComparison.CurrentCultureIgnoreCase)))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
|
||||||
|
var isMultiplePrimaryKey = dbColumns.Where(it => it.IsPrimarykey).Count() > 1 || entityColumns.Where(it => it.IsPrimarykey).Count() > 1;
|
||||||
|
|
||||||
|
|
||||||
|
var isChange = false;
|
||||||
|
foreach (var item in addColumns)
|
||||||
|
{
|
||||||
|
this.Context.DbMaintenance.AddColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
|
||||||
|
isChange = true;
|
||||||
|
}
|
||||||
|
if (entityInfo.IsDisabledDelete == false)
|
||||||
|
{
|
||||||
|
foreach (var item in dropColumns)
|
||||||
|
{
|
||||||
|
this.Context.DbMaintenance.DropColumn(tableName, item.DbColumnName);
|
||||||
|
isChange = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//foreach (var item in alterColumns)
|
||||||
|
//{
|
||||||
|
|
||||||
|
// if (this.Context.CurrentConnectionConfig.DbType == DbType.Oracle)
|
||||||
|
// {
|
||||||
|
// var entityColumnItem = entityColumns.FirstOrDefault(y => y.DbColumnName == item.DbColumnName);
|
||||||
|
// if (entityColumnItem != null && !string.IsNullOrEmpty(entityColumnItem.DataType))
|
||||||
|
// {
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
|
||||||
|
// isChange = true;
|
||||||
|
//}
|
||||||
|
foreach (var item in renameColumns)
|
||||||
|
{
|
||||||
|
this.Context.DbMaintenance.RenameColumn(tableName, item.OldDbColumnName, item.DbColumnName);
|
||||||
|
isChange = true;
|
||||||
|
}
|
||||||
|
var isAddPrimaryKey = false;
|
||||||
|
foreach (var item in entityColumns)
|
||||||
|
{
|
||||||
|
var dbColumn = dbColumns.FirstOrDefault(dc => dc.DbColumnName.Equals(item.DbColumnName, StringComparison.CurrentCultureIgnoreCase));
|
||||||
|
if (dbColumn == null) continue;
|
||||||
|
bool pkDiff, idEntityDiff;
|
||||||
|
KeyAction(item, dbColumn, out pkDiff, out idEntityDiff);
|
||||||
|
if (dbColumn != null && pkDiff && !idEntityDiff && isMultiplePrimaryKey == false)
|
||||||
|
{
|
||||||
|
var isAdd = item.IsPrimarykey;
|
||||||
|
if (isAdd)
|
||||||
|
{
|
||||||
|
isAddPrimaryKey = true;
|
||||||
|
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.Context.DbMaintenance.DropConstraint(tableName, string.Format("PK_{0}_{1}", tableName, item.DbColumnName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((pkDiff || idEntityDiff) && isMultiplePrimaryKey == false)
|
||||||
|
{
|
||||||
|
ChangeKey(entityInfo, tableName, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isAddPrimaryKey == false && entityColumns.Count(it => it.IsPrimarykey) == 1 && dbColumns.Count(it => it.IsPrimarykey) == 0)
|
||||||
|
{
|
||||||
|
var addPk = entityColumns.First(it => it.IsPrimarykey);
|
||||||
|
this.Context.DbMaintenance.AddPrimaryKey(tableName, addPk.DbColumnName);
|
||||||
|
}
|
||||||
|
if (isMultiplePrimaryKey)
|
||||||
|
{
|
||||||
|
var oldPkNames = dbColumns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName.ToLower()).OrderBy(it => it).ToList();
|
||||||
|
var newPkNames = entityColumns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName.ToLower()).OrderBy(it => it).ToList();
|
||||||
|
if (!Enumerable.SequenceEqual(oldPkNames, newPkNames))
|
||||||
|
{
|
||||||
|
Check.Exception(true, ErrorMessage.GetThrowMessage("Modification of multiple primary key tables is not supported. Delete tables while creating", "不支持修改多主键表,请删除表在创建"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if (isChange && IsBackupTable)
|
||||||
|
{
|
||||||
|
this.Context.DbMaintenance.BackupTable(tableName, tableName + DateTime.Now.ToString("yyyyMMddHHmmss"), MaxBackupDataRows);
|
||||||
|
}
|
||||||
|
ExistLogicEnd(entityColumns);
|
||||||
|
}
|
||||||
|
}
|
||||||
public override void NoExistLogic(EntityInfo entityInfo)
|
public override void NoExistLogic(EntityInfo entityInfo)
|
||||||
{
|
{
|
||||||
throw new NotSupportedException("TDengine 暂时不支持CodeFirst等方法还在开发");
|
List<DbColumnInfo> dbColumns = new List<DbColumnInfo>();
|
||||||
|
foreach (var item in entityInfo.Columns.OrderBy(it=>it.UnderType==typeof(DateTime)?0:1))
|
||||||
|
{
|
||||||
|
var addItem = EntityColumnToDbColumn(entityInfo, entityInfo.DbTableName, item);
|
||||||
|
dbColumns.Add(addItem);
|
||||||
|
}
|
||||||
|
this.Context.DbMaintenance.CreateTable(entityInfo.DbTableName, dbColumns);
|
||||||
|
}
|
||||||
|
protected override DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
|
||||||
|
{
|
||||||
|
DbColumnInfo result = new DbColumnInfo() { Length=item.Length,DecimalDigits=item.DecimalDigits,Scale=item.DecimalDigits ,TableName = tableName, DbColumnName = item.DbColumnName, DataType = item.DataType };
|
||||||
|
if (result.DataType.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
result.DataType = GetDatabaseTypeName(item.UnderType.Name);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetDatabaseTypeName(string typeName)
|
||||||
|
{
|
||||||
|
switch (typeName.ToLower())
|
||||||
|
{
|
||||||
|
case "bool":
|
||||||
|
return "BOOL";
|
||||||
|
case "datetime":
|
||||||
|
return "TIMESTAMP";
|
||||||
|
case "boolean":
|
||||||
|
return "BOOL";
|
||||||
|
case "byte":
|
||||||
|
return "TINYINT UNSIGNED";
|
||||||
|
case "sbyte":
|
||||||
|
return "TINYINT";
|
||||||
|
case "char":
|
||||||
|
return "NCHAR";
|
||||||
|
case "decimal":
|
||||||
|
return "FLOAT";
|
||||||
|
case "double":
|
||||||
|
return "DOUBLE";
|
||||||
|
case "float":
|
||||||
|
return "FLOAT";
|
||||||
|
case "int":
|
||||||
|
return "INT";
|
||||||
|
case "uint":
|
||||||
|
return "INT UNSIGNED";
|
||||||
|
case "long":
|
||||||
|
return "BIGINT";
|
||||||
|
case "ulong":
|
||||||
|
return "BIGINT UNSIGNED";
|
||||||
|
case "short":
|
||||||
|
return "SMALLINT";
|
||||||
|
case "ushort":
|
||||||
|
return "SMALLINT UNSIGNED";
|
||||||
|
case "string":
|
||||||
|
return "VARCHAR";
|
||||||
|
// 添加其他类型的映射关系
|
||||||
|
|
||||||
|
default:
|
||||||
|
return "VARCHAR"; // 如果未识别到类型,则返回原始类型名称
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -74,7 +74,7 @@ namespace SqlSugar.TDengine
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return "alter table {0} ALTER COLUMN {1} {2}{3} {4} {5} {6}";
|
return "alter table {0} MODIFY COLUMN {1} {2}{3} {4} {5} {6}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected override string BackupDataBaseSql
|
protected override string BackupDataBaseSql
|
||||||
@ -88,7 +88,7 @@ namespace SqlSugar.TDengine
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return "CREATE TABLE {0}(\r\n{1} $PrimaryKey)";
|
return "CREATE STABLE IF NOT EXISTS {0}(\r\n{1} ) TAGS(TagsTypeId VARCHAR(20))";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected override string CreateTableColumn
|
protected override string CreateTableColumn
|
||||||
@ -283,10 +283,10 @@ namespace SqlSugar.TDengine
|
|||||||
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
|
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
|
||||||
string dataSize = GetSize(columnInfo);
|
string dataSize = GetSize(columnInfo);
|
||||||
string dataType = columnInfo.DataType;
|
string dataType = columnInfo.DataType;
|
||||||
if (!string.IsNullOrEmpty(dataType))
|
//if (!string.IsNullOrEmpty(dataType))
|
||||||
{
|
//{
|
||||||
dataType = " type " + dataType;
|
// dataType = " type " + dataType;
|
||||||
}
|
//}
|
||||||
string nullType = "";
|
string nullType = "";
|
||||||
string primaryKey = null;
|
string primaryKey = null;
|
||||||
string identity = null;
|
string identity = null;
|
||||||
@ -355,22 +355,46 @@ namespace SqlSugar.TDengine
|
|||||||
// dataType = "varchar";
|
// dataType = "varchar";
|
||||||
//}
|
//}
|
||||||
string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
|
string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
|
||||||
if (item.DecimalDigits > 0&&item.Length>0 && dataType == "numeric")
|
//if (item.DecimalDigits > 0&&item.Length>0 && dataType?.ToLower()== "float")
|
||||||
|
//{
|
||||||
|
// item.Length = 0;
|
||||||
|
// dataSize = $"({item.Length},{item.DecimalDigits})";
|
||||||
|
//}
|
||||||
|
//if (item.DecimalDigits > 0 && item.Length > 0 && dataType?.ToLower() == "double")
|
||||||
|
//{
|
||||||
|
|
||||||
|
// dataSize = $"({item.Length},{item.DecimalDigits})";
|
||||||
|
//}
|
||||||
|
//if (item.DecimalDigits > 0 && item.Length > 0 && dataType?.ToLower() == "decimal")
|
||||||
|
//{
|
||||||
|
// dataSize = $"({item.Length},{item.DecimalDigits})";
|
||||||
|
//}
|
||||||
|
//if (item.DecimalDigits == 0 && item.Length == 0 && dataType?.ToLower() == "float")
|
||||||
|
//{
|
||||||
|
// dataType = $"FLOAT(18,4)";
|
||||||
|
//}
|
||||||
|
//if (item.DecimalDigits == 0 && item.Length == 0 && dataType?.ToLower() == "double")
|
||||||
|
//{
|
||||||
|
// dataType = $"DOUBLE(18,4)";
|
||||||
|
//}
|
||||||
|
if (item.Length==0&&dataType?.ToLower()?.IsIn("nchar", "varchar") ==true)
|
||||||
{
|
{
|
||||||
dataSize = $"({item.Length},{item.DecimalDigits})";
|
dataType = "VARCHAR(200)";
|
||||||
|
}
|
||||||
|
if (dataType?.ToLower()?.IsIn("float", "double") == true)
|
||||||
|
{
|
||||||
|
dataSize = null;
|
||||||
}
|
}
|
||||||
string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
|
|
||||||
string primaryKey = null;
|
string primaryKey = null;
|
||||||
string addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName.ToLower(isAutoToLowerCodeFirst)), dataType, dataSize, nullType, primaryKey, "");
|
string addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName.ToLower(isAutoToLowerCodeFirst)), dataType, dataSize, null, primaryKey, "");
|
||||||
if (item.IsIdentity)
|
|
||||||
{
|
|
||||||
string length = dataType.Substring(dataType.Length - 1);
|
|
||||||
string identityDataType = "serial" + length;
|
|
||||||
addItem = addItem.Replace(dataType, identityDataType);
|
|
||||||
}
|
|
||||||
columnArray.Add(addItem);
|
columnArray.Add(addItem);
|
||||||
}
|
}
|
||||||
string tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName(tableName.ToLower(isAutoToLowerCodeFirst)), string.Join(",\r\n", columnArray));
|
string tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName("STable_"+tableName.ToLower(isAutoToLowerCodeFirst)), string.Join(",\r\n", columnArray));
|
||||||
|
var childTableName = this.SqlBuilder.GetTranslationTableName(tableName.ToLower(isAutoToLowerCodeFirst));
|
||||||
|
var stableName = this.SqlBuilder.GetTranslationTableName("STable_"+tableName.ToLower(isAutoToLowerCodeFirst));
|
||||||
|
this.Context.Ado.ExecuteCommand(tableString);
|
||||||
|
var createChildSql = $"CREATE TABLE IF NOT EXISTS {childTableName} USING {stableName} TAGS('default')";
|
||||||
|
this.Context.Ado.ExecuteCommand(createChildSql);
|
||||||
return tableString;
|
return tableString;
|
||||||
}
|
}
|
||||||
public override bool IsAnyConstraint(string constraintName)
|
public override bool IsAnyConstraint(string constraintName)
|
||||||
@ -386,7 +410,7 @@ namespace SqlSugar.TDengine
|
|||||||
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
|
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
|
||||||
{
|
{
|
||||||
|
|
||||||
var sql = $"select * from {tableName} where 1=2 ";
|
var sql = $"select * from {this.SqlBuilder.GetTranslationColumnName( tableName)} where 1=2 ";
|
||||||
List<DbColumnInfo> result = new List<DbColumnInfo>();
|
List<DbColumnInfo> result = new List<DbColumnInfo>();
|
||||||
var dt=this.Context.Ado.GetDataTable(sql);
|
var dt=this.Context.Ado.GetDataTable(sql);
|
||||||
foreach (DataColumn item in dt.Columns)
|
foreach (DataColumn item in dt.Columns)
|
||||||
@ -411,17 +435,7 @@ namespace SqlSugar.TDengine
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (this.Context.CurrentConnectionConfig.MoreSettings == null) return true;
|
return false;
|
||||||
else if (
|
|
||||||
this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower == false &&
|
|
||||||
this.Context.CurrentConnectionConfig.MoreSettings?.PgSqlIsAutoToLowerCodeFirst == false)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ namespace SqlSugar.TDengine
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return "select current_date";
|
return " now() ";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,8 +39,7 @@ namespace SqlSugar.TDengine
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (this.Context.CurrentConnectionConfig.MoreSettings == null) return true;
|
return false;
|
||||||
return this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public override string GetTranslationColumnName(string propertyName)
|
public override string GetTranslationColumnName(string propertyName)
|
||||||
|
@ -39,6 +39,10 @@ namespace SqlSugar.TDengine
|
|||||||
base._DbConnection = value;
|
base._DbConnection = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public override void BeginTran()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public override void BeginTran(string transactionName)
|
public override void BeginTran(string transactionName)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user