Ngugi1 / sqlite-net

Automatically exported from code.google.com/p/sqlite-net
0 stars 0 forks source link

Add support for Replace #14

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,
Please add support for Replace Into operations. Here is my implementation:

public int ReplaceAll<T>(IEnumerable<T> rows)
        {
            if (rows == null)
                return 0;
            int c = 0;
            foreach (var r in rows)
            {
                c += Replace(r);
            }
            return c;
        }

        public int Replace<T>(T obj)
        {
            if (obj == null) {
                return 0;
            }

            var map = GetMapping (typeof(T));

            var pk = map.PK;

            if (pk == null) {
                throw new NotSupportedException ("Cannot replace " + map.TableName + ": it has no PK");
            }

            map.SetConnection(obj, this);

            var cols = from p in map.Columns
                where !p.IsAutoInc
                select p;
            var vals = from c in cols
                select c.GetValue (obj);

            var ps = new List<object> (vals);
            //ps.Insert (0, pk.GetValue (obj));

            var q = string.Format ("replace into \"{0}\"({1}) values ({2})", map.TableName, string.Join (",", (from c in cols
                select "\"" + c.Name + "\" ").ToArray ()), string.Join(",", (from c in cols
                select "?").ToArray()));

            var count = Execute (q, ps.ToArray ());

            var id = SQLite3.LastInsertRowid(Handle);
            map.SetAutoIncPK (obj, id);

            return count;
        }

Corneliu.

Original issue reported on code.google.com by corneliu...@gtempaccount.com on 17 Jun 2010 at 2:37

GoogleCodeExporter commented 9 years ago
Interesting, I've never used Replace Into before. I will get this addition in 
ASAP.

Original comment by frank.al...@gmail.com on 29 Jun 2010 at 6:54

GoogleCodeExporter commented 9 years ago

Original comment by frank.al...@gmail.com on 29 Jun 2010 at 6:54

GoogleCodeExporter commented 9 years ago
Is this the same as running an insert staatement passing in "OR REPLACE"?

Original comment by codingvi...@googlemail.com on 20 Jul 2010 at 8:40