public TableDefinition StorageTable() { var pgIdType = TypeMappings.GetPgType(_mapping.IdMember.GetMemberType()); var table = new TableDefinition(_mapping.Table, new TableColumn("id", pgIdType)); table.Columns.Add(new TableColumn("data", "jsonb") { Directive = "NOT NULL" }); table.Columns.Add(new TableColumn(DocumentMapping.LastModifiedColumn, "timestamp with time zone") { Directive = "DEFAULT transaction_timestamp()" }); table.Columns.Add(new TableColumn(DocumentMapping.VersionColumn, "uuid") { Directive = "NOT NULL" }); table.Columns.Add(new TableColumn(DocumentMapping.DotNetTypeColumn, "varchar")); _mapping.DuplicatedFields.Select(x => x.ToColumn()).Each(x => table.Columns.Add(x)); if (_mapping.IsHierarchy()) { table.Columns.Add(new TableColumn(DocumentMapping.DocumentTypeColumn, "varchar")); } return(table); }
public UpsertFunction(DocumentMapping mapping) { if (mapping == null) { throw new ArgumentNullException(nameof(mapping)); } _functionName = mapping.UpsertFunction; _tableName = mapping.Table; _primaryKeyConstraintName = "pk_" + mapping.Table.Name; var idType = mapping.IdMember.GetMemberType(); var pgIdType = TypeMappings.GetPgType(idType); Arguments.Add(new UpsertArgument { Arg = "docId", PostgresType = pgIdType, Column = "id", Members = new[] { mapping.IdMember } }); Arguments.Add(new DocJsonBodyArgument()); Arguments.AddRange(mapping.DuplicatedFields.Select(x => x.UpsertArgument)); Arguments.Add(new VersionArgument()); Arguments.Add(new DotNetTypeArgument()); if (mapping.IsHierarchy()) { Arguments.Add(new DocTypeArgument()); } }
public TableDefinition StorageTable() { var pgIdType = TypeMappings.GetPgType(_mapping.IdMember.GetMemberType()); var table = new TableDefinition(_mapping.Table, new TableColumn("id", pgIdType)); table.Columns.Add(new TableColumn("data", "jsonb") { Directive = "NOT NULL" }); table.Columns.Add(new TableColumn(DocumentMapping.LastModifiedColumn, "timestamp with time zone") { Directive = "DEFAULT transaction_timestamp()" }); table.Columns.Add(new TableColumn(DocumentMapping.VersionColumn, "uuid") { Directive = "NOT NULL default(md5(random()::text || clock_timestamp()::text)::uuid)" }); table.Columns.Add(new TableColumn(DocumentMapping.DotNetTypeColumn, "varchar")); _mapping.DuplicatedFields.Select(x => x.ToColumn()).Each(x => table.Columns.Add(x)); if (_mapping.IsHierarchy()) { table.Columns.Add(new TableColumn(DocumentMapping.DocumentTypeColumn, "varchar") { Directive = $"DEFAULT '{_mapping.AliasFor(_mapping.DocumentType)}'" }); } if (_mapping.DeleteStyle == DeleteStyle.SoftDelete) { table.Columns.Add(new TableColumn(DocumentMapping.DeletedColumn, "boolean") { Directive = "DEFAULT FALSE" }); table.Columns.Add(new TableColumn(DocumentMapping.DeletedAtColumn, "timestamp with time zone") { Directive = "NULL" }); } return(table); }
public UpsertFunction(DocumentMapping mapping) { if (mapping == null) throw new ArgumentNullException(nameof(mapping)); _functionName = mapping.UpsertFunction; _tableName = mapping.Table; _primaryKeyConstraintName = "pk_" + mapping.Table.Name; var idType = mapping.IdMember.GetMemberType(); var pgIdType = TypeMappings.GetPgType(idType); Arguments.Add(new UpsertArgument { Arg = "docId", PostgresType = pgIdType, Column = "id", Members = new[] {mapping.IdMember} }); Arguments.Add(new DocJsonBodyArgument()); Arguments.AddRange(mapping.DuplicatedFields.Select(x => x.UpsertArgument)); Arguments.Add(new VersionArgument()); Arguments.Add(new DotNetTypeArgument()); if (mapping.IsHierarchy()) { Arguments.Add(new DocTypeArgument()); } if (mapping.UseOptimisticConcurrency) { Arguments.Add(new CurrentVersionArgument()); } }
public static void GenerateDocumentStorage(DocumentMapping mapping, SourceWriter writer) { var upsertFunction = mapping.ToUpsertFunction(); var id_NpgsqlDbType = TypeMappings.ToDbType(mapping.IdMember.GetMemberType()); var typeName = mapping.DocumentType.GetTypeName(); var storeName = _storenameSanitizer.Replace(mapping.DocumentType.GetPrettyName(), string.Empty); var storageArguments = mapping.ToArguments().ToArray(); var ctorArgs = storageArguments.Select(x => x.ToCtorArgument()).Join(", "); var ctorLines = storageArguments.Select(x => x.ToCtorLine()).Join("\n"); var fields = storageArguments.Select(x => x.ToFieldDeclaration()).Join("\n"); var baseType = mapping.IsHierarchy() ? "HierarchicalResolver" : "Resolver"; var callBaseCtor = mapping.IsHierarchy() ? $": base({HierarchyArgument.Hierarchy})" : string.Empty; writer.Write( $@" BLOCK:public class {storeName}Storage : {baseType}<{typeName}>, IDocumentStorage, IBulkLoader<{typeName}>, IdAssignment<{typeName}>, IResolver<{typeName}> {fields} BLOCK:public {storeName}Storage({ctorArgs}) {callBaseCtor} {ctorLines} END public Type DocumentType => typeof ({typeName}); BLOCK:public NpgsqlCommand UpsertCommand(object document, string json) return UpsertCommand(({typeName})document, json); END BLOCK:public NpgsqlCommand LoaderCommand(object id) return new NpgsqlCommand(`select {mapping.SelectFields().Join(", ")} from {mapping.QualifiedTableName} as d where id = :id`).With(`id`, id); END BLOCK:public NpgsqlCommand DeleteCommandForId(object id) return new NpgsqlCommand(`delete from {mapping.QualifiedTableName} where id = :id`).With(`id`, id); END BLOCK:public NpgsqlCommand DeleteCommandForEntity(object entity) return DeleteCommandForId((({typeName})entity).{mapping.IdMember.Name}); END BLOCK:public NpgsqlCommand LoadByArrayCommand<T>(T[] ids) return new NpgsqlCommand(`select {mapping.SelectFields().Join(", ")} from {mapping.QualifiedTableName} as d where id = ANY(:ids)`).With(`ids`, ids); END BLOCK:public void Remove(IIdentityMap map, object entity) var id = Identity(entity); map.Remove<{typeName}>(id); END BLOCK:public void Delete(IIdentityMap map, object id) map.Remove<{typeName}>(id); END BLOCK:public void Store(IIdentityMap map, object id, object entity) map.Store<{typeName}>(id, ({typeName})entity); END BLOCK:public object Assign({typeName} document, out bool assigned) {mapping.IdStrategy.AssignmentBodyCode(mapping.IdMember)} return document.{mapping.IdMember.Name}; END BLOCK:public object Retrieve({typeName} document) return document.{mapping.IdMember.Name}; END public NpgsqlDbType IdType => NpgsqlDbType.{id_NpgsqlDbType}; BLOCK:public object Identity(object document) return (({typeName})document).{mapping.IdMember.Name}; END {upsertFunction.ToUpdateBatchMethod(typeName)} {upsertFunction.ToBulkInsertMethod(typeName)} END "); }