-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathSQLiteWriter.cs
More file actions
70 lines (61 loc) · 2.05 KB
/
SQLiteWriter.cs
File metadata and controls
70 lines (61 loc) · 2.05 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
namespace UnityDataTools.Analyzer.SQLite.Writers
{
public class SQLiteWriter : IDisposable
{
private SqliteConnection m_Database;
public SqliteConnection Connection => m_Database;
private string m_DatabaseName;
public SQLiteWriter(string databaseName)
{
m_DatabaseName = databaseName;
}
public void Begin()
{
if (m_Database != null)
{
throw new InvalidOperationException("SQLiteWriter.Begin called twice");
}
SqliteConnectionStringBuilder builder = new();
builder.DataSource = m_DatabaseName;
builder.Mode = SqliteOpenMode.ReadWriteCreate;
m_Database = new SqliteConnection(builder.ConnectionString);
File.WriteAllBytes(m_DatabaseName, Array.Empty<byte>());
try
{
m_Database.Open();
using var walCommand = m_Database.CreateCommand();
walCommand.CommandText = "PRAGMA journal_mode=WAL";
walCommand.ExecuteNonQuery();
}
catch (Exception e)
{
Console.Error.WriteLine($"Error creating database: {e.Message}");
}
using var command = m_Database.CreateCommand();
command.CommandText = Resources.Init;
command.ExecuteNonQuery();
}
public void End()
{
if (m_Database == null)
{
throw new InvalidOperationException("SQLiteWriter.End called before SQLiteWriter.Begin");
}
using var finalizeCommand = m_Database.CreateCommand();
finalizeCommand.CommandText = Resources.Finalize;
finalizeCommand.ExecuteNonQuery();
}
public void Dispose()
{
m_Database?.Dispose();
m_Database = null;
}
}
}