-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathSQLiteConnectionExtensions.cs
More file actions
188 lines (176 loc) · 8.2 KB
/
SQLiteConnectionExtensions.cs
File metadata and controls
188 lines (176 loc) · 8.2 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
/*
* Copyright (c) 2025 Gil Barbosa Reis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using SQLite.Csv;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
namespace SQLite
{
public static class SQLiteConnectionExtensions
{
public static byte[] Serialize(this SQLiteConnection db, string schema = null)
{
IntPtr buffer = SQLite3.Serialize(db.Handle, schema, out long size, SQLite3.SerializeFlags.None);
if (buffer == IntPtr.Zero)
{
return null;
}
try
{
var bytes = new byte[size];
Marshal.Copy(buffer, bytes, 0, (int)size);
return bytes;
}
finally
{
SQLite3.Free(buffer);
}
}
public static SQLiteAsset SerializeToAsset(this SQLiteConnection db, string schema = null, SQLiteOpenFlags openFlags = SQLiteOpenFlags.ReadOnly, bool storeDateTimeAsTicks = true, string streamingAssetsPath = null)
{
SQLiteAsset asset = ScriptableObject.CreateInstance<SQLiteAsset>();
asset.Bytes = db.Serialize(schema);
asset.OpenFlags = openFlags;
asset.StoreDateTimeAsTicks = storeDateTimeAsTicks;
asset.StreamingAssetsPath = streamingAssetsPath;
return asset;
}
public static SQLiteConnection Deserialize(this SQLiteConnection db, byte[] buffer, string schema = null, SQLite3.DeserializeFlags flags = SQLite3.DeserializeFlags.None)
{
return Deserialize(db, buffer, buffer.LongLength, schema, flags);
}
public static SQLiteConnection Deserialize(this SQLiteConnection db, byte[] buffer, long usedSize, string schema = null, SQLite3.DeserializeFlags flags = SQLite3.DeserializeFlags.None)
{
SQLite3.Result result = SQLite3.Deserialize(db.Handle, schema, buffer, usedSize, buffer.LongLength, flags);
if (result != SQLite3.Result.OK)
{
throw SQLiteException.New(result, SQLite3.GetErrmsg(db.Handle));
}
return db;
}
public static SQLiteConnection Deserialize(this SQLiteConnection db, NativeArray<byte> buffer, string schema = null, SQLite3.DeserializeFlags flags = SQLite3.DeserializeFlags.None)
{
return Deserialize(db, buffer, buffer.Length, schema, flags);
}
public static SQLiteConnection Deserialize(this SQLiteConnection db, NativeArray<byte> buffer, long usedSize, string schema = null, SQLite3.DeserializeFlags flags = SQLite3.DeserializeFlags.None)
{
SQLite3.Result result;
unsafe
{
result = SQLite3.Deserialize(db.Handle, schema, buffer.GetUnsafePtr(), usedSize, buffer.Length, flags);
}
if (result != SQLite3.Result.OK)
{
throw SQLiteException.New(result, SQLite3.GetErrmsg(db.Handle));
}
return db;
}
public static SQLiteConnection Deserialize(this SQLiteConnection db, ReadOnlySpan<byte> buffer, string schema = null, SQLite3.DeserializeFlags flags = SQLite3.DeserializeFlags.None)
{
return Deserialize(db, buffer, buffer.Length, schema, flags);
}
public static SQLiteConnection Deserialize(this SQLiteConnection db, ReadOnlySpan<byte> buffer, long usedSize, string schema = null, SQLite3.DeserializeFlags flags = SQLite3.DeserializeFlags.None)
{
SQLite3.Result result;
unsafe
{
fixed (void* ptr = buffer)
{
result = SQLite3.Deserialize(db.Handle, schema, ptr, usedSize, buffer.Length, flags);
}
}
if (result != SQLite3.Result.OK)
{
throw SQLiteException.New(result, SQLite3.GetErrmsg(db.Handle));
}
return db;
}
public static void ExecuteScript(this SQLiteConnection db, string sql)
{
SQLite3.Result result = SQLite3.Exec(db.Handle, sql, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
if (result != SQLite3.Result.OK)
{
throw SQLiteException.New(result, SQLite3.GetErrmsg(db.Handle));
}
}
/// <summary>
/// Import a CSV data stream into the table named <paramref name="tableName"/> inside the database.
/// The table will be created if it doesn't exist yet.
/// </summary>
/// <param name="db">Open database connection</param>
/// <param name="tableName">Name of the table that should be filled with data from the CSV data stream.</param>
/// <param name="csvStream">Data stream with CSV-formatted contents.</param>
/// <param name="separator">Separator used for parsing the CSV. Defaults to comma.</param>
/// <param name="maxFieldSize">Maximum field size allowed.</param>
/// <exception cref="ArgumentNullException">Thrown if any of <paramref name="db"/>, <paramref name="tableName"/> and <paramref name="csvStream"/> are null.</exception>
/// <exception cref="CsvException">Thrown if an error is found while parsing the CSV data.</exception>
public static void ImportCsvToTable(this SQLiteConnection db, string tableName, TextReader csvStream, CsvReader.SeparatorChar separator = CsvReader.SeparatorChar.Comma, int maxFieldSize = int.MaxValue)
{
if (db == null)
{
throw new ArgumentNullException(nameof(db));
}
if (string.IsNullOrWhiteSpace(tableName))
{
throw new ArgumentNullException(nameof(tableName));
}
if (csvStream == null)
{
throw new ArgumentNullException(nameof(csvStream));
}
var columns = new List<string>();
bool parsingHeader = true;
db.RunInTransaction(() =>
{
foreach (string field in CsvReader.ParseStream(csvStream, separator, maxFieldSize))
{
if (field == null) // newline
{
string joinedColumns = string.Join(", ", columns);
if (parsingHeader)
{
db.Execute($"CREATE TABLE IF NOT EXISTS {tableName} ({joinedColumns})");
parsingHeader = false;
}
else
{
db.Execute($"INSERT INTO {tableName} VALUES ({joinedColumns})");
}
columns.Clear();
}
else
{
if (parsingHeader && string.IsNullOrWhiteSpace(field))
{
throw new CsvException("Header cannot have empty column name.");
}
columns.Add(SQLiteConnection.Quote(field));
}
}
});
}
}
}