-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStorageContextTableManagement.cs
More file actions
119 lines (94 loc) · 3.87 KB
/
StorageContextTableManagement.cs
File metadata and controls
119 lines (94 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using CoreHelpers.WindowsAzure.Storage.Table.Extensions;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Azure.Data.Tables;
using System.Collections.Generic;
using System.Linq;
namespace CoreHelpers.WindowsAzure.Storage.Table
{
public partial class StorageContext : IStorageContext
{
private bool _autoCreateTable { get; set; } = false;
private string _tableNamePrefix;
public bool IsAutoCreateTableEnabled()
=> _autoCreateTable;
public IStorageContext EnableAutoCreateTable()
{
_autoCreateTable = true;
return this;
}
public void SetTableNamePrefix(string tableNamePrefix)
{
_tableNamePrefix = tableNamePrefix;
}
public string GetTableNamePrefix()
=> _tableNamePrefix;
public void OverrideTableName<T>(string table) where T : class, new()
{
OverrideTableName(typeof(T), table);
}
public void OverrideTableName(Type entityType, string tableName)
{
if (_entityMapperRegistry.ContainsKey(entityType))
{
// copy the mapper entry becasue it could be referenced
// from parent context
var duplicatedMapper = new StorageEntityMapper(_entityMapperRegistry[entityType]);
// override the table name
duplicatedMapper.TableName = tableName;
// re-register
_entityMapperRegistry[entityType] = duplicatedMapper;
}
}
public async Task<bool> ExistsTableAsync<T>()
{
var tc = GetTableClient(GetTableName(typeof(T)));
return await tc.ExistsAsync();
}
public async Task CreateTableAsync(Type entityType, bool ignoreErrorIfExists = true)
{
var tc = GetTableClient(GetTableName(entityType));
if (ignoreErrorIfExists)
await tc.CreateIfNotExistsAsync();
else
await tc.CreateAsync();
}
public Task CreateTableAsync<T>(bool ignoreErrorIfExists = true)
=> CreateTableAsync(typeof(T), ignoreErrorIfExists);
public void CreateTable<T>(bool ignoreErrorIfExists = true)
=> this.CreateTableAsync<T>(ignoreErrorIfExists).GetAwaiter().GetResult();
public async Task DropTableAsync(Type entityType, bool ignoreErrorIfNotExists = true)
{
var tc = GetTableClient(GetTableName(entityType));
if (ignoreErrorIfNotExists)
await tc.DeleteIfExistsAsync();
else
await tc.DeleteAsync();
}
public async Task DropTableAsync<T>(bool ignoreErrorIfNotExists = true)
=> await DropTableAsync(typeof(T), ignoreErrorIfNotExists);
public void DropTable<T>(bool ignoreErrorIfNotExists = true)
=> Task.Run(async () => await DropTableAsync(typeof(T), ignoreErrorIfNotExists)).Wait();
private string GetTableName<T>()
=> GetTableName(typeof(T));
private string GetTableName(Type entityType)
=> GetTableName(_entityMapperRegistry[entityType].TableName);
private string GetTableName(string tableName)
{
// get the table name
if (String.IsNullOrEmpty(_tableNamePrefix))
return tableName;
else
return Regex.Replace($"{_tableNamePrefix}{tableName}", "[^A-Za-z0-9]", "");
}
public async Task<List<string>> QueryTableList()
{
var tables = new List<string>();
var tablePages = tableServiceClient.QueryAsync().AsPages();
await foreach (var tablePage in tablePages)
tables.AddRange(tablePage.Values.Select(t => t.Name));
return tables;
}
}
}