praeclarum / sqlite-net

Simple, powerful, cross-platform SQLite client and ORM for .NET
MIT License
4.07k stars 1.42k forks source link

No method for LastInsertRowid() #194

Open louy opened 11 years ago

louy commented 11 years ago

Hello,

I was trying to get the id of an inserted object without executing a new query.

In the code I found a function SQLite3.LastInsertRowid(Sqlite3DatabaseHandle db) that is called inside the SQLiteConnection.Insert() function.

The Insert function code is


            var insertCmd = map.GetInsertCommand (this, extra);
            var count = insertCmd.ExecuteNonQuery (vals);

            if (map.HasAutoIncPK)
            {
                var id = SQLite3.LastInsertRowid (Handle);
                map.SetAutoIncPK (obj, id);
            }

            return count;

I can get the "map" by using conn.GetMapping(Type)

The map.SetAutoIncPK sets the the LastInsertRowid to a private property _autoPk

However, there's no way to read that! nothing like map.GetAutoIncPK I think being able to get the LastInsertRowid directly is better in performance than executing a new query to get it.

If there's something that i'm not aware of please tell me. I've just started using sqlite-net.

The only issue with implementing map.GetAutoIncPK is that you still can't get ids for multiple inserts. but at least it solves most of the problem and multiple inserts have a workaround.

I can submit map.GetAutoIncPK but do you think it's a good idea?

I've solved this by creating a new property SQLiteConnection.LastInsertRowid and assigning a value to it in SQLiteConnection.Insert(). Is this the best solution? And should i submit it?

basile-emotum commented 11 years ago

You still can retrieve the LastInsertRowId from your code without touching the library with SQLite3.LastInsertRowid (db.Handle); (db being your SQLiteConnection object)