using System.Collections.Generic;\r
using System.ComponentModel.Composition;\r
using System.Data;\r
-using System.Data.SQLite;\r
+using System.Data.SqlServerCe;\r
using System.Diagnostics;\r
using System.Diagnostics.Contracts;\r
using System.IO;\r
using Pithos.Network;\r
using log4net;\r
using Environment = System.Environment;\r
+using System.Data.SqlClient;\r
+using System.Data.SqlServerCe;\r
\r
namespace Pithos.Core.Agents\r
{\r
if (!Directory.Exists(_pithosDataPath))\r
Directory.CreateDirectory(_pithosDataPath);\r
\r
- var dbPath = Path.Combine(_pithosDataPath, "pithos.db");\r
+ var dbPath = Path.Combine(_pithosDataPath, "pithos.sdf");\r
+\r
\r
MigrateOldDb(dbPath, appDataPath);\r
\r
\r
var source = GetConfiguration(_pithosDataPath);\r
ActiveRecordStarter.Initialize(source,typeof(FileState),typeof(FileTag));\r
+ if (!File.Exists(dbPath))\r
+ ActiveRecordStarter.CreateSchema();\r
\r
UpgradeDatabase();\r
\r
\r
\r
- if (!File.Exists(dbPath))\r
- ActiveRecordStarter.CreateSchema();\r
\r
CreateTrigger();\r
\r
throw new ArgumentNullException("appDataPath");\r
Contract.EndContractBlock();\r
\r
- var oldDbPath = Path.Combine(appDataPath, "Pithos", "pithos.db");\r
+ var oldDbPath = Path.Combine(appDataPath, "Pithos", "pithos.sdf");\r
var oldDbInfo = new FileInfo(oldDbPath);\r
if (oldDbInfo.Exists && !File.Exists(dbPath))\r
{\r
}\r
}\r
\r
- private T? GetNull<T>(string commandText,SQLiteConnection connection) where T:struct \r
+ private T? GetNull<T>(string commandText, SqlCeConnection connection) where T:struct \r
{\r
- using (var command= new SQLiteCommand(commandText, connection))\r
+ using (var command= new SqlCeCommand(commandText, connection))\r
{\r
var result = command.ExecuteScalar();\r
if (result == null)\r
}\r
}\r
\r
- private T Get<T>(string commandText,SQLiteConnection connection) \r
+ private T Get<T>(string commandText,SqlCeConnection connection) \r
{\r
- using (var command= new SQLiteCommand(commandText, connection))\r
+ using (var command= new SqlCeCommand(commandText, connection))\r
{\r
var result = command.ExecuteScalar();\r
if (result == null)\r
}\r
}\r
\r
- private int Run(string commandText,SQLiteConnection connection)\r
+ private int Run(string commandText,SqlCeConnection connection)\r
{\r
- using (var command= new SQLiteCommand(commandText, connection))\r
+ using (var command= new SqlCeCommand(commandText, connection))\r
{\r
var result=command.ExecuteNonQuery();\r
return result;\r
\r
private void UpgradeDatabase()\r
{\r
- const string hasVersionText = "select 1 from sqlite_master where name='Version'";\r
+ const string hasVersionText = "select 1 from information_schema.tables where table_name='Version'";\r
\r
- const string hasFilestateText = "select 1 from sqlite_master where name='FileState'";\r
+ const string hasFilestateText = "select 1 from information_schema.tables where table_name='FileState'";\r
\r
const string getVersionCmd = "select Version from version where Id=1";\r
\r
- const string createVersionCmd = "create table Version(Id integer,Version TEXT);\n" +\r
+ const string createVersionCmd = "create table Version(Id integer,Version nvarchar(15));" +\r
"INSERT INTO VERSION (Id,Version) VALUES(1,'0.0.0.0');";\r
+/*\r
const string createFileStateCmd =\r
"CREATE TABLE FileState (Id UNIQUEIDENTIFIER not null, ObjectID TEXT COLLATE NOCASE, FilePath TEXT unique COLLATE NOCASE, OverlayStatus INTEGER, FileStatus INTEGER, ConflictReason TEXT, Checksum TEXT COLLATE NOCASE, ETag TEXT not null COLLATE NOCASE, LastMD5 TEXT not null COLLATE NOCASE,LastWriteDate DATETIME, LastLength INTEGER, Version INTEGER, VersionTimeStamp DATETIME, IsShared INTEGER, SharedBy TEXT, ShareWrite INTEGER, IsFolder INTEGER, Modified DATETIME,Hashes TEXT null COLLATE NOCASE, primary key (Id),unique (FilePath))";\r
const string upgradeText = "PRAGMA writable_schema = 1;\n" +\r
"UPDATE SQLITE_MASTER SET SQL = 'CREATE TABLE FileState (Id UNIQUEIDENTIFIER not null, ObjectID TEXT COLLATE NOCASE, FilePath TEXT unique COLLATE NOCASE, OverlayStatus INTEGER, FileStatus INTEGER, ConflictReason TEXT, Checksum TEXT COLLATE NOCASE, ETag TEXT not null COLLATE NOCASE, LastMD5 TEXT not null COLLATE NOCASE,LastWriteDate DATETIME, LastLength INTEGER, Version INTEGER, VersionTimeStamp DATETIME, IsShared INTEGER, SharedBy TEXT, ShareWrite INTEGER, IsFolder INTEGER, Modified DATETIME, Hashes TEXT null COLLATE NOCASE, primary key (Id),unique (FilePath))' WHERE NAME = 'FileState';\n" +\r
"PRAGMA writable_schema = 0;\n" +\r
"VACUUM;";\r
+*/\r
\r
using (var connection = GetConnection())\r
{\r
else\r
Run(createVersionCmd, connection);\r
\r
+/*\r
var hasFileState = false;\r
hasFileState = GetNull<long>(hasFilestateText, connection).HasValue;\r
if (!hasFileState)\r
{\r
Run(createFileStateCmd, connection);\r
}\r
+*/\r
\r
var actualVersion = Assembly.GetEntryAssembly().GetName().Version;\r
+/*\r
if (!hasVersion || actualVersion > storedVersion)\r
Run(upgradeText, connection);\r
+*/\r
\r
if (actualVersion != storedVersion)\r
- using (var updateVersionCmd = new SQLiteCommand("UPDATE VERSION SET Version=:version where ID=1",\r
+ using (var updateVersionCmd = new SqlCeCommand("UPDATE VERSION SET Version=@version where ID=1",\r
connection))\r
{\r
- updateVersionCmd.Parameters.AddWithValue(":version", actualVersion.ToString());\r
+ updateVersionCmd.Parameters.AddWithValue("@version", actualVersion.ToString());\r
var result = updateVersionCmd.ExecuteNonQuery();\r
Debug.Assert(result > 0);\r
}\r
\r
var properties = new Dictionary<string, string>\r
{\r
- {"connection.driver_class", "NHibernate.Driver.SQLite20Driver"},\r
- {"dialect", "NHibernate.Dialect.SQLiteDialect"},\r
+ {"connection.driver_class", "NHibernate.Driver.SqlServerCeDriver"},\r
+ {"dialect", "NHibernate.Dialect.MsSqlCeDialect"},\r
{"connection.provider", "NHibernate.Connection.DriverConnectionProvider"},\r
{\r
"proxyfactory.factory_class",\r
},\r
};\r
\r
- var connectionString = String.Format(@"Data Source={0}\pithos.db;Version=3;Enlist=N", pithosDbPath);\r
+ var connectionString = String.Format(@"Data Source={0}\pithos.sdf", pithosDbPath);\r
properties.Add("connection.connection_string", connectionString);\r
\r
var source = new InPlaceConfigurationSource(); \r
{\r
action();\r
}\r
- catch (SQLiteException ex)\r
+ catch (SqlException ex)\r
{\r
Log.ErrorFormat("[ERROR] SQL \n{0}", ex);\r
}\r
{\r
using (var session = ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof(FileState)))\r
{\r
- var walquery = session.CreateSQLQuery("PRAGMA journal_mode=WAL");\r
- walquery.List();\r
\r
//var updatecmd = session.CreateSQLQuery(\r
var updatecmd = session.CreateQuery(\r
using (var tx=session.BeginTransaction(IsolationLevel.ReadCommitted))\r
{\r
\r
- //var walquery = session.CreateSQLQuery("PRAGMA journal_mode=WAL");\r
- var walquery = session.CreateQuery("PRAGMA journal_mode=WAL");\r
- walquery.List();\r
\r
//var updatecmd = session.CreateSQLQuery(\r
var updatecmd = session.CreateQuery(\r
using (var tx=session.BeginTransaction(IsolationLevel.ReadCommitted))\r
{\r
\r
- //var walquery = session.CreateSQLQuery("PRAGMA journal_mode=WAL");\r
- var walquery = session.CreateSQLQuery("PRAGMA journal_mode=WAL");\r
- walquery.List();\r
-\r
-\r
//var updatecmd = session.CreateSQLQuery("update FileState set OverlayStatus= :overlayStatus, FileStatus= :fileStatus,ConflictReason= :conflictReason where FilePath = :path COLLATE NOCASE")\r
var updatecmd = session.CreateQuery("update FileState set OverlayStatus= :overlayStatus, FileStatus= :fileStatus,ConflictReason= :conflictReason where FilePath = :path")\r
.SetString("path", absolutePath)\r
{\r
\r
using (var connection = GetConnection())\r
- using (var command = new SQLiteCommand("select Id, FilePath, OverlayStatus,FileStatus ,Checksum ,ETag,Version ,VersionTimeStamp,IsShared ,SharedBy ,ShareWrite, LastMD5,LastLength,LastWriteDate from FileState where FilePath=:path COLLATE NOCASE", connection))\r
+ using (var command = new SqlCeCommand("select Id, FilePath, OverlayStatus,FileStatus ,Checksum ,ETag,Version ,VersionTimeStamp,IsShared ,SharedBy ,ShareWrite, LastMD5,LastLength,LastWriteDate from FileState where FilePath=:path COLLATE NOCASE", connection))\r
{\r
\r
command.Parameters.AddWithValue("path", path);\r
{\r
\r
using (var connection = GetConnection())\r
- using (var command = new SQLiteCommand("select OverlayStatus from FileState where FilePath=:path COLLATE NOCASE", connection))\r
+ using (var command = new SqlCeCommand("select OverlayStatus from FileState where FilePath=:path COLLATE NOCASE", connection))\r
{\r
\r
command.Parameters.AddWithValue("path", path);\r
\r
private string GetConnectionString()\r
{\r
- var connectionString = String.Format(@"Data Source={0}\pithos.db;Version=3;Enlist=N;Pooling=True", _pithosDataPath);\r
+ var connectionString = String.Format(@"Data Source={0}\pithos.sdf", _pithosDataPath);\r
return connectionString;\r
}\r
\r
- private SQLiteConnection GetConnection()\r
+ private SqlCeConnection GetConnection()\r
{\r
var connectionString = GetConnectionString();\r
- var connection = new SQLiteConnection(connectionString);\r
+ var connection = new SqlCeConnection(connectionString);\r
connection.Open();\r
- using(var cmd =connection.CreateCommand())\r
- {\r
- cmd.CommandText = "PRAGMA journal_mode=WAL";\r
- cmd.ExecuteNonQuery();\r
- }\r
return connection;\r
}\r
\r
using (var tx=session.BeginTransaction(IsolationLevel.ReadCommitted))\r
{\r
\r
- //var walquery = session.CreateSQLQuery("PRAGMA journal_mode=WAL");\r
- var walquery = session.CreateSQLQuery("PRAGMA journal_mode=WAL");\r
- walquery.List();\r
-\r
//An entry for the new path may exist, \r
IQuery deletecmd = session.CreateQuery(\r
"delete from FileState where FilePath=:path and ObjectID is null")\r
}\r
}\r
\r
- private bool StateExists(string filePath,SQLiteConnection connection)\r
+ private bool StateExists(string filePath,SqlCeConnection connection)\r
{\r
- using (var command = new SQLiteCommand("Select count(*) from FileState where FilePath=:path COLLATE NOCASE", connection))\r
+ using (var command = new SqlCeCommand("Select count(*) from FileState where FilePath=:path COLLATE NOCASE", connection))\r
{\r
command.Parameters.AddWithValue("path", filePath);\r
var result = command.ExecuteScalar();\r
\r
}\r
\r
- private bool StateExistsByID(string objectId,SQLiteConnection connection)\r
+ private bool StateExistsByID(string objectId,SqlCeConnection connection)\r
{\r
- using (var command = new SQLiteCommand("Select count(*) from FileState where ObjectId=:id", connection))\r
+ using (var command = new SqlCeCommand("Select count(*) from FileState where ObjectId=:id", connection))\r
{\r
command.Parameters.AddWithValue("id", objectId);\r
var result = command.ExecuteScalar();\r
\r
using (var connection = GetConnection())\r
{\r
- var command = new SQLiteCommand("select FileStatus from FileState where FilePath=:path COLLATE NOCASE", connection);\r
+ var command = new SqlCeCommand("select FileStatus from FileState where FilePath=:path COLLATE NOCASE", connection);\r
command.Parameters.AddWithValue("path", path);\r
\r
var statusValue = command.ExecuteScalar();\r
\r
using (var connection = GetConnection())\r
{\r
- var command = new SQLiteCommand("delete from FileState where FilePath = :path COLLATE NOCASE",\r
+ var command = new SqlCeCommand("delete from FileState where FilePath = :path COLLATE NOCASE",\r
connection);\r
\r
command.Parameters.AddWithValue("path", filePath);\r
\r
using (var connection = GetConnection())\r
{\r
- var command = new SQLiteCommand(@"delete from FileState where FilePath = :path or FilePath like :path || '\%' COLLATE NOCASE",\r
+ var command = new SqlCeCommand(@"delete from FileState where FilePath = :path or FilePath like :path || '\%' COLLATE NOCASE",\r
connection);\r
\r
command.Parameters.AddWithValue("path", filePath);\r