mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
ICodeFirst 增加 ConfigEntity 方法,现实干净实体无特性的需求
This commit is contained in:
parent
0bcacc706a
commit
4bf8d60361
45
FreeSql.Tests/DataAnnotations/FluentTest.cs
Normal file
45
FreeSql.Tests/DataAnnotations/FluentTest.cs
Normal 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";
|
||||||
|
}
|
||||||
|
}
|
@ -85,8 +85,7 @@ namespace FreeSql.Tests.MySql {
|
|||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Lazy() {
|
public void Lazy() {
|
||||||
//Type.GetType();
|
var tags = g.mysql.Select<Tag>().Where(a => a.Parent.Name == "xxx").LeftJoin(a => a.Parent_id == a.Parent.Id).ToList();
|
||||||
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 songs = g.mysql.Select<Song>().Limit(10).ToList();
|
var songs = g.mysql.Select<Song>().Limit(10).ToList();
|
||||||
|
|
||||||
|
@ -16,18 +16,19 @@ namespace FreeSql.DataAnnotations {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string DbType { get; set; }
|
public string DbType { get; set; }
|
||||||
|
|
||||||
|
internal bool? _IsPrimary, _IsIdentity, _IsNullable;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 主键
|
/// 主键
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsPrimary { get; set; }
|
public bool IsPrimary { get => _IsPrimary ?? false; set => _IsPrimary = value; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 自增标识
|
/// 自增标识
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsIdentity { get; set; }
|
public bool IsIdentity { get => _IsIdentity ?? false; set => _IsIdentity = value; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否可DBNull
|
/// 是否可DBNull
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsNullable { get; set; }
|
public bool IsNullable { get => _IsNullable ?? false; set => _IsNullable = value; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 数据库默认值
|
/// 数据库默认值
|
||||||
|
54
FreeSql/DataAnnotations/ColumnFluent.cs
Normal file
54
FreeSql/DataAnnotations/ColumnFluent.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
namespace FreeSql.DataAnnotations {
|
namespace FreeSql.DataAnnotations {
|
||||||
public class TableAttribute : Attribute {
|
public class TableAttribute : Attribute {
|
||||||
@ -15,5 +16,7 @@ namespace FreeSql.DataAnnotations {
|
|||||||
/// 查询过滤SQL,实现类似 a.IsDeleted = 1 功能
|
/// 查询过滤SQL,实现类似 a.IsDeleted = 1 功能
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string SelectFilter { get; set; }
|
public string SelectFilter { get; set; }
|
||||||
|
|
||||||
|
internal ConcurrentDictionary<string, ColumnAttribute> _columns = new ConcurrentDictionary<string, ColumnAttribute>(StringComparer.CurrentCultureIgnoreCase);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
43
FreeSql/DataAnnotations/TableFluent.cs
Normal file
43
FreeSql/DataAnnotations/TableFluent.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,10 +5,26 @@ using System.Collections.Generic;
|
|||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
public static class FreeSqlGlobalExtensions {
|
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>
|
||||||
/// 测量两个经纬度的距离,返回单位:米
|
/// 测量两个经纬度的距离,返回单位:米
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -3,6 +3,7 @@ using System;
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using FreeSql.DataAnnotations;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace FreeSql {
|
namespace FreeSql {
|
||||||
public interface ICodeFirst {
|
public interface ICodeFirst {
|
||||||
@ -48,5 +49,6 @@ namespace FreeSql {
|
|||||||
/// <param name="type"></param>
|
/// <param name="type"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
(int type, string dbtype, string dbtypeFull, bool? isnullable, object defaultValue)? GetDbInfo(Type type);
|
(int type, string dbtype, string dbtypeFull, bool? isnullable, object defaultValue)? GetDbInfo(Type type);
|
||||||
|
ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -194,8 +194,11 @@ namespace FreeSql.Internal {
|
|||||||
for (var a = tbidx + 1; a < _tables.Count; a++)
|
for (var a = tbidx + 1; a < _tables.Count; a++)
|
||||||
_tables[a].Type = SelectTableInfoType.From;
|
_tables[a].Type = SelectTableInfoType.From;
|
||||||
} else {
|
} else {
|
||||||
var find = _tables.Where((a, c) => c > 0 && a.Type == tbtype && string.IsNullOrEmpty(a.On)).LastOrDefault();
|
var find = _tables.Where((a, c) => c > 0 && (a.Type == tbtype || a.Type == SelectTableInfoType.From) && string.IsNullOrEmpty(a.On)).LastOrDefault();
|
||||||
if (find != null) find.On = filter;
|
if (find != null) {
|
||||||
|
find.Type = tbtype;
|
||||||
|
find.On = filter;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -355,13 +358,13 @@ namespace FreeSql.Internal {
|
|||||||
if (isQuoteName) name = _common.QuoteSqlName(name);
|
if (isQuoteName) name = _common.QuoteSqlName(name);
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
Func<TableInfo, string, SelectTableInfo> getOrAddTable = (tbtmp, alias) => {
|
Func<TableInfo, string, bool, SelectTableInfo> getOrAddTable = (tbtmp, alias, isa) => {
|
||||||
var finds = _tables.Where((a2, c2) => a2.Table.CsName == tbtmp.CsName).ToArray(); //外部表,内部表一起查
|
var finds = _tables.Where((a2, c2) => (isa || c2 > 0) && a2.Table.CsName == tbtmp.CsName).ToArray(); //外部表,内部表一起查
|
||||||
if (finds.Length > 1) {
|
if (finds.Length > 1) {
|
||||||
finds = _tables.Where((a2, c2) => a2.Table.CsName == tbtmp.CsName && a2.Type == SelectTableInfoType.Parent && a2.Alias == $"__parent_{alias}_parent__").ToArray(); //查询外部表
|
finds = _tables.Where((a2, c2) => a2.Table.CsName == tbtmp.CsName && a2.Type == SelectTableInfoType.Parent && a2.Alias == $"__parent_{alias}_parent__").ToArray(); //查询外部表
|
||||||
if (finds.Any() == false) {
|
if (finds.Any() == false) {
|
||||||
finds = _tables.Where((a2, c2) => a2.Table.CsName == tbtmp.CsName && a2.Type != SelectTableInfoType.Parent).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) => a2.Table.CsName == tbtmp.CsName && a2.Type != SelectTableInfoType.Parent && a2.Alias == alias).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();
|
var find = finds.FirstOrDefault();
|
||||||
@ -388,7 +391,7 @@ namespace FreeSql.Internal {
|
|||||||
if (tb2tmp != null) {
|
if (tb2tmp != null) {
|
||||||
if (exp2.NodeType == ExpressionType.Parameter) alias2 = (exp2 as ParameterExpression).Name;
|
if (exp2.NodeType == ExpressionType.Parameter) alias2 = (exp2 as ParameterExpression).Name;
|
||||||
else alias2 = $"{alias2}__{mp2.Member.Name}";
|
else alias2 = $"{alias2}__{mp2.Member.Name}";
|
||||||
find2 = getOrAddTable(tb2tmp, alias2);
|
find2 = getOrAddTable(tb2tmp, alias2, exp2.NodeType == ExpressionType.Parameter);
|
||||||
alias2 = find2.Alias;
|
alias2 = find2.Alias;
|
||||||
tb2 = tb2tmp;
|
tb2 = tb2tmp;
|
||||||
}
|
}
|
||||||
@ -397,7 +400,7 @@ namespace FreeSql.Internal {
|
|||||||
if (_selectColumnMap != null) {
|
if (_selectColumnMap != null) {
|
||||||
var tb3 = _common.GetTableByEntity(mp2.Type);
|
var tb3 = _common.GetTableByEntity(mp2.Type);
|
||||||
if (tb3 != null) {
|
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)
|
foreach (var tb3c in tb3.Columns.Values)
|
||||||
_selectColumnMap.Add(new SelectColumnInfo { Table = find3, Column = tb3c });
|
_selectColumnMap.Add(new SelectColumnInfo { Table = find3, Column = tb3c });
|
||||||
|
@ -283,8 +283,8 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
if (dicfield.ContainsKey(quoteName)) field.Append(" as").Append(index);
|
if (dicfield.ContainsKey(quoteName)) field.Append(" as").Append(index);
|
||||||
else dicfield.Add(quoteName, true);
|
else dicfield.Add(quoteName, true);
|
||||||
} else {
|
} else {
|
||||||
var tb2 = _tables.Where(a => a.Table.Type == prop.PropertyType && a.Alias.Contains(prop.Name)).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 => a.Table.Type == prop.PropertyType).FirstOrDefault();
|
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;
|
if (tb2 == null) continue;
|
||||||
foreach (var col2 in tb2.Table.Columns.Values) {
|
foreach (var col2 in tb2.Table.Columns.Values) {
|
||||||
if (index > 0) field.Append(", ");
|
if (index > 0) field.Append(", ");
|
||||||
|
@ -74,7 +74,7 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
var expt = _commonExpression.ExpressionWhereLambdaNoneForeignObject(null, cols, binaryExpression, null);
|
var expt = _commonExpression.ExpressionWhereLambdaNoneForeignObject(null, cols, binaryExpression, null);
|
||||||
if (cols.Any() == false) return this;
|
if (cols.Any() == false) return this;
|
||||||
foreach (var col in cols) {
|
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;
|
var replval = _orm.CodeFirst.GetDbInfo(col.Column.CsType.GenericTypeArguments.FirstOrDefault())?.defaultValue;
|
||||||
if (replval == null) continue;
|
if (replval == null) continue;
|
||||||
var replname = _commonUtils.QuoteSqlName(col.Column.Attribute.Name);
|
var replname = _commonUtils.QuoteSqlName(col.Column.Attribute.Name);
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using FreeSql.Internal.Model;
|
using FreeSql.DataAnnotations;
|
||||||
|
using FreeSql.Internal.Model;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -30,11 +32,45 @@ namespace FreeSql.Internal {
|
|||||||
_orm = orm;
|
_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) {
|
internal string WhereObject(TableInfo table, string aliasAndDot, object dywhere) {
|
||||||
if (dywhere == null) return "";
|
if (dywhere == null) return "";
|
||||||
var type = dywhere.GetType();
|
var type = dywhere.GetType();
|
||||||
var primarys = table.Columns.Values.Where(a => a.Attribute.IsPrimary).ToArray();
|
var primarys = table.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
|
||||||
if (primarys.Length == 1 && type == primarys.First().CsType) {
|
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)}";
|
return $"{aliasAndDot}{this.QuoteSqlName(primarys.First().Attribute.Name)} = {this.FormatSql("{0}", dywhere)}";
|
||||||
} else if (primarys.Length > 0 && type.FullName == table.Type.FullName) {
|
} else if (primarys.Length > 0 && type.FullName == table.Type.FullName) {
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
|
@ -30,7 +30,7 @@ namespace FreeSql.Internal {
|
|||||||
if (tbc.TryGetValue(entity, out var trytb)) return trytb;
|
if (tbc.TryGetValue(entity, out var trytb)) return trytb;
|
||||||
if (common.CodeFirst.GetDbInfo(entity) != null) return null;
|
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 = new TableInfo();
|
||||||
trytb.Type = entity;
|
trytb.Type = entity;
|
||||||
trytb.Properties = entity.GetProperties().ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase);
|
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) {
|
foreach (var p in trytb.Properties.Values) {
|
||||||
var tp = common.CodeFirst.GetDbInfo(p.PropertyType);
|
var tp = common.CodeFirst.GetDbInfo(p.PropertyType);
|
||||||
//if (tp == null) continue;
|
//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 (tp == null && colattr == null) {
|
||||||
if (common.CodeFirst.IsLazyLoading) {
|
if (common.CodeFirst.IsLazyLoading) {
|
||||||
var getIsVirtual = trytb.Type.GetMethod($"get_{p.Name}")?.IsVirtual;
|
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 (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
|
||||||
if (common.CodeFirst.IsSyncStructureToLower) colattr.Name = colattr.Name.ToLower();
|
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.IsNullable = false;
|
||||||
colattr.DbType += " NOT NULL";
|
colattr.DbType += " NOT NULL";
|
||||||
}
|
}
|
||||||
@ -89,10 +89,7 @@ namespace FreeSql.Internal {
|
|||||||
var consturctorType = p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType;
|
var consturctorType = p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType;
|
||||||
colattr.DbDefautValue = Activator.CreateInstance(consturctorType);
|
colattr.DbDefautValue = Activator.CreateInstance(consturctorType);
|
||||||
}
|
}
|
||||||
if (colattr.IsIdentity && new[] {
|
if (colattr.IsIdentity == true && (p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType)?.IsNumberType() == false)
|
||||||
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)
|
|
||||||
colattr.IsIdentity = false;
|
colattr.IsIdentity = false;
|
||||||
|
|
||||||
var col = new ColumnInfo {
|
var col = new ColumnInfo {
|
||||||
@ -104,9 +101,9 @@ namespace FreeSql.Internal {
|
|||||||
trytb.Columns.Add(colattr.Name, col);
|
trytb.Columns.Add(colattr.Name, col);
|
||||||
trytb.ColumnsByCs.Add(p.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) {
|
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)
|
foreach (var col in trytb.Primarys)
|
||||||
col.Attribute.IsPrimary = true;
|
col.Attribute.IsPrimary = true;
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ namespace FreeSql.Internal {
|
|||||||
if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
|
if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
|
||||||
if (common.CodeFirst.IsSyncStructureToLower) colattr.Name = colattr.Name.ToLower();
|
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.IsNullable = false;
|
||||||
colattr.DbType += " NOT NULL";
|
colattr.DbType += " NOT NULL";
|
||||||
}
|
}
|
||||||
@ -84,9 +84,9 @@ namespace FreeSql.Internal {
|
|||||||
trytb.Columns.Add(colattr.Name, col);
|
trytb.Columns.Add(colattr.Name, col);
|
||||||
trytb.ColumnsByCs.Add(p.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) {
|
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)
|
foreach(var col in trytb.Primarys)
|
||||||
col.Attribute.IsPrimary = true;
|
col.Attribute.IsPrimary = true;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using FreeSql.DatabaseModel;
|
using FreeSql.DataAnnotations;
|
||||||
|
using FreeSql.DatabaseModel;
|
||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
@ -126,7 +127,7 @@ namespace FreeSql.MySql {
|
|||||||
foreach (var tbcol in tb.Columns.Values) {
|
foreach (var tbcol in tb.Columns.Values) {
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
||||||
sb.Append(tbcol.Attribute.DbType);
|
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(",");
|
sb.Append(",");
|
||||||
}
|
}
|
||||||
if (tb.Primarys.Any() == false)
|
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) {
|
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));
|
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) {
|
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) ||
|
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
||||||
string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out 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 ||
|
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) {
|
foreach (var tbcol in tb.Columns.Values) {
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
||||||
sb.Append(tbcol.Attribute.DbType);
|
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(",");
|
sb.Append(",");
|
||||||
}
|
}
|
||||||
if (tb.Primarys.Any() == false)
|
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);
|
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
|
||||||
return affrows > 0;
|
return affrows > 0;
|
||||||
}
|
}
|
||||||
|
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -28,7 +28,7 @@ namespace FreeSql.Oracle.Curd {
|
|||||||
sbtb.Append(_commonUtils.QuoteSqlName(_table.DbName)).Append("(");
|
sbtb.Append(_commonUtils.QuoteSqlName(_table.DbName)).Append("(");
|
||||||
var colidx = 0;
|
var colidx = 0;
|
||||||
foreach (var col in _table.Columns.Values) {
|
foreach (var col in _table.Columns.Values) {
|
||||||
if (col.Attribute.IsIdentity) {
|
if (col.Attribute.IsIdentity == true) {
|
||||||
_identCol = col;
|
_identCol = col;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using FreeSql.DatabaseModel;
|
using FreeSql.DataAnnotations;
|
||||||
|
using FreeSql.DatabaseModel;
|
||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
using Oracle.ManagedDataAccess.Client;
|
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(" (");
|
sb.Append("execute immediate 'CREATE TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" (");
|
||||||
foreach (var tbcol in tb.Columns.Values) {
|
foreach (var tbcol in tb.Columns.Values) {
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
|
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)
|
if (tb.Primarys.Any() == false)
|
||||||
sb.Remove(sb.Length - 1, 1);
|
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 != tbstructcol.is_nullable) {
|
||||||
if (tbcol.Attribute.IsNullable == false)
|
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 '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)
|
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)
|
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");
|
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 '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");
|
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) {
|
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(" (");
|
sb.Append("execute immediate 'CREATE TABLE ").Append(tmptablename).Append(" (");
|
||||||
foreach (var tbcol in tb.Columns.Values) {
|
foreach (var tbcol in tb.Columns.Values) {
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
|
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)
|
if (tb.Primarys.Any() == false)
|
||||||
sb.Remove(sb.Length - 1, 1);
|
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);
|
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
|
||||||
return affrows > 0;
|
return affrows > 0;
|
||||||
}
|
}
|
||||||
|
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,7 +16,7 @@ namespace FreeSql.PostgreSQL.Curd {
|
|||||||
var sql = this.ToSql();
|
var sql = this.ToSql();
|
||||||
if (string.IsNullOrEmpty(sql)) return 0;
|
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) {
|
if (identCols.Any() == false) {
|
||||||
_orm.Ado.ExecuteNonQuery(CommandType.Text, sql, _params);
|
_orm.Ado.ExecuteNonQuery(CommandType.Text, sql, _params);
|
||||||
return 0;
|
return 0;
|
||||||
@ -27,7 +27,7 @@ namespace FreeSql.PostgreSQL.Curd {
|
|||||||
var sql = this.ToSql();
|
var sql = this.ToSql();
|
||||||
if (string.IsNullOrEmpty(sql)) return 0;
|
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) {
|
if (identCols.Any() == false) {
|
||||||
await _orm.Ado.ExecuteNonQueryAsync(CommandType.Text, sql, _params);
|
await _orm.Ado.ExecuteNonQueryAsync(CommandType.Text, sql, _params);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using FreeSql.DatabaseModel;
|
using FreeSql.DataAnnotations;
|
||||||
|
using FreeSql.DatabaseModel;
|
||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
using Newtonsoft.Json.Linq;
|
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(" (");
|
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" (");
|
||||||
foreach (var tbcol in tb.Columns.Values) {
|
foreach (var tbcol in tb.Columns.Values) {
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
|
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)
|
if (tb.Primarys.Any() == false)
|
||||||
sb.Remove(sb.Length - 1, 1);
|
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))
|
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");
|
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)
|
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)
|
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)
|
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");
|
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("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));
|
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.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) {
|
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(" (");
|
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(tmptablename).Append(" (");
|
||||||
foreach (var tbcol in tb.Columns.Values) {
|
foreach (var tbcol in tb.Columns.Values) {
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
|
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)
|
if (tb.Primarys.Any() == false)
|
||||||
sb.Remove(sb.Length - 1, 1);
|
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);
|
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
|
||||||
return affrows > 0;
|
return affrows > 0;
|
||||||
}
|
}
|
||||||
|
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,6 +8,7 @@ using System.Data;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
|
||||||
namespace FreeSql.SqlServer {
|
namespace FreeSql.SqlServer {
|
||||||
|
|
||||||
@ -123,8 +124,8 @@ namespace FreeSql.SqlServer {
|
|||||||
foreach (var tbcol in tb.Columns.Values) {
|
foreach (var tbcol in tb.Columns.Values) {
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
||||||
sb.Append(tbcol.Attribute.DbType);
|
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.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
|
||||||
if (tbcol.Attribute.IsPrimary) sb.Append(" primary key");
|
if (tbcol.Attribute.IsPrimary == true) sb.Append(" primary key");
|
||||||
sb.Append(",");
|
sb.Append(",");
|
||||||
}
|
}
|
||||||
sb.Remove(sb.Length - 1, 1).Append("\r\n);\r\n");
|
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);
|
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) {
|
if (tbcol.Attribute.IsNullable == false) {
|
||||||
var addcoldbdefault = tbcol.Attribute.DbDefautValue;
|
var addcoldbdefault = tbcol.Attribute.DbDefautValue;
|
||||||
if (addcoldbdefault != null) sbalter.Append(_commonUtils.FormatSql(" default({0})", addcoldbdefault));
|
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) {
|
foreach (var tbcol in tb.Columns.Values) {
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
||||||
sb.Append(tbcol.Attribute.DbType);
|
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.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
|
||||||
if (tbcol.Attribute.IsPrimary) sb.Append(" primary key");
|
if (tbcol.Attribute.IsPrimary == true) sb.Append(" primary key");
|
||||||
sb.Append(",");
|
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.Remove(sb.Length - 1, 1).Append("\r\n);\r\n");
|
||||||
sb.Append("ALTER TABLE ").Append(tmptablename).Append(" SET (LOCK_ESCALATION = TABLE);\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);
|
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
|
||||||
return affrows > 0;
|
return affrows > 0;
|
||||||
}
|
}
|
||||||
|
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using FreeSql.DatabaseModel;
|
using FreeSql.DataAnnotations;
|
||||||
|
using FreeSql.DatabaseModel;
|
||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
using System;
|
using System;
|
||||||
@ -100,7 +101,7 @@ namespace FreeSql.Sqlite {
|
|||||||
foreach (var tbcol in tb.Columns.Values) {
|
foreach (var tbcol in tb.Columns.Values) {
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
||||||
sb.Append(tbcol.Attribute.DbType);
|
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;
|
isIndent = true;
|
||||||
sb.Append(" PRIMARY KEY AUTOINCREMENT");
|
sb.Append(" PRIMARY KEY AUTOINCREMENT");
|
||||||
}
|
}
|
||||||
@ -180,7 +181,7 @@ namespace FreeSql.Sqlite {
|
|||||||
foreach (var tbcol in tb.Columns.Values) {
|
foreach (var tbcol in tb.Columns.Values) {
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
||||||
sb.Append(tbcol.Attribute.DbType);
|
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;
|
isIndent = true;
|
||||||
sb.Append(" PRIMARY KEY AUTOINCREMENT");
|
sb.Append(" PRIMARY KEY AUTOINCREMENT");
|
||||||
}
|
}
|
||||||
@ -235,6 +236,6 @@ namespace FreeSql.Sqlite {
|
|||||||
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
|
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
|
||||||
return affrows > 0;
|
return affrows > 0;
|
||||||
}
|
}
|
||||||
|
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user