Microsoft.Data.Sqlite

This commit is contained in:
Natsumi
2025-09-01 17:40:22 +12:00
parent 61d34db219
commit 6bbc46c9c4
5 changed files with 57 additions and 44 deletions

View File

@@ -1,18 +1,17 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Text.Json.Nodes;
using System.Threading;
using System.Text.Json;
using Microsoft.Data.Sqlite;
namespace VRCX
{
public class SQLite
{
public static SQLite Instance;
private readonly ReaderWriterLockSlim m_ConnectionLock;
private SQLiteConnection m_Connection;
private readonly ReaderWriterLockSlim _connectionLock;
private SqliteConnection _connection;
static SQLite()
{
@@ -21,7 +20,7 @@ namespace VRCX
public SQLite()
{
m_ConnectionLock = new ReaderWriterLockSlim();
_connectionLock = new ReaderWriterLockSlim();
}
public void Init()
@@ -34,18 +33,24 @@ namespace VRCX
if (!string.IsNullOrEmpty(jsonDataSource))
dataSource = jsonDataSource;
m_Connection = new SQLiteConnection($"Data Source=\"{dataSource}\";Version=3;PRAGMA locking_mode=NORMAL;PRAGMA busy_timeout=5000;PRAGMA journal_mode=WAL;PRAGMA optimize=0x10002;", true);
m_Connection.Open();
_connection = new SqliteConnection($"Data Source=\"{dataSource}\";Mode=ReadWriteCreate;Cache=Shared;");
_connection.Open();
using var command = _connection.CreateCommand();
command.CommandText = @"PRAGMA locking_mode=NORMAL;
PRAGMA busy_timeout=5000;
PRAGMA journal_mode=WAL;
PRAGMA optimize=0x10002;";
command.ExecuteNonQuery();
}
public void Exit()
{
m_Connection.Close();
m_Connection.Dispose();
_connection.Close();
_connection.Dispose();
}
public string ExecuteJson(string sql, IDictionary<string, object> args = null)
// for Electron
public string ExecuteJson(string sql, IDictionary<string, object>? args = null)
{
var result = Execute(sql, args);
if (result.Item1 != null)
@@ -63,17 +68,17 @@ namespace VRCX
});
}
public Tuple<string, object[]> Execute(string sql, IDictionary<string, object> args = null)
public Tuple<string?, object[][]?> Execute(string sql, IDictionary<string, object>? args = null)
{
m_ConnectionLock.EnterReadLock();
_connectionLock.EnterReadLock();
try
{
using var command = new SQLiteCommand(sql, m_Connection);
using var command = new SqliteCommand(sql, _connection);
if (args != null)
{
foreach (var arg in args)
{
command.Parameters.Add(new SQLiteParameter(arg.Key, arg.Value));
command.Parameters.Add(new SqliteParameter(arg.Key, arg.Value));
}
}
@@ -88,37 +93,37 @@ namespace VRCX
}
result.Add(values);
}
return new Tuple<string, object[]>(null, result.ToArray());
return new Tuple<string?, object[][]?>(null, result.ToArray());
}
catch (Exception ex)
{
return new Tuple<string, object[]>(ex.Message, null);
return new Tuple<string?, object[][]?>(ex.Message, null);
}
finally
{
m_ConnectionLock.ExitReadLock();
_connectionLock.ExitReadLock();
}
}
public int ExecuteNonQuery(string sql, IDictionary<string, object> args = null)
public int ExecuteNonQuery(string sql, IDictionary<string, object>? args = null)
{
int result = -1;
m_ConnectionLock.EnterWriteLock();
_connectionLock.EnterWriteLock();
try
{
using var command = new SQLiteCommand(sql, m_Connection);
using var command = new SqliteCommand(sql, _connection);
if (args != null)
{
foreach (var arg in args)
{
command.Parameters.Add(new SQLiteParameter(arg.Key, arg.Value));
command.Parameters.Add(new SqliteParameter(arg.Key, arg.Value));
}
}
result = command.ExecuteNonQuery();
}
finally
{
m_ConnectionLock.ExitWriteLock();
_connectionLock.ExitWriteLock();
}
return result;