Created
November 17, 2014 13:08
-
-
Save shadeglare/81e2cd46de94eff0dee9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_STANDALONE_WIN | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Threading; | |
using UnityEngine; | |
using SQLite; | |
using Apartama.Network; | |
namespace Apartama.Data | |
{ | |
/// <summary> | |
/// Represents the database backend for the desktop applications. | |
/// </summary> | |
public sealed class DatabaseContext | |
{ | |
private static DatabaseContext _instance; | |
/// <summary> | |
/// Gets the instance of the DatabaseContext class. | |
/// </summary> | |
public static DatabaseContext Instance | |
{ | |
get | |
{ | |
if (_instance == null) | |
{ | |
_instance = new DatabaseContext(); | |
} | |
return _instance; | |
} | |
} | |
private MonoBehaviour _databaseCoroutineManager; | |
private MonoBehaviour DatabaseCoroutineManager | |
{ | |
get | |
{ | |
if (_databaseCoroutineManager == null) | |
{ | |
_databaseCoroutineManager = new GameObject("DatabaseCoroutineManager").AddComponent<MonoBehaviour>(); | |
} | |
return _databaseCoroutineManager; | |
} | |
} | |
#if UNITY_EDITOR | |
private String connectionString = Application.dataPath + "//..//Compiled//Desktop//Contents//Apartama.db3"; | |
#else | |
private String connectionString = Application.dataPath + "//..//Contents//Apartama.db3"; | |
#endif | |
private DatabaseContext() | |
{ | |
//DO NOTHING. | |
} | |
/// <summary> | |
/// Performs an asynchronous query to the application database. | |
/// </summary> | |
/// <typeparam name="T">The type of an entity for a query.</typeparam> | |
/// <param name="query">An SQL query statement.</param> | |
/// <param name="callback">A callback method that processes the data after | |
/// the current query ends.</param> | |
/// <param name="args">Parameters for the current SQL query.</param> | |
public void QueryAsync<T>(String query, Action<IList<T>> callback, params object[] args) where T : new() | |
{ | |
DatabaseCoroutineManager.StartCoroutine(coroutineQueryAsync(query, callback, args)); | |
} | |
private IEnumerator coroutineQueryAsync<T>(String query, Action<IList<T>> callback, params object[] args) where T: new() | |
{ | |
List<T> entitySet = null; | |
Boolean isDone = false; | |
ThreadPool.QueueUserWorkItem((o) => | |
{ | |
SQLiteConnection connection = new SQLiteConnection(connectionString); | |
entitySet = connection.Query<T>(query, args); | |
connection.Close(); | |
isDone = true; | |
}); | |
while (!isDone) | |
{ | |
yield return 0; | |
} | |
callback(entitySet); | |
} | |
/// <summary> | |
/// Performs an asynchronous query to the application database. | |
/// </summary> | |
/// <typeparam name="T">The type of an entity for a query.</typeparam> | |
/// <param name="query">An SQL query statement.</param> | |
/// <param name="callback">A callback method that processes the data after | |
/// the current query ends.</param> | |
/// <param name="args">Parameters for the current SQL query.</param> | |
public void ExecuteScalarAsync<T>(String query, Action<T> callback, params object[] args) where T : new() | |
{ | |
DatabaseCoroutineManager.StartCoroutine(coroutineExecuteScalarAsync(query, callback, args)); | |
} | |
private IEnumerator coroutineExecuteScalarAsync<T>(String query, Action<T> callback, params object[] args) where T : new() | |
{ | |
T entity = default(T); | |
Boolean isDone = false; | |
ThreadPool.QueueUserWorkItem((o) => | |
{ | |
SQLiteConnection connection = new SQLiteConnection(connectionString); | |
entity = connection.ExecuteScalar<T>(query, args); | |
connection.Close(); | |
isDone = true; | |
}); | |
while (!isDone) | |
{ | |
yield return 0; | |
} | |
callback(entity); | |
} | |
/// <summary> | |
/// Performs an asynchronous query to the application database that | |
/// returns an integer value. | |
/// </summary> | |
/// <param name="query">An SQL query statement.</param> | |
/// <param name="callback">A callback method that processes the data after | |
/// the current query ends.</param> | |
/// <param name="args">Parameters for the current SQL query.</param> | |
public void ExecuteAsync(String query, Action<Int32> callback, params object[] args) | |
{ | |
DatabaseCoroutineManager.StartCoroutine(coroutineExecuteAsync(query, callback, args)); | |
} | |
private IEnumerator coroutineExecuteAsync(String query, Action<Int32> callback, params object[] args) | |
{ | |
Int32 rowAffected = -1; | |
Boolean isDone = false; | |
ThreadPool.QueueUserWorkItem((o) => | |
{ | |
SQLiteConnection connection = new SQLiteConnection(connectionString); | |
rowAffected = connection.Execute(query, args); | |
connection.Close(); | |
isDone = true; | |
}); | |
while (!isDone) | |
{ | |
yield return 0; | |
} | |
callback(rowAffected); | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment