-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbLib.cs
More file actions
159 lines (144 loc) · 6.17 KB
/
Copy pathdbLib.cs
File metadata and controls
159 lines (144 loc) · 6.17 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
//
// dbLib
// =====
// Library of common database functions shared by all of the separate database
// table classes.
//
// Revision History
// ================
// 27.11.2024 BRD Original version
// 04.03.2025 BRD Switched to a generic Object data type so that this dbLib
// class can be used by multiple different table objects
// simultaneously.
//
using System;
using System.IO;
using System.Text.Json;
namespace db {
internal class dbLib {
private string tableName = "";
private string directoryName = "";
private string lastError = "";
private Object data ;
//
// Constructor
// ===========
public dbLib(string tableName, Object data) {
lastError = "";
// Set the name of the database table in the Database folder. It will
// be a subdirectory of the Database folder located below the bin/Debug
// folder for this project.
this.tableName = tableName;
this.data = data;
// Automatically set a string to hold the path to the Database folder in the
// correct bin/Debug folder for this project.
directoryName = Directory.GetCurrentDirectory();
// Create the database and table if it does not exist.
if (!Directory.Exists(directoryName + "\\Database\\" + tableName + "\\")) {
try {
Directory.CreateDirectory(directoryName + "\\Database\\" + tableName + "\\");
} catch {
lastError = "Cannot create database in directory " +
directoryName;
}
}
}
//
// LastError
// =========
// Returns the last error message generated by this class.
//
public string LastError {
get { return lastError; }
}
//
// Read
// ====
// Reads the specified record from the database table and unpacks the data in the
// record if it is found. If the record is not found, all the database entries are
// automaticall set blank so a new record can be created by the program if necessary.
//
public Object Read(string ID) {
lastError = "";
string json;
if (directoryName.Trim() == "") {
lastError = "Database directory name is blank.";
} else if (tableName.Trim() == "") {
lastError = "Table name is blank.";
} else if (ID.Trim() == "") {
lastError = "The record ID is blank";
} else {
// Open the JSON file, read to the end, and convert the JSON data to a single object
// with named fields. This is called deserialising.
try {
StreamReader reader = new StreamReader(directoryName + "\\Database\\" + tableName + "\\" + ID);
json = reader.ReadToEnd();
reader.Close();
// The options variable sets up the parameters to make the DeSerialiszer
// case insensitive.
var JsonOptions = new JsonSerializerOptions();
JsonOptions.PropertyNameCaseInsensitive = true;
this.data = JsonSerializer.Deserialize<Object>(json, JsonOptions);
} catch (Exception e) {
// the record was not found.
lastError = e.Message;
}
}
return (Object) data;
}
//
// Update
// ======
// This function updates an existing customer or creates a new one.
// Before calling this function, the calling form needs to update
// each of the data fields.
//
public Boolean Update(string ID) {
Boolean updated = false;
string json = "";
lastError = "";
if (directoryName.Trim() == "") {
lastError = "Database directory name is blank.";
} else if (tableName.Trim() == "") {
lastError = "Table name is blank.";
} else if (ID.Trim() == "") {
lastError = "The record ID is blank";
} else {
// The options variable sets up the parameters to make the Serialiszer
// format the JSON values indented on individual lines. They are easier
// to read that way when the file is opened later in a text editor.
var options = new JsonSerializerOptions() {
WriteIndented = true
};
// Create the JSON format record for the table
json = JsonSerializer.Serialize(data, options);
// Write the record to the table
try {
StreamWriter writer = new StreamWriter(directoryName + "\\Database\\" + tableName + "\\" + ID);
writer.Write(json);
writer.Close();
updated = true;
} catch (Exception e) {
// the record could not be written, but the catch stops the
// system crashing.
lastError = "Could not write JSON text to the file. Error returned: \n\n" + e.Message;
}
}
return updated;
}
//
// Query
// =====
// Returns a string array containing the IDs of all
// the records in the table.
//
public string[] Query() {
string[] fileList = Directory.GetFiles(directoryName + "\\Database\\" + tableName);
for (int ptr = 0; ptr < fileList.Length; ptr++) {
// Extract just the file name from the list of files.
fileList[ptr] = Path.GetFileName(fileList[ptr]);
}
return fileList;
}
}
}