Skip to content

Commit 10ed464

Browse files
authored
Merge pull request #75 from gilzoide/feature/sqlite-asset
SQLite Asset + CSV support
2 parents 042550d + 777ca62 commit 10ed464

35 files changed

+1671
-18
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
## [Unreleased](https://github.com/gilzoide/unity-sqlite-net/compare/1.2.4...HEAD)
33
### Added
44
- Support for encrypting / decrypting databases by using [SQLite3 Multiple Ciphers](https://utelle.github.io/SQLite3MultipleCiphers/) implementation
5+
- [SQLiteAsset](Runtime/SQLiteAsset.cs): read-only SQLite database Unity assets.
6+
Files with the extensions ".sqlite", ".sqlite2" and ".sqlite3" will be imported as SQLite database assets.
7+
".csv" files can be imported as SQLite database assets by changing the importer to `SQLite.Editor.Csv.SQLiteAssetCsvImporter` in the Inspector.
8+
- `SQLiteConnection.SerializeToAsset` extension method for serializing a database to an instance of `SQLiteAsset`.
9+
- `SQLiteConnection.ImportCsvToTable` extension method for importing a CSV text stream as a new table inside the database.
510

611
### Changed
712
- Update SQLite to 3.50.1

Editor.meta

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

Editor/Csv.meta

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

Editor/Csv/SQLiteAssetCsvImporter.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 SQLite.Csv;
24+
using UnityEditor.AssetImporters;
25+
using UnityEngine;
26+
27+
namespace SQLite.Editor.Csv
28+
{
29+
[ScriptedImporter(0, null, new[] { "csv" })]
30+
public class SQLiteAssetCsvImporter : ScriptedImporter
31+
{
32+
[Header("SQLite asset options")]
33+
[Tooltip("Name of the table that will be created for holding the CSV data inside the database.")]
34+
[SerializeField] private string _tableName = "data";
35+
36+
[Tooltip("Flags controlling how the SQLite connection should be opened. 'ReadWrite' and 'Create' flags will be ignored, since SQLite assets are read-only.")]
37+
[SerializeField] private SQLiteOpenFlags _openFlags = SQLiteOpenFlags.ReadOnly;
38+
39+
[Tooltip("Whether to store DateTime properties as ticks (true) or strings (false).")]
40+
[SerializeField] private bool _storeDateTimeAsTicks = true;
41+
42+
[Tooltip("Name of the file created for the database inside Streaming Assets folder during builds.\n\n"
43+
+ "If empty, the database bytes will be stored in the asset itself.\n\n"
44+
+ "Loading databases from Streaming Assets is not supported in Android and WebGL platforms.")]
45+
[SerializeField] private string _streamingAssetsPath;
46+
47+
48+
[Header("CSV options")]
49+
[Tooltip("Which separator character will be used when parsing the CSV file.")]
50+
[SerializeField] private CsvReader.SeparatorChar _CSVSeparator = CsvReader.SeparatorChar.Comma;
51+
52+
[Tooltip("If true, the original CSV file will also be imported as a TextAsset")]
53+
[SerializeField] private bool _importCSVTextAsset = false;
54+
55+
[Header("Additional SQL")]
56+
[Tooltip("SQL script that will be run before reading CSV data. Use this for configuring the generated database using PRAGMAs like 'page_size'.")]
57+
[SerializeField, Multiline] private string _SQLBeforeReadingCSV = "";
58+
59+
[Tooltip("SQL script that will be run after reading CSV data. Use this for changing the table's schema, creating indices, etc.")]
60+
[SerializeField, Multiline] private string _SQLAfterReadingCSV = "";
61+
62+
public override void OnImportAsset(AssetImportContext ctx)
63+
{
64+
SQLiteAsset asset;
65+
using (var tempDb = new SQLiteConnection(""))
66+
using (var file = File.OpenRead(assetPath))
67+
using (var stream = new StreamReader(file))
68+
{
69+
if (!string.IsNullOrWhiteSpace(_SQLBeforeReadingCSV))
70+
{
71+
tempDb.Execute(_SQLBeforeReadingCSV);
72+
}
73+
tempDb.ImportCsvToTable(_tableName, stream, _CSVSeparator);
74+
if (!string.IsNullOrWhiteSpace(_SQLAfterReadingCSV))
75+
{
76+
tempDb.Execute(_SQLAfterReadingCSV);
77+
}
78+
79+
asset = tempDb.SerializeToAsset(null, _openFlags, _storeDateTimeAsTicks, _streamingAssetsPath);
80+
}
81+
ctx.AddObjectToAsset("sqlite", asset);
82+
ctx.SetMainObject(asset);
83+
84+
if (_importCSVTextAsset)
85+
{
86+
var textAsset = new TextAsset(File.ReadAllText(assetPath))
87+
{
88+
name = $"{Path.GetFileNameWithoutExtension(assetPath)}",
89+
};
90+
ctx.AddObjectToAsset("text", textAsset);
91+
}
92+
}
93+
}
94+
}

Editor/Csv/SQLiteAssetCsvImporter.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.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "Gilzoide.SqliteNet.Editor",
3+
"rootNamespace": "SQLite.Editor",
4+
"references": [
5+
"GUID:17f96cd3b93974f6493e51a2f25c1241"
6+
],
7+
"includePlatforms": [],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": false,
13+
"defineConstraints": [],
14+
"versionDefines": [],
15+
"noEngineReferences": false
16+
}

Editor/Gilzoide.SqliteNet.Editor.asmdef.meta

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

Editor/Icons.meta

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

Editor/Icons/d_solar_database-bold.png.meta

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

0 commit comments

Comments
 (0)