Skip to content

SQLite Asset + CSV support #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


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

### Changed
- Update SQLite to 3.50.1
Expand Down
8 changes: 8 additions & 0 deletions Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Editor/Csv.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions Editor/Csv/SQLiteAssetCsvImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.IO;
using SQLite.Csv;
using UnityEditor.AssetImporters;
using UnityEngine;

namespace SQLite.Editor.Csv
{
[ScriptedImporter(0, null, new[] { "csv" })]
public class SQLiteAssetCsvImporter : ScriptedImporter
{
[Header("SQLite asset options")]
[Tooltip("Name of the table that will be created for holding the CSV data inside the database.")]
[SerializeField] private string _tableName = "data";

[Tooltip("Flags controlling how the SQLite connection should be opened. 'ReadWrite' and 'Create' flags will be ignored, since SQLite assets are read-only.")]
[SerializeField] private SQLiteOpenFlags _openFlags = SQLiteOpenFlags.ReadOnly;

[Tooltip("Whether to store DateTime properties as ticks (true) or strings (false).")]
[SerializeField] private bool _storeDateTimeAsTicks = true;

[Tooltip("Name of the file created for the database inside Streaming Assets folder during builds.\n\n"
+ "If empty, the database bytes will be stored in the asset itself.\n\n"
+ "Loading databases from Streaming Assets is not supported in Android and WebGL platforms.")]
[SerializeField] private string _streamingAssetsPath;


[Header("CSV options")]
[Tooltip("Which separator character will be used when parsing the CSV file.")]
[SerializeField] private CsvReader.SeparatorChar _CSVSeparator = CsvReader.SeparatorChar.Comma;

[Tooltip("If true, the original CSV file will also be imported as a TextAsset")]
[SerializeField] private bool _importCSVTextAsset = false;

[Header("Additional SQL")]
[Tooltip("SQL script that will be run before reading CSV data. Use this for configuring the generated database using PRAGMAs like 'page_size'.")]
[SerializeField, Multiline] private string _SQLBeforeReadingCSV = "";

[Tooltip("SQL script that will be run after reading CSV data. Use this for changing the table's schema, creating indices, etc.")]
[SerializeField, Multiline] private string _SQLAfterReadingCSV = "";

public override void OnImportAsset(AssetImportContext ctx)
{
SQLiteAsset asset;
using (var tempDb = new SQLiteConnection(""))
using (var file = File.OpenRead(assetPath))
using (var stream = new StreamReader(file))
{
if (!string.IsNullOrWhiteSpace(_SQLBeforeReadingCSV))
{
tempDb.Execute(_SQLBeforeReadingCSV);
}
tempDb.ImportCsvToTable(_tableName, stream, _CSVSeparator);
if (!string.IsNullOrWhiteSpace(_SQLAfterReadingCSV))
{
tempDb.Execute(_SQLAfterReadingCSV);
}

asset = tempDb.SerializeToAsset(null, _openFlags, _storeDateTimeAsTicks, _streamingAssetsPath);
}
ctx.AddObjectToAsset("sqlite", asset);
ctx.SetMainObject(asset);

if (_importCSVTextAsset)
{
var textAsset = new TextAsset(File.ReadAllText(assetPath))
{
name = $"{Path.GetFileNameWithoutExtension(assetPath)}",
};
ctx.AddObjectToAsset("text", textAsset);
}
}
}
}
11 changes: 11 additions & 0 deletions Editor/Csv/SQLiteAssetCsvImporter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Editor/Gilzoide.SqliteNet.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "Gilzoide.SqliteNet.Editor",
"rootNamespace": "SQLite.Editor",
"references": [
"GUID:17f96cd3b93974f6493e51a2f25c1241"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Editor/Gilzoide.SqliteNet.Editor.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Editor/Icons.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Editor/Icons/d_solar_database-bold.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
192 changes: 192 additions & 0 deletions Editor/Icons/d_solar_database-bold.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Editor/Icons/d_solar_database-bold@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading