Browse Source

Update GBase database

SqlSugar5
sunkaixuan 2 years ago
parent
commit
295e2b4ba6
  1. 2
      Src/Asp.NetCore2/GbaseTest/Config.cs
  2. 10
      Src/Asp.NetCore2/GbaseTest/Demo/Demo0_SqlSugarClient.cs
  3. 16
      Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/CodeFirst/GBaseCodeFirst.cs
  4. 53
      Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/DbBind/GBaseDbBind.cs
  5. 214
      Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/DbMaintenance/GBaseDbMaintenance.cs
  6. 9
      Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/SqlBuilder/GBaseBuilder.cs

2
Src/Asp.NetCore2/GbaseTest/Config.cs

@ -16,7 +16,7 @@ namespace OrmTest
/// Account have permission to create database
/// 用有建库权限的数据库账号
/// </summary>
public static string ConnectionString = "Driver={GBase ODBC DRIVER (64-Bit)};Host=localhost;Service=19088;Server=gbase01;Database=testdb;Protocol=onsoctcp;Uid=gbasedbt;Pwd=GBase123;Db_locale=zh_CN.utf8;Client_locale=zh_CN.utf8";
public static string ConnectionString = "Driver={GBase ODBC DRIVER (64-Bit)};Host=localhost;Service=19088;Server=gbase01;Database=testdb1;Protocol=onsoctcp;Uid=gbasedbt;Pwd=GBase123;Db_locale=zh_CN.utf8;Client_locale=zh_CN.utf8";
/// <summary>
/// Account have permission to create database
/// 用有建库权限的数据库账号

10
Src/Asp.NetCore2/GbaseTest/Demo/Demo0_SqlSugarClient.cs

@ -68,10 +68,16 @@ namespace OrmTest
});
//If no exist create datebase
db.DbMaintenance.CreateDatabase();
//db.DbMaintenance.CreateDatabase();
//Use db query
var dt = db.Ado.GetDataTable("select 1");
var dt = db.Ado.GetDataTable(@"
select
trim(a.tabname) as name,
trim(b.comments) as comment
from systables a
left join syscomments b on b.tabname = a.tabname
where a.tabtype in ('T', 'V') and not (a.tabname like 'sys%') AND a.tabname <>'dual'");
//Create tables
db.CodeFirst.InitTables(typeof(OrderItem),typeof(Order));

16
Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/CodeFirst/GBaseCodeFirst.cs

@ -6,6 +6,10 @@ namespace SqlSugar.GBase
{
public class GBaseCodeFirst:CodeFirstProvider
{
protected override void ExistLogicEnd(List<EntityColumnInfo> dbColumns)
{
}
protected override string GetTableName(EntityInfo entityInfo)
{
var table= this.Context.EntityMaintenance.GetTableName(entityInfo.EntityName);
@ -14,20 +18,12 @@ namespace SqlSugar.GBase
if (tableArray.Length > 1 && noFormat)
{
var dbMain = new GBaseDbMaintenance() { Context = this.Context };
var schmes = dbMain.GetSchemas();
if (!schmes.Any(it => it.EqualCase(tableArray.First())))
{
return tableArray.Last();
}
else
{
return dbMain.SqlBuilder.GetTranslationTableName(table);
}
return dbMain.SqlBuilder.GetTranslationTableName(table);
}
else
{
return table;
}
}
}
}
}

53
Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/DbBind/GBaseDbBind.cs

@ -34,39 +34,28 @@ namespace SqlSugar.GBase
}
public static List<KeyValuePair<string, CSharpDataType>> MappingTypesConst = new List<KeyValuePair<string, CSharpDataType>>()
{
new KeyValuePair<string, CSharpDataType>("int",CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("nvarchar",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("sql_variant",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("text",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("char",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("ntext",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("nchar",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("hierarchyid",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("bigint",CSharpDataType.@long),
new KeyValuePair<string, CSharpDataType>("bit",CSharpDataType.@bool),
new KeyValuePair<string, CSharpDataType>("datetime",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("time",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("smalldatetime",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("timestamp",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("datetime2",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("date",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("decimal",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("single",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("money",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("numeric",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("smallmoney",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("float",CSharpDataType.@double),
new KeyValuePair<string, CSharpDataType>("float",CSharpDataType.Single),
new KeyValuePair<string, CSharpDataType>("real",CSharpDataType.@float),
new KeyValuePair<string, CSharpDataType>("smallint",CSharpDataType.@short),
new KeyValuePair<string, CSharpDataType>("tinyint",CSharpDataType.@byte),
new KeyValuePair<string, CSharpDataType>("uniqueidentifier",CSharpDataType.Guid),
new KeyValuePair<string, CSharpDataType>("binary",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("image",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("varbinary",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("datetimeoffset", CSharpDataType.DateTimeOffset),
new KeyValuePair<string, CSharpDataType>("datetimeoffset", CSharpDataType.DateTime)};
new KeyValuePair<string, CSharpDataType>("blob",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("boolean",CSharpDataType.@bool),
new KeyValuePair<string, CSharpDataType>("byte",CSharpDataType.@byte),
new KeyValuePair<string, CSharpDataType>("char",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("clob",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("DATETIME YEAR TO FRACTION(3)", CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("datetime", CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("date", CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("decimal", CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("float", CSharpDataType.@float),
new KeyValuePair<string, CSharpDataType>("int", CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("integer", CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("money", CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("nchar", CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("numeric", CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("nvarchar", CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("varchar", CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("text", CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("smallfloat", CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("serial", CSharpDataType.@int),
};
};
}

214
Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/DbMaintenance/GBaseDbMaintenance.cs

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace SqlSugar.GBase
{
@ -12,7 +13,7 @@ namespace SqlSugar.GBase
{
get
{
return "SELECT NAME FROM master.dbo.sysdatabases ORDER BY NAME";
throw new NotSupportedException();
}
}
protected override string GetColumnInfosByTableNameSql
@ -67,18 +68,24 @@ namespace SqlSugar.GBase
{
get
{
return @"SELECT s.Name,Convert(varchar(max),tbp.value) as Description
FROM sysobjects s
LEFT JOIN sys.extended_properties as tbp ON s.id=tbp.major_id and tbp.minor_id=0 AND (tbp.Name='MS_Description' OR tbp.Name is null) WHERE s.xtype IN('U') ";
return @"select
trim(a.tabname) as name,
trim(b.comments) as Description
from systables a
left join syscomments b on b.tabname = a.tabname
where a.tabtype in ('T') and not (a.tabname like 'sys%') AND a.tabname <>'dual' ";
}
}
protected override string GetViewInfoListSql
{
get
{
return @"SELECT s.Name,Convert(varchar(max),tbp.value) as Description
FROM sysobjects s
LEFT JOIN sys.extended_properties as tbp ON s.id=tbp.major_id and tbp.minor_id=0 AND (tbp.Name='MS_Description' OR tbp.Name is null) WHERE s.xtype IN('V') ";
return @"select
trim(a.tabname) as name,
trim(b.comments) as Description
from systables a
left join syscomments b on b.tabname = a.tabname
where a.tabtype in ('V') and not (a.tabname like 'sys%') AND a.tabname <>'dual' ";
}
}
#endregion
@ -275,7 +282,7 @@ namespace SqlSugar.GBase
{
get
{
return "select top 1 id from sysobjects";
return "SELECT TOP 1 * FROM Systables";
}
}
#endregion
@ -313,94 +320,39 @@ namespace SqlSugar.GBase
#endregion
#region Methods
public override bool IsAnyTable(string tableName, bool isCache = true)
protected override string GetCreateTableSql(string tableName, List<DbColumnInfo> columns)
{
if (tableName.Contains("."))
List<string> columnArray = new List<string>();
Check.Exception(columns.IsNullOrEmpty(), "No columns found ");
foreach (var item in columns)
{
var schemas = GetSchemas();
var first =this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').First());
var schemaInfo= schemas.FirstOrDefault(it=>it.EqualCase(first));
if (schemaInfo == null)
{
return base.IsAnyTable(tableName, isCache);
}
else
string columnName = this.SqlBuilder.GetTranslationTableName(item.DbColumnName);
string dataType = item.DataType;
string dataSize = GetSize(item);
string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
if (item.IsIdentity&&item.IsPrimarykey)
{
var result= this.Context.Ado.GetInt($"select object_id('{tableName}')");
return result > 0;
dataSize = "";
dataType = "serial primary key";
}
}
else
{
return base.IsAnyTable(tableName, isCache);
}
}
public List<string> GetSchemas()
{
return this.Context.Ado.SqlQuery<string>("SELECT name FROM sys.schemas where name <> 'dbo'");
}
public override bool DeleteTableRemark(string tableName)
{
string sql = string.Format(this.DeleteTableRemarkSql, tableName);
if (tableName.Contains("."))
{
var schemas = GetSchemas();
var tableSchemas = this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').First());
if (schemas.Any(y => y.EqualCase(tableSchemas)))
else if (item.IsIdentity)
{
sql = string.Format(this.DeleteTableRemarkSql, this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').Last()));
if (tableSchemas.EqualCase("user"))
{
sql = sql.Replace("'user'", "'SCHEMA'").Replace("dbo", $"'{tableSchemas}'");
}
else
{
sql = sql.Replace(",dbo,", $",{tableSchemas},").Replace("'user'", "'SCHEMA'");
}
dataSize = "";
dataType = "serial";
}
}
this.Context.Ado.ExecuteCommand(sql);
return true;
}
public override bool IsAnyTableRemark(string tableName)
{
string sql = string.Format(this.IsAnyTableRemarkSql, tableName);
if (tableName.Contains("."))
{
var schemas = GetSchemas();
var tableSchemas = this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').First());
if (schemas.Any(y => y.EqualCase(tableSchemas)))
else if (item.IsPrimarykey)
{
sql = string.Format(this.IsAnyTableRemarkSql, this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').Last()));
sql = sql.Replace("'dbo'", $"'{tableSchemas}'");
dataType = (dataType + " primary key");
}
//string identity = item.IsIdentity ? this.CreateTableIdentity : null;
string addItem = string.Format(this.CreateTableColumn, columnName, dataType, dataSize, nullType, "", "");
columnArray.Add(addItem);
}
var dt = this.Context.Ado.GetDataTable(sql);
return dt.Rows != null && dt.Rows.Count > 0;
}
public override bool AddTableRemark(string tableName, string description)
{
string sql = string.Format(this.AddTableRemarkSql, tableName, description);
if (tableName.Contains("."))
{
var schemas = GetSchemas();
var tableSchemas =this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').First());
if (schemas.Any(y => y.EqualCase(tableSchemas)))
{
sql = string.Format(this.AddTableRemarkSql, this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').Last()), description);
if (tableSchemas.EqualCase("user"))
{
sql = sql.Replace("N'user', N'dbo'", $"N'user', '{tableSchemas}'").Replace("N'user'", "N'SCHEMA'");
}
else
{
sql = sql.Replace("N'dbo'", $"N'{tableSchemas}'").Replace("N'user'", "N'SCHEMA'");
}
}
}
this.Context.Ado.ExecuteCommand(sql);
return true;
string tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray));
return tableString;
}
public override bool AddDefaultValue(string tableName, string columnName, string defaultValue)
{
if (defaultValue == "''")
@ -424,65 +376,7 @@ namespace SqlSugar.GBase
/// <returns></returns>
public override bool CreateDatabase(string databaseName, string databaseDirectory = null)
{
if (databaseDirectory != null)
{
try
{
if (!FileHelper.IsExistDirectory(databaseDirectory))
{
FileHelper.CreateDirectory(databaseDirectory);
}
}
catch
{
//Databases and sites are not in the same service
}
}
var oldDatabaseName = this.Context.Ado.Connection.Database;
var connection = this.Context.CurrentConnectionConfig.ConnectionString;
connection = connection.Replace(oldDatabaseName, "master");
var newDb = new SqlSugarClient(new ConnectionConfig()
{
DbType = this.Context.CurrentConnectionConfig.DbType,
IsAutoCloseConnection = true,
ConnectionString = connection
});
if (!GetDataBaseList(newDb).Any(it => it.Equals(databaseName, StringComparison.CurrentCultureIgnoreCase)))
{
var sql = CreateDataBaseSql;
if (databaseDirectory.HasValue())
{
sql += @"on primary
(
name = N'{0}',
filename = N'{1}\{0}.mdf',
size = 10mb,
maxsize = 100mb,
filegrowth = 1mb
),
(
name = N'{0}_ndf',
filename = N'{1}\{0}.ndf',
size = 10mb,
maxsize = 100mb,
filegrowth = 10 %
)
log on
(
name = N'{0}_log',
filename = N'{1}\{0}.ldf',
size = 100mb,
maxsize = 1gb,
filegrowth = 10mb
); ";
}
if (databaseName.Contains("."))
{
databaseName = $"[{databaseName}]";
}
newDb.Ado.ExecuteCommand(string.Format(sql, databaseName, databaseDirectory));
}
return true;
throw new NotSupportedException();
}
public override bool CreateTable(string tableName, List<DbColumnInfo> columns, bool isCreatePrimaryKey = true)
{
@ -497,21 +391,21 @@ namespace SqlSugar.GBase
}
string sql = GetCreateTableSql(tableName, columns);
this.Context.Ado.ExecuteCommand(sql);
if (isCreatePrimaryKey)
{
var pkColumns = columns.Where(it => it.IsPrimarykey).ToList();
if (pkColumns.Count > 1)
{
this.Context.DbMaintenance.AddPrimaryKeys(tableName, pkColumns.Select(it => it.DbColumnName).ToArray());
}
else
{
foreach (var item in pkColumns)
{
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
}
}
}
//if (isCreatePrimaryKey)
//{
// var pkColumns = columns.Where(it => it.IsPrimarykey).ToList();
// if (pkColumns.Count > 1)
// {
// this.Context.DbMaintenance.AddPrimaryKeys(tableName, pkColumns.Select(it => it.DbColumnName).ToArray());
// }
// else
// {
// foreach (var item in pkColumns)
// {
// this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
// }
// }
//}
return true;
}
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)

9
Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/SqlBuilder/GBaseBuilder.cs

@ -9,7 +9,12 @@ namespace SqlSugar.GBase
{
public class GBaseBuilder : SqlBuilderProvider
{
public override string SqlTranslationLeft { get { return "["; } }
public override string SqlTranslationRight { get { return "]"; } }
public override string SqlTranslationLeft { get { return ""; } }
public override string SqlTranslationRight { get { return ""; } }
public override string GetNoTranslationColumnName(string name)
{
return name;
}
}
}

Loading…
Cancel
Save