@@ -111,88 +111,122 @@ SQLitePCL.pretty has a very simple table mapping ORM, available in the SQLitePCL
111111
112112``` CSharp
113113
114- public void Example ()
114+ // Per thread builder to limit the number of instances.
115+ private static readonly ThreadLocal < TestObject .Builder > testObjectBuilder = new
116+ ThreadLocal<TestObject.Builder>(() => new TestObject.Builder());
117+
118+ public void Example()
119+ {
120+ // You must provide a result selector to convert a result set row into an object.
121+ // By making this an explicit parameter, SQLitePCL.pretty can support immutable object builders.
122+ var resultSelector =
123+ ResultSet .RowToObject (
124+ () => testObjectBuilder .Value ,
125+ o => ((TestObject .Builder )o ).Build ());
126+
127+ using (var db = SQLite3 .OpenInMemory ())
115128 {
116- var table = TableMapping .Create <TestObject >(
117- () => testObjectBuilder .Value ,
118- o => ((TestObject .Builder )o ).Build ());
129+ // Creates the table and indexes if they do not yet exist.
130+ db .InitTable <TestObject >();
131+
132+ // Insert a new object into the database that has nullable values.
133+ // InsertOrReplace returns the resulting object in the database along with
134+ // its new primary key and any default values assigned by the database.
135+ var inserted =
136+ db .InsertOrReplace (
137+ new TestObject .Builder () { Value = " Hello" }.Build (),
138+ resultSelector );
139+
140+ // Lookup the object in the database.
141+ TestObject found ;
142+ if (! db .TryFind (inserted .Id .Value , resultSelector , out found ))
143+ {
144+ throw new Exception (" item not found" );
145+ }
119146
120- using (var db = SQLite3 .OpenInMemory ())
147+ // We can use linq like syntax to write queries against the database.
148+ var query =
149+ SqlQuery .From <TestObject >()
150+ .Select ()
151+ // This creates a named bind parameter ":value"
152+ .Where <string >((x , value ) => x .Value .Contains (value ))
153+ .OrderBy (x => x .Value )
154+ .Take (100 )
155+ .Skip (0 );
156+
157+ using (var stmt = db .PrepareStatement (query ))
121158 {
122- db .InitTable (table );
123-
124- var inserted = db .InsertOrReplace (table , new TestObject .Builder () { Value = " Hello" }.Build ());
125-
126- TestObject found ;
127- if (! db .TryFind (table , inserted .Id , out found ))
128- {
129- throw new Exception (" item not found" );
130- }
131-
132- TestObject deleted ;
133- if (! db .TryDelete (table , inserted .Id , out deleted ))
134- {
135- throw new Exception (" deletion failed" );
136- }
159+ stmt .BindParameters [" :value" ].Bind (" ell" );
160+ // This will throw if no results are returned
161+ stmt .Query ().First ();
162+ }
163+
164+ // Delete the object from the database.
165+ TestObject deleted ;
166+ if (! db .TryDelete (inserted .Id .Value , resultSelector , out deleted ))
167+ {
168+ throw new Exception (" deletion failed" );
137169 }
138170 }
171+ }
139172
140- // Per thread builder to limit the number of instances.
141- private static readonly ThreadLocal < TestObject .Builder > testObjectBuilder = new
142- ThreadLocal<TestObject.Builder>(() => new TestObject.Builder());
143173
144- public sealed class TestObject : IEquatable<TestObject>
174+ public sealed class TestObject : IEquatable < TestObject >
175+ {
176+ public class Builder
145177 {
146- public class Builder
147- {
148- private long ? id ;
149- private string value ;
178+ private long ? id ;
179+ private string value ;
150180
151- public long ? Id { get { return id ; } set { this .id = value ; } }
152- public string Value { get { return value ; } set { this .value = value ; } }
181+ public long ? Id { get { return id ; } set { this .id = value ; } }
182+ public string Value { get { return value ; } set { this .value = value ; } }
153183
154- public TestObject Build ()
155- {
156- return new TestObject (id , value );
157- }
184+ public TestObject Build ()
185+ {
186+ return new TestObject (id , value );
158187 }
188+ }
159189
160- private long ? id ;
161- private string value ;
190+ private long ? id ;
191+ private string value ;
162192
163- private TestObject (long ? id , string value )
164- {
165- this .id = id ;
166- this .value = value ;
167- }
193+ private TestObject (long ? id , string value )
194+ {
195+ this .id = id ;
196+ this .value = value ;
197+ }
168198
169- [PrimaryKey ]
170- public long ? Id { get { return id ; } }
199+ [PrimaryKey ]
200+ public long ? Id { get { return id ; } }
171201
172- public string Value { get { return value ; } }
202+ public string Value { get { return value ; } }
173203
174- public bool Equals (TestObject other )
175- {
176- return this .Id == other .Id &&
177- this .Value == other .Value ;
178- }
204+ public bool Equals (TestObject other )
205+ {
206+ return this .Id == other .Id &&
207+ this .Value == other .Value ;
208+ }
179209
180- public override bool Equals (object other )
181- {
182- return other is TestObject && this .Equals ((TestObject )other );
183- }
210+ public override bool Equals (object other )
211+ {
212+ return other is TestObject && this .Equals ((TestObject )other );
213+ }
184214
185- public override int GetHashCode ()
186- {
187- int hash = 17 ;
188- hash = hash * 31 + this .Id .GetHashCode ();
189- hash = hash * 31 + this .Value .GetHashCode ();
215+ public override int GetHashCode ()
216+ {
217+ int hash = 17 ;
218+ hash = hash * 31 + this .Id .GetHashCode ();
219+ hash = hash * 31 + this .Value .GetHashCode ();
190220
191- return hash ;
192- }
221+ return hash ;
193222 }
223+ }
194224```
195225
226+ # I'm getting crashes due to EntryPointNotFoundExceptions
227+
228+ SQLite has continued to have active developement over the past few years . Unfortunately both Apple and Google aren 't paying attention and have continued to ship ancient versions. SQLitePCL.raw includes optional DLLs that can be included in your application that also include a statically linked recent version of SQLite. You should use these instead, as SQLitePCL.pretty is dependent upon many modern SQLite features.
229+
196230# How does this compare to...
197231## [SQLitePCL.raw](https://github.com/ericsink/SQLitePCL.raw)
198232
@@ -209,4 +243,4 @@ SQLitePCL is the original PCL library release by MSOpenTech. Stylistically, this
209243
210244## [SQLite-Net](https : // github.com/praeclarum/sqlite-net)
211245
212- SQLite - Net provides an ORM similar to the one provided by SQLitePCL .pretty to manipulate database objects . The most notable difference , is that SQLite -Net supports linq based querying of a database which is not yet supported ( maybe never ) by SQLitePCL . pretty . On the converse , SQLitePCL .pretty is more friendly to functional programmers who want to persist immutable objects to a SQLite database .
246+ SQLite - Net provides an ORM similar to the one provided by SQLitePCL .pretty to manipulate database objects . SQLite - NET is exceptionally well tested in the wild , and should generally be your default choice when being conservative . On the converse , SQLitePCL .pretty has extensive unit test coverage and supports many newer features available in more recent SQLite versions .
0 commit comments