-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileDatabase.cs
More file actions
231 lines (215 loc) · 8.41 KB
/
FileDatabase.cs
File metadata and controls
231 lines (215 loc) · 8.41 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
using FileDB.Core.Data;
using FileDB.Core.Data.Tables;
using FileDB.Core.File;
using FileDB.Core.Reader;
using FileDB.Core.Writer;
using FileDB.Function;
using FileDB.Serialization;
using System.IO;
using System.Reflection;
using System.Reflection.Metadata;
namespace FileDB
{
public class FileDatabase
{
public const string SETTING_TABLE = "*.tbl";
public const string SETTING_DATABASE = "*.fdb";
public class PropertyDatabase
{
public string NameDatabase { get; set; } = string.Empty;
public DateTime CreateDatabase { get; set; } = DateTime.Now;
public PropertyDatabase()
{
NameDatabase = string.Empty;
CreateDatabase = DateTime.Now;
}
public PropertyDatabase(string nameDatabase, DateTime createDatabase)
{
NameDatabase = nameDatabase;
CreateDatabase = createDatabase;
}
}
private string _name;
private DateTime _createDateTime;
private DirectoryInfo _databaseDirectory;
private Dictionary<string, Table> _rootTables;
private bool _isLoadDatabase;
public FileDatabase(DirectoryInfo directoryIn)
{
_isLoadDatabase = false;
_rootTables = new Dictionary<string, Table>();
_databaseDirectory = directoryIn;
}
public string Name => _name;
public Table this[string name] => _rootTables[name];
public string Path => _databaseDirectory.FullName;
public IReadOnlyCollection<Table> RootTables => _rootTables.Values;
public DateTime CreateDateTime => _createDateTime;
public Table GetRootTable(string tableNameIn)
{
if (!ContainRootTable(tableNameIn))
throw new ArgumentNullException(nameof(tableNameIn));
return _rootTables[tableNameIn];
}
public bool ContainTable(string nameIn)
{
Table table;
foreach(var root in RootTables)
{
if(root.TryGetTable(nameIn, out table!))
return true;
}
return false;
}
public bool TryGetTable(string nameIn, out Table? tableOut)
{
tableOut = null;
foreach(var root in RootTables)
{
if(root.TryGetTable(nameIn, out tableOut))
return true;
}
return false;
}
public bool ContainRootTable(string nameIn)
{
return _rootTables.ContainsKey(nameIn);
}
public Table CreateRootTable(string nameIn)
{
var property = new PropertyTable() {NameTable = nameIn};
return this.CreateRootTable(property);
}
public Table CreateRootTable(PropertyTable propertyIn)
{
if(string.IsNullOrEmpty(propertyIn.NameTable))
throw new ArgumentNullException(nameof(propertyIn.NameTable));
DirectoryInfo info = _databaseDirectory.CreateSubdirectory(propertyIn.NameTable);
Record record = FileSerializer.Serialize<PropertyTable>(propertyIn);
this.WriteData(FunctionFile.FullFileName(info.FullName,
propertyIn.NameTable, TypeFormat.TBL), record);
var table = new Table(info, propertyIn);
_rootTables.Add(table.Name, table);
return table;
}
public void CreateDatabase(PropertyDatabase settingIn)
{
if (string.IsNullOrEmpty(settingIn.NameDatabase))
throw new ArgumentNullException(nameof(_name));
if (_isLoadDatabase == true)
throw new Exception("База уже создана");
_databaseDirectory.Create();
settingIn.CreateDatabase = DateTime.Now;
this.SetProperty(settingIn);
this.SetFileDatabase();
_isLoadDatabase = true;
}
public void Initialize()
{
if(_isLoadDatabase)
throw new Exception("Таблица уже создана");
PropertyDatabase propertyDB = this.GetFileDatabase();
this.SetProperty(propertyDB);
DirectoryInfo[] directoryTables = _databaseDirectory.GetDirectories();
FileInfo[] files = this.GetFileTable(directoryTables);
foreach(var file in files)
{
if(file == null)
throw new ArgumentNullException(nameof(file));
var record = this.ReadData(file.FullName);
var property = FileSerializer.Deserialize<PropertyTable>(record);
var table = new Table(file.Directory!, property);
_rootTables.Add(property.NameTable, table);
table.Initialize();
}
_isLoadDatabase = true;
}
public Record[] SelectRecord(string recordIn)
{
FileInfo[] files = _databaseDirectory.GetFiles("*.txt", SearchOption.AllDirectories);
int count = 0;
List<Record> list = new List<Record>();
for (int index = 0; index < files.Length; index++)
{
var reader = new ReaderFileTxt(files[index].FullName);
var data = reader.Read();
if (data.Coding.Contains(recordIn))
list.Add(this.ReadData(files[index].FullName));
}
return list.ToArray();
}
/// <summary>
/// Возвращает запись, по ссылочной записи
/// </summary>
/// <param name="linkIn">Запись с ссылкой</param>
/// <returns>Если запись существует, то возвращает её, иначе null</returns>
public Record? LinkRecord(RecordLink linkIn)
{
if(File.Exists(linkIn.FullName))
return ReadData(linkIn.FullName);
return null;
}
private FileInfo[] GetFileTable(DirectoryInfo[] directories)
{
var files = new FileInfo[directories.Length];
for(int index = 0; index < directories.Length;index++)
{
var directory = directories[index];
var tmp = directory.GetFiles(SETTING_TABLE, SearchOption.TopDirectoryOnly);
files[index] = tmp.First();
}
return files;
}
private Record ReadData(string pathIn)
{
var reader = new ReaderFileTxt(pathIn);
var data = reader.Read();
if(string.IsNullOrEmpty(data.Coding))
throw new ArgumentNullException(nameof(pathIn));
return ReaderData.Read(data);
}
private void WriteData(string pathIn, Record recordIn)
{
var writer = new WriterFileTxt(pathIn);
string data = WriterData.Write(recordIn);
writer.Write(data);
}
private void AppendData(string pathIn, string value)
{
var writer = new AppendFileText(pathIn);
writer.Write(value);
}
private PropertyDatabase GetFileDatabase()
{
FileInfo[] info = _databaseDirectory.GetFiles(
SETTING_DATABASE, SearchOption.TopDirectoryOnly);
if (info.Length == 0)
throw new Exception("Файл базы данных не найден");
var file = info.First();
Record record = this.ReadData(file.FullName);
PropertyDatabase property = FileSerializer.Deserialize<PropertyDatabase>(record);
return property;
}
private void SetFileDatabase()
{
var property = this.GetProperty();
var record = FileSerializer.Serialize<PropertyDatabase>(property);
this.WriteData(FunctionFile.FullFileName(_databaseDirectory.FullName,
property.NameDatabase, TypeFormat.FDB), record);
}
private PropertyDatabase GetProperty()
{
var property = new PropertyDatabase
{
NameDatabase = _name,
CreateDatabase = DateTime.Now
};
return property;
}
private void SetProperty(PropertyDatabase propertyIn)
{
_name = propertyIn.NameDatabase;
_createDateTime = propertyIn.CreateDatabase;
}
}
}