Skip to content

Commit 8de3261

Browse files
authored
Merge pull request #76 from gilzoide/feature/sql-importer
Add ".sql" scripted importers
2 parents 10ed464 + d3025c9 commit 8de3261

8 files changed

+132
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
".csv" files can be imported as SQLite database assets by changing the importer to `SQLite.Editor.Csv.SQLiteAssetCsvImporter` in the Inspector.
88
- `SQLiteConnection.SerializeToAsset` extension method for serializing a database to an instance of `SQLiteAsset`.
99
- `SQLiteConnection.ImportCsvToTable` extension method for importing a CSV text stream as a new table inside the database.
10+
- Support for importing ".sql" files as either a `TextAsset` or a `SQLiteAsset`.
11+
- `SQLiteConnection.ExecuteScript` extension method for executing a SQL script with multiple statements with a single call.
1012

1113
### Changed
1214
- Update SQLite to 3.50.1

Editor/SQLAssetDatabaseImporter.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2025 Gil Barbosa Reis
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
using System.IO;
23+
using UnityEditor.AssetImporters;
24+
using UnityEngine;
25+
26+
namespace SQLite.Editor
27+
{
28+
[ScriptedImporter(0, null, new[] { "sql" })]
29+
public class SQLAssetDatabaseImporter : ScriptedImporter
30+
{
31+
[Header("SQLite asset options")]
32+
[Tooltip("Flags controlling how the SQLite connection should be opened. 'ReadWrite' and 'Create' flags will be ignored, since SQLite assets are read-only.")]
33+
[SerializeField] private SQLiteOpenFlags _openFlags = SQLiteOpenFlags.ReadOnly;
34+
35+
[Tooltip("Whether to store DateTime properties as ticks (true) or strings (false).")]
36+
[SerializeField] private bool _storeDateTimeAsTicks = true;
37+
38+
[Tooltip("Name of the file created for the database inside Streaming Assets folder during builds.\n\n"
39+
+ "If empty, the database bytes will be stored in the asset itself.\n\n"
40+
+ "Loading databases from Streaming Assets is not supported in Android and WebGL platforms.")]
41+
[SerializeField] private string _streamingAssetsPath;
42+
43+
public override void OnImportAsset(AssetImportContext ctx)
44+
{
45+
SQLiteAsset asset;
46+
using (var tempDb = new SQLiteConnection(""))
47+
{
48+
string contents = File.ReadAllText(assetPath);
49+
tempDb.ExecuteScript(contents);
50+
asset = tempDb.SerializeToAsset(null, _openFlags, _storeDateTimeAsTicks, _streamingAssetsPath);
51+
}
52+
ctx.AddObjectToAsset("main", asset);
53+
ctx.SetMainObject(asset);
54+
}
55+
}
56+
}

Editor/SQLAssetDatabaseImporter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/SQLAssetTextImporter.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2025 Gil Barbosa Reis
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
using System.IO;
23+
using UnityEditor.AssetImporters;
24+
using UnityEngine;
25+
26+
namespace SQLite.Editor
27+
{
28+
[ScriptedImporter(0, "sql")]
29+
public class SQLAssetTextImporter : ScriptedImporter
30+
{
31+
public override void OnImportAsset(AssetImportContext ctx)
32+
{
33+
string contents = File.ReadAllText(assetPath);
34+
TextAsset asset = new(contents);
35+
ctx.AddObjectToAsset("main", asset);
36+
ctx.SetMainObject(asset);
37+
}
38+
}
39+
}

Editor/SQLAssetTextImporter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This package provides the excelent [SQLite-net](https://github.com/praeclarum/sq
1818
- [SQLiteAsset](Runtime/SQLiteAsset.cs): read-only SQLite database Unity assets.
1919
+ Files with the extensions ".sqlite", ".sqlite2" and ".sqlite3" will be imported as SQLite database assets.
2020
+ ".csv" files can be imported as SQLite database assets by changing the importer to `SQLite.Editor.Csv.SQLiteAssetCsvImporter` in the Inspector.
21+
+ ".sql" files are imported as Text assets by default, but can be imported as SQLite database assets by changing the importer to `SQLite.Editor.SQLAssetDatabaseImporter`.
2122
+ Use the `CreateConnection()` method for connecting to the database provided by the asset.
2223
Make sure to `Dispose()` of any connections you create.
2324
+ SQLite assets may be loaded from Streaming Assets folder or from memory, depending on the value of their "Streaming Assets Path" property.

Runtime/SQLiteConnectionExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ public static SQLiteConnection Deserialize(this SQLiteConnection db, ReadOnlySpa
117117
return db;
118118
}
119119

120+
public static void ExecuteScript(this SQLiteConnection db, string sql)
121+
{
122+
SQLite3.Result result = SQLite3.Exec(db.Handle, sql, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
123+
if (result != SQLite3.Result.OK)
124+
{
125+
throw SQLiteException.New(result, SQLite3.GetErrmsg(db.Handle));
126+
}
127+
}
128+
120129
/// <summary>
121130
/// Import a CSV data stream into the table named <paramref name="tableName"/> inside the database.
122131
/// The table will be created if it doesn't exist yet.

Runtime/SQLiteExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public enum DeserializeFlags : uint
6969
[DllImport(LibraryPath, EntryPoint = "sqlite3_column_bytes16", CallingConvention = CallingConvention.Cdecl)]
7070
public static extern int ColumnBytes16(IntPtr stmt, int index);
7171

72+
[DllImport(LibraryPath, EntryPoint = "sqlite3_exec", CallingConvention = CallingConvention.Cdecl)]
73+
public static extern Result Exec(IntPtr db, [MarshalAs(UnmanagedType.LPStr)] string sql, IntPtr callback, IntPtr userdata, IntPtr errorMessagePtr);
74+
7275
#if UNITY_WEBGL && !UNITY_EDITOR
7376
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
7477
public static extern int idbvfs_register(int makeDefault);

0 commit comments

Comments
 (0)