public SQLiteConnection GetSQLiteConnection()
{
if (sqliteConnection == null || sqliteConnection.State == System.Data.ConnectionState.Broken)
{
try
{
Logger.Info(String.Format("Checking whether database {0} exists", databaseFileName));
bool createDatabase = !File.Exists(databaseFileName);
sqliteConnection = new SQLiteConnection("Data Source=" + databaseFileName + ";PRAGMA journal_mode=WAL");
sqliteConnection.Open();
if (createDatabase)
{
string command =
@"CREATE TABLE IF NOT EXISTS files (
path TEXT PRIMARY KEY, /* Remote path of the folder, on the CMIS server side */
localPath TEXT, /* Local path, sometimes different due to local filesystem constraints */
id TEXT,
serverSideModificationDate DATE,
metadata TEXT,
checksum TEXT); /* Checksum of both data and metadata */
CREATE INDEX IF NOT EXISTS files_localPath_index ON files (localPath);
CREATE INDEX IF NOT EXISTS files_id_index ON files (id);
CREATE TABLE IF NOT EXISTS folders (
path TEXT PRIMARY KEY, /* Remote path of the folder, on the CMIS server side */
localPath TEXT, /* Local path, sometimes different due to local filesystem constraints */
id TEXT,
serverSideModificationDate DATE,
metadata TEXT,
checksum TEXT); /* Checksum of metadata */
CREATE INDEX IF NOT EXISTS folders_localPath_index ON folders (localPath);
CREATE INDEX IF NOT EXISTS folders_id_index ON folders (id);
CREATE TABLE IF NOT EXISTS general (
key TEXT PRIMARY KEY,
value TEXT); /* Other data such as ChangeLog token */
CREATE TABLE IF NOT EXISTS downloads (
PATH TEXT PRIMARY KEY,
serverSideModificationDate DATE); /* Download */
CREATE TABLE IF NOT EXISTS failedoperations (
path TEXT PRIMARY KEY,
lastLocalModificationDate DATE,
uploadCounter INTEGER,
downloadCounter INTEGER,
changeCounter INTEGER,
deleteCounter INTEGER,
uploadMessage TEXT,
downloadMessage TEXT,
changeMessage TEXT,
deleteMessage TEXT); /* Failed Operations*/
DROP TABLE IF EXISTS faileduploads; /* Drop old upload Counter Table*/";
ExecuteSQLAction(command, null);
ExecuteSQLAction("PRAGMA user_version=" + SchemaVersion.ToString(), null);
Logger.Info("Database created");
}
else
{
DatabaseMigration.Migrate(databaseFileName);
}
}
catch (Exception e)
{
Logger.Error("Error creating database: " + e.Message, e);
throw;
}
}
return sqliteConnection;
}