ICodeFirst 增加 ConfigEntity 方法,现实干净实体无特性的需求

This commit is contained in:
28810 2019-01-22 12:56:45 +08:00
parent 0bcacc706a
commit 4bf8d60361
22 changed files with 270 additions and 65 deletions

View File

@ -0,0 +1,45 @@
using FreeSql.DataAnnotations;
using Xunit;
namespace FreeSql.Tests.DataAnnotations {
public class FluentTest {
[Fact]
public void Fluent() {
g.mysql.CodeFirst
.ConfigEntity<TestFluenttb1>(a => {
a.Name("xxdkdkdk1").SelectFilter("a.Id22 > 0");
a.Property(b => b.Id).Name("Id22").IsIdentity(true);
a.Property(b => b.name).DbType("varchar(100)").IsNullable(true);
})
.ConfigEntity<TestFluenttb2>(a => {
a.Name("xxdkdkdk2").SelectFilter("a.Idx > 0");
a.Property(b => b.Id).Name("Id22").IsIdentity(true);
a.Property(b => b.name).DbType("varchar(100)").IsNullable(true);
});
var ddl1 = g.mysql.CodeFirst.GetComparisonDDLStatements<TestFluenttb1>();
var ddl2 = g.mysql.CodeFirst.GetComparisonDDLStatements<TestFluenttb2>();
var t1id = g.mysql.Insert<TestFluenttb1>().AppendData(new TestFluenttb1 { }).ExecuteIdentity();
var t1 = g.mysql.Select<TestFluenttb1>(t1id).ToOne();
var t2lastId = g.mysql.Select<TestFluenttb2>().Max(a => a.Id);
var t2affrows = g.mysql.Insert<TestFluenttb2>().AppendData(new TestFluenttb2 { Id = t2lastId + 1 }).ExecuteAffrows();
var t2 = g.mysql.Select<TestFluenttb2>(t2lastId + 1).ToOne();
}
}
class TestFluenttb1 {
public int Id { get; set; }
public string name { get; set; } = "defaultValue";
}
[Table(Name = "cccccdddwww")]
class TestFluenttb2 {
[Column(Name = "Idx", IsPrimary = true, IsIdentity = false)]
public int Id { get; set; }
public string name { get; set; } = "defaultValue";
}
}

View File

@ -85,8 +85,7 @@ namespace FreeSql.Tests.MySql {
[Fact]
public void Lazy() {
//Type.GetType();
var sql = g.mysql.Select<Tag>().Where(a => g.mysql.Select<Song_tag>().Where(b => b.Tag_id == a.Id && b.Song_id == 1).Any());
var tags = g.mysql.Select<Tag>().Where(a => a.Parent.Name == "xxx").LeftJoin(a => a.Parent_id == a.Parent.Id).ToList();
var songs = g.mysql.Select<Song>().Limit(10).ToList();

View File

@ -16,18 +16,19 @@ namespace FreeSql.DataAnnotations {
/// </summary>
public string DbType { get; set; }
internal bool? _IsPrimary, _IsIdentity, _IsNullable;
/// <summary>
/// 主键
/// </summary>
public bool IsPrimary { get; set; }
public bool IsPrimary { get => _IsPrimary ?? false; set => _IsPrimary = value; }
/// <summary>
/// 自增标识
/// </summary>
public bool IsIdentity { get; set; }
public bool IsIdentity { get => _IsIdentity ?? false; set => _IsIdentity = value; }
/// <summary>
/// 是否可DBNull
/// </summary>
public bool IsNullable { get; set; }
public bool IsNullable { get => _IsNullable ?? false; set => _IsNullable = value; }
/// <summary>
/// 数据库默认值

View File

@ -0,0 +1,54 @@
using System;
namespace FreeSql.DataAnnotations {
public class ColumnFluent<T> {
public ColumnFluent(ColumnAttribute column) {
_column = column;
}
ColumnAttribute _column;
/// <summary>
/// 数据库列名
/// </summary>
public ColumnFluent<T> Name(string value) {
_column.Name = value;
return this;
}
/// <summary>
/// 指定数据库旧的列名修改实体属性命名时同时设置此参数为修改之前的值CodeFirst才可以正确修改数据库字段否则将视为【新增字段】
/// </summary>
public ColumnFluent<T> OldName(string value) {
_column.OldName = value;
return this;
}
/// <summary>
/// 数据库类型,如: varchar(255)
/// </summary>
public ColumnFluent<T> DbType(string value) {
_column.DbType = value;
return this;
}
/// <summary>
/// 主键
/// </summary>
public ColumnFluent<T> IsPrimary(bool value) {
_column.IsPrimary = value;
return this;
}
/// 自增标识
/// </summary>
public ColumnFluent<T> IsIdentity(bool value) {
_column.IsIdentity = value;
return this;
}
/// <summary>
/// 是否可DBNull
/// </summary>
public ColumnFluent<T> IsNullable(bool value) {
_column.IsNullable = value;
return this;
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
namespace FreeSql.DataAnnotations {
public class TableAttribute : Attribute {
@ -15,5 +16,7 @@ namespace FreeSql.DataAnnotations {
/// 查询过滤SQL实现类似 a.IsDeleted = 1 功能
/// </summary>
public string SelectFilter { get; set; }
internal ConcurrentDictionary<string, ColumnAttribute> _columns = new ConcurrentDictionary<string, ColumnAttribute>(StringComparer.CurrentCultureIgnoreCase);
}
}

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq.Expressions;
namespace FreeSql.DataAnnotations {
public class TableFluent<T> {
public TableFluent(TableAttribute table) {
_table = table;
}
TableAttribute _table;
/// <summary>
/// 数据库表名
/// </summary>
public TableFluent<T> Name(string value) {
_table.Name = value;
return this;
}
/// <summary>
/// 指定数据库旧的表名修改实体命名时同时设置此参数为修改之前的值CodeFirst才可以正确修改数据库表否则将视为【创建新表】
/// </summary>
public TableFluent<T> OldName(string value) {
_table.OldName = value;
return this;
}
/// <summary>
/// 查询过滤SQL实现类似 a.IsDeleted = 1 功能
/// </summary>
public TableFluent<T> SelectFilter(string value) {
_table.SelectFilter = value;
return this;
}
public ColumnFluent<TProto> Property<TProto>(Expression<Func<T, TProto>> column) {
var proto = (column.Body as MemberExpression)?.Member;
if (proto == null) throw new FormatException($"错误的表达式格式 {column}");
var col = _table._columns.GetOrAdd(proto.Name, name => new ColumnAttribute { Name = proto.Name });
return new ColumnFluent<TProto>(col);
}
}
}

View File

@ -5,10 +5,26 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
public static class FreeSqlGlobalExtensions {
static Lazy<Dictionary<Type, bool>> dicIsNumberType = new Lazy<Dictionary<Type, bool>>(() => new Dictionary<Type, bool> {
[typeof(sbyte)] = true,
[typeof(short)] = true,
[typeof(int)] = true,
[typeof(long)] = true,
[typeof(byte)] = true,
[typeof(ushort)] = true,
[typeof(uint)] = true,
[typeof(ulong)] = true,
[typeof(double)] = true,
[typeof(float)] = true,
[typeof(decimal)] = true
});
public static bool IsNumberType(this Type that) => that == null ? false : dicIsNumberType.Value.ContainsKey(that.GenericTypeArguments.FirstOrDefault() ?? that);
/// <summary>
/// 测量两个经纬度的距离,返回单位:米
/// </summary>

View File

@ -3,6 +3,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;

View File

@ -1,4 +1,5 @@
using System;
using FreeSql.DataAnnotations;
using System;
namespace FreeSql {
public interface ICodeFirst {
@ -48,5 +49,6 @@ namespace FreeSql {
/// <param name="type"></param>
/// <returns></returns>
(int type, string dbtype, string dbtypeFull, bool? isnullable, object defaultValue)? GetDbInfo(Type type);
ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity);
}
}

View File

@ -194,8 +194,11 @@ namespace FreeSql.Internal {
for (var a = tbidx + 1; a < _tables.Count; a++)
_tables[a].Type = SelectTableInfoType.From;
} else {
var find = _tables.Where((a, c) => c > 0 && a.Type == tbtype && string.IsNullOrEmpty(a.On)).LastOrDefault();
if (find != null) find.On = filter;
var find = _tables.Where((a, c) => c > 0 && (a.Type == tbtype || a.Type == SelectTableInfoType.From) && string.IsNullOrEmpty(a.On)).LastOrDefault();
if (find != null) {
find.Type = tbtype;
find.On = filter;
}
}
}
@ -355,13 +358,13 @@ namespace FreeSql.Internal {
if (isQuoteName) name = _common.QuoteSqlName(name);
return name;
}
Func<TableInfo, string, SelectTableInfo> getOrAddTable = (tbtmp, alias) => {
var finds = _tables.Where((a2, c2) => a2.Table.CsName == tbtmp.CsName).ToArray(); //外部表,内部表一起查
Func<TableInfo, string, bool, SelectTableInfo> getOrAddTable = (tbtmp, alias, isa) => {
var finds = _tables.Where((a2, c2) => (isa || c2 > 0) && a2.Table.CsName == tbtmp.CsName).ToArray(); //外部表,内部表一起查
if (finds.Length > 1) {
finds = _tables.Where((a2, c2) => a2.Table.CsName == tbtmp.CsName && a2.Type == SelectTableInfoType.Parent && a2.Alias == $"__parent_{alias}_parent__").ToArray(); //查询外部表
if (finds.Any() == false) {
finds = _tables.Where((a2, c2) => a2.Table.CsName == tbtmp.CsName && a2.Type != SelectTableInfoType.Parent).ToArray(); //查询内部表
if (finds.Length > 1) finds = _tables.Where((a2, c2) => a2.Table.CsName == tbtmp.CsName && a2.Type != SelectTableInfoType.Parent && a2.Alias == alias).ToArray();
finds = _tables.Where((a2, c2) => (isa || c2 > 0) && a2.Table.CsName == tbtmp.CsName && a2.Type != SelectTableInfoType.Parent).ToArray(); //查询内部表
if (finds.Length > 1) finds = _tables.Where((a2, c2) => (isa || c2 > 0) && a2.Table.CsName == tbtmp.CsName && a2.Type != SelectTableInfoType.Parent && a2.Alias == alias).ToArray();
}
}
var find = finds.FirstOrDefault();
@ -388,7 +391,7 @@ namespace FreeSql.Internal {
if (tb2tmp != null) {
if (exp2.NodeType == ExpressionType.Parameter) alias2 = (exp2 as ParameterExpression).Name;
else alias2 = $"{alias2}__{mp2.Member.Name}";
find2 = getOrAddTable(tb2tmp, alias2);
find2 = getOrAddTable(tb2tmp, alias2, exp2.NodeType == ExpressionType.Parameter);
alias2 = find2.Alias;
tb2 = tb2tmp;
}
@ -397,7 +400,7 @@ namespace FreeSql.Internal {
if (_selectColumnMap != null) {
var tb3 = _common.GetTableByEntity(mp2.Type);
if (tb3 != null) {
var find3 = getOrAddTable(tb2tmp, $"{alias2}__{mp2.Member.Name}");
var find3 = getOrAddTable(tb2tmp, $"{alias2}__{mp2.Member.Name}", exp2.NodeType == ExpressionType.Parameter);
foreach (var tb3c in tb3.Columns.Values)
_selectColumnMap.Add(new SelectColumnInfo { Table = find3, Column = tb3c });

View File

@ -283,8 +283,8 @@ namespace FreeSql.Internal.CommonProvider {
if (dicfield.ContainsKey(quoteName)) field.Append(" as").Append(index);
else dicfield.Add(quoteName, true);
} else {
var tb2 = _tables.Where(a => a.Table.Type == prop.PropertyType && a.Alias.Contains(prop.Name)).FirstOrDefault();
if (tb2 == null && props.Where(pw => pw.Value.PropertyType == prop.PropertyType).Count() == 1) tb2 = _tables.Where(a => a.Table.Type == prop.PropertyType).FirstOrDefault();
var tb2 = _tables.Where((a, b) => b > 0 && a.Table.Type == prop.PropertyType && a.Alias.Contains(prop.Name)).FirstOrDefault(); //判断 b > 0 防止 parent 递归关系
if (tb2 == null && props.Where(pw => pw.Value.PropertyType == prop.PropertyType).Count() == 1) tb2 = _tables.Where((a, b) => b > 0 && a.Table.Type == prop.PropertyType).FirstOrDefault();
if (tb2 == null) continue;
foreach (var col2 in tb2.Table.Columns.Values) {
if (index > 0) field.Append(", ");

View File

@ -74,7 +74,7 @@ namespace FreeSql.Internal.CommonProvider {
var expt = _commonExpression.ExpressionWhereLambdaNoneForeignObject(null, cols, binaryExpression, null);
if (cols.Any() == false) return this;
foreach (var col in cols) {
if (col.Column.Attribute.IsNullable) {
if (col.Column.Attribute.IsNullable == true) {
var replval = _orm.CodeFirst.GetDbInfo(col.Column.CsType.GenericTypeArguments.FirstOrDefault())?.defaultValue;
if (replval == null) continue;
var replname = _commonUtils.QuoteSqlName(col.Column.Attribute.Name);

View File

@ -1,6 +1,8 @@
using FreeSql.Internal.Model;
using FreeSql.DataAnnotations;
using FreeSql.Internal.Model;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
@ -30,11 +32,45 @@ namespace FreeSql.Internal {
_orm = orm;
}
ConcurrentDictionary<Type, TableAttribute> dicConfigEntity = new ConcurrentDictionary<Type, TableAttribute>();
internal ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) {
var type = typeof(T);
var table = dicConfigEntity.GetOrAdd(type, new TableAttribute());
var fluent = new TableFluent<T>(table);
entity?.Invoke(fluent);
return _orm.CodeFirst;
}
internal TableAttribute GetEntityTableAttribute(Type entityType) {
var attr = entityType.GetCustomAttributes(typeof(TableAttribute), false).LastOrDefault() as TableAttribute;
if (dicConfigEntity.TryGetValue(entityType, out var trytb) == false) return attr;
if (attr == null) attr = new TableAttribute();
if (string.IsNullOrEmpty(attr.Name)) attr.Name = trytb.Name;
if (string.IsNullOrEmpty(attr.OldName)) attr.OldName = trytb.OldName;
if (string.IsNullOrEmpty(attr.SelectFilter)) attr.SelectFilter = trytb.SelectFilter;
return attr;
}
internal ColumnAttribute GetEntityColumnAttribute(Type entityType, PropertyInfo proto) {
var attr = proto.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute;
if (dicConfigEntity.TryGetValue(entityType, out var trytb) == false) return attr;
if (trytb._columns.TryGetValue(proto.Name, out var trycol) == false) return attr;
if (attr == null) attr = new ColumnAttribute();
if (string.IsNullOrEmpty(attr.Name)) attr.Name = trycol.Name;
if (string.IsNullOrEmpty(attr.OldName)) attr.OldName = trycol.OldName;
if (string.IsNullOrEmpty(attr.DbType)) attr.DbType = trycol.DbType;
if (attr._IsPrimary == null) attr._IsPrimary = trycol.IsPrimary;
if (attr._IsIdentity == null) attr._IsIdentity = trycol.IsIdentity;
if (attr._IsNullable == null) attr._IsNullable = trycol.IsNullable;
if (attr.DbDefautValue == null) attr.DbDefautValue = trycol.DbDefautValue;
return attr;
}
internal string WhereObject(TableInfo table, string aliasAndDot, object dywhere) {
if (dywhere == null) return "";
var type = dywhere.GetType();
var primarys = table.Columns.Values.Where(a => a.Attribute.IsPrimary).ToArray();
if (primarys.Length == 1 && type == primarys.First().CsType) {
var primarys = table.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
if (primarys.Length == 1 && (type == primarys.First().CsType || type.IsNumberType() && primarys.First().CsType.IsNumberType())) {
return $"{aliasAndDot}{this.QuoteSqlName(primarys.First().Attribute.Name)} = {this.FormatSql("{0}", dywhere)}";
} else if (primarys.Length > 0 && type.FullName == table.Type.FullName) {
var sb = new StringBuilder();

View File

@ -30,7 +30,7 @@ namespace FreeSql.Internal {
if (tbc.TryGetValue(entity, out var trytb)) return trytb;
if (common.CodeFirst.GetDbInfo(entity) != null) return null;
var tbattr = entity.GetCustomAttributes(typeof(TableAttribute), false).LastOrDefault() as TableAttribute;
var tbattr = common.GetEntityTableAttribute(entity);
trytb = new TableInfo();
trytb.Type = entity;
trytb.Properties = entity.GetProperties().ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase);
@ -46,7 +46,7 @@ namespace FreeSql.Internal {
foreach (var p in trytb.Properties.Values) {
var tp = common.CodeFirst.GetDbInfo(p.PropertyType);
//if (tp == null) continue;
var colattr = p.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute;
var colattr = common.GetEntityColumnAttribute(entity, p);
if (tp == null && colattr == null) {
if (common.CodeFirst.IsLazyLoading) {
var getIsVirtual = trytb.Type.GetMethod($"get_{p.Name}")?.IsVirtual;
@ -72,7 +72,7 @@ namespace FreeSql.Internal {
if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
if (common.CodeFirst.IsSyncStructureToLower) colattr.Name = colattr.Name.ToLower();
if ((colattr.IsNullable == false || colattr.IsIdentity || colattr.IsPrimary) && colattr.DbType.Contains("NOT NULL") == false) {
if ((colattr.IsNullable != true || colattr.IsIdentity == true || colattr.IsPrimary == true) && colattr.DbType.Contains("NOT NULL") == false) {
colattr.IsNullable = false;
colattr.DbType += " NOT NULL";
}
@ -89,10 +89,7 @@ namespace FreeSql.Internal {
var consturctorType = p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType;
colattr.DbDefautValue = Activator.CreateInstance(consturctorType);
}
if (colattr.IsIdentity && new[] {
typeof(sbyte), typeof(short), typeof(int), typeof(long),
typeof(byte), typeof(ushort), typeof(uint), typeof(ulong),
typeof(double), typeof(float), typeof(decimal) }.Contains(p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType) == false)
if (colattr.IsIdentity == true && (p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType)?.IsNumberType() == false)
colattr.IsIdentity = false;
var col = new ColumnInfo {
@ -104,9 +101,9 @@ namespace FreeSql.Internal {
trytb.Columns.Add(colattr.Name, col);
trytb.ColumnsByCs.Add(p.Name, col);
}
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary).ToArray();
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
if (trytb.Primarys.Any() == false) {
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity).ToArray();
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity == true).ToArray();
foreach (var col in trytb.Primarys)
col.Attribute.IsPrimary = true;
}

View File

@ -57,7 +57,7 @@ namespace FreeSql.Internal {
if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
if (common.CodeFirst.IsSyncStructureToLower) colattr.Name = colattr.Name.ToLower();
if ((colattr.IsNullable == false || colattr.IsIdentity || colattr.IsPrimary) && colattr.DbType.Contains("NOT NULL") == false) {
if ((colattr.IsNullable != true || colattr.IsIdentity == true || colattr.IsPrimary == true) && colattr.DbType.Contains("NOT NULL") == false) {
colattr.IsNullable = false;
colattr.DbType += " NOT NULL";
}
@ -84,9 +84,9 @@ namespace FreeSql.Internal {
trytb.Columns.Add(colattr.Name, col);
trytb.ColumnsByCs.Add(p.Name, col);
}
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary).ToArray();
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
if (trytb.Primarys.Any() == false) {
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity).ToArray();
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity == true).ToArray();
foreach(var col in trytb.Primarys)
col.Attribute.IsPrimary = true;
}

View File

@ -1,4 +1,5 @@
using FreeSql.DatabaseModel;
using FreeSql.DataAnnotations;
using FreeSql.DatabaseModel;
using FreeSql.Internal;
using FreeSql.Internal.Model;
using MySql.Data.MySqlClient;
@ -126,7 +127,7 @@ namespace FreeSql.MySql {
foreach (var tbcol in tb.Columns.Values) {
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
sb.Append(tbcol.Attribute.DbType);
if (tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" AUTO_INCREMENT");
if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" AUTO_INCREMENT");
sb.Append(",");
}
if (tb.Primarys.Any() == false)
@ -170,7 +171,7 @@ where a.table_schema in ({0}) and a.table_name in ({1})".FormatMySql(tboldname ?
if (istmpatler == false) {
var existsPrimary = ExecuteScalar(tbname[0], "select 1 from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where table_schema={0} and table_name={1} limit 1".FormatMySql(tbname));
foreach (var tbcol in tb.Columns.Values) {
var isIdentityChanged = tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1;
var isIdentityChanged = tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1;
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out tbstructcol)) {
if ((tbcol.Attribute.DbType.IndexOf(" unsigned", StringComparison.CurrentCultureIgnoreCase) != -1) != tbstructcol.is_unsigned ||
@ -208,7 +209,7 @@ where a.table_schema in ({0}) and a.table_name in ({1})".FormatMySql(tboldname ?
foreach (var tbcol in tb.Columns.Values) {
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
sb.Append(tbcol.Attribute.DbType);
if (tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" AUTO_INCREMENT");
if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" AUTO_INCREMENT");
sb.Append(",");
}
if (tb.Primarys.Any() == false)
@ -267,6 +268,6 @@ where a.table_schema in ({0}) and a.table_name in ({1})".FormatMySql(tboldname ?
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
return affrows > 0;
}
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
}
}

View File

@ -28,7 +28,7 @@ namespace FreeSql.Oracle.Curd {
sbtb.Append(_commonUtils.QuoteSqlName(_table.DbName)).Append("(");
var colidx = 0;
foreach (var col in _table.Columns.Values) {
if (col.Attribute.IsIdentity) {
if (col.Attribute.IsIdentity == true) {
_identCol = col;
continue;
}

View File

@ -1,4 +1,5 @@
using FreeSql.DatabaseModel;
using FreeSql.DataAnnotations;
using FreeSql.DatabaseModel;
using FreeSql.Internal;
using FreeSql.Internal.Model;
using Oracle.ManagedDataAccess.Client;
@ -106,7 +107,7 @@ namespace FreeSql.Oracle {
sb.Append("execute immediate 'CREATE TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" (");
foreach (var tbcol in tb.Columns.Values) {
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
if (tbcol.Attribute.IsIdentity) seqcols.Add((tbcol, tbname, true));
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, true));
}
if (tb.Primarys.Any() == false)
sb.Remove(sb.Length - 1, 1);
@ -184,10 +185,10 @@ where owner={{0}} and table_name={{1}}".FormatOracleSQL(tboldname ?? tbname);
if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable) {
if (tbcol.Attribute.IsNullable == false)
sbalter.Append("execute immediate 'UPDATE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" SET ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(_commonUtils.FormatSql(" = {0}", tbcol.Attribute.DbDefautValue).Replace("'", "''")).Append(" WHERE ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" IS NULL';\r\n");
sbalter.Append("execute immediate 'ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" MODIFY ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" ").Append(tbcol.Attribute.IsNullable ? "" : "NOT").Append(" NULL';\r\n");
sbalter.Append("execute immediate 'ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" MODIFY ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" ").Append(tbcol.Attribute.IsNullable == true ? "" : "NOT").Append(" NULL';\r\n");
}
if (tbcol.Attribute.IsIdentity != tbstructcol.is_identity)
seqcols.Add((tbcol, tbname, tbcol.Attribute.IsIdentity));
seqcols.Add((tbcol, tbname, tbcol.Attribute.IsIdentity == true));
if (tbstructcol.column == tbcol.Attribute.OldName)
//修改列名
sbalter.Append("execute immediate 'ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" RENAME ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.OldName)).Append(" TO ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append("';\r\n");
@ -199,7 +200,7 @@ where owner={{0}} and table_name={{1}}".FormatOracleSQL(tboldname ?? tbname);
sbalter.Append("execute immediate 'UPDATE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" SET ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(_commonUtils.FormatSql(" = {0}", tbcol.Attribute.DbDefautValue).Replace("'", "''")).Append("';\r\n");
sbalter.Append("execute immediate 'ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" MODIFY ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" NOT NULL';\r\n");
}
if (tbcol.Attribute.IsIdentity) seqcols.Add((tbcol, tbname, tbcol.Attribute.IsIdentity));
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, tbcol.Attribute.IsIdentity == true));
}
}
if (istmpatler == false) {
@ -217,7 +218,7 @@ where owner={{0}} and table_name={{1}}".FormatOracleSQL(tboldname ?? tbname);
sb.Append("execute immediate 'CREATE TABLE ").Append(tmptablename).Append(" (");
foreach (var tbcol in tb.Columns.Values) {
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
if (tbcol.Attribute.IsIdentity) seqcols.Add((tbcol, tbname, true));
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, true));
}
if (tb.Primarys.Any() == false)
sb.Remove(sb.Length - 1, 1);
@ -305,6 +306,6 @@ where owner={{0}} and table_name={{1}}".FormatOracleSQL(tboldname ?? tbname);
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
return affrows > 0;
}
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
}
}

View File

@ -16,7 +16,7 @@ namespace FreeSql.PostgreSQL.Curd {
var sql = this.ToSql();
if (string.IsNullOrEmpty(sql)) return 0;
var identCols = _table.Columns.Where(a => a.Value.Attribute.IsIdentity);
var identCols = _table.Columns.Where(a => a.Value.Attribute.IsIdentity == true);
if (identCols.Any() == false) {
_orm.Ado.ExecuteNonQuery(CommandType.Text, sql, _params);
return 0;
@ -27,7 +27,7 @@ namespace FreeSql.PostgreSQL.Curd {
var sql = this.ToSql();
if (string.IsNullOrEmpty(sql)) return 0;
var identCols = _table.Columns.Where(a => a.Value.Attribute.IsIdentity);
var identCols = _table.Columns.Where(a => a.Value.Attribute.IsIdentity == true);
if (identCols.Any() == false) {
await _orm.Ado.ExecuteNonQueryAsync(CommandType.Text, sql, _params);
return 0;

View File

@ -1,4 +1,5 @@
using FreeSql.DatabaseModel;
using FreeSql.DataAnnotations;
using FreeSql.DatabaseModel;
using FreeSql.Internal;
using FreeSql.Internal.Model;
using Newtonsoft.Json.Linq;
@ -149,7 +150,7 @@ namespace FreeSql.PostgreSQL {
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" (");
foreach (var tbcol in tb.Columns.Values) {
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
if (tbcol.Attribute.IsIdentity) seqcols.Add((tbcol, tbname, true));
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, true));
}
if (tb.Primarys.Any() == false)
sb.Remove(sb.Length - 1, 1);
@ -223,9 +224,9 @@ where ns.nspname = {0} and c.relname = {1}".FormatPostgreSQL(tboldname ?? tbname
tbcol.Attribute.DbType.Contains("[]") != (tbstructcol.attndims > 0))
sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" TYPE ").Append(tbcol.Attribute.DbType.Split(' ').First()).Append(";\r\n");
if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable)
sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" ").Append(tbcol.Attribute.IsNullable ? "DROP" : "SET").Append(" NOT NULL;\r\n");
sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" ").Append(tbcol.Attribute.IsNullable == true ? "DROP" : "SET").Append(" NOT NULL;\r\n");
if (tbcol.Attribute.IsIdentity != tbstructcol.is_identity)
seqcols.Add((tbcol, tbname, tbcol.Attribute.IsIdentity));
seqcols.Add((tbcol, tbname, tbcol.Attribute.IsIdentity == true));
if (tbstructcol.column == tbcol.Attribute.OldName)
//修改列名
sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" RENAME COLUMN ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.OldName)).Append(" TO ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(";\r\n");
@ -235,7 +236,7 @@ where ns.nspname = {0} and c.relname = {1}".FormatPostgreSQL(tboldname ?? tbname
sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD COLUMN ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType.Split(' ').First()).Append(";\r\n");
sbalter.Append("UPDATE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" SET ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(_commonUtils.FormatSql(" = {0};\r\n", tbcol.Attribute.DbDefautValue));
if (tbcol.Attribute.IsNullable == false) sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" SET NOT NULL;\r\n");
if (tbcol.Attribute.IsIdentity) seqcols.Add((tbcol, tbname, tbcol.Attribute.IsIdentity));
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, tbcol.Attribute.IsIdentity == true));
}
}
if (istmpatler == false) {
@ -257,7 +258,7 @@ where pg_namespace.nspname={0} and pg_class.relname={1} and pg_constraint.contyp
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(tmptablename).Append(" (");
foreach (var tbcol in tb.Columns.Values) {
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
if (tbcol.Attribute.IsIdentity) seqcols.Add((tbcol, tbname, true));
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, true));
}
if (tb.Primarys.Any() == false)
sb.Remove(sb.Length - 1, 1);
@ -319,6 +320,6 @@ where pg_namespace.nspname={0} and pg_class.relname={1} and pg_constraint.contyp
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
return affrows > 0;
}
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
}
}

View File

@ -8,6 +8,7 @@ using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using FreeSql.DataAnnotations;
namespace FreeSql.SqlServer {
@ -123,8 +124,8 @@ namespace FreeSql.SqlServer {
foreach (var tbcol in tb.Columns.Values) {
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
sb.Append(tbcol.Attribute.DbType);
if (tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
if (tbcol.Attribute.IsPrimary) sb.Append(" primary key");
if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
if (tbcol.Attribute.IsPrimary == true) sb.Append(" primary key");
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1).Append("\r\n);\r\n");
@ -188,7 +189,7 @@ use " + database, tboldname ?? tbname);
}
//添加列
sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}.{tbname[2]}")).Append(" ADD ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType);
if (tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sbalter.Append(" identity(1,1)");
if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sbalter.Append(" identity(1,1)");
if (tbcol.Attribute.IsNullable == false) {
var addcoldbdefault = tbcol.Attribute.DbDefautValue;
if (addcoldbdefault != null) sbalter.Append(_commonUtils.FormatSql(" default({0})", addcoldbdefault));
@ -219,10 +220,10 @@ use " + database, tboldname ?? tbname);
foreach (var tbcol in tb.Columns.Values) {
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
sb.Append(tbcol.Attribute.DbType);
if (tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
if (tbcol.Attribute.IsPrimary) sb.Append(" primary key");
if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
if (tbcol.Attribute.IsPrimary == true) sb.Append(" primary key");
sb.Append(",");
idents = idents || tbcol.Attribute.IsIdentity;
idents = idents || tbcol.Attribute.IsIdentity == true;
}
sb.Remove(sb.Length - 1, 1).Append("\r\n);\r\n");
sb.Append("ALTER TABLE ").Append(tmptablename).Append(" SET (LOCK_ESCALATION = TABLE);\r\n");
@ -277,6 +278,6 @@ use " + database, tboldname ?? tbname);
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
return affrows > 0;
}
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
}
}

View File

@ -1,4 +1,5 @@
using FreeSql.DatabaseModel;
using FreeSql.DataAnnotations;
using FreeSql.DatabaseModel;
using FreeSql.Internal;
using FreeSql.Internal.Model;
using System;
@ -100,7 +101,7 @@ namespace FreeSql.Sqlite {
foreach (var tbcol in tb.Columns.Values) {
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
sb.Append(tbcol.Attribute.DbType);
if (tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("AUTOINCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) {
if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTOINCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) {
isIndent = true;
sb.Append(" PRIMARY KEY AUTOINCREMENT");
}
@ -180,7 +181,7 @@ namespace FreeSql.Sqlite {
foreach (var tbcol in tb.Columns.Values) {
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
sb.Append(tbcol.Attribute.DbType);
if (tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("AUTOINCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) {
if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTOINCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) {
isIndent = true;
sb.Append(" PRIMARY KEY AUTOINCREMENT");
}
@ -235,6 +236,6 @@ namespace FreeSql.Sqlite {
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
return affrows > 0;
}
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
}
}