Support for "insert or replace" and "insert or ignore" #169

Open
JonnyWideFoot opened this Issue Feb 26, 2013 · 4 comments

Projects

None yet

2 participants

@JonnyWideFoot

I may be missing something (?), but I can't find these insert variants in the current codebase - only Insert(), which resolves to: 'insert into' commands. Is there any plan to add 'insert or ignore'?

I'm happy to contribute code, but I'd be a newbie here, so would need a few pointers on roughly how you guys would like it done...

Thanks,
Jon

@JonnyWideFoot

Ok, thanks for the info :-) I'm assuming "Insert or ignore" would be implemented in the same way.

That method is not in my version from the NuGet package. How do I find out which is the Git SHA1 for the version in the nuget package?

Do we know when the next official release is likely to be?

Thanks,
Jon

@JonnyWideFoot

Sorry - I was using 1.0.5. Found the code now...

@JonnyWideFoot
using System;
using System.Threading.Tasks;

namespace SQLite
{
    public partial class SQLiteAsyncConnection
    {
        public Task<int> InsertOrReplaceAsync(object item)
        {
            return Task.Factory.StartNew(() =>
                    {
                        SQLiteConnectionWithLock conn = GetConnection();
                        using (conn.Lock())
                        {
                            return conn.InsertOrReplace(item);
                        }
                    });
        }

        public Task<int> InsertOrIgnoreAsync(object item)
        {
            return Task.Factory.StartNew(() =>
                    {
                        SQLiteConnectionWithLock conn = GetConnection();
                        using (conn.Lock())
                        {
                            return conn.InsertOrIgnore(item);
                        }
                    });
        }
    }

    public partial class SQLiteConnection
    {
        public int InsertOrIgnore(object obj)
        {
            if (obj == null)
            {
                return 0;
            }
            return Insert(obj, "OR IGNORE", obj.GetType());
        }

        public int InsertOrIgnore(object obj, Type objType)
        {
            return Insert(obj, "OR IGNORE", objType);
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment