|
| 1 | +using System; |
| 2 | +using System.Data; |
| 3 | +using System.Data.SqlClient; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Config.SqlStreamStore; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using Microsoft.Extensions.Configuration.Ini; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Microsoft.Extensions.Primitives; |
| 11 | +using SqlStreamStore; |
| 12 | + |
| 13 | +namespace Config.SqlstreamStore.Example |
| 14 | +{ |
| 15 | + class Program |
| 16 | + { |
| 17 | + private static ILogger _logger; |
| 18 | + static void Main(string[] args) |
| 19 | + { |
| 20 | + |
| 21 | + var cts = new CancellationTokenSource(); |
| 22 | + |
| 23 | + // We're first building configuraiton from the ini file |
| 24 | + // This contains the connection string |
| 25 | + var ini = new ConfigurationBuilder() |
| 26 | + .AddIniFile("Settings.ini") |
| 27 | + .Build(); |
| 28 | + |
| 29 | + // On a background thread, create the database and start initializing |
| 30 | + Task.Run(() => CreateDatabase(ini["ConnectionString"], cts.Token), cts.Token); |
| 31 | + |
| 32 | + IStreamStore store = null; |
| 33 | + |
| 34 | + var sssConfig = new ConfigurationBuilder() |
| 35 | + .AddStreamStore( |
| 36 | + subscribeToChanges: true, |
| 37 | + factory: config => |
| 38 | + store = new MsSqlStreamStore(new MsSqlStreamStoreSettings(config["connectionString"])) |
| 39 | + ) |
| 40 | + .AddConfiguration(ini) |
| 41 | + .Build(); |
| 42 | + |
| 43 | + ChangeToken.OnChange(sssConfig.GetReloadToken, () => |
| 44 | + { |
| 45 | + Console.WriteLine("Settings changed:"); |
| 46 | + PrintSettings(sssConfig); |
| 47 | + }); |
| 48 | + |
| 49 | + Console.WriteLine("Found settings at startup:"); |
| 50 | + PrintSettings(sssConfig); |
| 51 | + |
| 52 | + Console.ReadLine(); |
| 53 | + cts.Cancel(); |
| 54 | + store.Dispose(); |
| 55 | + } |
| 56 | + |
| 57 | + private static void PrintSettings(IConfigurationRoot sssConfig) |
| 58 | + { |
| 59 | + foreach (var setting in sssConfig.GetChildren()) |
| 60 | + { |
| 61 | + Console.WriteLine(setting.Key + "=" + setting.Value); |
| 62 | + } |
| 63 | + Console.WriteLine(); |
| 64 | + } |
| 65 | + |
| 66 | + private static async Task CreateDatabase(string connectionString, CancellationToken ct) |
| 67 | + { |
| 68 | + try |
| 69 | + { |
| 70 | + await Task.Delay(1000); |
| 71 | + Console.WriteLine("Creating database schema"); |
| 72 | + await Task.Delay(3000); |
| 73 | + |
| 74 | + var mgr = new DatabaseManager(connectionString); |
| 75 | + mgr.EnsureDatabaseExists(10, 10); |
| 76 | + |
| 77 | + var msSqlStreamStoreSettings = new MsSqlStreamStoreSettings(connectionString); |
| 78 | + |
| 79 | + var store = new MsSqlStreamStore(msSqlStreamStoreSettings); |
| 80 | + if (!(await store.CheckSchema(ct)).IsMatch()) |
| 81 | + { |
| 82 | + await store.CreateSchema(ct); |
| 83 | + } |
| 84 | + |
| 85 | + var repo = new ConfigRepository(store); |
| 86 | + |
| 87 | + while (!ct.IsCancellationRequested) |
| 88 | + { |
| 89 | + await Task.Delay(1000, ct); |
| 90 | + await repo.Modify(ct, ("setting1", DateTime.Now.ToLongTimeString())); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + catch (Exception ex) |
| 95 | + { |
| 96 | + Console.WriteLine("error while creating database: " + ex.Message); |
| 97 | + throw; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public class DatabaseManager |
| 103 | + { |
| 104 | + |
| 105 | + public DatabaseManager(string connectionString) |
| 106 | + { |
| 107 | + _connectionStringBuilder = new SqlConnectionStringBuilder(connectionString); |
| 108 | + } |
| 109 | + |
| 110 | + private readonly SqlConnectionStringBuilder _connectionStringBuilder; |
| 111 | + |
| 112 | + private string DatabaseName |
| 113 | + { |
| 114 | + get |
| 115 | + { |
| 116 | + var databaseName = _connectionStringBuilder.InitialCatalog; |
| 117 | + return databaseName; |
| 118 | + } |
| 119 | + } |
| 120 | + public void DropDatabase() |
| 121 | + { |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + public void EnsureDatabaseExists(int size, int fileGrowth) |
| 126 | + { |
| 127 | + var masterConnectionString = GetMasterConnectionString(); |
| 128 | + |
| 129 | + using (var connection = new SqlConnection(masterConnectionString.ConnectionString)) |
| 130 | + { |
| 131 | + connection.Open(); |
| 132 | + |
| 133 | + bool exists; |
| 134 | + var dbExists = $"SELECT count(*) FROM master.dbo.sysdatabases where name ='{DatabaseName}'"; |
| 135 | + using (var command = new SqlCommand(dbExists, connection)) |
| 136 | + { |
| 137 | + exists = (int)command.ExecuteScalar() > 0; |
| 138 | + } |
| 139 | + |
| 140 | + if (exists) |
| 141 | + { |
| 142 | + Console.WriteLine($"Database instance {DatabaseName} already exists."); |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + string databasePath; |
| 147 | + using (var command = new SqlCommand(@" |
| 148 | + SELECT SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1) |
| 149 | + FROM master.sys.master_files |
| 150 | + WHERE database_id = 1 AND file_id = 1", |
| 151 | + connection)) |
| 152 | + { |
| 153 | + databasePath = command.ExecuteScalar() as string; |
| 154 | + } |
| 155 | + |
| 156 | + //_writeOutput($"Database instance {databaseName} does not exist. Creating..."); |
| 157 | + var sql = $@"CREATE DATABASE [{DatabaseName}] |
| 158 | + ON PRIMARY ( |
| 159 | + NAME = N'{DatabaseName}', |
| 160 | + FILENAME = N'{databasePath}{DatabaseName}.mdf', |
| 161 | + SIZE = {size}MB, |
| 162 | + FILEGROWTH = {fileGrowth}MB |
| 163 | + ) |
| 164 | + LOG ON ( |
| 165 | + NAME = N'{DatabaseName}_log', |
| 166 | + FILENAME = N'{databasePath}{DatabaseName}_log.ldf' |
| 167 | + )"; |
| 168 | + |
| 169 | + using (var command = new SqlCommand(sql, connection)) |
| 170 | + { |
| 171 | + command.ExecuteNonQuery(); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private SqlConnectionStringBuilder GetMasterConnectionString() |
| 178 | + { |
| 179 | + var masterConnectionString = new SqlConnectionStringBuilder(_connectionStringBuilder.ConnectionString); |
| 180 | + masterConnectionString.InitialCatalog = "master"; |
| 181 | + return masterConnectionString; |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | +} |
0 commit comments