-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchoolDatabase.cs
More file actions
252 lines (215 loc) · 7.75 KB
/
Copy pathSchoolDatabase.cs
File metadata and controls
252 lines (215 loc) · 7.75 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace SaemDesk
{
/// <summary>
/// SchoolDatabase - School 데이터베이스 관리
/// 데이터베이스 초기화, 백업, 복원 등
/// Board.cs와 동일한 패턴
/// </summary>
public static class SchoolDatabase
{
/// <summary>
/// ⭐ 전체 DB 경로 (Public - 모든 곳에서 사용)
/// Data 폴더 자동 생성 및 전체 경로 반환
/// </summary>
public static string DbPath
{
get
{
string dataDir = Settings.UserDataPath;
if (!Directory.Exists(dataDir))
{
Directory.CreateDirectory(dataDir);
Debug.WriteLine($"[SchoolDatabase] 데이터 폴더 생성: {dataDir}");
}
return Path.Combine(dataDir, Settings.SchoolDB.Value);
}
}
/// <summary>
/// 데이터 폴더 경로
/// </summary>
public static string DataDirectory => Settings.UserDataPath;
#region Initialization
/// <summary>
/// School 데이터베이스 초기화
/// App.xaml.cs에서 호출
/// </summary>
public static async Task InitAsync()
{
try
{
// 데이터 디렉토리 생성
string dataDir = Settings.DataDirectory;
if (!Directory.Exists(dataDir))
{
Directory.CreateDirectory(dataDir);
Debug.WriteLine($"[SchoolDatabase] 데이터 디렉토리 생성: {dataDir}");
}
Debug.WriteLine($"[SchoolDatabase] DB 경로: {DbPath}");
// DB 파일이 이미 있고 초기화 완료 상태면 스킵
if (File.Exists(DbPath) && Settings.School_Inited.Value)
{
Debug.WriteLine("[SchoolDatabase] 이미 초기화됨 — 스킵");
return;
}
Debug.WriteLine("[SchoolDatabase] 데이터베이스 초기화 시작");
bool success = await InitDatabaseAsync();
if (success)
{
Settings.School_Inited.Set(true);
Debug.WriteLine("[SchoolDatabase] 초기화 완료");
}
else
{
Debug.WriteLine("[SchoolDatabase] 데이터베이스 초기화 실패");
}
}
catch (Exception ex)
{
Debug.WriteLine($"[SchoolDatabase] 초기화 중 예외 발생: {ex.Message}");
Debug.WriteLine($"[SchoolDatabase] StackTrace: {ex.StackTrace}");
}
}
/// <summary>
/// 데이터베이스 테이블 생성
/// </summary>
private static async Task<bool> InitDatabaseAsync()
{
try
{
using var initializer = new Database.DatabaseInitializer(DbPath);
return await initializer.InitializeAsync();
}
catch (Exception ex)
{
Debug.WriteLine($"[SchoolDatabase] DB 초기화 실패: {ex.Message}");
return false;
}
}
#endregion
#region Database Management
/// <summary>
/// 데이터베이스 백업
/// </summary>
public static async Task<bool> BackupDatabaseAsync()
{
try
{
if (!File.Exists(DbPath))
{
Debug.WriteLine("[SchoolDatabase] 백업할 DB 파일이 없습니다.");
return false;
}
string backupDir = Path.Combine(Settings.UserDataPath, "Backup");
if (!Directory.Exists(backupDir))
{
Directory.CreateDirectory(backupDir);
}
string backupFileName = $"School_{DateTime.Now:yyyyMMdd_HHmmss}.db";
string backupPath = Path.Combine(backupDir, backupFileName);
await Task.Run(() =>
{
Helpers.DbFileHelper.Checkpoint(DbPath); // WAL 병합 후 단일 파일 복사
File.Copy(DbPath, backupPath, true);
});
Debug.WriteLine($"[SchoolDatabase] DB 백업 완료: {backupPath}");
return true;
}
catch (Exception ex)
{
Debug.WriteLine($"[SchoolDatabase] DB 백업 실패: {ex.Message}");
return false;
}
}
/// <summary>
/// 데이터베이스 복원
/// </summary>
public static async Task<bool> RestoreDatabaseAsync(string backupPath)
{
try
{
if (!File.Exists(backupPath))
{
Debug.WriteLine($"[SchoolDatabase] 백업 파일이 존재하지 않습니다: {backupPath}");
return false;
}
await Task.Run(() =>
{
File.Copy(backupPath, DbPath, true);
Helpers.DbFileHelper.DeleteSidecars(DbPath); // 고아 -wal/-shm 제거
});
Debug.WriteLine($"[SchoolDatabase] DB 복원 완료: {backupPath}");
Settings.School_Inited.Set(false); // 재초기화 필요
return true;
}
catch (Exception ex)
{
Debug.WriteLine($"[SchoolDatabase] DB 복원 실패: {ex.Message}");
return false;
}
}
/// <summary>
/// 데이터베이스 완전 초기화 (모든 데이터 삭제)
/// </summary>
public static async Task<bool> ResetDatabaseAsync()
{
try
{
Debug.WriteLine("[SchoolDatabase] 데이터베이스 완전 초기화 시작");
// DB 파일 삭제
if (File.Exists(DbPath))
{
File.Delete(DbPath);
Debug.WriteLine("[SchoolDatabase] 기존 DB 파일 삭제 완료");
}
// 재초기화
Settings.School_Inited.Set(false);
await InitAsync();
Debug.WriteLine("[SchoolDatabase] 데이터베이스 완전 초기화 완료");
return true;
}
catch (Exception ex)
{
Debug.WriteLine($"[SchoolDatabase] DB 초기화 실패: {ex.Message}");
return false;
}
}
#endregion
#region Utility Methods
/// <summary>
/// DB 파일 존재 여부 확인
/// </summary>
public static bool DatabaseExists()
{
return File.Exists(DbPath);
}
/// <summary>
/// DB 파일 크기 가져오기 (bytes)
/// </summary>
public static long GetDatabaseSize()
{
if (!File.Exists(DbPath))
return 0;
var fileInfo = new FileInfo(DbPath);
return fileInfo.Length;
}
/// <summary>
/// DB 파일 크기 문자열로 가져오기 (KB, MB)
/// </summary>
public static string GetDatabaseSizeFormatted()
{
long bytes = GetDatabaseSize();
if (bytes == 0)
return "0 KB";
if (bytes < 1024)
return $"{bytes} B";
if (bytes < 1024 * 1024)
return $"{bytes / 1024.0:F2} KB";
return $"{bytes / (1024.0 * 1024.0):F2} MB";
}
#endregion
}
}