public RelationCommandCacheKey(SqlCommandType commandTypeType, Type type, string relationName, string schemaName, InsertReturns returns) { CommandTypeType = commandTypeType; Type = type; RelationName = relationName; SchemaName = schemaName; Returns = returns; }
private async Task GetOrCreateCommand(Type type, string relationName, string schemaName, InsertReturns returns) { var cacheEntry = new RelationCommandCacheKey(SqlCommandType.Insert, type, relationName, schemaName, returns); if (GlobalMemoryCache.ContainsKey(cacheEntry)) { await SetCommand(GlobalMemoryCache.Get <RelationCommandCacheKey, NpgsqlCommand>(cacheEntry)); return; } var resolver = Repository.DbCaseResolver; var primaryKeys = new List <string>(); var columnNames = new List <string>(); var propertyNames = ReflectionFactory.PropertiesInfo(type).Select(s => resolver(s.Name)).ToArray(); foreach (var columnEntity in Repository.Schema.GetColumns(relationName, schemaName)) { if (columnEntity.IsPrimaryKey) { primaryKeys.Add(columnEntity.ColumnName); } if (!columnEntity.IsGenerated && propertyNames.Contains(columnEntity.ColumnName)) { columnNames.Add(columnEntity.ColumnName); } } var into = string.Join(", ", columnNames.Select(s => Repository.Schema.Quote(s))); var values = string.Join(", ", columnNames.Select(s => $"@{s}")); var returning = returns == InsertReturns.Json ? $"json_build_object({string.Join(", ", primaryKeys.Select(s => $"'{s}', {s}"))})" : $"{string.Join(", ", primaryKeys)}"; await SetSql($"INSERT INTO {QuotedRelationName} ({into}) VALUES ({values}) RETURNING {returning};"); GlobalMemoryCache.AddOrUpdate(cacheEntry, Command); }