From 74395343e13ae7ca108cf61d79451160d0a89ae3 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Thu, 25 May 2017 17:39:33 -0700 Subject: [PATCH] Add non-generic versions of all functions --- src/SQLite.cs | 111 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 98 insertions(+), 13 deletions(-) diff --git a/src/SQLite.cs b/src/SQLite.cs index 51072b37..cb9315cb 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -320,7 +320,7 @@ public IEnumerable TableMappings { /// The mapping represents the schema of the columns of the database and contains /// methods to set and get properties of objects. /// - public TableMapping GetMapping(Type type, CreateFlags createFlags = CreateFlags.None) + public TableMapping GetMapping (Type type, CreateFlags createFlags = CreateFlags.None) { if (_mappings == null) { _mappings = new Dictionary (); @@ -336,13 +336,16 @@ public TableMapping GetMapping(Type type, CreateFlags createFlags = CreateFlags. /// /// Retrieves the mapping that is automatically generated for the given type. /// + /// + /// Optional flags allowing implicit PK and indexes based on naming conventions + /// /// /// The mapping represents the schema of the columns of the database and contains /// methods to set and get properties of objects. /// - public TableMapping GetMapping () + public TableMapping GetMapping (CreateFlags createFlags = CreateFlags.None) { - return GetMapping (typeof (T)); + return GetMapping (typeof (T), createFlags); } private struct IndexedColumn @@ -364,13 +367,21 @@ private struct IndexInfo /// public int DropTable() { - var map = GetMapping (typeof (T)); - - var query = string.Format("drop table if exists \"{0}\"", map.TableName); - - return Execute (query); + return DropTable (GetMapping (typeof (T))); } + /// + /// Executes a "drop table" on the database. This is non-recoverable. + /// + /// + /// The TableMapping used to identify the table. + /// + public int DropTable (TableMapping map) + { + var query = string.Format ("drop table if exists \"{0}\"", map.TableName); + return Execute (query); + } + /// /// Executes a "create table if not exists" on the database. It also /// creates any specified indexes on the columns of the table. It uses @@ -821,8 +832,25 @@ public IEnumerable DeferredQuery(TableMapping map, string query, params /// public T Get (object pk) where T : new() { - var map = GetMapping (typeof(T)); - return Query (map.GetByPrimaryKeySql, pk).First (); + var map = GetMapping (typeof (T)); + return Query (map.GetByPrimaryKeySql, pk).First (); + } + + /// + /// Attempts to retrieve an object with the given primary key from the table + /// associated with the specified type. Use of this method requires that + /// the given type have a designated PrimaryKey (using the PrimaryKeyAttribute). + /// + /// + /// The primary key. + /// + /// + /// The object with the given primary key. Throws a not found exception + /// if the object is not found. + /// + public object Get (object pk, TableMapping map) + { + return Query (map, map.GetByPrimaryKeySql, pk).First (); } /// @@ -868,7 +896,7 @@ public IEnumerable DeferredQuery(TableMapping map, string query, params /// The primary key. /// /// - /// The TableMapping used to identify the object type. + /// The TableMapping used to identify the table. /// /// /// The object with the given primary key or null @@ -914,6 +942,28 @@ public object Find (object pk, TableMapping map) return Query (query, args).FirstOrDefault (); } + /// + /// Attempts to retrieve the first object that matches the query from the table + /// associated with the specified type. + /// + /// + /// The TableMapping used to identify the table. + /// + /// + /// The fully escaped SQL. + /// + /// + /// Arguments to substitute for the occurences of '?' in the query. + /// + /// + /// The object that matches the given predicate or null + /// if the object is not found. + /// + public object FindWithQuery (TableMapping map, string query, params object[] args) + { + return Query (map, query, args).FirstOrDefault (); + } + /// /// Whether has been called and the database is waiting for a . /// @@ -1520,7 +1570,23 @@ public int Delete (object objectToDelete) /// public int Delete (object primaryKey) { - var map = GetMapping (typeof (T)); + return Delete (primaryKey, GetMapping (typeof (T))); + } + + /// + /// Deletes the object with the specified primary key. + /// + /// + /// The primary key of the object to delete. + /// + /// + /// The TableMapping used to identify the table. + /// + /// + /// The number of objects deleted. + /// + public int Delete (object primaryKey, TableMapping map) + { var pk = map.PK; if (pk == null) { throw new NotSupportedException ("Cannot delete " + map.TableName + ": it has no PK"); @@ -1546,7 +1612,26 @@ public int Delete (object primaryKey) public int DeleteAll () { var map = GetMapping (typeof (T)); - var query = string.Format("delete from \"{0}\"", map.TableName); + return DeleteAll (map); + } + + /// + /// Deletes all the objects from the specified table. + /// WARNING WARNING: Let me repeat. It deletes ALL the objects from the + /// specified table. Do you really want to do that? + /// + /// + /// The TableMapping used to identify the table. + /// + /// + /// The number of objects deleted. + /// + /// + /// The type of objects to delete. + /// + public int DeleteAll (TableMapping map) + { + var query = string.Format ("delete from \"{0}\"", map.TableName); var count = Execute (query); if (count > 0) OnTableChanged (map, NotifyTableChangedAction.Delete);