Skip to content

Commit 2895528

Browse files
committed
Merge branch 'pgsql'
# Conflicts: # Signum.Engine/Engine/SchemaGenerator.cs # Signum.Engine/Engine/SchemaSynchronizer.cs # Signum.Engine/Engine/SqlBuilder.cs # Signum.Engine/Linq/ExpressionVisitor/QueryFormatter.cs # Signum.React/Signum.React.csproj
2 parents 9cd36ef + f78f5f7 commit 2895528

File tree

93 files changed

+5799
-1823
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+5799
-1823
lines changed

Signum.Engine/Administrator.cs

Lines changed: 67 additions & 48 deletions
Large diffs are not rendered by default.

Signum.Engine/Basics/TypeLogic.cs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,37 +44,35 @@ public static void Start(SchemaBuilder sb)
4444
{
4545
if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
4646
{
47-
Schema current = Schema.Current;
47+
Schema schema = Schema.Current;
4848

49-
current.SchemaCompleted += () =>
49+
sb.Include<TypeEntity>()
50+
.WithQuery(() => t => new
51+
{
52+
Entity = t,
53+
t.Id,
54+
t.TableName,
55+
t.CleanName,
56+
t.ClassName,
57+
t.Namespace,
58+
});
59+
60+
schema.SchemaCompleted += () =>
5061
{
51-
var attributes = current.Tables.Keys.Select(t => KeyValuePair.Create(t, t.GetCustomAttribute<EntityKindAttribute>(true))).ToList();
62+
var attributes = schema.Tables.Keys.Select(t => KeyValuePair.Create(t, t.GetCustomAttribute<EntityKindAttribute>(true))).ToList();
5263

5364
var errors = attributes.Where(a => a.Value == null).ToString(a => "Type {0} does not have an EntityTypeAttribute".FormatWith(a.Key.Name), "\r\n");
5465

5566
if (errors.HasText())
5667
throw new InvalidOperationException(errors);
5768
};
5869

59-
current.Initializing += () =>
70+
schema.Initializing += () =>
6071
{
61-
current.typeCachesLazy.Load();
72+
schema.typeCachesLazy.Load();
6273
};
6374

64-
current.typeCachesLazy = sb.GlobalLazy(() => new TypeCaches(current),
65-
new InvalidateWith(typeof(TypeEntity)),
66-
Schema.Current.InvalidateMetadata);
67-
68-
sb.Include<TypeEntity>()
69-
.WithQuery(() => t => new
70-
{
71-
Entity = t,
72-
t.Id,
73-
t.TableName,
74-
t.CleanName,
75-
t.ClassName,
76-
t.Namespace,
77-
});
75+
schema.typeCachesLazy = sb.GlobalLazy(() => new TypeCaches(schema), new InvalidateWith(typeof(TypeEntity)), Schema.Current.InvalidateMetadata);
7876

7977
TypeEntity.SetTypeEntityCallbacks(
8078
t => TypeToEntity.GetOrThrow(t),
@@ -92,15 +90,16 @@ join t in Schema.Current.Tables.Keys on dn.FullClassName equals (EnumEntity.Extr
9290
public static SqlPreCommand? Schema_Synchronizing(Replacements replacements)
9391
{
9492
var schema = Schema.Current;
93+
var isPostgres = schema.Settings.IsPostgres;
9594

9695
Dictionary<string, TypeEntity> should = GenerateSchemaTypes().ToDictionaryEx(s => s.TableName, "tableName in memory");
9796

9897
var currentList = Administrator.TryRetrieveAll<TypeEntity>(replacements);
9998

10099
{ //External entities are nt asked in SchemaSynchronizer
101100
replacements.AskForReplacements(
102-
currentList.Where(t => schema.IsExternalDatabase(ObjectName.Parse(t.TableName).Schema.Database)).Select(a => a.TableName).ToHashSet(),
103-
should.Values.Where(t => schema.IsExternalDatabase(ObjectName.Parse(t.TableName).Schema.Database)).Select(a => a.TableName).ToHashSet(),
101+
currentList.Where(t => schema.IsExternalDatabase(ObjectName.Parse(t.TableName, isPostgres).Schema.Database)).Select(a => a.TableName).ToHashSet(),
102+
should.Values.Where(t => schema.IsExternalDatabase(ObjectName.Parse(t.TableName, isPostgres).Schema.Database)).Select(a => a.TableName).ToHashSet(),
104103
Replacements.KeyTables);
105104
}
106105

@@ -109,12 +108,12 @@ join t in Schema.Current.Tables.Keys on dn.FullClassName equals (EnumEntity.Extr
109108

110109
{ //Temporal solution until applications are updated
111110
var repeated =
112-
should.Keys.Select(k => ObjectName.Parse(k)).GroupBy(a => a.Name).Where(a => a.Count() > 1).Select(a => a.Key).Concat(
113-
current.Keys.Select(k => ObjectName.Parse(k)).GroupBy(a => a.Name).Where(a => a.Count() > 1).Select(a => a.Key)).ToList();
111+
should.Keys.Select(k => ObjectName.Parse(k, isPostgres)).GroupBy(a => a.Name).Where(a => a.Count() > 1).Select(a => a.Key).Concat(
112+
current.Keys.Select(k => ObjectName.Parse(k, isPostgres)).GroupBy(a => a.Name).Where(a => a.Count() > 1).Select(a => a.Key)).ToList();
114113

115114
Func<string, string> simplify = tn =>
116115
{
117-
ObjectName name = ObjectName.Parse(tn);
116+
ObjectName name = ObjectName.Parse(tn, isPostgres);
118117
return repeated.Contains(name.Name) ? name.ToString() : name.Name;
119118
};
120119

@@ -138,8 +137,8 @@ join t in Schema.Current.Tables.Keys on dn.FullClassName equals (EnumEntity.Extr
138137

139138
if (c.TableName != s.TableName)
140139
{
141-
var pc = ObjectName.Parse(c.TableName);
142-
var ps = ObjectName.Parse(s.TableName);
140+
var pc = ObjectName.Parse(c.TableName, isPostgres);
141+
var ps = ObjectName.Parse(s.TableName, isPostgres);
143142

144143
if (!EqualsIgnoringDatabasePrefix(pc, ps))
145144
{

Signum.Engine/BulkInserter.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public static int BulkInsert<T>(this IEnumerable<T> entities,
2828
using (HeavyProfiler.Log(nameof(BulkInsert), () => typeof(T).TypeName()))
2929
using (Transaction tr = new Transaction())
3030
{
31-
3231
var table = Schema.Current.Table(typeof(T));
3332

3433
if (!disableIdentity && table.IdentityBehaviour && table.TablesMList().Any())
@@ -155,27 +154,28 @@ public static int BulkInsertTable<T>(IEnumerable<T> entities,
155154

156155
var t = Schema.Current.Table<T>();
157156
bool disableIdentityBehaviour = copyOptions.HasFlag(SqlBulkCopyOptions.KeepIdentity);
158-
bool oldIdentityBehaviour = t.IdentityBehaviour;
159157

160158
DataTable dt = new DataTable();
161-
foreach (var c in t.Columns.Values.Where(c => !(c is SystemVersionedInfo.Column) && (disableIdentityBehaviour || !c.IdentityBehaviour)))
159+
var columns = t.Columns.Values.Where(c => !(c is SystemVersionedInfo.SqlServerPeriodColumn) && (disableIdentityBehaviour || !c.IdentityBehaviour)).ToList();
160+
foreach (var c in columns)
162161
dt.Columns.Add(new DataColumn(c.Name, c.Type.UnNullify()));
163162

164-
if (disableIdentityBehaviour) t.IdentityBehaviour = false;
165-
foreach (var e in list)
163+
using (disableIdentityBehaviour ? Administrator.DisableIdentity(t, behaviourOnly: true) : null)
166164
{
167-
if (!e.IsNew)
168-
throw new InvalidOperationException("Entites should be new");
169-
t.SetToStrField(e);
170-
dt.Rows.Add(t.BulkInsertDataRow(e));
165+
foreach (var e in list)
166+
{
167+
if (!e.IsNew)
168+
throw new InvalidOperationException("Entites should be new");
169+
t.SetToStrField(e);
170+
dt.Rows.Add(t.BulkInsertDataRow(e));
171+
}
171172
}
172-
if (disableIdentityBehaviour) t.IdentityBehaviour = oldIdentityBehaviour;
173173

174174
using (Transaction tr = new Transaction())
175175
{
176176
Schema.Current.OnPreBulkInsert(typeof(T), inMListTable: false);
177177

178-
Executor.BulkCopy(dt, t.Name, copyOptions, timeout);
178+
Executor.BulkCopy(dt, columns, t.Name, copyOptions, timeout);
179179

180180
foreach (var item in list)
181181
item.SetNotModified();
@@ -272,8 +272,8 @@ public static int BulkInsertMListTable<E, V>(
272272
var maxRowId = updateParentTicks.Value ? Database.MListQuery(mListProperty).Max(a => (PrimaryKey?)a.RowId) : null;
273273

274274
DataTable dt = new DataTable();
275-
276-
foreach (var c in mlistTable.Columns.Values.Where(c => !(c is SystemVersionedInfo.Column) && !c.IdentityBehaviour))
275+
var columns = mlistTable.Columns.Values.Where(c => !(c is SystemVersionedInfo.SqlServerPeriodColumn) && !c.IdentityBehaviour).ToList();
276+
foreach (var c in columns)
277277
dt.Columns.Add(new DataColumn(c.Name, c.Type.UnNullify()));
278278

279279
var list = mlistElements.ToList();
@@ -287,7 +287,7 @@ public static int BulkInsertMListTable<E, V>(
287287
{
288288
Schema.Current.OnPreBulkInsert(typeof(E), inMListTable: true);
289289

290-
Executor.BulkCopy(dt, mlistTable.Name, copyOptions, timeout);
290+
Executor.BulkCopy(dt, columns, mlistTable.Name, copyOptions, timeout);
291291

292292
var result = list.Count;
293293

@@ -327,8 +327,9 @@ public static int BulkInsertView<T>(this IEnumerable<T> entities,
327327

328328
bool disableIdentityBehaviour = copyOptions.HasFlag(SqlBulkCopyOptions.KeepIdentity);
329329

330+
var columns = t.Columns.Values.ToList();
330331
DataTable dt = new DataTable();
331-
foreach (var c in t.Columns.Values)
332+
foreach (var c in columns)
332333
dt.Columns.Add(new DataColumn(c.Name, c.Type.UnNullify()));
333334

334335
foreach (var e in entities)
@@ -340,7 +341,7 @@ public static int BulkInsertView<T>(this IEnumerable<T> entities,
340341
{
341342
Schema.Current.OnPreBulkInsert(typeof(T), inMListTable: false);
342343

343-
Executor.BulkCopy(dt, t.Name, copyOptions, timeout);
344+
Executor.BulkCopy(dt, columns, t.Name, copyOptions, timeout);
344345

345346
return tr.Commit(list.Count);
346347
}

Signum.Engine/CodeGeneration/EntityCodeGenerator.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Text;
77
using System.Text.RegularExpressions;
8+
using Signum.Engine.Engine;
89
using Signum.Engine.Maps;
910
using Signum.Entities;
1011
using Signum.Entities.Reflection;
@@ -26,7 +27,7 @@ public class EntityCodeGenerator
2627

2728
public virtual void GenerateEntitiesFromDatabaseTables()
2829
{
29-
CurrentSchema = Schema.Current;
30+
CurrentSchema = Schema.Current;
3031

3132
var tables = GetTables();
3233

@@ -70,7 +71,9 @@ protected virtual string GetProjectFolder()
7071

7172
protected virtual List<DiffTable> GetTables()
7273
{
73-
return SchemaSynchronizer.DefaultGetDatabaseDescription(Schema.Current.DatabaseNames()).Values.ToList();
74+
return Schema.Current.Settings.IsPostgres ?
75+
PostgresCatalogSchema.GetDatabaseDescription(Schema.Current.DatabaseNames()).Values.ToList() :
76+
SysTablesSchema.GetDatabaseDescription(Schema.Current.DatabaseNames()).Values.ToList();
7477
}
7578

7679
protected virtual void GetSolutionInfo(out string solutionFolder, out string solutionName)
@@ -538,7 +541,7 @@ protected virtual string GetTicksColumnAttribute(DiffTable table)
538541
{
539542
StringBuilder sb = new StringBuilder();
540543
sb.Append("TableName(\"" + objectName.Name + "\"");
541-
if (objectName.Schema != SchemaName.Default)
544+
if (objectName.Schema != SchemaName.Default(CurrentSchema.Settings.IsPostgres))
542545
sb.Append(", SchemaName = \"" + objectName.Schema.Name + "\"");
543546

544547
if (objectName.Schema.Database != null)
@@ -636,7 +639,7 @@ protected virtual IEnumerable<string> GetPropertyAttributes(DiffTable table, Dif
636639
parts.Add("Min = " + min);
637640

638641
if (col.Length != -1)
639-
parts.Add("Max = " + col.Length / DiffColumn.BytesPerChar(col.SqlDbType));
642+
parts.Add("Max = " + col.Length);
640643

641644
return "StringLengthValidator(" + parts.ToString(", ") + ")";
642645
}
@@ -706,7 +709,7 @@ protected virtual bool HasUniqueIndex(DiffTable table, DiffColumn col)
706709
ix.FilterDefinition == null &&
707710
ix.Columns.Only()?.Let(ic => ic.ColumnName == col.Name && ic.IsIncluded == false) == true &&
708711
ix.IsUnique &&
709-
ix.Type == DiffIndexType.NonClustered);
712+
ix.IsPrimary);
710713
}
711714

712715
protected virtual string DefaultColumnName(DiffTable table, DiffColumn col)
@@ -743,19 +746,19 @@ protected virtual List<string> GetSqlDbTypeParts(DiffColumn col, Type type)
743746
{
744747
List<string> parts = new List<string>();
745748
var pair = CurrentSchema.Settings.GetSqlDbTypePair(type);
746-
if (pair.SqlDbType != col.SqlDbType)
747-
parts.Add("SqlDbType = SqlDbType." + col.SqlDbType);
749+
if (pair.DbType.SqlServer != col.DbType.SqlServer)
750+
parts.Add("SqlDbType = SqlDbType." + col.DbType.SqlServer);
748751

749-
var defaultSize = CurrentSchema.Settings.GetSqlSize(null, null, pair.SqlDbType);
752+
var defaultSize = CurrentSchema.Settings.GetSqlSize(null, null, pair.DbType);
750753
if (defaultSize != null)
751754
{
752-
if (!(defaultSize == col.Precision || defaultSize == col.Length / DiffColumn.BytesPerChar(col.SqlDbType) || defaultSize == int.MaxValue && col.Length == -1))
755+
if (!(defaultSize == col.Precision || defaultSize == col.Length || defaultSize == int.MaxValue && col.Length == -1))
753756
parts.Add("Size = " + (col.Length == -1 ? "int.MaxValue" :
754-
col.Length != 0 ? (col.Length / DiffColumn.BytesPerChar(col.SqlDbType)).ToString() :
757+
col.Length != 0 ? col.Length.ToString() :
755758
col.Precision != 0 ? col.Precision.ToString() : "0"));
756759
}
757760

758-
var defaultScale = CurrentSchema.Settings.GetSqlScale(null, null, col.SqlDbType);
761+
var defaultScale = CurrentSchema.Settings.GetSqlScale(null, null, col.DbType);
759762
if (defaultScale != null)
760763
{
761764
if (!(col.Scale == defaultScale))
@@ -801,7 +804,7 @@ protected virtual bool IsLite(DiffTable table, DiffColumn col)
801804

802805
protected internal virtual Type GetValueType(DiffColumn col)
803806
{
804-
switch (col.SqlDbType)
807+
switch (col.DbType.SqlServer)
805808
{
806809
case SqlDbType.BigInt: return typeof(long);
807810
case SqlDbType.Binary: return typeof(byte[]);
@@ -834,7 +837,7 @@ protected internal virtual Type GetValueType(DiffColumn col)
834837
case SqlDbType.Udt: return Schema.Current.Settings.UdtSqlName
835838
.SingleOrDefaultEx(kvp => StringComparer.InvariantCultureIgnoreCase.Equals(kvp.Value, col.UserTypeName))
836839
.Key;
837-
default: throw new NotImplementedException("Unknown translation for " + col.SqlDbType);
840+
default: throw new NotImplementedException("Unknown translation for " + col.DbType.SqlServer);
838841
}
839842
}
840843

Signum.Engine/CodeGeneration/LogicCodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ protected virtual bool IsSimpleValueType(Type type)
426426
{
427427
var t = CurrentSchema.Settings.TryGetSqlDbTypePair(type.UnNullify());
428428

429-
return t != null && t.UserDefinedTypeName == null && t.SqlDbType != SqlDbType.Image && t.SqlDbType != SqlDbType.VarBinary;
429+
return t != null && t.UserDefinedTypeName == null && (t.DbType.IsNumber() || t.DbType.IsString() || t.DbType.IsDate());
430430
}
431431

432432
protected virtual string WriteOperations(Type type)

0 commit comments

Comments
 (0)