-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
277 lines (243 loc) · 8.93 KB
/
Copy pathProgram.cs
File metadata and controls
277 lines (243 loc) · 8.93 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System;
using System.Globalization;
using System.IO;
using Npgsql;
// ---------------------------------------------------------------------------
// Configuration – update these before running
// ---------------------------------------------------------------------------
const string ConnectionString =
"Host=localhost;Port=5432;Database=imdb;Username=postgres;Password=postgres";
// Resolve the project root via the compiler-injected source file path
string projectRoot = GetSourceFileDirectory();
var csvFiles = Directory.GetFiles(projectRoot, "*.csv", SearchOption.TopDirectoryOnly);
if (csvFiles.Length == 0)
{
Console.Error.WriteLine("ERROR: No .csv file found in the project directory.");
return;
}
string CsvFilePath = csvFiles[0];
Console.WriteLine($"Found CSV: {CsvFilePath}");
// ---------------------------------------------------------------------------
await using var conn = new NpgsqlConnection(ConnectionString);
await conn.OpenAsync();
// Watchlist CSV columns (0-indexed):
// 0 Position
// 1 Const
// 2 Created
// 3 Modified
// 4 Description
// 5 Title
// 6 Original Title
// 7 URL
// 8 Title Type
// 9 IMDb Rating
// 10 Runtime (mins)
// 11 Year
// 12 Genres
// 13 Num Votes
// 14 Release Date
// 15 Directors
// 16 Your Rating
// 17 Date Rated
// Create the table if it doesn't already exist
await using (var cmd = conn.CreateCommand())
{
cmd.CommandText = """
CREATE TABLE IF NOT EXISTS imdb_watchlist (
const VARCHAR(20) PRIMARY KEY,
position INTEGER,
created DATE,
modified DATE,
description TEXT,
title TEXT,
original_title TEXT,
url TEXT,
title_type VARCHAR(50),
imdb_rating NUMERIC(3,1),
runtime_mins SMALLINT,
year SMALLINT,
genres TEXT,
num_votes INTEGER,
release_date TEXT,
directors TEXT,
your_rating SMALLINT,
date_rated DATE
);
""";
await cmd.ExecuteNonQueryAsync();
}
Console.WriteLine("Table ready. Reading CSV…");
int inserted = 0, skipped = 0, errors = 0;
using var reader = new StreamReader(CsvFilePath);
// Skip header line
string? header = await reader.ReadLineAsync();
if (header is null)
{
Console.WriteLine("CSV file is empty.");
return;
}
string? line;
int lineNumber = 1;
while ((line = await reader.ReadLineAsync()) is not null)
{
lineNumber++;
if (string.IsNullOrWhiteSpace(line)) continue;
try
{
// Parse CSV respecting quoted fields
string[] fields = SplitCsvLine(line);
if (fields.Length < 18)
{
Console.WriteLine($"Line {lineNumber}: too few fields ({fields.Length}), skipping.");
skipped++;
continue;
}
int? position = ParseInt(fields[0]);
string @const = fields[1].Trim();
DateOnly? created = ParseDate(fields[2]);
DateOnly? modified = ParseDate(fields[3]);
string description = fields[4].Trim();
string title = fields[5].Trim();
string originalTitle = fields[6].Trim();
string url = fields[7].Trim();
string titleType = fields[8].Trim();
decimal? imdbRating = ParseDecimal(fields[9]);
short? runtimeMins = ParseShort(fields[10]);
short? year = ParseShort(fields[11]);
string genres = fields[12].Trim();
int? numVotes = ParseInt(fields[13]);
string releaseDate = fields[14].Trim();
string directors = fields[15].Trim();
short? yourRating = ParseShort(fields[16]);
DateOnly? dateRated = ParseDate(fields[17]);
await using var cmd = conn.CreateCommand();
cmd.CommandText = """
INSERT INTO imdb_watchlist
(const, position, created, modified, description, title, original_title,
url, title_type, imdb_rating, runtime_mins, year,
genres, num_votes, release_date, directors, your_rating, date_rated)
VALUES
(@const, @position, @created, @modified, @description, @title, @originalTitle,
@url, @titleType, @imdbRating, @runtimeMins, @year,
@genres, @numVotes, @releaseDate, @directors, @yourRating, @dateRated)
ON CONFLICT (const) DO NOTHING;
""";
AddParam(cmd, "const", @const);
AddNullableParam(cmd, "position", position);
AddNullableParam(cmd, "created", created);
AddNullableParam(cmd, "modified", modified);
AddParam(cmd, "description", description);
AddParam(cmd, "title", title);
AddParam(cmd, "originalTitle", originalTitle);
AddParam(cmd, "url", url);
AddParam(cmd, "titleType", titleType);
AddNullableParam(cmd, "imdbRating", imdbRating);
AddNullableParam(cmd, "runtimeMins", runtimeMins);
AddNullableParam(cmd, "year", year);
AddParam(cmd, "genres", genres);
AddNullableParam(cmd, "numVotes", numVotes);
AddParam(cmd, "releaseDate", releaseDate);
AddParam(cmd, "directors", directors);
AddNullableParam(cmd, "yourRating", yourRating);
AddNullableParam(cmd, "dateRated", dateRated);
int rowsAffected = await cmd.ExecuteNonQueryAsync();
if (rowsAffected == 0)
{
Console.WriteLine($" Row '{@const}' already exists (skipping duplicate).");
skipped++;
continue;
}
inserted++;
if (inserted % 100 == 0)
Console.WriteLine($" {inserted} rows inserted so far…");
}
catch (Exception ex)
{
errors++;
Console.WriteLine($"Line {lineNumber}: ERROR – {ex.Message}");
}
}
Console.WriteLine();
Console.WriteLine($"Done! Inserted: {inserted} | Skipped: {skipped} | Errors: {errors}");
// ---------------------------------------------------------------------------
// Helper methods
// ---------------------------------------------------------------------------
/// <summary>
/// Returns the directory containing this source file (i.e., the project root).
/// The compiler fills in the CallerFilePath at build time.
/// </summary>
static string GetSourceFileDirectory(
[System.Runtime.CompilerServices.CallerFilePath] string path = "")
=> System.IO.Path.GetDirectoryName(path)!;
static void AddParam(NpgsqlCommand cmd, string name, string value)
=> cmd.Parameters.AddWithValue(name, (object?)value ?? DBNull.Value);
static void AddNullableParam<T>(NpgsqlCommand cmd, string name, T? value) where T : struct
=> cmd.Parameters.AddWithValue(name, value.HasValue ? (object)value.Value : DBNull.Value);
static short? ParseShort(string s)
=> short.TryParse(s.Trim(), out short v) ? v : null;
static int? ParseInt(string s)
=> int.TryParse(s.Trim(), out int v) ? v : null;
static decimal? ParseDecimal(string s)
=> decimal.TryParse(s.Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out decimal v) ? v : null;
static DateOnly? ParseDate(string s)
{
s = s.Trim();
if (DateOnly.TryParseExact(s, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var d))
return d;
return null;
}
/// <summary>
/// Splits a CSV line into fields, handling quoted fields that may contain commas or newlines.
/// </summary>
static string[] SplitCsvLine(string line)
{
var fields = new System.Collections.Generic.List<string>();
int i = 0;
while (i <= line.Length)
{
if (i == line.Length)
{
fields.Add(string.Empty);
break;
}
if (line[i] == '"')
{
// Quoted field
i++; // skip opening quote
var sb = new System.Text.StringBuilder();
while (i < line.Length)
{
if (line[i] == '"')
{
if (i + 1 < line.Length && line[i + 1] == '"')
{
// Escaped double-quote inside quoted field
sb.Append('"');
i += 2;
}
else
{
i++; // skip closing quote
break;
}
}
else
{
sb.Append(line[i++]);
}
}
fields.Add(sb.ToString());
// Skip the comma separator
if (i < line.Length && line[i] == ',') i++;
}
else
{
// Unquoted field
int start = i;
while (i < line.Length && line[i] != ',') i++;
fields.Add(line[i > start ? start..i : start..i]);
if (i < line.Length) i++; // skip comma
}
}
return fields.ToArray();
}