-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtilities.cs
86 lines (74 loc) · 2.43 KB
/
Utilities.cs
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
using SqliteToCsv.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Text;
namespace SqliteToCsv
{
public static class Utilities
{
public static List<Table> GetTablesInfo(SQLiteConnection connection)
{
List<Table> tables = new List<Table>();
DataTable tablesData = connection.GetSchema("Tables");
foreach (DataRow row in tablesData.Rows)
{
Table newTable = new Table(row["TABLE_NAME"].ToString());
SQLiteCommand cmd = new SQLiteCommand($"select * from {newTable.Name}", connection);
SQLiteDataReader reader = cmd.ExecuteReader();
for (int i = 0; i < reader.FieldCount; i++)
{
newTable.Columns.Add(reader.GetName(i));
}
tables.Add(newTable);
}
return tables;
}
public static string[] SanitizeRowOfStrings2(object[] input)
{
string[] output = new string[input.Length];
for (int i = 0; i < input.Length; i++)
{
output[i] = SanitizeString(Convert.ToString(input[i]));
}
return output;
}
public static string SanitizeString(string input)
{
int capacity = input.Length + (int)(input.Length * 0.1f); //Input length + 10%, probably unecessary for short strings....
StringBuilder builder = new StringBuilder(capacity);
builder.Append('"');
for (int i = 0; i < input.Length; i++)
{
if (input[i] == '"')
{
builder.Append('"');
}
builder.Append(input[i]);
}
builder.Append('"');
return builder.ToString();
}
public static void AppendToFile(string path, string fileName, string[] data)
{
EnsurePathExists(path);
File.AppendAllLines(Path.Combine(path, fileName), data);
}
public static void EnsurePathExists(string path)
{
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}