private PcdbFile(string path, PcdbGameVersion gameVersion) { FileName = path; database = new SQLiteDatabase(FileName); database.BeginTransaction(); { database.ExecuteNonQuery(new[] {"PRAGMA encoding = 'UTF-8'", "PRAGMA foreign_keys = ON"}); switch (gameVersion) { case PcdbGameVersion.Unknown: database.RollBackTransaction(); database.Close(); throw new NotSupportedException("Unknown game version."); case PcdbGameVersion.SHOC: database.RollBackTransaction(); database.Close(); throw new NotSupportedException("S.T.A.L.K.E.R.: Shadow of Chernobyl is not supported."); case PcdbGameVersion.CS: database.ExecuteNonQuery(new[] { "CREATE TABLE DBVERSION (VERSION INTEGER UNSIGNED NOT NULL)", "INSERT INTO DBVERSION VALUES ('" + (int)SupportedRevision + "')", "CREATE TABLE DBTYPE (TYPEID TINYINT UNSIGNED NOT NULL)", "INSERT INTO DBTYPE VALUES ('2')", "CREATE TABLE HASHES (HASH CHAR(32) NOT NULL PRIMARY KEY, INFO TINYTEXT NULL)", "CREATE TABLE NAMES (HASH CHAR(32) NOT NULL, NAME TINYTEXT NOT NULL, " + "DATEINFO TIMESTAMP NULL, FOREIGN KEY (HASH) REFERENCES HASHES(HASH), " + "PRIMARY KEY (HASH, NAME))", "CREATE TABLE IPS (HASH CHAR(32) NOT NULL, IP TINYTEXT NOT NULL, " + "DATEINFO TIMESTAMP NULL, FOREIGN KEY (HASH) REFERENCES HASHES(HASH), " + "PRIMARY KEY (HASH, IP))" }); break; case PcdbGameVersion.COP: database.ExecuteNonQuery(new[] { "CREATE TABLE DBVERSION (VERSION INTEGER UNSIGNED NOT NULL)", "INSERT INTO DBVERSION VALUES ('" + (int)SupportedRevision + "')", "CREATE TABLE DBTYPE (TYPEID TINYINT UNSIGNED NOT NULL)", "INSERT INTO DBTYPE VALUES ('3')", "CREATE TABLE HASHES (HASH CHAR(32) NOT NULL PRIMARY KEY, INFO TINYTEXT NULL)", "CREATE TABLE NAMES (HASH CHAR(32) NOT NULL, NAME TINYTEXT NOT NULL, " + "DATEINFO TIMESTAMP NULL, FOREIGN KEY (HASH) REFERENCES HASHES(HASH), " + "PRIMARY KEY (HASH, NAME))", "CREATE TABLE IPS (HASH CHAR(32) NOT NULL, IP TINYTEXT NOT NULL, " + "DATEINFO TIMESTAMP NULL, FOREIGN KEY (HASH) REFERENCES HASHES(HASH), " + "PRIMARY KEY (HASH, IP))", "CREATE TABLE GSIDS (HASH CHAR(32) NOT NULL, GSID INT UNSIGNED NOT NULL, " + "DATEINFO TIMESTAMP NULL, FOREIGN KEY (HASH) REFERENCES HASHES(HASH), " + "PRIMARY KEY (HASH, GSID))" }); break; } } database.CommitTransaction(); database.Close(); Revision = (int)SupportedRevision; }
public static SQLiteDatabase CreateInMemoryDB(string filename) { // returns a copy of a database in memory // create an empty memory db first SQLiteDatabase memdb = new SQLiteDatabase(":memory:"); try { // open an existing on disk db SQLiteDatabase ondiskdb = new SQLiteDatabase(filename); ondiskdb.Open(); // get user & schema version int user_version = ondiskdb.GetUserVersion(); int schema_version = ondiskdb.GetSchemaVersion(); // get text encoding string text_encoding = ondiskdb.GetTextEncoding(); // get sql for creating structures string sql = "select sql from sqlite_master where name not like 'sqlite_%'"; DataTable structure = ondiskdb.Select(sql); // get user tables sql = "select name from sqlite_master where type = 'table' and name not like 'sqlite_%'"; DataTable usertables = ondiskdb.Select(sql); // crate identically structures in memmory db memdb.Open(); foreach (DataRow row in structure.Rows) { memdb.DBCommand.CommandText = row[0].ToString(); memdb.DBCommand.ExecuteNonQuery(); } // set user & schema version memdb.SetUserVerion(user_version); memdb.SetSchemaVerion(schema_version); // set text encoding memdb.SetTextEncoding(text_encoding); user_version = memdb.GetUserVersion(); text_encoding = memdb.GetTextEncoding(); // close on disk db ondiskdb.Close(); // attach on disk db to memory db memdb.AttachDatabase(filename, "ondisk"); memdb.BeginTransaction(); foreach (DataRow row in usertables.Rows) { memdb.DBCommand.CommandText = "insert into main." + row[0].ToString() + " select * from ondisk." + row[0].ToString(); memdb.DBCommand.ExecuteNonQuery(); } memdb.Commit(); memdb.DetachDatabase("ondisk"); memdb.DiskFileName = filename; } catch (Exception ex) { Console.WriteLine(ex.Message); } return(memdb); }
public void BeginTransaction(System.Data.SQLite.SQLiteDatabase db = null) { if (db == null) { db = this.db; } if (db == null) { return; } db.BeginTransaction(); }