Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Enhance ConnectionString parsing of SQLiteSyncProvider to support Journal Mode=WAL #17

Closed
thefonzi opened this issue Nov 13, 2017 · 4 comments

Comments

@thefonzi
Copy link

Hi,
actually, adding for example a ";" at the end of SQLite ConnectionString produces a database file like: TestDB.db;
Could this be parsed by using "Data Source=TestDB.db; Journal Mode=WAL;"
It is actually possible activating Journal Mode=WAL, adding this line in SQLiteSyncProvider.cs:
builder.JournalMode = SQLiteJournalModeEnum.Wal;

Maybe it is possible to read this setting from ConnectionString

@thefonzi
Copy link
Author

thefonzi commented Nov 13, 2017

Should be enough to change this code. I have tested.

public SQLiteSyncProvider(string filePath) : base()
        {
            this.filePath = filePath;
            var builder = new SQLiteConnectionStringBuilder(); // { DataSource = filePath };
            builder.ConnectionString = filePath;
            // prefer to store guid in text
            builder.BinaryGUID = false;            
            
            this.ConnectionString = builder.ConnectionString;
        }

@thefonzi
Copy link
Author

if you make the change indicated above (builder.ConnectionString=filepath), the passed connectionstring for SQLite can look like this:
"Data Source=D:\database\myData.db;Journal Mode=WAL;Page Size=8192" or similar.

Please apply this change.

@Mimetis
Copy link
Owner

Mimetis commented Nov 15, 2017

Here is my suggestion :

First of all, adding a method to be able to construct your ConnectionString through a nice object builder, SQLiteConnectionStringBuilder :

       public SQLiteSyncProvider(SQLiteConnectionStringBuilder sQLiteConnectionStringBuilder) : base()
        {
            if (String.IsNullOrEmpty(sQLiteConnectionStringBuilder.DataSource))
                throw new Exception("You have to provide at least a DataSource property to be able to connect to your SQlite database.");

            // prefer to store guid in text
            sQLiteConnectionStringBuilder.BinaryGUID = false;

            this.ConnectionString = sQLiteConnectionStringBuilder.ConnectionString;
        }

For your need, I will also modify the initial constructor like this:

        public SQLiteSyncProvider(string filePath) : base()
        {
            this.filePath = filePath;
            var builder = new SQLiteConnectionStringBuilder();

            if (filePath.ToLowerInvariant().StartsWith("data source"))
            {
                builder.ConnectionString = filePath;
            }
            else
            {
                var fileInfo = new FileInfo(filePath);

                if (!Directory.Exists(fileInfo.Directory.FullName))
                    throw new Exception($"filePath directory {fileInfo.Directory.FullName} does not exists.");

                if (string.IsNullOrEmpty(fileInfo.Name))
                    throw new Exception($"SQLite database file path needs a file name");

                builder.DataSource = filePath;
            }

            // prefer to store guid in text
            builder.BinaryGUID = false;
            this.ConnectionString = builder.ConnectionString;
        }

Like that, I keep the descendant compatibility :)

Is it make sense for you ?

Mimetis added a commit that referenced this issue Nov 15, 2017
@Mimetis Mimetis closed this as completed Nov 15, 2017
@thefonzi
Copy link
Author

Perfect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants