diff --git a/Src/Asp.NetCore2/GbaseTest/Config.cs b/Src/Asp.NetCore2/GbaseTest/Config.cs index b5e80a511..63863c796 100644 --- a/Src/Asp.NetCore2/GbaseTest/Config.cs +++ b/Src/Asp.NetCore2/GbaseTest/Config.cs @@ -16,7 +16,7 @@ namespace OrmTest /// Account have permission to create database /// 用有建库权限的数据库账号 /// - 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"; /// /// Account have permission to create database /// 用有建库权限的数据库账号 diff --git a/Src/Asp.NetCore2/GbaseTest/Demo/Demo0_SqlSugarClient.cs b/Src/Asp.NetCore2/GbaseTest/Demo/Demo0_SqlSugarClient.cs index a5fdc11ea..e5200ed5f 100644 --- a/Src/Asp.NetCore2/GbaseTest/Demo/Demo0_SqlSugarClient.cs +++ b/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)); diff --git a/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/CodeFirst/GBaseCodeFirst.cs b/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/CodeFirst/GBaseCodeFirst.cs index 119c5db5a..c932243da 100644 --- a/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/CodeFirst/GBaseCodeFirst.cs +++ b/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 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; } } - } + } } diff --git a/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/DbBind/GBaseDbBind.cs b/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/DbBind/GBaseDbBind.cs index 56c318119..4d550ec80 100644 --- a/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/DbBind/GBaseDbBind.cs +++ b/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/DbBind/GBaseDbBind.cs @@ -34,39 +34,28 @@ namespace SqlSugar.GBase } public static List> MappingTypesConst = new List>() { - new KeyValuePair("int",CSharpDataType.@int), - new KeyValuePair("varchar",CSharpDataType.@string), - new KeyValuePair("nvarchar",CSharpDataType.@string), - new KeyValuePair("sql_variant",CSharpDataType.@string), - new KeyValuePair("text",CSharpDataType.@string), - new KeyValuePair("char",CSharpDataType.@string), - new KeyValuePair("ntext",CSharpDataType.@string), - new KeyValuePair("nchar",CSharpDataType.@string), - new KeyValuePair("hierarchyid",CSharpDataType.@string), new KeyValuePair("bigint",CSharpDataType.@long), - new KeyValuePair("bit",CSharpDataType.@bool), - new KeyValuePair("datetime",CSharpDataType.DateTime), - new KeyValuePair("time",CSharpDataType.DateTime), - new KeyValuePair("smalldatetime",CSharpDataType.DateTime), - new KeyValuePair("timestamp",CSharpDataType.byteArray), - new KeyValuePair("datetime2",CSharpDataType.DateTime), - new KeyValuePair("date",CSharpDataType.DateTime), - new KeyValuePair("decimal",CSharpDataType.@decimal), - new KeyValuePair("single",CSharpDataType.@decimal), - new KeyValuePair("money",CSharpDataType.@decimal), - new KeyValuePair("numeric",CSharpDataType.@decimal), - new KeyValuePair("smallmoney",CSharpDataType.@decimal), - new KeyValuePair("float",CSharpDataType.@double), - new KeyValuePair("float",CSharpDataType.Single), - new KeyValuePair("real",CSharpDataType.@float), - new KeyValuePair("smallint",CSharpDataType.@short), - new KeyValuePair("tinyint",CSharpDataType.@byte), - new KeyValuePair("uniqueidentifier",CSharpDataType.Guid), - new KeyValuePair("binary",CSharpDataType.byteArray), - new KeyValuePair("image",CSharpDataType.byteArray), - new KeyValuePair("varbinary",CSharpDataType.byteArray), - new KeyValuePair("datetimeoffset", CSharpDataType.DateTimeOffset), - new KeyValuePair("datetimeoffset", CSharpDataType.DateTime)}; + new KeyValuePair("blob",CSharpDataType.byteArray), + new KeyValuePair("boolean",CSharpDataType.@bool), + new KeyValuePair("byte",CSharpDataType.@byte), + new KeyValuePair("char",CSharpDataType.@string), + new KeyValuePair("clob",CSharpDataType.@string), + new KeyValuePair("DATETIME YEAR TO FRACTION(3)", CSharpDataType.DateTime), + new KeyValuePair("datetime", CSharpDataType.DateTime), + new KeyValuePair("date", CSharpDataType.DateTime), + new KeyValuePair("decimal", CSharpDataType.@decimal), + new KeyValuePair("float", CSharpDataType.@float), + new KeyValuePair("int", CSharpDataType.@int), + new KeyValuePair("integer", CSharpDataType.@int), + new KeyValuePair("money", CSharpDataType.@decimal), + new KeyValuePair("nchar", CSharpDataType.@string), + new KeyValuePair("numeric", CSharpDataType.@decimal), + new KeyValuePair("nvarchar", CSharpDataType.@string), + new KeyValuePair("varchar", CSharpDataType.@string), + new KeyValuePair("text", CSharpDataType.@string), + new KeyValuePair("smallfloat", CSharpDataType.@decimal), + new KeyValuePair("serial", CSharpDataType.@int), + }; }; } diff --git a/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/DbMaintenance/GBaseDbMaintenance.cs b/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/DbMaintenance/GBaseDbMaintenance.cs index e047ab714..0b787ae13 100644 --- a/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/DbMaintenance/GBaseDbMaintenance.cs +++ b/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 columns) { - if (tableName.Contains(".")) + List columnArray = new List(); + 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 GetSchemas() - { - return this.Context.Ado.SqlQuery("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 /// 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 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 GetColumnInfosByTableName(string tableName, bool isCache = true) diff --git a/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/SqlBuilder/GBaseBuilder.cs b/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/SqlBuilder/GBaseBuilder.cs index 6b8651c63..59f01a937 100644 --- a/Src/Asp.NetCore2/SqlSugar.GBaseCore/GBase/SqlBuilder/GBaseBuilder.cs +++ b/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; + } } + }