├── .gitattributes ├── .gitignore ├── ImplicitChanges.txt ├── Makefile ├── README.mdown ├── SQLite.sln ├── examples ├── Stocks │ ├── AssemblyInfo.cs │ ├── Main.cs │ ├── Stocks.cs │ ├── Stocks.csproj │ └── Stocks.sln ├── StocksImplicit │ ├── AssemblyInfo.cs │ ├── Main.cs │ ├── Stocks.cs │ ├── StocksImplicit.csproj │ └── StocksImplicit.sln └── StocksTouch │ ├── AddStockView.xib │ ├── AddStockView.xib.cs │ ├── AddStockView.xib.designer.cs │ ├── Info.plist │ ├── Main.cs │ ├── MainWindow.xib │ ├── MainWindow.xib.designer.cs │ ├── StocksTouch.csproj │ ├── StocksTouch.sln │ ├── StocksView.xib │ ├── StocksView.xib.cs │ └── StocksView.xib.designer.cs ├── lib ├── metro │ ├── ARM │ │ └── sqlite3.dll │ ├── x64 │ │ └── sqlite3.dll │ └── x86 │ │ └── sqlite3.dll └── wp7 │ └── Community.CsharpSqlite.WinPhone.dll ├── license.md ├── logo.gif ├── logo.psd ├── nuget ├── NuGet.exe ├── build.bat └── sqlite-net.nuspec ├── src ├── SQLite.MonoTouchAdmin.cs ├── SQLite.cs └── SQLiteAsync.cs └── tests ├── AsyncTests.cs ├── BooleanTest.cs ├── ByteArrayTest.cs ├── CollateTest.cs ├── ConnectionTrackingTest.cs ├── ContainsTest.cs ├── CreateTableImplicitTest.cs ├── CreateTableTest.cs ├── DateTimeTest.cs ├── DeleteTest.cs ├── DropTableTest.cs ├── EqualsTest.cs ├── ExceptionAssert.cs ├── GuidTests.cs ├── InheritanceTest.cs ├── InsertTest.cs ├── JoinTest.cs ├── LinqTest.cs ├── MappingTest.cs ├── MigrationTest.cs ├── NullableTest.cs ├── OpenTests.cs ├── SQLite.Tests.csproj ├── SQLiteMetroTests.sln ├── SQLiteMetroTests ├── Images │ ├── UnitTestLogo.png │ ├── UnitTestSmallLogo.png │ ├── UnitTestSplashScreen.png │ └── UnitTestStoreLogo.png ├── Package.appxmanifest ├── Properties │ └── AssemblyInfo.cs ├── SQLiteMetroTests.csproj └── SQLiteMetroTests_TemporaryKey.pfx ├── SQLiteTouchTests.sln ├── SQLiteTouchTests ├── AppDelegate.cs ├── Info.plist ├── Main.cs └── SQLiteTouchTests.csproj ├── ScalarTest.cs ├── SkipTest.cs ├── StringQueryTest.cs ├── TestDb.cs ├── TransactionTest.cs ├── UnicodeTest.cs └── UniqueTest.cs /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.pidb 3 | *.userprefs 4 | *.user 5 | *.suo 6 | _Resharper* 7 | obj 8 | bin 9 | test-results 10 | TestResults 11 | *.nupkg -------------------------------------------------------------------------------- /ImplicitChanges.txt: -------------------------------------------------------------------------------- 1 | 76 2 | [Flags] 3 | public enum CreateFlags 4 | { 5 | None = 0, 6 | ImplicitPK = 1, // create a primary key for field called 'Id' 7 | ImplicitIndex = 2, // create an index for fields ending in 'Id' 8 | AllImplicit = 3, // do both above 9 | 10 | AutoIncPK = 4 // force PK field to be auto inc 11 | } 12 | 13 | 14 | 233 15 | public TableMapping GetMapping (Type type, CreateFlags createFlags = CreateFlags.None) 16 | { 17 | if (_mappings == null) { 18 | _mappings = new Dictionary (); 19 | } 20 | TableMapping map; 21 | if (!_mappings.TryGetValue (type.FullName, out map)) { 22 | map = new TableMapping (type, createFlags); 23 | _mappings [type.FullName] = map; 24 | } 25 | return map; 26 | } 27 | 28 | 293 29 | public int CreateTable(CreateFlags createFlags = CreateFlags.None) 30 | { 31 | return CreateTable(typeof (T), createFlags); 32 | } 33 | 34 | 308 35 | public int CreateTable(Type ty, CreateFlags createFlags = CreateFlags.None) 36 | { 37 | if (_tables == null) { 38 | _tables = new Dictionary (); 39 | } 40 | TableMapping map; 41 | if (!_tables.TryGetValue (ty.FullName, out map)) { 42 | map = GetMapping(ty, createFlags); 43 | 44 | 45 | 1436 46 | public TableMapping (Type type, CreateFlags createFlags = CreateFlags.None) 47 | 48 | 1464 49 | cols.Add (new Column (p, createFlags)); 50 | 51 | 1603 52 | public Column (PropertyInfo prop, CreateFlags createFlags = CreateFlags.None) 53 | { 54 | var colAttr = (ColumnAttribute)prop.GetCustomAttributes (typeof(ColumnAttribute), true).FirstOrDefault (); 55 | 56 | _prop = prop; 57 | Name = colAttr == null ? prop.Name : colAttr.Name; 58 | //If this type is Nullable then Nullable.GetUnderlyingType returns the T, otherwise it returns null, so get the actual type instead 59 | ColumnType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType; 60 | Collation = Orm.Collation (prop); 61 | IsAutoInc = Orm.IsAutoInc (prop); 62 | IsPK = Orm.IsPK (prop) || (((createFlags & CreateFlags.ImplicitPK) == CreateFlags.ImplicitPK) && prop.Name == Orm.ImplicitPkName); 63 | 64 | IsAutoInc = Orm.IsAutoInc(prop) || (IsPK && ((createFlags & CreateFlags.AutoIncPK) == CreateFlags.AutoIncPK)); 65 | Indices = Orm.GetIndices(prop); 66 | if (!Indices.Any() 67 | && !IsPK 68 | && ((createFlags & CreateFlags.ImplicitIndex) == CreateFlags.ImplicitIndex) 69 | && Name.EndsWith(Orm.ImplicitIndexSuffix) 70 | ) 71 | { 72 | Indices = new IndexedAttribute[] {new IndexedAttribute()}; 73 | } 74 | IsNullable = !IsPK; 75 | MaxStringLength = Orm.MaxStringLength (prop); 76 | } 77 | 78 | 1643 79 | public const string ImplicitPkName = "Id"; 80 | public const string ImplicitIndexSuffix = "Id"; -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | DIST = sqlite-net-$(VER) 3 | 4 | CP = cp -Rf 5 | RM = rm -Rf 6 | 7 | all: examples tests 8 | 9 | examples: 10 | xbuild examples/Stocks/Stocks.csproj 11 | 12 | tests: SQLite.Tests.dll 13 | nunit-console2 SQLite.Tests.dll 14 | 15 | SQLite.Tests.dll: tests/BooleanTest.cs src/SQLite.cs 16 | gmcs tests/BooleanTest.cs src/SQLite.cs -r:NUnit.Framework -target:library -out:SQLite.Tests.dll 17 | 18 | dist: 19 | rm -Rf $(DIST) 20 | rm -Rf $(DIST).zip 21 | mkdir $(DIST) 22 | cp -Rf src/SQLite.cs $(DIST)/ 23 | cp -Rf src/SQLite.MonoTouchAdmin.cs $(DIST)/ 24 | cp -Rf readme.txt $(DIST)/ 25 | rm -Rf $(DIST)/src/.svn 26 | cp -Rf examples $(DIST)/ 27 | rm -Rf $(DIST)/examples/.svn 28 | rm -Rf $(DIST)/examples/Stocks/.svn 29 | rm -Rf $(DIST)/examples/Stocks/bin 30 | rm -Rf $(DIST)/examples/Stocks/obj 31 | rm -Rf $(DIST)/examples/Stocks/*.pidb 32 | rm -Rf $(DIST)/examples/Stocks/*.userprefs 33 | rm -Rf $(DIST)/examples/StocksTouch/.svn 34 | rm -Rf $(DIST)/examples/StocksTouch/bin 35 | rm -Rf $(DIST)/examples/StocksTouch/obj 36 | rm -Rf $(DIST)/examples/StocksTouch/*.pidb 37 | rm -Rf $(DIST)/examples/StocksTouch/*.userprefs 38 | rm -Rf $(DIST)/.DS_Store 39 | zip -9 -r $(DIST).zip $(DIST) 40 | rm -Rf $(DIST) 41 | -------------------------------------------------------------------------------- /README.mdown: -------------------------------------------------------------------------------- 1 | 2 | # sqlite-net 3 | 4 | sqlite-net is an open source, minimal library to allow .NET and Mono applications to store data in [http://www.sqlite.org SQLite 3 databases]. It is written in C# and is meant to be simply compiled in with your projects. It was first designed to work with [MonoTouch](http://xamarin.com) on the iPhone, but has grown up to work on all the platforms (Mono for Android, .NET, Silverlight, WP7, WinRT, Azure, etc.). 5 | 6 | sqlite-net was designed as a quick and convenient database layer. Its design follows from these *goals*: 7 | 8 | * Very easy to integrate with existing projects and with MonoTouch projects. 9 | 10 | * Thin wrapper over SQLite and should be fast and efficient. (The library should not be the performance bottleneck of your queries.) 11 | 12 | * Very simple methods for executing CRUD operations and queries safely (using parameters) and for retrieving the results of those query in a strongly typed fashion. 13 | 14 | * Works with your data model without forcing you to change your classes. (Contains a small reflection-driven ORM layer.) 15 | 16 | * 0 dependencies aside from a [compiled form of the sqlite2 library](http://www.sqlite.org/download.html). 17 | 18 | *Non-goals* include: 19 | 20 | * Not an ADO.NET implementation. This is not a full SQLite driver. If you need that, use [Mono.Data.SQLite](http://www.mono-project.com/SQLite) or [csharp-sqlite](http://code.google.com/p/csharp-sqlite/). 21 | 22 | 23 | ## Meta 24 | 25 | This is an open source project that welcomes contributions/suggestions/bug reports from those who use it. If you have any ideas on how to improve the library, please [post an issue here on github](https://github.com/praeclarum/sqlite-net/issues). Please check out the [How to Contribute](https://github.com/praeclarum/sqlite-net/wiki/How-to-Contribute). 26 | 27 | 28 | # Example Time! 29 | 30 | Please consult the Wiki for, ahem, [complete documentation](https://github.com/praeclarum/sqlite-net/wiki). 31 | 32 | The library contains simple attributes that you can use to control the construction of tables. In a simple stock program, you might use: 33 | 34 | public class Stock 35 | { 36 | [PrimaryKey, AutoIncrement] 37 | public int Id { get; set; } 38 | [MaxLength(8)] 39 | public string Symbol { get; set; } 40 | } 41 | 42 | public class Valuation 43 | { 44 | [PrimaryKey, AutoIncrement] 45 | public int Id { get; set; } 46 | [Indexed] 47 | public int StockId { get; set; } 48 | public DateTime Time { get; set; } 49 | public decimal Price { get; set; } 50 | } 51 | 52 | Once you've defined the objects in your model you have a choice of APIs. You can use the "synchronous API" where calls 53 | block one at a time, or you can use the "asynchronous API" where calls do not block. You may care to use the asynchronous 54 | API for mobile applications in order to increase reponsiveness. 55 | 56 | Both APIs are explained in the two sections below. 57 | 58 | ## Synchronous API 59 | 60 | Once you have defined your entity, you can automatically generate tables in your database by calling `CreateTable`: 61 | 62 | var db = new SQLiteConnection("foofoo"); 63 | db.CreateTable(); 64 | db.CreateTable(); 65 | 66 | You can insert rows in the database using `Insert`. If the table contains an auto-incremented primary key, then the value for that key will be available to you after the insert: 67 | 68 | public static void AddStock(SQLiteConnection db, string symbol) { 69 | var s = db.Insert(new Stock() { 70 | Symbol = symbol 71 | }); 72 | Console.WriteLine("{0} == {1}", s.Symbol, s.Id); 73 | } 74 | 75 | Similar methods exist for `Update` and `Delete`. 76 | 77 | The most straightforward way to query for data is using the `Table` method. This can take predicates for constraining via WHERE clauses and/or adding ORDER BY clauses: 78 | 79 | var conn = new SQLiteConnection("foofoo"); 80 | var query = conn.Table().Where(v => v.Symbol.StartsWith("A")); 81 | 82 | foreach (var stock in query) 83 | Debug.WriteLine("Stock: " + stock.Symbol); 84 | 85 | You can also query the database at a low-level using the `Query` method: 86 | 87 | public static IEnumerable QueryValuations (SQLiteConnection db, Stock stock) 88 | { 89 | return db.Query ("select * from Valuation where StockId = ?", stock.Id); 90 | } 91 | 92 | The generic parameter to the `Query` method specifies the type of object to create for each row. It can be one of your table classes, or any other class whose public properties match the column returned by the query. For instance, we could rewrite the above query as: 93 | 94 | public class Val { 95 | public decimal Money { get; set; } 96 | public DateTime Date { get; set; } 97 | } 98 | public static IEnumerable QueryVals (SQLiteConnection db, Stock stock) 99 | { 100 | return db.Query ("select 'Price' as 'Money', 'Time' as 'Date' from Valuation where StockId = ?", stock.Id); 101 | } 102 | 103 | You can perform low-level updates of the database using the `Execute` method. 104 | 105 | ## Asynchronous API 106 | 107 | The asynchronous library uses the Task Parallel Library (TPL). As such, normal use of `Task` objects, and the `async` and `await` keywords 108 | will work for you. 109 | 110 | Once you have defined your entity, you can automatically generate tables by calling `CreateTableAsync`: 111 | 112 | var conn = new SQLiteAsyncConnection("foofoo"); 113 | conn.CreateTableAsync().ContinueWith((results) => 114 | { 115 | Debug.WriteLine("Table created!"); 116 | }); 117 | 118 | You can insert rows in the database using `Insert`. If the table contains an auto-incremented primary key, then the value for that key will be available to you after the insert: 119 | 120 | Stock stock = new Stock() 121 | { 122 | Symbol = "AAPL" 123 | }; 124 | 125 | var conn = new SQLiteAsyncConnection("foofoo"); 126 | conn.InsertAsync(stock).ContinueWith((t) => 127 | { 128 | Debug.WriteLine("New customer ID: {0}", stock.Id); 129 | }); 130 | 131 | Similar methods exist for `UpdateAsync` and `DeleteAsync`. 132 | 133 | Querying for data is most straightforwardly done using the `Table` method. This will return an `AsyncTableQuery` instance back, whereupon 134 | you can add predictates for constraining via WHERE clauses and/or adding ORDER BY. The database is not physically touched until one of the special 135 | retrieval methods - `ToListAsync`, `FirstAsync`, or `FirstOrDefaultAsync` - is called. 136 | 137 | var conn = new SQLiteAsyncConnection("foofoo"); 138 | var query = conn.Table().Where(v => v.Symbol.StartsWith("A")); 139 | 140 | query.ToListAsync().ContinueWith((t) => 141 | { 142 | foreach (var stock in t.Result) 143 | Debug.WriteLine("Stock: " + stock.Symbol); 144 | }); 145 | 146 | There are a number of low-level methods available. You can also query the database directly via the `QueryAsync` method. Over and above the change 147 | operations provided by `InsertAsync` etc you can issue `ExecuteAsync` methods to change sets of data directly within the database. 148 | 149 | Another helpful method is `ExecuteScalarAsync`. This allows you to return a scalar value from the database easily: 150 | 151 | var conn = new SQLiteAsyncConnection("foofoo"); 152 | conn.ExecuteScalarAsync("select count(*) from Stock", null).ContinueWith((t) => 153 | { 154 | Debug.WriteLine(string.Format("Found '{0}' stock items.", t.Result)); 155 | }); 156 | 157 | 158 | ## Special note on use within Windows Store Apps (Windows 8/WinRT) 159 | 160 | sqlite-net is fully compliant with WinRT Metro-style apps and will pass Microsoft Store validation. 161 | 162 | Please note: 163 | 164 | * Database files will always be created in the path returned by `Windows.Storage.ApplicationData.Current.LocalFolder.Path`. 165 | 166 | * You will need a copy of sqlite3.dll for your app as well. You can get this from sqlite.org with an installer to the SQLite for Windows Runtime SDK. 167 | 168 | 169 | -------------------------------------------------------------------------------- /SQLite.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLite.Tests", "tests\SQLite.Tests.csproj", "{6947A8F1-99BE-4DD1-AD4D-D89425CE67A2}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Any CPU = Debug|Any CPU 9 | Release|Any CPU = Release|Any CPU 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {6947A8F1-99BE-4DD1-AD4D-D89425CE67A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 13 | {6947A8F1-99BE-4DD1-AD4D-D89425CE67A2}.Debug|Any CPU.Build.0 = Debug|Any CPU 14 | {6947A8F1-99BE-4DD1-AD4D-D89425CE67A2}.Release|Any CPU.ActiveCfg = Release|Any CPU 15 | {6947A8F1-99BE-4DD1-AD4D-D89425CE67A2}.Release|Any CPU.Build.0 = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(MonoDevelopProperties) = preSolution 18 | StartupItem = tests\SQLite.Tests.csproj 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /examples/Stocks/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | 4 | // Information about this assembly is defined by the following attributes. 5 | // Change them to the values specific to your project. 6 | 7 | [assembly: AssemblyTitle("Stocks")] 8 | [assembly: AssemblyDescription("An example of using sqlite-net")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("")] 12 | [assembly: AssemblyCopyright("")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". 17 | // The form "{Major}.{Minor}.*" will automatically update the build and revision, 18 | // and "{Major}.{Minor}.{Build}.*" will update just the revision. 19 | 20 | [assembly: AssemblyVersion("1.0.*")] 21 | 22 | // The following attributes are used to specify the signing key for the assembly, 23 | // if desired. See the Mono documentation for more information about signing. 24 | 25 | //[assembly: AssemblyDelaySign(false)] 26 | //[assembly: AssemblyKeyFile("")] 27 | -------------------------------------------------------------------------------- /examples/Stocks/Main.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Path = System.IO.Path; 5 | 6 | using SQLite; 7 | 8 | namespace Stocks.CommandLine 9 | { 10 | class Program 11 | { 12 | public static void Main (string[] args) 13 | { 14 | new Program ().Run (); 15 | } 16 | 17 | Database _db; 18 | 19 | void Initialize () 20 | { 21 | var dbPath = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "Stocks.db"); 22 | _db = new Database (dbPath); 23 | } 24 | 25 | void DisplayStock (string stockSymbol) 26 | { 27 | var stock = _db.QueryStock (stockSymbol); 28 | 29 | if (stock == null) { 30 | Console.WriteLine ("I don't know about {0}", stockSymbol); 31 | Console.WriteLine ("Run \"up {0}\" to update the stock", stockSymbol); 32 | } else { 33 | 34 | // 35 | // Display the last 1 week 36 | // 37 | foreach (var v in _db.QueryValuations (stock)) { 38 | Console.WriteLine (" {0}", v); 39 | } 40 | 41 | } 42 | } 43 | 44 | void UpdateStock (string stockSymbol) 45 | { 46 | _db.UpdateStock(stockSymbol); 47 | } 48 | 49 | void ListStocks () 50 | { 51 | foreach (var stock in _db.QueryAllStocks ()) { 52 | Console.WriteLine (stock); 53 | } 54 | } 55 | 56 | void DisplayBanner () 57 | { 58 | Console.WriteLine ("Stocks - a demo of sqlite-net"); 59 | Console.WriteLine ("Using " + _db.Database); 60 | Console.WriteLine (); 61 | } 62 | 63 | void DisplayHelp (string cmd) 64 | { 65 | Action display = (c, h) => { Console.WriteLine ("{0} {1}", c, h); }; 66 | var cmds = new SortedDictionary { 67 | { 68 | "ls", 69 | "\t List all known stocks" 70 | }, 71 | { 72 | "exit", 73 | "\t Exit stocks" 74 | }, 75 | { 76 | "up stock", 77 | "Updates stock" 78 | }, 79 | { 80 | "help", 81 | "\t Displays help" 82 | }, 83 | { 84 | "stock", 85 | "\t Displays latest valuations for stock" 86 | } 87 | }; 88 | if (cmds.ContainsKey (cmd)) { 89 | display (cmd, cmds[cmd]); 90 | } else { 91 | foreach (var ch in cmds) { 92 | display (ch.Key, ch.Value); 93 | } 94 | } 95 | } 96 | 97 | void Run () 98 | { 99 | var WS = new char[] { 100 | ' ', 101 | '\t', 102 | '\r', 103 | '\n' 104 | }; 105 | 106 | Initialize (); 107 | 108 | DisplayBanner (); 109 | DisplayHelp (""); 110 | 111 | for (;;) { 112 | Console.Write ("$ "); 113 | var cmdline = Console.ReadLine (); 114 | 115 | var args = cmdline.Split (WS, StringSplitOptions.RemoveEmptyEntries); 116 | if (args.Length < 1) 117 | continue; 118 | var cmd = args[0].ToLowerInvariant (); 119 | 120 | if (cmd == "?" || cmd == "help") { 121 | DisplayHelp (""); 122 | } else if (cmd == "exit") { 123 | break; 124 | } else if (cmd == "ls") { 125 | ListStocks (); 126 | } else if (cmd == "up") { 127 | if (args.Length == 2) { 128 | UpdateStock (args[1].ToUpperInvariant ()); 129 | } else { 130 | DisplayHelp ("up stock"); 131 | } 132 | } else { 133 | DisplayStock (cmd.ToUpperInvariant ()); 134 | } 135 | } 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /examples/Stocks/Stocks.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Path = System.IO.Path; 5 | 6 | using SQLite; 7 | using System.Globalization; 8 | 9 | namespace Stocks 10 | { 11 | public class Stock 12 | { 13 | [PrimaryKey, AutoIncrement] 14 | public int Id { get; set; } 15 | [MaxLength(8)] 16 | public string Symbol { get; set; } 17 | 18 | public override string ToString () 19 | { 20 | return Symbol; 21 | } 22 | } 23 | 24 | public class Valuation 25 | { 26 | [PrimaryKey, AutoIncrement] 27 | public int Id { get; set; } 28 | [Indexed] 29 | public int StockId { get; set; } 30 | public DateTime Time { get; set; } 31 | public decimal Price { get; set; } 32 | 33 | public override string ToString () 34 | { 35 | return string.Format ("{0:MMM dd yy} {1:C}", Time, Price); 36 | } 37 | } 38 | 39 | public class Database : SQLiteConnection 40 | { 41 | public Database (string path) : base(path) 42 | { 43 | CreateTable (); 44 | CreateTable (); 45 | } 46 | public IEnumerable QueryValuations (Stock stock) 47 | { 48 | return Table ().Where(x => x.StockId == stock.Id); 49 | } 50 | public Valuation QueryLatestValuation (Stock stock) 51 | { 52 | return Table ().Where(x => x.StockId == stock.Id).OrderByDescending(x => x.Time).Take(1).FirstOrDefault(); 53 | } 54 | public Stock QueryStock (string stockSymbol) 55 | { 56 | return (from s in Table () 57 | where s.Symbol == stockSymbol 58 | select s).FirstOrDefault (); 59 | } 60 | public IEnumerable QueryAllStocks () 61 | { 62 | return from s in Table () 63 | orderby s.Symbol 64 | select s; 65 | } 66 | 67 | public void UpdateStock (string stockSymbol) 68 | { 69 | // 70 | // Ensure that there is a valid Stock in the DB 71 | // 72 | var stock = QueryStock (stockSymbol); 73 | if (stock == null) { 74 | stock = new Stock { Symbol = stockSymbol }; 75 | Insert (stock); 76 | } 77 | 78 | // 79 | // When was it last valued? 80 | // 81 | var latest = QueryLatestValuation (stock); 82 | var latestDate = latest != null ? latest.Time : new DateTime (1950, 1, 1); 83 | 84 | // 85 | // Get the latest valuations 86 | // 87 | try { 88 | var newVals = new YahooScraper ().GetValuations (stock, latestDate + TimeSpan.FromHours (23), DateTime.Now); 89 | InsertAll (newVals); 90 | } catch (System.Net.WebException ex) { 91 | Console.WriteLine (ex); 92 | } 93 | } 94 | } 95 | 96 | public class YahooScraper 97 | { 98 | public IEnumerable GetValuations (Stock stock, DateTime start, DateTime end) 99 | { 100 | var t = "http://ichart.finance.yahoo.com/table.csv?s={0}&d={1}&e={2}&f={3}&g=d&a={4}&b={5}&c={6}&ignore=.csv"; 101 | var url = string.Format (t, stock.Symbol, end.Month - 1, end.Day, end.Year, start.Month - 1, start.Day, start.Year); 102 | Console.WriteLine ("GET {0}", url); 103 | var req = System.Net.WebRequest.Create (url); 104 | using (var resp = new System.IO.StreamReader (req.GetResponse ().GetResponseStream ())) { 105 | var first = true; 106 | var dateCol = 0; 107 | var priceCol = 6; 108 | for (var line = resp.ReadLine (); line != null; line = resp.ReadLine ()) { 109 | var parts = line.Split (','); 110 | if (first) { 111 | dateCol = Array.IndexOf (parts, "Date"); 112 | priceCol = Array.IndexOf (parts, "Adj Close"); 113 | first = false; 114 | } else { 115 | yield return new Valuation { 116 | StockId = stock.Id, 117 | Price = decimal.Parse (parts[priceCol], CultureInfo.InvariantCulture), 118 | Time = DateTime.Parse (parts[dateCol]) 119 | }; 120 | } 121 | } 122 | } 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /examples/Stocks/Stocks.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | AnyCPU 6 | 9.0.21022 7 | 2.0 8 | {9E5D6B89-B69B-486B-9F7B-406BE8690589} 9 | Exe 10 | Stocks 11 | Stocks 12 | v3.5 13 | 14 | 15 | true 16 | full 17 | false 18 | bin\Debug 19 | DEBUG 20 | prompt 21 | 4 22 | 23 | 24 | none 25 | false 26 | bin\Release 27 | prompt 28 | 4 29 | 30 | 31 | 32 | 33 | 3.5 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /examples/Stocks/Stocks.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 10.00 3 | # Visual Studio 2008 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stocks", "Stocks.csproj", "{9E5D6B89-B69B-486B-9F7B-406BE8690589}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Any CPU = Debug|Any CPU 9 | Release|Any CPU = Release|Any CPU 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {9E5D6B89-B69B-486B-9F7B-406BE8690589}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 13 | {9E5D6B89-B69B-486B-9F7B-406BE8690589}.Debug|Any CPU.Build.0 = Debug|Any CPU 14 | {9E5D6B89-B69B-486B-9F7B-406BE8690589}.Release|Any CPU.ActiveCfg = Release|Any CPU 15 | {9E5D6B89-B69B-486B-9F7B-406BE8690589}.Release|Any CPU.Build.0 = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(MonoDevelopProperties) = preSolution 18 | StartupItem = Stocks.csproj 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /examples/StocksImplicit/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | 4 | // Information about this assembly is defined by the following attributes. 5 | // Change them to the values specific to your project. 6 | 7 | [assembly: AssemblyTitle("Stocks")] 8 | [assembly: AssemblyDescription("An example of using sqlite-net")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("")] 12 | [assembly: AssemblyCopyright("")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". 17 | // The form "{Major}.{Minor}.*" will automatically update the build and revision, 18 | // and "{Major}.{Minor}.{Build}.*" will update just the revision. 19 | 20 | [assembly: AssemblyVersion("1.0.*")] 21 | 22 | // The following attributes are used to specify the signing key for the assembly, 23 | // if desired. See the Mono documentation for more information about signing. 24 | 25 | //[assembly: AssemblyDelaySign(false)] 26 | //[assembly: AssemblyKeyFile("")] 27 | -------------------------------------------------------------------------------- /examples/StocksImplicit/Main.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Path = System.IO.Path; 5 | 6 | using SQLite; 7 | 8 | namespace Stocks.CommandLine 9 | { 10 | class Program 11 | { 12 | public static void Main (string[] args) 13 | { 14 | new Program ().Run (); 15 | } 16 | 17 | Database _db; 18 | 19 | void Initialize () 20 | { 21 | var dbPath = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "StocksImplicit.db"); 22 | _db = new Database (dbPath); 23 | } 24 | 25 | void DisplayStock (string stockSymbol) 26 | { 27 | var stock = _db.QueryStock (stockSymbol); 28 | 29 | if (stock == null) { 30 | Console.WriteLine ("I don't know about {0}", stockSymbol); 31 | Console.WriteLine ("Run \"up {0}\" to update the stock", stockSymbol); 32 | } else { 33 | 34 | // 35 | // Display the last 1 week 36 | // 37 | foreach (var v in _db.QueryValuations (stock)) { 38 | Console.WriteLine (" {0}", v); 39 | } 40 | 41 | } 42 | } 43 | 44 | void UpdateStock (string stockSymbol) 45 | { 46 | _db.UpdateStock(stockSymbol); 47 | } 48 | 49 | void ListStocks () 50 | { 51 | foreach (var stock in _db.QueryAllStocks ()) { 52 | Console.WriteLine (stock); 53 | } 54 | } 55 | 56 | void DisplayBanner () 57 | { 58 | Console.WriteLine ("Stocks - a demo of sqlite-net"); 59 | Console.WriteLine ("Using " + _db.DatabaseName); 60 | Console.WriteLine (); 61 | } 62 | 63 | void DisplayHelp (string cmd) 64 | { 65 | Action display = (c, h) => { Console.WriteLine ("{0} {1}", c, h); }; 66 | var cmds = new SortedDictionary { 67 | { 68 | "ls", 69 | "\t List all known stocks" 70 | }, 71 | { 72 | "exit", 73 | "\t Exit stocks" 74 | }, 75 | { 76 | "up stock", 77 | "Updates stock" 78 | }, 79 | { 80 | "help", 81 | "\t Displays help" 82 | }, 83 | { 84 | "stock", 85 | "\t Displays latest valuations for stock" 86 | } 87 | }; 88 | if (cmds.ContainsKey (cmd)) { 89 | display (cmd, cmds[cmd]); 90 | } else { 91 | foreach (var ch in cmds) { 92 | display (ch.Key, ch.Value); 93 | } 94 | } 95 | } 96 | 97 | void Run () 98 | { 99 | var WS = new char[] { 100 | ' ', 101 | '\t', 102 | '\r', 103 | '\n' 104 | }; 105 | 106 | Initialize (); 107 | 108 | DisplayBanner (); 109 | DisplayHelp (""); 110 | 111 | for (;;) { 112 | Console.Write ("$ "); 113 | var cmdline = Console.ReadLine (); 114 | 115 | var args = cmdline.Split (WS, StringSplitOptions.RemoveEmptyEntries); 116 | if (args.Length < 1) 117 | continue; 118 | var cmd = args[0].ToLowerInvariant (); 119 | 120 | if (cmd == "?" || cmd == "help") { 121 | DisplayHelp (""); 122 | } else if (cmd == "exit") { 123 | break; 124 | } else if (cmd == "ls") { 125 | ListStocks (); 126 | } else if (cmd == "up") { 127 | if (args.Length == 2) { 128 | UpdateStock (args[1].ToUpperInvariant ()); 129 | } else { 130 | DisplayHelp ("up stock"); 131 | } 132 | } else { 133 | DisplayStock (cmd.ToUpperInvariant ()); 134 | } 135 | } 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /examples/StocksImplicit/Stocks.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Path = System.IO.Path; 5 | 6 | using SQLite; 7 | using System.Globalization; 8 | 9 | namespace Stocks 10 | { 11 | public class Stock 12 | { 13 | public Guid Id { get; set; } 14 | public string Symbol { get; set; } 15 | 16 | public override string ToString () 17 | { 18 | return Symbol; 19 | } 20 | } 21 | 22 | public class Valuation 23 | { 24 | public Guid Id { get; set; } 25 | public Guid StockId { get; set; } 26 | public DateTime Time { get; set; } 27 | public decimal Price { get; set; } 28 | 29 | public override string ToString () 30 | { 31 | return string.Format ("{0:MMM dd yy} {1:C}", Time, Price); 32 | } 33 | } 34 | 35 | public class Database : SQLiteConnection 36 | { 37 | public Database (string path) : base(path) 38 | { 39 | CreateTable(CreateFlags.AllImplicit | CreateFlags.AutoIncPK); 40 | CreateTable(CreateFlags.AllImplicit | CreateFlags.AutoIncPK); 41 | _DatabaseName = path; 42 | } 43 | 44 | private string _DatabaseName; 45 | public string DatabaseName 46 | { 47 | get { return _DatabaseName; } 48 | } 49 | 50 | public IEnumerable QueryValuations (Stock stock) 51 | { 52 | return Table ().Where(x => x.StockId == stock.Id); 53 | } 54 | 55 | public Valuation QueryLatestValuation (Stock stock) 56 | { 57 | return Table ().Where(x => x.StockId == stock.Id).OrderByDescending(x => x.Time).Take(1).FirstOrDefault(); 58 | } 59 | public Stock QueryStock (string stockSymbol) 60 | { 61 | return (from s in Table () 62 | where s.Symbol == stockSymbol 63 | select s).FirstOrDefault (); 64 | } 65 | public IEnumerable QueryAllStocks () 66 | { 67 | return from s in Table () 68 | orderby s.Symbol 69 | select s; 70 | } 71 | 72 | public void UpdateStock (string stockSymbol) 73 | { 74 | // 75 | // Ensure that there is a valid Stock in the DB 76 | // 77 | var stock = QueryStock (stockSymbol); 78 | if (stock == null) { 79 | stock = new Stock { Symbol = stockSymbol }; 80 | Insert (stock); 81 | } 82 | 83 | // 84 | // When was it last valued? 85 | // 86 | var latest = QueryLatestValuation (stock); 87 | var latestDate = latest != null ? latest.Time : new DateTime (1950, 1, 1); 88 | 89 | // 90 | // Get the latest valuations 91 | // 92 | try { 93 | var newVals = new YahooScraper ().GetValuations (stock, latestDate + TimeSpan.FromHours (23), DateTime.Now); 94 | InsertAll (newVals); 95 | } catch (System.Net.WebException ex) { 96 | Console.WriteLine (ex); 97 | } 98 | } 99 | } 100 | 101 | public class YahooScraper 102 | { 103 | public IEnumerable GetValuations (Stock stock, DateTime start, DateTime end) 104 | { 105 | var t = "http://ichart.finance.yahoo.com/table.csv?s={0}&d={1}&e={2}&f={3}&g=d&a={4}&b={5}&c={6}&ignore=.csv"; 106 | var url = string.Format (t, stock.Symbol, end.Month - 1, end.Day, end.Year, start.Month - 1, start.Day, start.Year); 107 | Console.WriteLine ("GET {0}", url); 108 | var req = System.Net.WebRequest.Create (url); 109 | using (var resp = new System.IO.StreamReader (req.GetResponse ().GetResponseStream ())) { 110 | var first = true; 111 | var dateCol = 0; 112 | var priceCol = 6; 113 | for (var line = resp.ReadLine (); line != null; line = resp.ReadLine ()) { 114 | var parts = line.Split (','); 115 | if (first) { 116 | dateCol = Array.IndexOf (parts, "Date"); 117 | priceCol = Array.IndexOf (parts, "Adj Close"); 118 | first = false; 119 | } else { 120 | yield return new Valuation { 121 | StockId = stock.Id, 122 | Price = decimal.Parse (parts[priceCol], CultureInfo.InvariantCulture), 123 | Time = DateTime.Parse (parts[dateCol]) 124 | }; 125 | } 126 | } 127 | } 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /examples/StocksImplicit/StocksImplicit.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 9.0.21022 7 | 2.0 8 | {9E5D6B89-B69B-486B-9F7B-406BE8690589} 9 | Exe 10 | Stocks 11 | Stocks 12 | v3.5 13 | 14 | 15 | 16 | 17 | 3.5 18 | publish\ 19 | true 20 | Disk 21 | false 22 | Foreground 23 | 7 24 | Days 25 | false 26 | false 27 | true 28 | 0 29 | 1.0.0.%2a 30 | false 31 | false 32 | true 33 | 34 | 35 | true 36 | full 37 | false 38 | bin\Debug 39 | DEBUG 40 | prompt 41 | 4 42 | x86 43 | 44 | 45 | none 46 | false 47 | bin\Release 48 | prompt 49 | 4 50 | 51 | 52 | 53 | 54 | 3.5 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | False 66 | .NET Framework 3.5 SP1 Client Profile 67 | false 68 | 69 | 70 | False 71 | .NET Framework 3.5 SP1 72 | true 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /examples/StocksImplicit/StocksImplicit.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StocksImplicit", "StocksImplicit.csproj", "{9E5D6B89-B69B-486B-9F7B-406BE8690589}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Any CPU = Debug|Any CPU 9 | Release|Any CPU = Release|Any CPU 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {9E5D6B89-B69B-486B-9F7B-406BE8690589}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 13 | {9E5D6B89-B69B-486B-9F7B-406BE8690589}.Debug|Any CPU.Build.0 = Debug|Any CPU 14 | {9E5D6B89-B69B-486B-9F7B-406BE8690589}.Release|Any CPU.ActiveCfg = Release|Any CPU 15 | {9E5D6B89-B69B-486B-9F7B-406BE8690589}.Release|Any CPU.Build.0 = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | GlobalSection(MonoDevelopProperties) = preSolution 21 | StartupItem = Stocks.csproj 22 | EndGlobalSection 23 | EndGlobal 24 | -------------------------------------------------------------------------------- /examples/StocksTouch/AddStockView.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 768 5 | 10K549 6 | 1938 7 | 1038.36 8 | 461.00 9 | 10 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 11 | 933 12 | 13 | 14 | YES 15 | IBUILabel 16 | IBUIView 17 | IBUITextField 18 | IBProxyObject 19 | 20 | 21 | YES 22 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 23 | 24 | 25 | PluginDependencyRecalculationVersion 26 | 27 | 28 | 29 | YES 30 | 31 | IBFilesOwner 32 | IBCocoaTouchFramework 33 | 34 | 35 | IBFirstResponder 36 | IBCocoaTouchFramework 37 | 38 | 39 | 40 | 292 41 | 42 | YES 43 | 44 | 45 | 292 46 | {{114, 76}, {186, 31}} 47 | 48 | 49 | 50 | NO 51 | NO 52 | IBCocoaTouchFramework 53 | 0 54 | AAPL 55 | 3 56 | 57 | 3 58 | MAA 59 | 60 | 2 61 | 62 | 63 | YES 64 | YES 65 | 17 66 | 67 | 3 68 | 1 69 | 9 70 | IBCocoaTouchFramework 71 | 72 | 1 73 | 74 | Helvetica 75 | Helvetica 76 | 0 77 | 24 78 | 79 | 80 | Helvetica 81 | 24 82 | 16 83 | 84 | 85 | 86 | 87 | 292 88 | {{20, 76}, {97, 32}} 89 | 90 | 91 | 92 | NO 93 | YES 94 | NO 95 | IBCocoaTouchFramework 96 | SYMBOL 97 | 98 | 3 99 | MC41AA 100 | 101 | 102 | 103 | 1 104 | 10 105 | 106 | 1 107 | 17 108 | 109 | 110 | Helvetica 111 | 17 112 | 16 113 | 114 | 115 | 116 | {{0, 64}, {320, 416}} 117 | 118 | 119 | 120 | 121 | 3 122 | MQA 123 | 124 | 125 | 126 | 127 | NO 128 | 129 | IBCocoaTouchFramework 130 | 131 | 132 | 133 | 134 | YES 135 | 136 | 137 | view 138 | 139 | 140 | 141 | 7 142 | 143 | 144 | 145 | symbolName 146 | 147 | 148 | 149 | 11 150 | 151 | 152 | 153 | 154 | YES 155 | 156 | 0 157 | 158 | YES 159 | 160 | 161 | 162 | 163 | 164 | 1 165 | 166 | 167 | YES 168 | 169 | 170 | 171 | 172 | 173 | 174 | -1 175 | 176 | 177 | File's Owner 178 | 179 | 180 | -2 181 | 182 | 183 | 184 | 185 | 9 186 | 187 | 188 | 189 | 190 | 10 191 | 192 | 193 | 194 | 195 | 196 | 197 | YES 198 | 199 | YES 200 | -1.CustomClassName 201 | -1.IBPluginDependency 202 | -2.CustomClassName 203 | -2.IBPluginDependency 204 | 1.IBPluginDependency 205 | 10.IBPluginDependency 206 | 9.IBPluginDependency 207 | 208 | 209 | YES 210 | AddStockView 211 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 212 | UIResponder 213 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 214 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 215 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 216 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 217 | 218 | 219 | 220 | YES 221 | 222 | 223 | 224 | 225 | 226 | YES 227 | 228 | 229 | 230 | 231 | 13 232 | 233 | 234 | 0 235 | IBCocoaTouchFramework 236 | 237 | com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS 238 | 239 | 240 | 241 | com.apple.InterfaceBuilder.CocoaTouchPlugin.InterfaceBuilder3 242 | 243 | 244 | YES 245 | 3 246 | 933 247 | 248 | 249 | -------------------------------------------------------------------------------- /examples/StocksTouch/AddStockView.xib.cs: -------------------------------------------------------------------------------- 1 | 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using MonoTouch.Foundation; 6 | using MonoTouch.UIKit; 7 | 8 | namespace Stocks.Touch 9 | { 10 | public partial class AddStockView : UIViewController 11 | { 12 | Database _db; 13 | 14 | public event EventHandler Finished = delegate {}; 15 | 16 | public AddStockView (Database db) 17 | : base("AddStockView", null) 18 | { 19 | _db = db; 20 | 21 | Title = "New Symbol"; 22 | NavigationItem.LeftBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Cancel, Cancel); 23 | NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Done, AddSymbol); 24 | } 25 | 26 | UITextField _symbolName; 27 | 28 | public override void ViewDidLoad () 29 | { 30 | _symbolName = symbolName; 31 | _symbolName.ShouldReturn = delegate { 32 | AddSymbol (symbolName, EventArgs.Empty); 33 | return true; 34 | }; 35 | } 36 | 37 | void Cancel (object sender, EventArgs e) 38 | { 39 | DismissModalViewControllerAnimated(true); 40 | } 41 | 42 | void AddSymbol (object sender, EventArgs e) 43 | { 44 | _db.UpdateStock (symbolName.Text); 45 | DismissModalViewControllerAnimated (true); 46 | Finished (this, EventArgs.Empty); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /examples/StocksTouch/AddStockView.xib.designer.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Mono Runtime Version: 2.0.50727.1433 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | // ------------------------------------------------------------------------------ 10 | 11 | namespace Stocks.Touch { 12 | 13 | 14 | // Base type probably should be MonoTouch.UIKit.UIViewController or subclass 15 | [MonoTouch.Foundation.Register("AddStockView")] 16 | public partial class AddStockView { 17 | 18 | #pragma warning disable 0169 19 | [MonoTouch.Foundation.Connect("view")] 20 | private MonoTouch.UIKit.UIView view { 21 | get { 22 | return ((MonoTouch.UIKit.UIView)(this.GetNativeField("view"))); 23 | } 24 | set { 25 | this.SetNativeField("view", value); 26 | } 27 | } 28 | 29 | [MonoTouch.Foundation.Connect("symbolName")] 30 | private MonoTouch.UIKit.UITextField symbolName { 31 | get { 32 | return ((MonoTouch.UIKit.UITextField)(this.GetNativeField("symbolName"))); 33 | } 34 | set { 35 | this.SetNativeField("symbolName", value); 36 | } 37 | } 38 | 39 | [MonoTouch.Foundation.Connect("addBtn")] 40 | private MonoTouch.UIKit.UIButton addBtn { 41 | get { 42 | return ((MonoTouch.UIKit.UIButton)(this.GetNativeField("addBtn"))); 43 | } 44 | set { 45 | this.SetNativeField("addBtn", value); 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /examples/StocksTouch/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NSMainNibFile 6 | MainWindow 7 | 8 | 9 | -------------------------------------------------------------------------------- /examples/StocksTouch/Main.cs: -------------------------------------------------------------------------------- 1 | 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using MonoTouch.Foundation; 6 | using MonoTouch.UIKit; 7 | using Path = System.IO.Path; 8 | using SQLite; 9 | 10 | namespace Stocks.Touch 11 | { 12 | public class Application 13 | { 14 | static void Main (string[] args) 15 | { 16 | UIApplication.Main (args); 17 | } 18 | } 19 | 20 | public partial class AppDelegate : UIApplicationDelegate 21 | { 22 | Database _db; 23 | UINavigationController _navigationController; 24 | 25 | public override bool FinishedLaunching (UIApplication app, NSDictionary options) 26 | { 27 | _db = new Database (Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "stocks.db")); 28 | _db.Trace = true; 29 | 30 | var stocksView = new StocksView (_db); 31 | 32 | _navigationController = new UINavigationController (stocksView); 33 | 34 | window.AddSubview (_navigationController.View); 35 | 36 | window.MakeKeyAndVisible (); 37 | 38 | return true; 39 | } 40 | 41 | // This method is required in iPhoneOS 3.0 42 | public override void OnActivated (UIApplication application) 43 | { 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /examples/StocksTouch/MainWindow.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 768 5 | 9J61 6 | 677 7 | 949.46 8 | 353.00 9 | 10 | YES 11 | 12 | 13 | 14 | YES 15 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 16 | 17 | 18 | YES 19 | 20 | YES 21 | 22 | 23 | YES 24 | 25 | 26 | 27 | YES 28 | 29 | IBFilesOwner 30 | 31 | 32 | IBFirstResponder 33 | 34 | 35 | 36 | 37 | 1316 38 | 39 | {320, 480} 40 | 41 | 42 | 1 43 | MSAxIDEAA 44 | 45 | NO 46 | NO 47 | 48 | 49 | 50 | 51 | 52 | YES 53 | 54 | 55 | delegate 56 | 57 | 58 | 59 | 5 60 | 61 | 62 | 63 | window 64 | 65 | 66 | 67 | 7 68 | 69 | 70 | 71 | 72 | YES 73 | 74 | 0 75 | 76 | YES 77 | 78 | 79 | 80 | 81 | 82 | 2 83 | 84 | 85 | YES 86 | 87 | 88 | 89 | 90 | -1 91 | 92 | 93 | RmlsZSdzIE93bmVyA 94 | 95 | 96 | 4 97 | 98 | 99 | App Delegate 100 | 101 | 102 | -2 103 | 104 | 105 | 106 | 107 | 108 | 109 | YES 110 | 111 | YES 112 | -1.CustomClassName 113 | -2.CustomClassName 114 | 2.IBAttributePlaceholdersKey 115 | 2.IBEditorWindowLastContentRect 116 | 2.IBPluginDependency 117 | 2.UIWindow.visibleAtLaunch 118 | 4.CustomClassName 119 | 4.IBPluginDependency 120 | 121 | 122 | YES 123 | UIApplication 124 | UIResponder 125 | 126 | YES 127 | 128 | YES 129 | 130 | 131 | YES 132 | 133 | 134 | {{593, 276}, {320, 480}} 135 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 136 | 137 | AppDelegate 138 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 139 | 140 | 141 | 142 | YES 143 | 144 | YES 145 | 146 | 147 | YES 148 | 149 | 150 | 151 | 152 | YES 153 | 154 | YES 155 | 156 | 157 | YES 158 | 159 | 160 | 161 | 7 162 | 163 | 164 | 165 | YES 166 | 167 | AppDelegate 168 | 169 | window 170 | id 171 | 172 | 173 | IBUserSource 174 | 175 | 176 | 177 | 178 | 179 | 0 180 | 181 | 3 182 | 3.0 183 | 184 | -------------------------------------------------------------------------------- /examples/StocksTouch/MainWindow.xib.designer.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace Stocks.Touch 3 | { 4 | // Base type probably should be MonoTouch.Foundation.NSObject or subclass 5 | [MonoTouch.Foundation.Register("AppDelegate")] 6 | public partial class AppDelegate 7 | { 8 | 9 | [MonoTouch.Foundation.Connect("window")] 10 | private MonoTouch.UIKit.UIWindow window { 11 | get { return ((MonoTouch.UIKit.UIWindow)(this.GetNativeField ("window"))); } 12 | set { this.SetNativeField ("window", value); } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /examples/StocksTouch/StocksTouch.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | iPhoneSimulator 6 | 9.0.21022 7 | 2.0 8 | {082A4687-D717-4879-8AB1-3CE2BE1F16A0} 9 | {6BC8ED88-2882-458C-8E55-DFD12B67127B};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 10 | Exe 11 | Stocks.Touch 12 | StocksTouch 13 | 14 | 15 | true 16 | full 17 | false 18 | bin\iPhoneSimulator\Debug 19 | DEBUG 20 | prompt 21 | 4 22 | -linksdkonly 23 | None 24 | true 25 | 26 | 27 | 28 | none 29 | false 30 | bin\iPhoneSimulator\Release 31 | prompt 32 | 4 33 | 34 | 35 | 36 | true 37 | full 38 | false 39 | bin\iPhone\Debug 40 | DEBUG 41 | prompt 42 | 4 43 | iPhone Developer 44 | true 45 | 46 | 47 | 48 | none 49 | false 50 | bin\iPhone\Release 51 | prompt 52 | 4 53 | iPhone Developer 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | MainWindow.xib 65 | 66 | 67 | 68 | 69 | 70 | StocksView.xib 71 | 72 | 73 | StocksView.xib 74 | 75 | 76 | AddStockView.xib 77 | 78 | 79 | AddStockView.xib 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /examples/StocksTouch/StocksTouch.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StocksTouch", "StocksTouch.csproj", "{082A4687-D717-4879-8AB1-3CE2BE1F16A0}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|iPhoneSimulator = Debug|iPhoneSimulator 9 | Release|iPhoneSimulator = Release|iPhoneSimulator 10 | Debug|iPhone = Debug|iPhone 11 | Release|iPhone = Release|iPhone 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {082A4687-D717-4879-8AB1-3CE2BE1F16A0}.Debug|iPhone.ActiveCfg = Debug|iPhone 15 | {082A4687-D717-4879-8AB1-3CE2BE1F16A0}.Debug|iPhone.Build.0 = Debug|iPhone 16 | {082A4687-D717-4879-8AB1-3CE2BE1F16A0}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator 17 | {082A4687-D717-4879-8AB1-3CE2BE1F16A0}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator 18 | {082A4687-D717-4879-8AB1-3CE2BE1F16A0}.Release|iPhone.ActiveCfg = Release|iPhone 19 | {082A4687-D717-4879-8AB1-3CE2BE1F16A0}.Release|iPhone.Build.0 = Release|iPhone 20 | {082A4687-D717-4879-8AB1-3CE2BE1F16A0}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator 21 | {082A4687-D717-4879-8AB1-3CE2BE1F16A0}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator 22 | EndGlobalSection 23 | GlobalSection(MonoDevelopProperties) = preSolution 24 | StartupItem = StocksTouch.csproj 25 | EndGlobalSection 26 | EndGlobal 27 | -------------------------------------------------------------------------------- /examples/StocksTouch/StocksView.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 768 5 | 9L30 6 | 680 7 | 949.54 8 | 353.00 9 | 10 | YES 11 | 12 | 13 | 14 | YES 15 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 16 | 17 | 18 | YES 19 | 20 | YES 21 | 22 | 23 | YES 24 | 25 | 26 | 27 | YES 28 | 29 | IBFilesOwner 30 | 31 | 32 | IBFirstResponder 33 | 34 | 35 | 36 | 292 37 | 38 | YES 39 | 40 | 41 | 274 42 | {320, 460} 43 | 44 | 45 | 3 46 | MQA 47 | 48 | NO 49 | YES 50 | NO 51 | NO 52 | 1 53 | 0 54 | YES 55 | 4.400000e+01 56 | 2.200000e+01 57 | 2.200000e+01 58 | 59 | 60 | {320, 460} 61 | 62 | 63 | 3 64 | MQA 65 | 66 | 2 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | YES 75 | 76 | 77 | view 78 | 79 | 80 | 81 | 7 82 | 83 | 84 | 85 | table 86 | 87 | 88 | 89 | 9 90 | 91 | 92 | 93 | 94 | YES 95 | 96 | 0 97 | 98 | YES 99 | 100 | 101 | 102 | 103 | 104 | 1 105 | 106 | 107 | YES 108 | 109 | 110 | 111 | 112 | 113 | -1 114 | 115 | 116 | RmlsZSdzIE93bmVyA 117 | 118 | 119 | -2 120 | 121 | 122 | 123 | 124 | 8 125 | 126 | 127 | 128 | 129 | 130 | 131 | YES 132 | 133 | YES 134 | -1.CustomClassName 135 | -2.CustomClassName 136 | 1.IBEditorWindowLastContentRect 137 | 1.IBPluginDependency 138 | 8.IBPluginDependency 139 | 140 | 141 | YES 142 | StocksView 143 | UIResponder 144 | {{406, 235}, {320, 480}} 145 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 146 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 147 | 148 | 149 | 150 | YES 151 | 152 | YES 153 | 154 | 155 | YES 156 | 157 | 158 | 159 | 160 | YES 161 | 162 | YES 163 | 164 | 165 | YES 166 | 167 | 168 | 169 | 9 170 | 171 | 172 | 173 | YES 174 | 175 | StocksView 176 | 177 | YES 178 | 179 | YES 180 | table 181 | view 182 | 183 | 184 | YES 185 | id 186 | id 187 | 188 | 189 | 190 | IBUserSource 191 | 192 | 193 | 194 | 195 | 196 | 0 197 | 198 | 3 199 | 3.1 200 | 201 | 202 | -------------------------------------------------------------------------------- /examples/StocksTouch/StocksView.xib.cs: -------------------------------------------------------------------------------- 1 | 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using MonoTouch.Foundation; 6 | using MonoTouch.UIKit; 7 | using SQLite.MonoTouchAdmin; 8 | 9 | 10 | namespace Stocks.Touch 11 | { 12 | public partial class StocksView : UIViewController 13 | { 14 | Database _db; 15 | 16 | public StocksView (Database db) 17 | { 18 | _db = db; 19 | 20 | Title = "Symbols"; 21 | } 22 | 23 | public override void ViewDidLoad () 24 | { 25 | var ds = new SymbolsData (_db); 26 | 27 | table.DataSource = ds; 28 | table.SetEditing (true, false); 29 | 30 | NavigationItem.RightBarButtonItem = new UIBarButtonItem (UIBarButtonSystemItem.Add, delegate { 31 | var c = new AddStockView (_db); 32 | c.Finished += delegate { 33 | ds.Refresh (); 34 | table.ReloadData (); 35 | }; 36 | var n = new UINavigationController (c); 37 | NavigationController.PresentModalViewController(n, true); 38 | }); 39 | NavigationItem.LeftBarButtonItem = new UIBarButtonItem ("Admin", UIBarButtonItemStyle.Plain, delegate { 40 | var c = new SQLiteAdmin(_db); 41 | NavigationController.PushViewController(c.NewTablesViewController(), true); 42 | }); 43 | } 44 | 45 | public class SymbolsData : UITableViewDataSource 46 | { 47 | List rows; 48 | Database _db; 49 | 50 | public SymbolsData(Database db) { 51 | _db = db; 52 | rows = _db.QueryAllStocks().ToList(); 53 | } 54 | 55 | public void Refresh () { 56 | rows = _db.QueryAllStocks().ToList(); 57 | } 58 | 59 | public override void MoveRow (UITableView tableView, NSIndexPath sourceIndexPath, NSIndexPath destinationIndexPath) 60 | { 61 | var item = rows[sourceIndexPath.Row]; 62 | rows.RemoveAt(sourceIndexPath.Row); 63 | rows.Insert(destinationIndexPath.Row, item); 64 | } 65 | 66 | public override bool CanMoveRow (UITableView tableView, NSIndexPath indexPath) 67 | { 68 | return true; 69 | } 70 | 71 | public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath) 72 | { 73 | switch (editingStyle) { 74 | case UITableViewCellEditingStyle.Delete: 75 | _db.Delete(rows[indexPath.Row]); 76 | rows.RemoveAt(indexPath.Row); 77 | tableView.DeleteRows(new[]{indexPath}, UITableViewRowAnimation.Fade); 78 | break; 79 | } 80 | } 81 | 82 | public override int NumberOfSections (UITableView tableView) 83 | { 84 | return 1; 85 | } 86 | 87 | public override int RowsInSection (UITableView tableview, int section) 88 | { 89 | return rows.Count; 90 | } 91 | 92 | public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) 93 | { 94 | var cell = tableView.DequeueReusableCell ("cell"); 95 | if (cell == null) { 96 | cell = new UITableViewCell (UITableViewCellStyle.Subtitle, "cell"); 97 | } 98 | cell.ShowsReorderControl = true; 99 | var stock = rows[indexPath.Row]; 100 | var val = _db.QueryLatestValuation (stock); 101 | cell.TextLabel.Text = stock.Symbol; 102 | cell.DetailTextLabel.Text = val != null ? val.Price.ToString () : "?"; 103 | return cell; 104 | } 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /examples/StocksTouch/StocksView.xib.designer.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Mono Runtime Version: 2.0.50727.1433 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | // ------------------------------------------------------------------------------ 10 | 11 | namespace Stocks.Touch { 12 | 13 | 14 | // Base type probably should be MonoTouch.UIKit.UIViewController or subclass 15 | [MonoTouch.Foundation.Register("StocksView")] 16 | public partial class StocksView { 17 | 18 | [MonoTouch.Foundation.Connect("view")] 19 | private MonoTouch.UIKit.UIView view { 20 | get { 21 | return ((MonoTouch.UIKit.UIView)(this.GetNativeField("view"))); 22 | } 23 | set { 24 | this.SetNativeField("view", value); 25 | } 26 | } 27 | 28 | [MonoTouch.Foundation.Connect("table")] 29 | private MonoTouch.UIKit.UITableView table { 30 | get { 31 | return ((MonoTouch.UIKit.UITableView)(this.GetNativeField("table"))); 32 | } 33 | set { 34 | this.SetNativeField("table", value); 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /lib/metro/ARM/sqlite3.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koush/sqlite-net/18026a6dfa7e5c911fca227aa622a35a79b7ffff/lib/metro/ARM/sqlite3.dll -------------------------------------------------------------------------------- /lib/metro/x64/sqlite3.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koush/sqlite-net/18026a6dfa7e5c911fca227aa622a35a79b7ffff/lib/metro/x64/sqlite3.dll -------------------------------------------------------------------------------- /lib/metro/x86/sqlite3.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koush/sqlite-net/18026a6dfa7e5c911fca227aa622a35a79b7ffff/lib/metro/x86/sqlite3.dll -------------------------------------------------------------------------------- /lib/wp7/Community.CsharpSqlite.WinPhone.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koush/sqlite-net/18026a6dfa7e5c911fca227aa622a35a79b7ffff/lib/wp7/Community.CsharpSqlite.WinPhone.dll -------------------------------------------------------------------------------- /license.md: -------------------------------------------------------------------------------- 1 | License 2 | === 3 | 4 | Copyright (c) 2009-2012 Krueger Systems, Inc. 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. -------------------------------------------------------------------------------- /logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koush/sqlite-net/18026a6dfa7e5c911fca227aa622a35a79b7ffff/logo.gif -------------------------------------------------------------------------------- /logo.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koush/sqlite-net/18026a6dfa7e5c911fca227aa622a35a79b7ffff/logo.psd -------------------------------------------------------------------------------- /nuget/NuGet.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koush/sqlite-net/18026a6dfa7e5c911fca227aa622a35a79b7ffff/nuget/NuGet.exe -------------------------------------------------------------------------------- /nuget/build.bat: -------------------------------------------------------------------------------- 1 | nuget pack sqlite-net.nuspec -o .\ -------------------------------------------------------------------------------- /nuget/sqlite-net.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1.0.7 5 | Frank Krueger 6 | Frank Krueger,Tim Heuer 7 | https://github.com/praeclarum/sqlite-net 8 | sqlite-net 9 | sqlite-net 10 | https://github.com/praeclarum/sqlite-net/blob/master/license.md 11 | false 12 | sqlite-net is an open source, minimal library to allow .NET and Mono applications to store data in SQLite databases. It is written in C# 3.0 and is meant to be simply compiled in with your projects. It was first designed to work with MonoTouch on the iPhone, but should work in any other CLI environment. 13 | sqlite sql monotouch database metro winrt 14 | A .NET client library to access SQLite embedded database files in a LINQ manner. 15 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/SQLite.MonoTouchAdmin.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2009-2010 Krueger Systems, Inc. 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the Software is 9 | // furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | // THE SOFTWARE. 21 | // 22 | 23 | using System; 24 | using System.Collections.Generic; 25 | using System.Linq; 26 | using MonoTouch.Foundation; 27 | using MonoTouch.UIKit; 28 | using System.Drawing; 29 | 30 | 31 | namespace SQLite.MonoTouchAdmin 32 | { 33 | 34 | public class SQLiteAdmin 35 | { 36 | public SQLiteConnection Connection { get; private set; } 37 | 38 | public SQLiteAdmin (SQLiteConnection connection) 39 | { 40 | if (connection == null) 41 | throw new ArgumentNullException ("connection"); 42 | Connection = connection; 43 | } 44 | 45 | public UIViewController NewTablesViewController () 46 | { 47 | var c = new TablesViewController (Connection); 48 | return c; 49 | } 50 | 51 | public static RectangleF GetTableRect() { 52 | return new RectangleF (0, 0, 320, 416); 53 | } 54 | } 55 | 56 | public class TablesViewController : UIViewController 57 | { 58 | public SQLiteConnection Connection { get; private set; } 59 | 60 | public TableMapping[] TableMappings { get; private set; } 61 | public UITableView UITable { get; private set; } 62 | 63 | public Data DataSource { get; private set; } 64 | 65 | public TablesViewController (SQLiteConnection connection) 66 | { 67 | if (connection == null) 68 | throw new ArgumentNullException ("connection"); 69 | Connection = connection; 70 | 71 | TableMappings = Connection.TableMappings.ToArray (); 72 | 73 | UITable = new UITableView (SQLiteAdmin.GetTableRect(), UITableViewStyle.Plain); 74 | DataSource = new Data (this); 75 | UITable.DataSource = DataSource; 76 | UITable.Delegate = new Del (this); 77 | } 78 | 79 | public override void ViewDidLoad () 80 | { 81 | View.AddSubview (UITable); 82 | if (NavigationItem != null) { 83 | NavigationItem.Title = Connection.GetType().Name; 84 | } 85 | } 86 | 87 | public class Del : UITableViewDelegate 88 | { 89 | TablesViewController _c; 90 | public Del (TablesViewController c) 91 | { 92 | _c = c; 93 | } 94 | public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 95 | { 96 | var table = _c.DataSource.GetValue(indexPath); 97 | if (_c.NavigationController != null) { 98 | var c = new TableViewController(table, _c.Connection); 99 | _c.NavigationController.PushViewController(c, true); 100 | } 101 | } 102 | } 103 | 104 | public class Data : UITableViewDataSource 105 | { 106 | TablesViewController _c; 107 | public Data (TablesViewController c) 108 | { 109 | _c = c; 110 | } 111 | public override int NumberOfSections (UITableView tableView) 112 | { 113 | return 1; 114 | } 115 | public override int RowsInSection (UITableView tableview, int section) 116 | { 117 | return _c.TableMappings.Length; 118 | } 119 | public TableMapping GetValue (NSIndexPath indexPath) { 120 | return _c.TableMappings[indexPath.Row]; 121 | } 122 | public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) 123 | { 124 | var cell = tableView.DequeueReusableCell ("table"); 125 | if (cell == null) { 126 | cell = new UITableViewCell (UITableViewCellStyle.Default, "table"); 127 | } 128 | var table = GetValue(indexPath); 129 | cell.TextLabel.Text = table.TableName; 130 | return cell; 131 | } 132 | } 133 | } 134 | 135 | public class TableViewController : UIViewController 136 | { 137 | public SQLiteConnection Connection { get; private set; } 138 | 139 | public TableMapping Table { get; private set; } 140 | public List Rows { get; private set; } 141 | public UITableView UITable { get; private set; } 142 | 143 | int PageSize = 100; 144 | 145 | public TableViewController (TableMapping table, SQLiteConnection connection) 146 | { 147 | if (table == null) 148 | throw new ArgumentNullException ("table"); 149 | if (connection == null) 150 | throw new ArgumentNullException ("connection"); 151 | Table = table; 152 | Connection = connection; 153 | 154 | Rows = new List(); 155 | 156 | UITable = new UITableView (SQLiteAdmin.GetTableRect(), UITableViewStyle.Plain); 157 | UITable.DataSource = new Data (this); 158 | 159 | GetMoreData(); 160 | UITable.ReloadData(); 161 | } 162 | 163 | void GetMoreData() { 164 | var pk = Table.PK; 165 | if (pk == null) { 166 | Rows.AddRange(Connection.Query(Table, 167 | "select * from \"" + Table.TableName + "\"")); 168 | } 169 | else { 170 | var lastId = Rows.Count > 0 ? pk.GetValue(Rows[Rows.Count-1]) : 0; 171 | Rows.AddRange(Connection.Query(Table, 172 | "select * from \"" + Table.TableName + "\"" + 173 | " where \"" + pk.Name + "\" > ? " + 174 | " order by \"" + pk.Name + "\"" + 175 | " limit " + PageSize, lastId)); 176 | } 177 | } 178 | 179 | public override void ViewDidLoad () 180 | { 181 | if (NavigationItem != null) { 182 | NavigationItem.Title = Table.TableName; 183 | } 184 | View.AddSubview(UITable); 185 | } 186 | 187 | public class Data : UITableViewDataSource { 188 | TableViewController _c; 189 | public Data (TableViewController c) 190 | { 191 | _c = c; 192 | } 193 | public override int NumberOfSections (UITableView tableView) 194 | { 195 | return 1; 196 | } 197 | public override int RowsInSection (UITableView tableview, int section) 198 | { 199 | return _c.Rows.Count; 200 | } 201 | public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) 202 | { 203 | var cell = tableView.DequeueReusableCell ("row"); 204 | if (cell == null) { 205 | cell = new UITableViewCell (UITableViewCellStyle.Default, "row"); 206 | } 207 | var row = _c.Rows[indexPath.Row]; 208 | cell.TextLabel.Text = row.ToString(); 209 | return cell; 210 | } 211 | } 212 | } 213 | 214 | } 215 | -------------------------------------------------------------------------------- /src/SQLiteAsync.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2012 Krueger Systems, Inc. 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the Software is 9 | // furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | // THE SOFTWARE. 21 | // 22 | 23 | using System; 24 | using System.Collections; 25 | using System.Collections.Generic; 26 | using System.Linq; 27 | using System.Linq.Expressions; 28 | using System.Threading; 29 | using System.Threading.Tasks; 30 | 31 | namespace SQLite 32 | { 33 | public partial class SQLiteAsyncConnection 34 | { 35 | SQLiteConnectionString _connectionString; 36 | 37 | public SQLiteAsyncConnection (string databasePath, bool storeDateTimeAsTicks = false) 38 | { 39 | _connectionString = new SQLiteConnectionString (databasePath, storeDateTimeAsTicks); 40 | } 41 | 42 | SQLiteConnectionWithLock GetConnection () 43 | { 44 | return SQLiteConnectionPool.Shared.GetConnection (_connectionString); 45 | } 46 | 47 | public Task CreateTableAsync () 48 | where T : new () 49 | { 50 | return CreateTablesAsync (typeof (T)); 51 | } 52 | 53 | public Task CreateTablesAsync () 54 | where T : new () 55 | where T2 : new () 56 | { 57 | return CreateTablesAsync (typeof (T), typeof (T2)); 58 | } 59 | 60 | public Task CreateTablesAsync () 61 | where T : new () 62 | where T2 : new () 63 | where T3 : new () 64 | { 65 | return CreateTablesAsync (typeof (T), typeof (T2), typeof (T3)); 66 | } 67 | 68 | public Task CreateTablesAsync () 69 | where T : new () 70 | where T2 : new () 71 | where T3 : new () 72 | where T4 : new () 73 | { 74 | return CreateTablesAsync (typeof (T), typeof (T2), typeof (T3), typeof (T4)); 75 | } 76 | 77 | public Task CreateTablesAsync () 78 | where T : new () 79 | where T2 : new () 80 | where T3 : new () 81 | where T4 : new () 82 | where T5 : new () 83 | { 84 | return CreateTablesAsync (typeof (T), typeof (T2), typeof (T3), typeof (T4), typeof (T5)); 85 | } 86 | 87 | public Task CreateTablesAsync (params Type[] types) 88 | { 89 | return Task.Factory.StartNew (() => { 90 | CreateTablesResult result = new CreateTablesResult (); 91 | var conn = GetConnection (); 92 | using (conn.Lock ()) { 93 | foreach (Type type in types) { 94 | int aResult = conn.CreateTable (type); 95 | result.Results[type] = aResult; 96 | } 97 | } 98 | return result; 99 | }); 100 | } 101 | 102 | public Task DropTableAsync () 103 | where T : new () 104 | { 105 | return Task.Factory.StartNew (() => { 106 | var conn = GetConnection (); 107 | using (conn.Lock ()) { 108 | return conn.DropTable (); 109 | } 110 | }); 111 | } 112 | 113 | public Task InsertAsync (object item) 114 | { 115 | return Task.Factory.StartNew (() => { 116 | var conn = GetConnection (); 117 | using (conn.Lock ()) { 118 | return conn.Insert (item); 119 | } 120 | }); 121 | } 122 | 123 | public Task UpdateAsync (object item) 124 | { 125 | return Task.Factory.StartNew (() => { 126 | var conn = GetConnection (); 127 | using (conn.Lock ()) { 128 | return conn.Update (item); 129 | } 130 | }); 131 | } 132 | 133 | public Task DeleteAsync (object item) 134 | { 135 | return Task.Factory.StartNew (() => { 136 | var conn = GetConnection (); 137 | using (conn.Lock ()) { 138 | return conn.Delete (item); 139 | } 140 | }); 141 | } 142 | 143 | public Task GetAsync(object pk) 144 | where T : new() 145 | { 146 | return Task.Factory.StartNew(() => 147 | { 148 | var conn = GetConnection(); 149 | using (conn.Lock()) 150 | { 151 | return conn.Get(pk); 152 | } 153 | }); 154 | } 155 | 156 | public Task FindAsync (object pk) 157 | where T : new () 158 | { 159 | return Task.Factory.StartNew (() => { 160 | var conn = GetConnection (); 161 | using (conn.Lock ()) { 162 | return conn.Find (pk); 163 | } 164 | }); 165 | } 166 | 167 | public Task GetAsync (Expression> predicate) 168 | where T : new() 169 | { 170 | return Task.Factory.StartNew(() => 171 | { 172 | var conn = GetConnection(); 173 | using (conn.Lock()) 174 | { 175 | return conn.Get (predicate); 176 | } 177 | }); 178 | } 179 | 180 | public Task FindAsync (Expression> predicate) 181 | where T : new () 182 | { 183 | return Task.Factory.StartNew (() => { 184 | var conn = GetConnection (); 185 | using (conn.Lock ()) { 186 | return conn.Find (predicate); 187 | } 188 | }); 189 | } 190 | 191 | public Task ExecuteAsync (string query, params object[] args) 192 | { 193 | return Task.Factory.StartNew (() => { 194 | var conn = GetConnection (); 195 | using (conn.Lock ()) { 196 | return conn.Execute (query, args); 197 | } 198 | }); 199 | } 200 | 201 | public Task InsertAllAsync (IEnumerable items) 202 | { 203 | return Task.Factory.StartNew (() => { 204 | var conn = GetConnection (); 205 | using (conn.Lock ()) { 206 | return conn.InsertAll (items); 207 | } 208 | }); 209 | } 210 | 211 | [Obsolete("Will cause a deadlock if any call in action ends up in a different thread. Use RunInTransactionAsync(Action) instead.")] 212 | public Task RunInTransactionAsync (Action action) 213 | { 214 | return Task.Factory.StartNew (() => { 215 | var conn = this.GetConnection (); 216 | using (conn.Lock ()) { 217 | conn.BeginTransaction (); 218 | try { 219 | action (this); 220 | conn.Commit (); 221 | } 222 | catch (Exception) { 223 | conn.Rollback (); 224 | throw; 225 | } 226 | } 227 | }); 228 | } 229 | 230 | public Task RunInTransactionAsync(Action action) 231 | { 232 | return Task.Factory.StartNew(() => 233 | { 234 | var conn = this.GetConnection(); 235 | using (conn.Lock()) 236 | { 237 | conn.BeginTransaction(); 238 | try 239 | { 240 | action(conn); 241 | conn.Commit(); 242 | } 243 | catch (Exception) 244 | { 245 | conn.Rollback(); 246 | throw; 247 | } 248 | } 249 | }); 250 | } 251 | 252 | public AsyncTableQuery Table () 253 | where T : new () 254 | { 255 | // 256 | // This isn't async as the underlying connection doesn't go out to the database 257 | // until the query is performed. The Async methods are on the query iteself. 258 | // 259 | var conn = GetConnection (); 260 | return new AsyncTableQuery (conn.Table ()); 261 | } 262 | 263 | public Task ExecuteScalarAsync (string sql, params object[] args) 264 | { 265 | return Task.Factory.StartNew (() => { 266 | var conn = GetConnection (); 267 | using (conn.Lock ()) { 268 | var command = conn.CreateCommand (sql, args); 269 | return command.ExecuteScalar (); 270 | } 271 | }); 272 | } 273 | 274 | public Task> QueryAsync (string sql, params object[] args) 275 | where T : new () 276 | { 277 | return Task>.Factory.StartNew (() => { 278 | var conn = GetConnection (); 279 | using (conn.Lock ()) { 280 | return conn.Query (sql, args); 281 | } 282 | }); 283 | } 284 | } 285 | 286 | // 287 | // TODO: Bind to AsyncConnection.GetConnection instead so that delayed 288 | // execution can still work after a Pool.Reset. 289 | // 290 | public class AsyncTableQuery 291 | where T : new () 292 | { 293 | TableQuery _innerQuery; 294 | 295 | public AsyncTableQuery (TableQuery innerQuery) 296 | { 297 | _innerQuery = innerQuery; 298 | } 299 | 300 | public AsyncTableQuery Where (Expression> predExpr) 301 | { 302 | return new AsyncTableQuery (_innerQuery.Where (predExpr)); 303 | } 304 | 305 | public AsyncTableQuery Skip (int n) 306 | { 307 | return new AsyncTableQuery (_innerQuery.Skip (n)); 308 | } 309 | 310 | public AsyncTableQuery Take (int n) 311 | { 312 | return new AsyncTableQuery (_innerQuery.Take (n)); 313 | } 314 | 315 | public AsyncTableQuery OrderBy (Expression> orderExpr) 316 | { 317 | return new AsyncTableQuery (_innerQuery.OrderBy (orderExpr)); 318 | } 319 | 320 | public AsyncTableQuery OrderByDescending (Expression> orderExpr) 321 | { 322 | return new AsyncTableQuery (_innerQuery.OrderByDescending (orderExpr)); 323 | } 324 | 325 | public Task> ToListAsync () 326 | { 327 | return Task.Factory.StartNew (() => { 328 | using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock ()) { 329 | return _innerQuery.ToList (); 330 | } 331 | }); 332 | } 333 | 334 | public Task CountAsync () 335 | { 336 | return Task.Factory.StartNew (() => { 337 | using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock ()) { 338 | return _innerQuery.Count (); 339 | } 340 | }); 341 | } 342 | 343 | public Task ElementAtAsync (int index) 344 | { 345 | return Task.Factory.StartNew (() => { 346 | using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock ()) { 347 | return _innerQuery.ElementAt (index); 348 | } 349 | }); 350 | } 351 | 352 | public Task FirstAsync () 353 | { 354 | return Task.Factory.StartNew(() => { 355 | using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock ()) { 356 | return _innerQuery.First (); 357 | } 358 | }); 359 | } 360 | 361 | public Task FirstOrDefaultAsync () 362 | { 363 | return Task.Factory.StartNew(() => { 364 | using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock ()) { 365 | return _innerQuery.FirstOrDefault (); 366 | } 367 | }); 368 | } 369 | } 370 | 371 | public class CreateTablesResult 372 | { 373 | public Dictionary Results { get; private set; } 374 | 375 | internal CreateTablesResult () 376 | { 377 | this.Results = new Dictionary (); 378 | } 379 | } 380 | 381 | class SQLiteConnectionPool 382 | { 383 | class Entry 384 | { 385 | public SQLiteConnectionString ConnectionString { get; private set; } 386 | public SQLiteConnectionWithLock Connection { get; private set; } 387 | 388 | public Entry (SQLiteConnectionString connectionString) 389 | { 390 | ConnectionString = connectionString; 391 | Connection = new SQLiteConnectionWithLock (connectionString); 392 | } 393 | 394 | public void OnApplicationSuspended () 395 | { 396 | Connection.Dispose (); 397 | Connection = null; 398 | } 399 | } 400 | 401 | readonly Dictionary _entries = new Dictionary (); 402 | readonly object _entriesLock = new object (); 403 | 404 | static readonly SQLiteConnectionPool _shared = new SQLiteConnectionPool (); 405 | 406 | /// 407 | /// Gets the singleton instance of the connection tool. 408 | /// 409 | public static SQLiteConnectionPool Shared 410 | { 411 | get 412 | { 413 | return _shared; 414 | } 415 | } 416 | 417 | public SQLiteConnectionWithLock GetConnection (SQLiteConnectionString connectionString) 418 | { 419 | lock (_entriesLock) { 420 | Entry entry; 421 | string key = connectionString.ConnectionString; 422 | 423 | if (!_entries.TryGetValue (key, out entry)) { 424 | entry = new Entry (connectionString); 425 | _entries[key] = entry; 426 | } 427 | 428 | return entry.Connection; 429 | } 430 | } 431 | 432 | /// 433 | /// Closes all connections managed by this pool. 434 | /// 435 | public void Reset () 436 | { 437 | lock (_entriesLock) { 438 | foreach (var entry in _entries.Values) { 439 | entry.OnApplicationSuspended (); 440 | } 441 | _entries.Clear (); 442 | } 443 | } 444 | 445 | /// 446 | /// Call this method when the application is suspended. 447 | /// 448 | /// Behaviour here is to close any open connections. 449 | public void ApplicationSuspended () 450 | { 451 | Reset (); 452 | } 453 | } 454 | 455 | class SQLiteConnectionWithLock : SQLiteConnection 456 | { 457 | readonly object _lockPoint = new object (); 458 | 459 | public SQLiteConnectionWithLock (SQLiteConnectionString connectionString) 460 | : base (connectionString.DatabasePath, connectionString.StoreDateTimeAsTicks) 461 | { 462 | } 463 | 464 | public IDisposable Lock () 465 | { 466 | return new LockWrapper (_lockPoint); 467 | } 468 | 469 | private class LockWrapper : IDisposable 470 | { 471 | object _lockPoint; 472 | 473 | public LockWrapper (object lockPoint) 474 | { 475 | _lockPoint = lockPoint; 476 | Monitor.Enter (_lockPoint); 477 | } 478 | 479 | public void Dispose () 480 | { 481 | Monitor.Exit (_lockPoint); 482 | } 483 | } 484 | } 485 | } 486 | 487 | -------------------------------------------------------------------------------- /tests/BooleanTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using SQLite; 7 | 8 | using System.Diagnostics; 9 | 10 | #if NETFX_CORE 11 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 12 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 13 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 14 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 15 | #else 16 | using NUnit.Framework; 17 | #endif 18 | 19 | 20 | namespace SQLite.Tests 21 | { 22 | [TestFixture] 23 | public class BooleanTest 24 | { 25 | public class VO 26 | { 27 | [AutoIncrement, PrimaryKey] 28 | public int ID { get; set; } 29 | public bool Flag { get; set; } 30 | public String Text { get; set; } 31 | 32 | public override string ToString() 33 | { 34 | return string.Format("VO:: ID:{0} Flag:{1} Text:{2}", ID, Flag, Text); 35 | } 36 | } 37 | public class DbAcs : SQLiteConnection 38 | { 39 | public DbAcs(String path) 40 | : base(path) 41 | { 42 | } 43 | 44 | public void buildTable() 45 | { 46 | CreateTable(); 47 | } 48 | 49 | public int CountWithFlag(Boolean flag) 50 | { 51 | var cmd = CreateCommand("SELECT COUNT(*) FROM VO Where Flag = ?", flag); 52 | return cmd.ExecuteScalar(); 53 | } 54 | } 55 | 56 | [Test] 57 | public void TestBoolean() 58 | { 59 | var tmpFile = TestPath.GetTempFileName(); 60 | var db = new DbAcs(tmpFile); 61 | db.buildTable(); 62 | for (int i = 0; i < 10; i++) 63 | db.Insert(new VO() { Flag = (i % 3 == 0), Text = String.Format("VO{0}", i) }); 64 | 65 | // count vo which flag is true 66 | Assert.AreEqual(4, db.CountWithFlag(true)); 67 | Assert.AreEqual(6, db.CountWithFlag(false)); 68 | 69 | Debug.WriteLine("VO with true flag:"); 70 | foreach (var vo in db.Query("SELECT * FROM VO Where Flag = ?", true)) 71 | Debug.WriteLine (vo.ToString ()); 72 | 73 | Debug.WriteLine ("VO with false flag:"); 74 | foreach (var vo in db.Query("SELECT * FROM VO Where Flag = ?", false)) 75 | Debug.WriteLine (vo.ToString ()); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /tests/ByteArrayTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using SQLite; 7 | 8 | #if NETFX_CORE 9 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 10 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 11 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 12 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 13 | #else 14 | using NUnit.Framework; 15 | #endif 16 | 17 | 18 | namespace SQLite.Tests 19 | { 20 | [TestFixture] 21 | public class ByteArrayTest 22 | { 23 | public class ByteArrayClass 24 | { 25 | [PrimaryKey, AutoIncrement] 26 | public int ID { get; set; } 27 | 28 | public byte[] bytes { get; set; } 29 | 30 | public void AssertEquals(ByteArrayClass other) 31 | { 32 | Assert.AreEqual(other.ID, ID); 33 | if (other.bytes == null || bytes == null) { 34 | Assert.IsNull (other.bytes); 35 | Assert.IsNull (bytes); 36 | } 37 | else { 38 | Assert.AreEqual(other.bytes.Length, bytes.Length); 39 | for (var i = 0; i < bytes.Length; i++) { 40 | Assert.AreEqual(other.bytes[i], bytes[i]); 41 | } 42 | } 43 | } 44 | } 45 | 46 | [Test] 47 | [Description("Create objects with various byte arrays and check they can be stored and retrieved correctly")] 48 | public void ByteArrays() 49 | { 50 | //Byte Arrays for comparisson 51 | ByteArrayClass[] byteArrays = new ByteArrayClass[] { 52 | new ByteArrayClass() { bytes = new byte[] { 1, 2, 3, 4, 250, 252, 253, 254, 255 } }, //Range check 53 | new ByteArrayClass() { bytes = new byte[] { 0 } }, //null bytes need to be handled correctly 54 | new ByteArrayClass() { bytes = new byte[] { 0, 0 } }, 55 | new ByteArrayClass() { bytes = new byte[] { 0, 1, 0 } }, 56 | new ByteArrayClass() { bytes = new byte[] { 1, 0, 1 } }, 57 | new ByteArrayClass() { bytes = new byte[] { } }, //Empty byte array should stay empty (and not become null) 58 | new ByteArrayClass() { bytes = null } //Null should be supported 59 | }; 60 | 61 | SQLiteConnection database = new SQLiteConnection(TestPath.GetTempFileName()); 62 | database.CreateTable(); 63 | 64 | //Insert all of the ByteArrayClass 65 | foreach (ByteArrayClass b in byteArrays) 66 | database.Insert(b); 67 | 68 | //Get them back out 69 | ByteArrayClass[] fetchedByteArrays = database.Table().OrderBy(x => x.ID).ToArray(); 70 | 71 | Assert.AreEqual(fetchedByteArrays.Length, byteArrays.Length); 72 | //Check they are the same 73 | for (int i = 0; i < byteArrays.Length; i++) 74 | { 75 | byteArrays[i].AssertEquals(fetchedByteArrays[i]); 76 | } 77 | } 78 | 79 | [Test] 80 | [Description("Create A large byte array and check it can be stored and retrieved correctly")] 81 | public void LargeByteArray() 82 | { 83 | const int byteArraySize = 1024 * 1024; 84 | byte[] bytes = new byte[byteArraySize]; 85 | for (int i = 0; i < byteArraySize; i++) 86 | bytes[i] = (byte)(i % 256); 87 | 88 | ByteArrayClass byteArray = new ByteArrayClass() { bytes = bytes }; 89 | 90 | SQLiteConnection database = new SQLiteConnection(TestPath.GetTempFileName()); 91 | database.CreateTable(); 92 | 93 | //Insert the ByteArrayClass 94 | database.Insert(byteArray); 95 | 96 | //Get it back out 97 | ByteArrayClass[] fetchedByteArrays = database.Table().ToArray(); 98 | 99 | Assert.AreEqual(fetchedByteArrays.Length, 1); 100 | 101 | //Check they are the same 102 | byteArray.AssertEquals(fetchedByteArrays[0]); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /tests/CollateTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using SQLite; 7 | 8 | #if NETFX_CORE 9 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 10 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 11 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 12 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 13 | #else 14 | using NUnit.Framework; 15 | #endif 16 | 17 | using System.Diagnostics; 18 | 19 | namespace SQLite.Tests 20 | { 21 | [TestFixture] 22 | public class CollateTest 23 | { 24 | public class TestObj 25 | { 26 | [AutoIncrement, PrimaryKey] 27 | public int Id { get; set; } 28 | 29 | public string CollateDefault { get; set; } 30 | 31 | [Collation("BINARY")] 32 | public string CollateBinary { get; set; } 33 | 34 | [Collation("RTRIM")] 35 | public string CollateRTrim { get; set; } 36 | 37 | [Collation("NOCASE")] 38 | public string CollateNoCase { get; set; } 39 | 40 | public override string ToString () 41 | { 42 | return string.Format("[TestObj: Id={0}]", Id); 43 | } 44 | } 45 | 46 | public class TestDb : SQLiteConnection 47 | { 48 | public TestDb(String path) 49 | : base(path) 50 | { 51 | Trace = true; 52 | CreateTable(); 53 | } 54 | } 55 | 56 | [Test] 57 | public void Collate() 58 | { 59 | var obj = new TestObj() { 60 | CollateDefault = "Alpha ", 61 | CollateBinary = "Alpha ", 62 | CollateRTrim = "Alpha ", 63 | CollateNoCase = "Alpha ", 64 | }; 65 | 66 | var db = new TestDb(TestPath.GetTempFileName()); 67 | 68 | db.Insert(obj); 69 | 70 | Assert.AreEqual(1, (from o in db.Table() where o.CollateDefault == "Alpha " select o).Count()); 71 | Assert.AreEqual(0, (from o in db.Table() where o.CollateDefault == "ALPHA " select o).Count()); 72 | Assert.AreEqual(0, (from o in db.Table() where o.CollateDefault == "Alpha" select o).Count()); 73 | Assert.AreEqual(0, (from o in db.Table() where o.CollateDefault == "ALPHA" select o).Count()); 74 | 75 | Assert.AreEqual(1, (from o in db.Table() where o.CollateBinary == "Alpha " select o).Count()); 76 | Assert.AreEqual(0, (from o in db.Table() where o.CollateBinary == "ALPHA " select o).Count()); 77 | Assert.AreEqual(0, (from o in db.Table() where o.CollateBinary == "Alpha" select o).Count()); 78 | Assert.AreEqual(0, (from o in db.Table() where o.CollateBinary == "ALPHA" select o).Count()); 79 | 80 | Assert.AreEqual(1, (from o in db.Table() where o.CollateRTrim == "Alpha " select o).Count()); 81 | Assert.AreEqual(0, (from o in db.Table() where o.CollateRTrim == "ALPHA " select o).Count()); 82 | Assert.AreEqual(1, (from o in db.Table() where o.CollateRTrim == "Alpha" select o).Count()); 83 | Assert.AreEqual(0, (from o in db.Table() where o.CollateRTrim == "ALPHA" select o).Count()); 84 | 85 | Assert.AreEqual(1, (from o in db.Table() where o.CollateNoCase == "Alpha " select o).Count()); 86 | Assert.AreEqual(1, (from o in db.Table() where o.CollateNoCase == "ALPHA " select o).Count()); 87 | Assert.AreEqual(0, (from o in db.Table() where o.CollateNoCase == "Alpha" select o).Count()); 88 | Assert.AreEqual(0, (from o in db.Table() where o.CollateNoCase == "ALPHA" select o).Count()); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /tests/ConnectionTrackingTest.cs: -------------------------------------------------------------------------------- 1 | 2 | using System; 3 | using System.IO; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using SQLite; 8 | 9 | #if NETFX_CORE 10 | using Microsoft.VisualStudio.TestTools.UnitTesting; 11 | using SetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute; 12 | using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; 13 | using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; 14 | #else 15 | using NUnit.Framework; 16 | #endif 17 | 18 | 19 | namespace SQLite.Tests 20 | { 21 | [TestFixture] 22 | public class ConnectionTrackingTest 23 | { 24 | public class Product 25 | { 26 | [AutoIncrement, PrimaryKey] 27 | public int Id { get; set; } 28 | public string Name { get; set; } 29 | public decimal Price { get; set; } 30 | 31 | public TestDb Connection { get; private set; } 32 | 33 | public OrderLine[] GetOrderLines () 34 | { 35 | return Connection.Table ().Where (o => o.ProductId == Id).ToArray (); 36 | } 37 | } 38 | public class OrderLine 39 | { 40 | [AutoIncrement, PrimaryKey] 41 | public int Id { get; set; } 42 | public int ProductId { get; set; } 43 | public int Quantity { get; set; } 44 | public decimal UnitPrice { get; set; } 45 | 46 | public TestDb Connection { get; private set; } 47 | } 48 | 49 | public class TestDb : SQLiteConnection 50 | { 51 | public TestDb () : base(TestPath.GetTempFileName ()) 52 | { 53 | CreateTable (); 54 | CreateTable (); 55 | Trace = true; 56 | } 57 | } 58 | 59 | [Test] 60 | public void CreateThem () 61 | { 62 | var db = new TestDb (); 63 | 64 | var foo = new Product { Name = "Foo", Price = 10.0m }; 65 | var bar = new Product { Name = "Bar", Price = 0.10m }; 66 | db.Insert (foo); 67 | db.Insert (bar); 68 | db.Insert (new OrderLine { ProductId = foo.Id, Quantity = 6, UnitPrice = 10.01m }); 69 | db.Insert (new OrderLine { ProductId = foo.Id, Quantity = 3, UnitPrice = 0.02m }); 70 | db.Insert (new OrderLine { ProductId = bar.Id, Quantity = 9, UnitPrice = 100.01m }); 71 | 72 | var lines = foo.GetOrderLines (); 73 | 74 | Assert.AreEqual (lines.Length, 2, "Has 2 order lines"); 75 | Assert.AreEqual (foo.Connection, db, "foo.Connection was set"); 76 | Assert.AreEqual (lines[0].Connection, db, "lines[0].Connection was set"); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /tests/ContainsTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using SQLite; 7 | 8 | #if NETFX_CORE 9 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 10 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 11 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 12 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 13 | #else 14 | using NUnit.Framework; 15 | #endif 16 | 17 | using System.Diagnostics; 18 | 19 | namespace SQLite.Tests 20 | { 21 | [TestFixture] 22 | public class ContainsTest 23 | { 24 | public class TestObj 25 | { 26 | [AutoIncrement, PrimaryKey] 27 | public int Id { get; set; } 28 | 29 | public string Name { get; set; } 30 | 31 | public override string ToString () 32 | { 33 | return string.Format("[TestObj: Id={0}, Name={1}]", Id, Name); 34 | } 35 | } 36 | 37 | public class TestDb : SQLiteConnection 38 | { 39 | public TestDb(String path) 40 | : base(path) 41 | { 42 | CreateTable(); 43 | } 44 | } 45 | 46 | [Test] 47 | public void ContainsConstantData() 48 | { 49 | int n = 20; 50 | var cq =from i in Enumerable.Range(1, n) 51 | select new TestObj() { 52 | Name = i.ToString() 53 | }; 54 | 55 | var db = new TestDb(TestPath.GetTempFileName()); 56 | 57 | db.InsertAll(cq); 58 | 59 | db.Trace = true; 60 | 61 | var tensq = new string[] { "0", "10", "20" }; 62 | var tens = (from o in db.Table() where tensq.Contains(o.Name) select o).ToList(); 63 | Assert.AreEqual(2, tens.Count); 64 | 65 | var moreq = new string[] { "0", "x", "99", "10", "20", "234324" }; 66 | var more = (from o in db.Table() where moreq.Contains(o.Name) select o).ToList(); 67 | Assert.AreEqual(2, more.Count); 68 | } 69 | 70 | [Test] 71 | public void ContainsQueriedData() 72 | { 73 | int n = 20; 74 | var cq =from i in Enumerable.Range(1, n) 75 | select new TestObj() { 76 | Name = i.ToString() 77 | }; 78 | 79 | var db = new TestDb(TestPath.GetTempFileName()); 80 | 81 | db.InsertAll(cq); 82 | 83 | db.Trace = true; 84 | 85 | var tensq = new string[] { "0", "10", "20" }; 86 | var tens = (from o in db.Table() where tensq.Contains(o.Name) select o).ToList(); 87 | Assert.AreEqual(2, tens.Count); 88 | 89 | var moreq = new string[] { "0", "x", "99", "10", "20", "234324" }; 90 | var more = (from o in db.Table() where moreq.Contains(o.Name) select o).ToList(); 91 | Assert.AreEqual(2, more.Count); 92 | 93 | // https://github.com/praeclarum/sqlite-net/issues/28 94 | var moreq2 = moreq.ToList (); 95 | var more2 = (from o in db.Table() where moreq2.Contains(o.Name) select o).ToList(); 96 | Assert.AreEqual(2, more2.Count); 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /tests/CreateTableImplicitTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Linq.Expressions; 5 | 6 | #if NETFX_CORE 7 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 8 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 9 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 10 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 11 | #else 12 | using NUnit.Framework; 13 | #endif 14 | 15 | namespace SQLite.Tests 16 | { 17 | [TestFixture] 18 | public class CreateTableImplicitTest 19 | { 20 | 21 | class NoAttributes 22 | { 23 | public int Id { get; set; } 24 | public string AColumn { get; set; } 25 | public int IndexedId { get; set; } 26 | } 27 | 28 | class PkAttribute 29 | { 30 | [PrimaryKey] 31 | public int Id { get; set; } 32 | public string AColumn { get; set; } 33 | public int IndexedId { get; set; } 34 | } 35 | 36 | private void CheckPK(TestDb db) 37 | { 38 | for (int i = 1; i <= 10; i++) 39 | { 40 | var na = new NoAttributes { Id = i, AColumn = i.ToString(), IndexedId = 0 }; 41 | db.Insert(na); 42 | } 43 | var item = db.Get(2); 44 | Assert.IsNotNull(item); 45 | Assert.AreEqual(2, item.Id); 46 | } 47 | 48 | [Test] 49 | public void WithoutImplicitMapping () 50 | { 51 | var db = new TestDb (); 52 | 53 | db.CreateTable(); 54 | 55 | var mapping = db.GetMapping(); 56 | 57 | Assert.IsNull (mapping.PK); 58 | 59 | var column = mapping.Columns[2]; 60 | Assert.AreEqual("IndexedId", column.Name); 61 | Assert.IsFalse(column.Indices.Any()); 62 | 63 | Assert.Throws(typeof(AssertionException), () => CheckPK(db)); 64 | } 65 | 66 | [Test] 67 | public void ImplicitPK() 68 | { 69 | var db = new TestDb(); 70 | 71 | db.CreateTable(CreateFlags.ImplicitPK); 72 | 73 | var mapping = db.GetMapping(); 74 | 75 | Assert.IsNotNull(mapping.PK); 76 | Assert.AreEqual("Id", mapping.PK.Name); 77 | Assert.IsTrue(mapping.PK.IsPK); 78 | Assert.IsFalse(mapping.PK.IsAutoInc); 79 | 80 | CheckPK(db); 81 | } 82 | 83 | 84 | [Test] 85 | public void ImplicitAutoInc() 86 | { 87 | var db = new TestDb(); 88 | 89 | db.CreateTable(CreateFlags.AutoIncPK); 90 | 91 | var mapping = db.GetMapping(); 92 | 93 | Assert.IsNotNull(mapping.PK); 94 | Assert.AreEqual("Id", mapping.PK.Name); 95 | Assert.IsTrue(mapping.PK.IsPK); 96 | Assert.IsTrue(mapping.PK.IsAutoInc); 97 | } 98 | 99 | [Test] 100 | public void ImplicitIndex() 101 | { 102 | var db = new TestDb(); 103 | 104 | db.CreateTable(CreateFlags.ImplicitIndex); 105 | 106 | var mapping = db.GetMapping(); 107 | var column = mapping.Columns[2]; 108 | Assert.AreEqual("IndexedId", column.Name); 109 | Assert.IsTrue(column.Indices.Any()); 110 | } 111 | 112 | [Test] 113 | public void ImplicitPKAutoInc() 114 | { 115 | var db = new TestDb(); 116 | 117 | db.CreateTable(typeof(NoAttributes), CreateFlags.ImplicitPK | CreateFlags.AutoIncPK); 118 | 119 | var mapping = db.GetMapping(); 120 | 121 | Assert.IsNotNull(mapping.PK); 122 | Assert.AreEqual("Id", mapping.PK.Name); 123 | Assert.IsTrue(mapping.PK.IsPK); 124 | Assert.IsTrue(mapping.PK.IsAutoInc); 125 | } 126 | 127 | [Test] 128 | public void ImplicitAutoIncAsPassedInTypes() 129 | { 130 | var db = new TestDb(); 131 | 132 | db.CreateTable(typeof(PkAttribute), CreateFlags.AutoIncPK); 133 | 134 | var mapping = db.GetMapping(); 135 | 136 | Assert.IsNotNull(mapping.PK); 137 | Assert.AreEqual("Id", mapping.PK.Name); 138 | Assert.IsTrue(mapping.PK.IsPK); 139 | Assert.IsTrue(mapping.PK.IsAutoInc); 140 | } 141 | 142 | [Test] 143 | public void ImplicitPkAsPassedInTypes() 144 | { 145 | var db = new TestDb(); 146 | 147 | db.CreateTable(typeof(NoAttributes), CreateFlags.ImplicitPK); 148 | 149 | var mapping = db.GetMapping(); 150 | 151 | Assert.IsNotNull(mapping.PK); 152 | Assert.AreEqual("Id", mapping.PK.Name); 153 | Assert.IsTrue(mapping.PK.IsPK); 154 | Assert.IsFalse(mapping.PK.IsAutoInc); 155 | } 156 | 157 | [Test] 158 | public void ImplicitPKAutoIncAsPassedInTypes() 159 | { 160 | var db = new TestDb(); 161 | 162 | db.CreateTable(typeof(NoAttributes), CreateFlags.ImplicitPK | CreateFlags.AutoIncPK); 163 | 164 | var mapping = db.GetMapping(); 165 | 166 | Assert.IsNotNull(mapping.PK); 167 | Assert.AreEqual("Id", mapping.PK.Name); 168 | Assert.IsTrue(mapping.PK.IsPK); 169 | Assert.IsTrue(mapping.PK.IsAutoInc); 170 | } 171 | } 172 | } 173 | 174 | -------------------------------------------------------------------------------- /tests/CreateTableTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | #if NETFX_CORE 5 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 6 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 7 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 8 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 9 | #else 10 | using NUnit.Framework; 11 | #endif 12 | 13 | 14 | namespace SQLite.Tests 15 | { 16 | [TestFixture] 17 | public class CreateTableTest 18 | { 19 | [Test] 20 | public void CreateThem () 21 | { 22 | var db = new TestDb (); 23 | 24 | db.CreateTable (); 25 | db.CreateTable (); 26 | db.CreateTable (); 27 | db.CreateTable (); 28 | 29 | VerifyCreations(db); 30 | } 31 | 32 | [Test] 33 | public void CreateAsPassedInTypes () 34 | { 35 | var db = new TestDb(); 36 | 37 | db.CreateTable(typeof(Product)); 38 | db.CreateTable(typeof(Order)); 39 | db.CreateTable(typeof(OrderLine)); 40 | db.CreateTable(typeof(OrderHistory)); 41 | 42 | VerifyCreations(db); 43 | } 44 | 45 | [Test] 46 | public void CreateTwice () 47 | { 48 | var db = new TestDb (); 49 | 50 | db.CreateTable (); 51 | db.CreateTable (); 52 | db.CreateTable (); 53 | db.CreateTable (); 54 | db.CreateTable (); 55 | 56 | VerifyCreations(db); 57 | } 58 | 59 | private static void VerifyCreations(TestDb db) 60 | { 61 | var orderLine = db.GetMapping(typeof(OrderLine)); 62 | Assert.AreEqual(6, orderLine.Columns.Length); 63 | 64 | var l = new OrderLine() 65 | { 66 | Status = OrderLineStatus.Shipped 67 | }; 68 | db.Insert(l); 69 | var lo = db.Table().First(x => x.Status == OrderLineStatus.Shipped); 70 | Assert.AreEqual(lo.Id, l.Id); 71 | } 72 | 73 | class Issue115_MyObject 74 | { 75 | [PrimaryKey] 76 | public string UniqueId { get; set; } 77 | public byte OtherValue { get; set; } 78 | } 79 | 80 | [Test] 81 | public void Issue115_MissingPrimaryKey () 82 | { 83 | using (var conn = new TestDb ()) { 84 | 85 | conn.CreateTable (); 86 | conn.InsertAll (from i in Enumerable.Range (0, 10) select new Issue115_MyObject { 87 | UniqueId = i.ToString (), 88 | OtherValue = (byte)(i * 10), 89 | }); 90 | 91 | var query = conn.Table (); 92 | foreach (var itm in query) { 93 | itm.OtherValue++; 94 | Assert.AreEqual (1, conn.Update (itm, typeof(Issue115_MyObject))); 95 | } 96 | } 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /tests/DateTimeTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | #if NETFX_CORE 4 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 5 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 6 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 7 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 8 | #else 9 | using NUnit.Framework; 10 | #endif 11 | 12 | namespace SQLite.Tests 13 | { 14 | [TestFixture] 15 | public class DateTimeTest 16 | { 17 | class TestObj 18 | { 19 | [PrimaryKey, AutoIncrement] 20 | public int Id { get; set; } 21 | 22 | public string Name { get; set; } 23 | public DateTime ModifiedTime { get; set; } 24 | } 25 | 26 | 27 | [Test] 28 | public void AsTicks () 29 | { 30 | var db = new TestDb (storeDateTimeAsTicks: true); 31 | TestDateTime (db); 32 | } 33 | 34 | [Test] 35 | public void AsStrings () 36 | { 37 | var db = new TestDb (storeDateTimeAsTicks: false); 38 | TestDateTime (db); 39 | } 40 | 41 | [Test] 42 | public void AsyncAsTicks () 43 | { 44 | var db = new SQLiteAsyncConnection (TestPath.GetTempFileName (), true); 45 | TestAsyncDateTime (db); 46 | } 47 | 48 | [Test] 49 | public void AsyncAsString () 50 | { 51 | var db = new SQLiteAsyncConnection (TestPath.GetTempFileName (), false); 52 | TestAsyncDateTime (db); 53 | } 54 | 55 | void TestAsyncDateTime (SQLiteAsyncConnection db) 56 | { 57 | db.CreateTableAsync ().Wait (); 58 | 59 | TestObj o, o2; 60 | 61 | // 62 | // Ticks 63 | // 64 | o = new TestObj { 65 | ModifiedTime = new DateTime (2012, 1, 14, 3, 2, 1), 66 | }; 67 | db.InsertAsync (o).Wait (); 68 | o2 = db.GetAsync (o.Id).Result; 69 | Assert.AreEqual (o.ModifiedTime, o2.ModifiedTime); 70 | } 71 | 72 | void TestDateTime (TestDb db) 73 | { 74 | db.CreateTable (); 75 | 76 | TestObj o, o2; 77 | 78 | // 79 | // Ticks 80 | // 81 | o = new TestObj { 82 | ModifiedTime = new DateTime (2012, 1, 14, 3, 2, 1), 83 | }; 84 | db.Insert (o); 85 | o2 = db.Get (o.Id); 86 | Assert.AreEqual (o.ModifiedTime, o2.ModifiedTime); 87 | } 88 | } 89 | } 90 | 91 | -------------------------------------------------------------------------------- /tests/DeleteTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | #if NETFX_CORE 5 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 6 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 7 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 8 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 9 | #else 10 | using NUnit.Framework; 11 | #endif 12 | 13 | namespace SQLite.Tests 14 | { 15 | [TestFixture] 16 | public class DeleteTest 17 | { 18 | class TestTable 19 | { 20 | [PrimaryKey, AutoIncrement] 21 | public int Id { get; set; } 22 | public int Datum { get; set; } 23 | } 24 | 25 | const int Count = 100; 26 | 27 | SQLiteConnection CreateDb () 28 | { 29 | var db = new TestDb (); 30 | db.CreateTable (); 31 | var items = from i in Enumerable.Range (0, Count) 32 | select new TestTable { Datum = 1000+i }; 33 | db.InsertAll (items); 34 | Assert.AreEqual (Count, db.Table ().Count ()); 35 | return db; 36 | } 37 | 38 | [Test] 39 | public void DeleteEntityOne () 40 | { 41 | var db = CreateDb (); 42 | 43 | var r = db.Delete (db.Get (1)); 44 | 45 | Assert.AreEqual (1, r); 46 | Assert.AreEqual (Count - 1, db.Table ().Count ()); 47 | } 48 | 49 | [Test] 50 | public void DeletePKOne () 51 | { 52 | var db = CreateDb (); 53 | 54 | var r = db.Delete (1); 55 | 56 | Assert.AreEqual (1, r); 57 | Assert.AreEqual (Count - 1, db.Table ().Count ()); 58 | } 59 | 60 | [Test] 61 | public void DeletePKNone () 62 | { 63 | var db = CreateDb (); 64 | 65 | var r = db.Delete (348597); 66 | 67 | Assert.AreEqual (0, r); 68 | Assert.AreEqual (Count, db.Table ().Count ()); 69 | } 70 | 71 | [Test] 72 | public void DeleteAll () 73 | { 74 | var db = CreateDb (); 75 | 76 | var r = db.DeleteAll (); 77 | 78 | Assert.AreEqual (Count, r); 79 | Assert.AreEqual (0, db.Table ().Count ()); 80 | } 81 | } 82 | } 83 | 84 | -------------------------------------------------------------------------------- /tests/DropTableTest.cs: -------------------------------------------------------------------------------- 1 | 2 | using System; 3 | using System.IO; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using SQLite; 8 | 9 | #if NETFX_CORE 10 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 11 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 12 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 13 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 14 | #else 15 | using NUnit.Framework; 16 | #endif 17 | 18 | 19 | namespace SQLite.Tests 20 | { 21 | [TestFixture] 22 | public class DropTableTest 23 | { 24 | public class Product 25 | { 26 | [AutoIncrement, PrimaryKey] 27 | public int Id { get; set; } 28 | public string Name { get; set; } 29 | public decimal Price { get; set; } 30 | } 31 | 32 | public class TestDb : SQLiteConnection 33 | { 34 | public TestDb () : base(TestPath.GetTempFileName ()) 35 | { 36 | Trace = true; 37 | } 38 | } 39 | 40 | [Test] 41 | public void CreateInsertDrop () 42 | { 43 | var db = new TestDb (); 44 | 45 | db.CreateTable (); 46 | 47 | db.Insert (new Product { 48 | Name = "Hello", 49 | Price = 16, 50 | }); 51 | 52 | var n = db.Table ().Count (); 53 | 54 | Assert.AreEqual (1, n); 55 | 56 | db.DropTable (); 57 | 58 | ExceptionAssert.Throws(() => db.Table ().Count ()); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /tests/EqualsTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | #if NETFX_CORE 4 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 5 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 6 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 7 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 8 | #else 9 | using NUnit.Framework; 10 | #endif 11 | 12 | namespace SQLite.Tests 13 | { 14 | [TestFixture] 15 | class EqualsTest 16 | { 17 | public abstract class TestObjBase 18 | { 19 | [AutoIncrement, PrimaryKey] 20 | public int Id { get; set; } 21 | 22 | public T Data { get; set; } 23 | 24 | public DateTime Date { get; set; } 25 | } 26 | 27 | public class TestObjString : TestObjBase { } 28 | 29 | public class TestDb : SQLiteConnection 30 | { 31 | public TestDb(String path) 32 | : base(path) 33 | { 34 | CreateTable(); 35 | } 36 | } 37 | 38 | [Test] 39 | public void CanCompareAnyField() 40 | { 41 | var n = 20; 42 | var cq =from i in Enumerable.Range(1, n) 43 | select new TestObjString { 44 | Data = Convert.ToString(i), 45 | Date = new DateTime(2013, 1, i) 46 | }; 47 | 48 | var db = new TestDb(TestPath.GetTempFileName()); 49 | db.InsertAll(cq); 50 | 51 | var results = db.Table().Where(o => o.Data.Equals("10")); 52 | Assert.AreEqual(results.Count(), 1); 53 | Assert.AreEqual(results.FirstOrDefault().Data, "10"); 54 | 55 | results = db.Table().Where(o => o.Id.Equals(10)); 56 | Assert.AreEqual(results.Count(), 1); 57 | Assert.AreEqual(results.FirstOrDefault().Data, "10"); 58 | 59 | var date = new DateTime(2013, 1, 10); 60 | results = db.Table().Where(o => o.Date.Equals(date)); 61 | Assert.AreEqual(results.Count(), 1); 62 | Assert.AreEqual(results.FirstOrDefault().Data, "10"); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /tests/ExceptionAssert.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | #if NETFX_CORE 8 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 9 | #else 10 | using NUnit.Framework; 11 | #endif 12 | 13 | namespace SQLite.Tests 14 | { 15 | public class ExceptionAssert 16 | { 17 | public static T Throws(Action action) where T : Exception 18 | { 19 | try 20 | { 21 | action(); 22 | } 23 | catch (T ex) 24 | { 25 | return ex; 26 | } 27 | 28 | Assert.Fail("Expected exception of type {0}.", typeof(T)); 29 | 30 | return null; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/GuidTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | #if NETFX_CORE 8 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 9 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 10 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 11 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 12 | #else 13 | using NUnit.Framework; 14 | #endif 15 | 16 | 17 | namespace SQLite.Tests { 18 | [TestFixture] 19 | public class GuidTests { 20 | public class TestObj { 21 | [PrimaryKey] 22 | public Guid Id { get; set; } 23 | public String Text { get; set; } 24 | 25 | public override string ToString() { 26 | return string.Format("[TestObj: Id={0}, Text={1}]", Id, Text); 27 | } 28 | 29 | } 30 | 31 | public class TestDb : SQLiteConnection { 32 | public TestDb(String path) 33 | : base(path) { 34 | CreateTable(); 35 | } 36 | } 37 | 38 | [Test] 39 | public void ShouldPersistAndReadGuid() { 40 | var db = new TestDb(TestPath.GetTempFileName()); 41 | 42 | var obj1 = new TestObj() { Id=new Guid("36473164-C9E4-4CDF-B266-A0B287C85623"), Text = "First Guid Object" }; 43 | var obj2 = new TestObj() { Id=new Guid("BC5C4C4A-CA57-4B61-8B53-9FD4673528B6"), Text = "Second Guid Object" }; 44 | 45 | var numIn1 = db.Insert(obj1); 46 | var numIn2 = db.Insert(obj2); 47 | Assert.AreEqual(1, numIn1); 48 | Assert.AreEqual(1, numIn2); 49 | 50 | var result = db.Query("select * from TestObj").ToList(); 51 | Assert.AreEqual(2, result.Count); 52 | Assert.AreEqual(obj1.Text, result[0].Text); 53 | Assert.AreEqual(obj2.Text, result[1].Text); 54 | 55 | Assert.AreEqual(obj1.Id, result[0].Id); 56 | Assert.AreEqual(obj2.Id, result[1].Id); 57 | 58 | db.Close(); 59 | } 60 | 61 | [Test] 62 | public void AutoGuid_HasGuid() 63 | { 64 | var db = new SQLiteConnection(TestPath.GetTempFileName()); 65 | db.CreateTable(CreateFlags.AutoIncPK); 66 | 67 | var guid1 = new Guid("36473164-C9E4-4CDF-B266-A0B287C85623"); 68 | var guid2 = new Guid("BC5C4C4A-CA57-4B61-8B53-9FD4673528B6"); 69 | 70 | var obj1 = new TestObj() { Id = guid1, Text = "First Guid Object" }; 71 | var obj2 = new TestObj() { Id = guid2, Text = "Second Guid Object" }; 72 | 73 | var numIn1 = db.Insert(obj1); 74 | var numIn2 = db.Insert(obj2); 75 | Assert.AreEqual(guid1, obj1.Id); 76 | Assert.AreEqual(guid2, obj2.Id); 77 | 78 | db.Close(); 79 | } 80 | 81 | [Test] 82 | public void AutoGuid_EmptyGuid() 83 | { 84 | var db = new SQLiteConnection(TestPath.GetTempFileName()); 85 | db.CreateTable(CreateFlags.AutoIncPK); 86 | 87 | var guid1 = new Guid("36473164-C9E4-4CDF-B266-A0B287C85623"); 88 | var guid2 = new Guid("BC5C4C4A-CA57-4B61-8B53-9FD4673528B6"); 89 | 90 | var obj1 = new TestObj() { Text = "First Guid Object" }; 91 | var obj2 = new TestObj() { Text = "Second Guid Object" }; 92 | 93 | Assert.AreEqual(Guid.Empty, obj1.Id); 94 | Assert.AreEqual(Guid.Empty, obj2.Id); 95 | 96 | var numIn1 = db.Insert(obj1); 97 | var numIn2 = db.Insert(obj2); 98 | Assert.AreNotEqual(Guid.Empty, obj1.Id); 99 | Assert.AreNotEqual(Guid.Empty, obj2.Id); 100 | Assert.AreNotEqual(obj1.Id, obj2.Id); 101 | 102 | db.Close(); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /tests/InheritanceTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | #if NETFX_CORE 5 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 6 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 7 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 8 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 9 | #else 10 | using NUnit.Framework; 11 | #endif 12 | 13 | 14 | namespace SQLite.Tests 15 | { 16 | [TestFixture] 17 | public class InheritanceTest 18 | { 19 | class Base 20 | { 21 | [PrimaryKey] 22 | public int Id { get; set; } 23 | 24 | public string BaseProp { get; set; } 25 | } 26 | 27 | class Derived : Base 28 | { 29 | public string DerivedProp { get; set; } 30 | } 31 | 32 | 33 | [Test] 34 | public void InheritanceWorks () 35 | { 36 | var db = new TestDb (); 37 | 38 | var mapping = db.GetMapping (); 39 | 40 | Assert.AreEqual (3, mapping.Columns.Length); 41 | Assert.AreEqual ("Id", mapping.PK.Name); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/InsertTest.cs: -------------------------------------------------------------------------------- 1 | 2 | using System; 3 | using System.IO; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using SQLite; 8 | 9 | #if NETFX_CORE 10 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 11 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 12 | using TearDown = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestCleanupAttribute; 13 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 14 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 15 | #else 16 | using NUnit.Framework; 17 | #endif 18 | 19 | using System.Diagnostics; 20 | 21 | namespace SQLite.Tests 22 | { 23 | [TestFixture] 24 | public class InsertTest 25 | { 26 | private TestDb _db; 27 | 28 | public class TestObj 29 | { 30 | [AutoIncrement, PrimaryKey] 31 | public int Id { get; set; } 32 | public String Text { get; set; } 33 | 34 | public override string ToString () 35 | { 36 | return string.Format("[TestObj: Id={0}, Text={1}]", Id, Text); 37 | } 38 | 39 | } 40 | 41 | public class TestObj2 42 | { 43 | [PrimaryKey] 44 | public int Id { get; set; } 45 | public String Text { get; set; } 46 | 47 | public override string ToString() 48 | { 49 | return string.Format("[TestObj: Id={0}, Text={1}]", Id, Text); 50 | } 51 | 52 | } 53 | 54 | public class OneColumnObj 55 | { 56 | [AutoIncrement, PrimaryKey] 57 | public int Id { get; set; } 58 | } 59 | 60 | public class UniqueObj 61 | { 62 | [PrimaryKey] 63 | public int Id { get; set; } 64 | } 65 | 66 | public class TestDb : SQLiteConnection 67 | { 68 | public TestDb(String path) 69 | : base(path) 70 | { 71 | CreateTable(); 72 | CreateTable(); 73 | CreateTable(); 74 | CreateTable(); 75 | } 76 | } 77 | 78 | [SetUp] 79 | public void Setup() 80 | { 81 | _db = new TestDb(TestPath.GetTempFileName()); 82 | } 83 | [TearDown] 84 | public void TearDown() 85 | { 86 | if (_db != null) _db.Close(); 87 | } 88 | 89 | [Test] 90 | public void InsertALot() 91 | { 92 | int n = 10000; 93 | var q = from i in Enumerable.Range(1, n) 94 | select new TestObj() { 95 | Text = "I am" 96 | }; 97 | var objs = q.ToArray(); 98 | _db.Trace = false; 99 | 100 | var sw = new Stopwatch(); 101 | sw.Start(); 102 | 103 | var numIn = _db.InsertAll(objs); 104 | 105 | sw.Stop(); 106 | 107 | Assert.AreEqual(numIn, n, "Num inserted must = num objects"); 108 | 109 | var inObjs = _db.CreateCommand("select * from TestObj").ExecuteQuery().ToArray(); 110 | 111 | for (var i = 0; i < inObjs.Length; i++) { 112 | Assert.AreEqual(i+1, objs[i].Id); 113 | Assert.AreEqual(i+1, inObjs[i].Id); 114 | Assert.AreEqual("I am", inObjs[i].Text); 115 | } 116 | 117 | var numCount = _db.CreateCommand("select count(*) from TestObj").ExecuteScalar(); 118 | 119 | Assert.AreEqual(numCount, n, "Num counted must = num objects"); 120 | } 121 | 122 | [Test] 123 | public void InsertTwoTimes() 124 | { 125 | var obj1 = new TestObj() { Text = "GLaDOS loves testing!" }; 126 | var obj2 = new TestObj() { Text = "Keep testing, just keep testing" }; 127 | 128 | 129 | var numIn1 = _db.Insert(obj1); 130 | var numIn2 = _db.Insert(obj2); 131 | Assert.AreEqual(1, numIn1); 132 | Assert.AreEqual(1, numIn2); 133 | 134 | var result = _db.Query("select * from TestObj").ToList(); 135 | Assert.AreEqual(2, result.Count); 136 | Assert.AreEqual(obj1.Text, result[0].Text); 137 | Assert.AreEqual(obj2.Text, result[1].Text); 138 | } 139 | 140 | [Test] 141 | public void InsertIntoTwoTables() 142 | { 143 | var obj1 = new TestObj() { Text = "GLaDOS loves testing!" }; 144 | var obj2 = new TestObj2() { Text = "Keep testing, just keep testing" }; 145 | 146 | var numIn1 = _db.Insert(obj1); 147 | Assert.AreEqual(1, numIn1); 148 | var numIn2 = _db.Insert(obj2); 149 | Assert.AreEqual(1, numIn2); 150 | 151 | var result1 = _db.Query("select * from TestObj").ToList(); 152 | Assert.AreEqual(numIn1, result1.Count); 153 | Assert.AreEqual(obj1.Text, result1.First().Text); 154 | 155 | var result2 = _db.Query("select * from TestObj2").ToList(); 156 | Assert.AreEqual(numIn2, result2.Count); 157 | } 158 | 159 | [Test] 160 | public void InsertWithExtra() 161 | { 162 | var obj1 = new TestObj2() { Id=1, Text = "GLaDOS loves testing!" }; 163 | var obj2 = new TestObj2() { Id=1, Text = "Keep testing, just keep testing" }; 164 | var obj3 = new TestObj2() { Id=1, Text = "Done testing" }; 165 | 166 | _db.Insert(obj1); 167 | 168 | 169 | try { 170 | _db.Insert(obj2); 171 | Assert.Fail("Expected unique constraint violation"); 172 | } 173 | catch (SQLiteException) { 174 | } 175 | _db.Insert(obj2, "OR REPLACE"); 176 | 177 | 178 | try { 179 | _db.Insert(obj3); 180 | Assert.Fail("Expected unique constraint violation"); 181 | } 182 | catch (SQLiteException) { 183 | } 184 | _db.Insert(obj3, "OR IGNORE"); 185 | 186 | var result = _db.Query("select * from TestObj2").ToList(); 187 | Assert.AreEqual(1, result.Count); 188 | Assert.AreEqual(obj2.Text, result.First().Text); 189 | } 190 | 191 | [Test] 192 | public void InsertIntoOneColumnAutoIncrementTable() 193 | { 194 | var obj = new OneColumnObj(); 195 | _db.Insert(obj); 196 | 197 | var result = _db.Get(1); 198 | Assert.AreEqual(1, result.Id); 199 | } 200 | 201 | [Test] 202 | public void InsertAllSuccessOutsideTransaction() 203 | { 204 | var testObjects = Enumerable.Range(1, 20).Select(i => new UniqueObj { Id = i }).ToList(); 205 | 206 | _db.InsertAll(testObjects); 207 | 208 | Assert.AreEqual(testObjects.Count, _db.Table().Count()); 209 | } 210 | 211 | [Test] 212 | public void InsertAllFailureOutsideTransaction() 213 | { 214 | var testObjects = Enumerable.Range(1, 20).Select(i => new UniqueObj { Id = i }).ToList(); 215 | testObjects[testObjects.Count - 1].Id = 1; // causes the insert to fail because of duplicate key 216 | 217 | ExceptionAssert.Throws(() => _db.InsertAll(testObjects)); 218 | 219 | Assert.AreEqual(0, _db.Table().Count()); 220 | } 221 | 222 | [Test] 223 | public void InsertAllSuccessInsideTransaction() 224 | { 225 | var testObjects = Enumerable.Range(1, 20).Select(i => new UniqueObj { Id = i }).ToList(); 226 | 227 | _db.RunInTransaction(() => { 228 | _db.InsertAll(testObjects); 229 | }); 230 | 231 | Assert.AreEqual(testObjects.Count, _db.Table().Count()); 232 | } 233 | 234 | [Test] 235 | public void InsertAllFailureInsideTransaction() 236 | { 237 | var testObjects = Enumerable.Range(1, 20).Select(i => new UniqueObj { Id = i }).ToList(); 238 | testObjects[testObjects.Count - 1].Id = 1; // causes the insert to fail because of duplicate key 239 | 240 | ExceptionAssert.Throws(() => _db.RunInTransaction(() => { 241 | _db.InsertAll(testObjects); 242 | })); 243 | 244 | Assert.AreEqual(0, _db.Table().Count()); 245 | } 246 | 247 | [Test] 248 | public void InsertOrReplace () 249 | { 250 | _db.Trace = true; 251 | _db.InsertAll (from i in Enumerable.Range(1, 20) select new TestObj { Text = "#" + i }); 252 | 253 | Assert.AreEqual (20, _db.Table ().Count ()); 254 | 255 | var t = new TestObj { Id = 5, Text = "Foo", }; 256 | _db.InsertOrReplace (t); 257 | 258 | var r = (from x in _db.Table () orderby x.Id select x).ToList (); 259 | Assert.AreEqual (20, r.Count); 260 | Assert.AreEqual ("Foo", r[4].Text); 261 | } 262 | } 263 | } 264 | -------------------------------------------------------------------------------- /tests/JoinTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | //using System.Linq; 5 | using System.Text; 6 | using SQLite; 7 | 8 | #if NETFX_CORE 9 | using Microsoft.VisualStudio.TestTools.UnitTesting; 10 | using SetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute; 11 | using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; 12 | using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; 13 | #else 14 | using NUnit.Framework; 15 | #endif 16 | 17 | using System.Diagnostics; 18 | 19 | namespace SQLite.Tests 20 | { 21 | [TestFixture] 22 | public class JoinTest 23 | { 24 | TestDb _db; 25 | 26 | [SetUp] 27 | public void SetUp () 28 | { 29 | _db = new TestDb (); 30 | _db.CreateTable (); 31 | _db.CreateTable (); 32 | _db.CreateTable (); 33 | 34 | var p1 = new Product { Name = "One", }; 35 | var p2 = new Product { Name = "Two", }; 36 | var p3 = new Product { Name = "Three", }; 37 | _db.InsertAll (new [] { p1, p2, p3 } ); 38 | 39 | var o1 = new Order { PlacedTime = DateTime.Now, }; 40 | var o2 = new Order { PlacedTime = DateTime.Now, }; 41 | _db.InsertAll (new [] { o1, o2 } ); 42 | 43 | _db.InsertAll (new [] { 44 | new OrderLine { 45 | OrderId = o1.Id, 46 | ProductId = p1.Id, 47 | Quantity = 1, 48 | }, 49 | new OrderLine { 50 | OrderId = o1.Id, 51 | ProductId = p2.Id, 52 | Quantity = 2, 53 | }, 54 | new OrderLine { 55 | OrderId = o2.Id, 56 | ProductId = p3.Id, 57 | Quantity = 3, 58 | }, 59 | }); 60 | } 61 | 62 | class R 63 | { 64 | } 65 | 66 | //[Test] 67 | public void JoinThenWhere () 68 | { 69 | var q = from ol in _db.Table () 70 | join o in _db.Table () on ol.OrderId equals o.Id 71 | where o.Id == 1 72 | select new { o.Id, ol.ProductId, ol.Quantity }; 73 | 74 | var r = System.Linq.Enumerable.ToList (q); 75 | 76 | Assert.AreEqual (2, r.Count); 77 | } 78 | 79 | //[Test] 80 | public void WhereThenJoin () 81 | { 82 | var q = from ol in _db.Table () 83 | where ol.OrderId == 1 84 | join o in _db.Table () on ol.OrderId equals o.Id 85 | select new { o.Id, ol.ProductId, ol.Quantity }; 86 | 87 | var r = System.Linq.Enumerable.ToList (q); 88 | 89 | Assert.AreEqual (2, r.Count); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /tests/LinqTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Collections.Generic; 4 | 5 | #if NETFX_CORE 6 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 7 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 8 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 9 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 10 | #else 11 | using NUnit.Framework; 12 | #endif 13 | 14 | namespace SQLite.Tests 15 | { 16 | [TestFixture] 17 | public class LinqTest 18 | { 19 | TestDb CreateDb () 20 | { 21 | var db = new TestDb (); 22 | db.CreateTable (); 23 | db.CreateTable (); 24 | db.CreateTable (); 25 | db.CreateTable (); 26 | return db; 27 | } 28 | 29 | [Test] 30 | public void FunctionParameter () 31 | { 32 | var db = CreateDb (); 33 | 34 | db.Insert (new Product { 35 | Name = "A", 36 | Price = 20, 37 | }); 38 | 39 | db.Insert (new Product { 40 | Name = "B", 41 | Price = 10, 42 | }); 43 | 44 | Func> GetProductsWithPriceAtLeast = delegate(decimal val) { 45 | return (from p in db.Table () where p.Price > val select p).ToList (); 46 | }; 47 | 48 | var r = GetProductsWithPriceAtLeast (15); 49 | Assert.AreEqual (1, r.Count); 50 | Assert.AreEqual ("A", r [0].Name); 51 | } 52 | 53 | [Test] 54 | public void WhereGreaterThan () 55 | { 56 | var db = CreateDb (); 57 | 58 | db.Insert (new Product { 59 | Name = "A", 60 | Price = 20, 61 | }); 62 | 63 | db.Insert (new Product { 64 | Name = "B", 65 | Price = 10, 66 | }); 67 | 68 | Assert.AreEqual (2, db.Table ().Count ()); 69 | 70 | var r = (from p in db.Table () where p.Price > 15 select p).ToList (); 71 | Assert.AreEqual (1, r.Count); 72 | Assert.AreEqual ("A", r [0].Name); 73 | } 74 | 75 | [Test] 76 | public void GetWithExpression () 77 | { 78 | var db = CreateDb(); 79 | 80 | db.Insert (new Product { 81 | Name = "A", 82 | Price = 20, 83 | }); 84 | 85 | db.Insert (new Product { 86 | Name = "B", 87 | Price = 10, 88 | }); 89 | 90 | db.Insert(new Product 91 | { 92 | Name = "C", 93 | Price = 5, 94 | }); 95 | 96 | Assert.AreEqual (3, db.Table ().Count ()); 97 | 98 | var r = db.Get(x => x.Price == 10); 99 | Assert.IsNotNull(r); 100 | Assert.AreEqual ("B", r.Name); 101 | } 102 | 103 | [Test] 104 | public void FindWithExpression () 105 | { 106 | var db = CreateDb(); 107 | 108 | var r = db.Find (x => x.Price == 10); 109 | Assert.IsNull (r); 110 | } 111 | 112 | [Test] 113 | public void OrderByCast () 114 | { 115 | var db = CreateDb(); 116 | 117 | db.Insert (new Product { 118 | Name = "A", 119 | TotalSales = 1, 120 | }); 121 | db.Insert (new Product { 122 | Name = "B", 123 | TotalSales = 100, 124 | }); 125 | 126 | var nocast = (from p in db.Table () orderby p.TotalSales descending select p).ToList (); 127 | Assert.AreEqual (2, nocast.Count); 128 | Assert.AreEqual ("B", nocast [0].Name); 129 | 130 | var cast = (from p in db.Table () orderby (int)p.TotalSales descending select p).ToList (); 131 | Assert.AreEqual (2, cast.Count); 132 | Assert.AreEqual ("B", cast [0].Name); 133 | } 134 | 135 | public class Issue96_A 136 | { 137 | [ AutoIncrement, PrimaryKey] 138 | public int ID { get; set; } 139 | public string AddressLine { get; set; } 140 | 141 | [Indexed] 142 | public int? ClassB { get; set; } 143 | [Indexed] 144 | public int? ClassC { get; set; } 145 | } 146 | 147 | public class Issue96_B 148 | { 149 | [ AutoIncrement, PrimaryKey] 150 | public int ID { get; set; } 151 | public string CustomerName { get; set; } 152 | } 153 | 154 | public class Issue96_C 155 | { 156 | [ AutoIncrement, PrimaryKey] 157 | public int ID { get; set; } 158 | public string SupplierName { get; set; } 159 | } 160 | 161 | [Test] 162 | public void Issue96_NullabelIntsInQueries () 163 | { 164 | var db = CreateDb(); 165 | db.CreateTable (); 166 | 167 | var id = 42; 168 | 169 | db.Insert (new Issue96_A { 170 | ClassB = id, 171 | }); 172 | db.Insert (new Issue96_A { 173 | ClassB = null, 174 | }); 175 | db.Insert (new Issue96_A { 176 | ClassB = null, 177 | }); 178 | db.Insert (new Issue96_A { 179 | ClassB = null, 180 | }); 181 | 182 | 183 | Assert.AreEqual (1, db.Table().Where(p => p.ClassB == id).Count ()); 184 | Assert.AreEqual (3, db.Table().Where(p => p.ClassB == null).Count ()); 185 | } 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /tests/MappingTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | #if NETFX_CORE 5 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 6 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 7 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 8 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 9 | #else 10 | using NUnit.Framework; 11 | #endif 12 | 13 | namespace SQLite.Tests 14 | { 15 | [TestFixture] 16 | public class MappingTest 17 | { 18 | [Table ("AGoodTableName")] 19 | class AFunnyTableName 20 | { 21 | [PrimaryKey] 22 | public int Id { get; set; } 23 | 24 | [Column ("AGoodColumnName")] 25 | public string AFunnyColumnName { get; set; } 26 | } 27 | 28 | 29 | [Test] 30 | public void HasGoodNames () 31 | { 32 | var db = new TestDb (); 33 | 34 | db.CreateTable (); 35 | 36 | var mapping = db.GetMapping (); 37 | 38 | Assert.AreEqual ("AGoodTableName", mapping.TableName); 39 | 40 | Assert.AreEqual ("Id", mapping.Columns [0].Name); 41 | Assert.AreEqual ("AGoodColumnName", mapping.Columns [1].Name); 42 | } 43 | 44 | #region Issue #86 45 | 46 | [Table("foo")] 47 | public class Foo 48 | { 49 | [Column("baz")] 50 | public int Bar { get; set; } 51 | } 52 | 53 | [Test] 54 | public void Issue86 () 55 | { 56 | var db = new TestDb (); 57 | db.CreateTable (); 58 | 59 | db.Insert (new Foo { Bar = 42 } ); 60 | db.Insert (new Foo { Bar = 69 } ); 61 | 62 | var found42 = db.Table ().Where (f => f.Bar == 42).FirstOrDefault(); 63 | Assert.IsNotNull (found42); 64 | 65 | var ordered = new List(db.Table().OrderByDescending(f => f.Bar)); 66 | Assert.AreEqual(2, ordered.Count); 67 | Assert.AreEqual(69, ordered[0].Bar); 68 | Assert.AreEqual(42, ordered[1].Bar); 69 | } 70 | 71 | #endregion 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /tests/MigrationTest.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Text; 3 | using SQLite; 4 | 5 | #if NETFX_CORE 6 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 7 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 8 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 9 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 10 | #else 11 | using NUnit.Framework; 12 | #endif 13 | 14 | using System.IO; 15 | 16 | namespace SQLite.Tests 17 | { 18 | [TestFixture] 19 | public class MigrationTest 20 | { 21 | [Table ("Test")] 22 | class LowerId { 23 | public int Id { get; set; } 24 | } 25 | [Table ("Test")] 26 | class UpperId { 27 | public int ID { get; set; } 28 | } 29 | 30 | [Test] 31 | public void UpperAndLowerColumnNames () 32 | { 33 | using (var db = new TestDb (true) { Trace = true } ) { 34 | db.CreateTable (); 35 | db.CreateTable (); 36 | 37 | var cols = db.GetTableInfo ("Test").ToList (); 38 | Assert.That (cols.Count, Is.EqualTo (1)); 39 | Assert.That (cols[0].Name, Is.EqualTo ("Id")); 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/NullableTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using SQLite; 6 | 7 | #if NETFX_CORE 8 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 9 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 10 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 11 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 12 | #else 13 | using NUnit.Framework; 14 | #endif 15 | 16 | using System.IO; 17 | 18 | namespace SQLite.Tests 19 | { 20 | [TestFixture] 21 | public class NullableTest 22 | { 23 | public class NullableIntClass 24 | { 25 | [PrimaryKey, AutoIncrement] 26 | public int ID { get; set; } 27 | 28 | public Nullable NullableInt { get; set; } 29 | 30 | public override bool Equals(object obj) 31 | { 32 | NullableIntClass other = (NullableIntClass)obj; 33 | return this.ID == other.ID && this.NullableInt == other.NullableInt; 34 | } 35 | 36 | public override int GetHashCode () 37 | { 38 | return ID.GetHashCode () + NullableInt.GetHashCode (); 39 | } 40 | } 41 | 42 | [Test] 43 | [Description("Create a table with a nullable int column then insert and select against it")] 44 | public void NullableInt() 45 | { 46 | SQLiteConnection db = new SQLiteConnection(TestPath.GetTempFileName()); 47 | db.CreateTable(); 48 | 49 | NullableIntClass withNull = new NullableIntClass() { NullableInt = null }; 50 | NullableIntClass with0 = new NullableIntClass() { NullableInt = 0 }; 51 | NullableIntClass with1 = new NullableIntClass() { NullableInt = 1 }; 52 | NullableIntClass withMinus1 = new NullableIntClass() { NullableInt = -1 }; 53 | 54 | db.Insert(withNull); 55 | db.Insert(with0); 56 | db.Insert(with1); 57 | db.Insert(withMinus1); 58 | 59 | NullableIntClass[] results = db.Table().OrderBy(x => x.ID).ToArray(); 60 | 61 | Assert.AreEqual(4, results.Length); 62 | 63 | Assert.AreEqual(withNull, results[0]); 64 | Assert.AreEqual(with0, results[1]); 65 | Assert.AreEqual(with1, results[2]); 66 | Assert.AreEqual(withMinus1, results[3]); 67 | } 68 | 69 | 70 | public class NullableFloatClass 71 | { 72 | [PrimaryKey, AutoIncrement] 73 | public int ID { get; set; } 74 | 75 | public Nullable NullableFloat { get; set; } 76 | 77 | public override bool Equals(object obj) 78 | { 79 | NullableFloatClass other = (NullableFloatClass)obj; 80 | return this.ID == other.ID && this.NullableFloat == other.NullableFloat; 81 | } 82 | 83 | public override int GetHashCode () 84 | { 85 | return ID.GetHashCode () + NullableFloat.GetHashCode (); 86 | } 87 | } 88 | 89 | [Test] 90 | [Description("Create a table with a nullable int column then insert and select against it")] 91 | public void NullableFloat() 92 | { 93 | SQLiteConnection db = new SQLiteConnection(TestPath.GetTempFileName()); 94 | db.CreateTable(); 95 | 96 | NullableFloatClass withNull = new NullableFloatClass() { NullableFloat = null }; 97 | NullableFloatClass with0 = new NullableFloatClass() { NullableFloat = 0 }; 98 | NullableFloatClass with1 = new NullableFloatClass() { NullableFloat = 1 }; 99 | NullableFloatClass withMinus1 = new NullableFloatClass() { NullableFloat = -1 }; 100 | 101 | db.Insert(withNull); 102 | db.Insert(with0); 103 | db.Insert(with1); 104 | db.Insert(withMinus1); 105 | 106 | NullableFloatClass[] results = db.Table().OrderBy(x => x.ID).ToArray(); 107 | 108 | Assert.AreEqual(4, results.Length); 109 | 110 | Assert.AreEqual(withNull, results[0]); 111 | Assert.AreEqual(with0, results[1]); 112 | Assert.AreEqual(with1, results[2]); 113 | Assert.AreEqual(withMinus1, results[3]); 114 | } 115 | 116 | 117 | 118 | public class StringClass 119 | { 120 | [PrimaryKey, AutoIncrement] 121 | public int ID { get; set; } 122 | 123 | //Strings are allowed to be null by default 124 | public string StringData { get; set; } 125 | 126 | public override bool Equals(object obj) 127 | { 128 | StringClass other = (StringClass)obj; 129 | return this.ID == other.ID && this.StringData == other.StringData; 130 | } 131 | 132 | public override int GetHashCode () 133 | { 134 | return ID.GetHashCode () + StringData.GetHashCode (); 135 | } 136 | } 137 | 138 | [Test] 139 | public void NullableString() 140 | { 141 | SQLiteConnection db = new SQLiteConnection(TestPath.GetTempFileName()); 142 | db.CreateTable(); 143 | 144 | StringClass withNull = new StringClass() { StringData = null }; 145 | StringClass withEmpty = new StringClass() { StringData = "" }; 146 | StringClass withData = new StringClass() { StringData = "data" }; 147 | 148 | db.Insert(withNull); 149 | db.Insert(withEmpty); 150 | db.Insert(withData); 151 | 152 | StringClass[] results = db.Table().OrderBy(x => x.ID).ToArray(); 153 | 154 | Assert.AreEqual(3, results.Length); 155 | 156 | Assert.AreEqual(withNull, results[0]); 157 | Assert.AreEqual(withEmpty, results[1]); 158 | Assert.AreEqual(withData, results[2]); 159 | } 160 | 161 | [Test] 162 | public void WhereNotNull() 163 | { 164 | SQLiteConnection db = new SQLiteConnection(TestPath.GetTempFileName()); 165 | db.CreateTable(); 166 | 167 | NullableIntClass withNull = new NullableIntClass() { NullableInt = null }; 168 | NullableIntClass with0 = new NullableIntClass() { NullableInt = 0 }; 169 | NullableIntClass with1 = new NullableIntClass() { NullableInt = 1 }; 170 | NullableIntClass withMinus1 = new NullableIntClass() { NullableInt = -1 }; 171 | 172 | db.Insert(withNull); 173 | db.Insert(with0); 174 | db.Insert(with1); 175 | db.Insert(withMinus1); 176 | 177 | NullableIntClass[] results = db.Table().Where(x => x.NullableInt != null).OrderBy(x => x.ID).ToArray(); 178 | 179 | Assert.AreEqual(3, results.Length); 180 | 181 | Assert.AreEqual(with0, results[0]); 182 | Assert.AreEqual(with1, results[1]); 183 | Assert.AreEqual(withMinus1, results[2]); 184 | } 185 | 186 | [Test] 187 | public void WhereNull() 188 | { 189 | SQLiteConnection db = new SQLiteConnection(TestPath.GetTempFileName()); 190 | db.CreateTable(); 191 | 192 | NullableIntClass withNull = new NullableIntClass() { NullableInt = null }; 193 | NullableIntClass with0 = new NullableIntClass() { NullableInt = 0 }; 194 | NullableIntClass with1 = new NullableIntClass() { NullableInt = 1 }; 195 | NullableIntClass withMinus1 = new NullableIntClass() { NullableInt = -1 }; 196 | 197 | db.Insert(withNull); 198 | db.Insert(with0); 199 | db.Insert(with1); 200 | db.Insert(withMinus1); 201 | 202 | NullableIntClass[] results = db.Table().Where(x => x.NullableInt == null).OrderBy(x => x.ID).ToArray(); 203 | 204 | Assert.AreEqual(1, results.Length); 205 | Assert.AreEqual(withNull, results[0]); 206 | } 207 | 208 | [Test] 209 | public void StringWhereNull() 210 | { 211 | SQLiteConnection db = new SQLiteConnection(TestPath.GetTempFileName()); 212 | db.CreateTable(); 213 | 214 | StringClass withNull = new StringClass() { StringData = null }; 215 | StringClass withEmpty = new StringClass() { StringData = "" }; 216 | StringClass withData = new StringClass() { StringData = "data" }; 217 | 218 | db.Insert(withNull); 219 | db.Insert(withEmpty); 220 | db.Insert(withData); 221 | 222 | StringClass[] results = db.Table().Where(x => x.StringData == null).OrderBy(x => x.ID).ToArray(); 223 | Assert.AreEqual(1, results.Length); 224 | Assert.AreEqual(withNull, results[0]); 225 | } 226 | 227 | [Test] 228 | public void StringWhereNotNull() 229 | { 230 | SQLiteConnection db = new SQLiteConnection(TestPath.GetTempFileName()); 231 | db.CreateTable(); 232 | 233 | StringClass withNull = new StringClass() { StringData = null }; 234 | StringClass withEmpty = new StringClass() { StringData = "" }; 235 | StringClass withData = new StringClass() { StringData = "data" }; 236 | 237 | db.Insert(withNull); 238 | db.Insert(withEmpty); 239 | db.Insert(withData); 240 | 241 | StringClass[] results = db.Table().Where(x => x.StringData != null).OrderBy(x => x.ID).ToArray(); 242 | Assert.AreEqual(2, results.Length); 243 | Assert.AreEqual(withEmpty, results[0]); 244 | Assert.AreEqual(withData, results[1]); 245 | } 246 | } 247 | } 248 | -------------------------------------------------------------------------------- /tests/OpenTests.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Text; 3 | using SQLite; 4 | 5 | #if NETFX_CORE 6 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 7 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 8 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 9 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 10 | #else 11 | using NUnit.Framework; 12 | #endif 13 | 14 | using System.IO; 15 | 16 | namespace SQLite.Tests 17 | { 18 | [TestFixture] 19 | public class OpenTest 20 | { 21 | const string UnicodeText = "\u01F427 \u221E"; 22 | 23 | [Test] 24 | public void UnicodePaths() 25 | { 26 | var path = Path.GetTempFileName () + UnicodeText; 27 | 28 | using (var db = new SQLiteConnection (path, true)) { 29 | db.CreateTable (); 30 | } 31 | 32 | Assert.That (new FileInfo (path).Length, Is.GreaterThan (0), path); 33 | } 34 | 35 | [Test] 36 | public void UnicodePathsAsync() 37 | { 38 | var path = Path.GetTempFileName () + UnicodeText; 39 | 40 | var db = new SQLiteAsyncConnection (path, true); 41 | db.CreateTableAsync ().Wait (); 42 | 43 | Assert.That (new FileInfo (path).Length, Is.GreaterThan (0), path); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/SQLite.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 9.0.21022 7 | 2.0 8 | {6947A8F1-99BE-4DD1-AD4D-D89425CE67A2} 9 | Library 10 | SQLite.Tests 11 | SQLite.Tests 12 | 13 | 14 | True 15 | full 16 | False 17 | bin\Debug 18 | DEBUG 19 | prompt 20 | 4 21 | False 22 | 23 | 24 | none 25 | False 26 | bin\Release 27 | prompt 28 | 4 29 | False 30 | 31 | 32 | 33 | 34 | 35 | False 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | SQLiteAsync.cs 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /tests/SQLiteMetroTests.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 11 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLiteMetroTests", "SQLiteMetroTests\SQLiteMetroTests.csproj", "{909DE315-2F55-4A20-A9AD-460D4B2A3908}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|ARM = Debug|ARM 9 | Debug|Any CPU = Debug|Any CPU 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|ARM = Release|ARM 13 | Release|Any CPU = Release|Any CPU 14 | Release|x64 = Release|x64 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Debug|ARM.ActiveCfg = Debug|ARM 19 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Debug|ARM.Build.0 = Debug|ARM 20 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Debug|ARM.Deploy.0 = Debug|ARM 21 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 22 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Debug|Any CPU.Build.0 = Debug|Any CPU 23 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Debug|Any CPU.Deploy.0 = Debug|Any CPU 24 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Debug|x64.ActiveCfg = Debug|x64 25 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Debug|x64.Build.0 = Debug|x64 26 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Debug|x64.Deploy.0 = Debug|x64 27 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Debug|x86.ActiveCfg = Debug|x86 28 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Debug|x86.Build.0 = Debug|x86 29 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Debug|x86.Deploy.0 = Debug|x86 30 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Release|ARM.ActiveCfg = Release|ARM 31 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Release|ARM.Build.0 = Release|ARM 32 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Release|ARM.Deploy.0 = Release|ARM 33 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Release|Any CPU.ActiveCfg = Release|Any CPU 34 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Release|Any CPU.Build.0 = Release|Any CPU 35 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Release|Any CPU.Deploy.0 = Release|Any CPU 36 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Release|x64.ActiveCfg = Release|x64 37 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Release|x64.Build.0 = Release|x64 38 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Release|x64.Deploy.0 = Release|x64 39 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Release|x86.ActiveCfg = Release|x86 40 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Release|x86.Build.0 = Release|x86 41 | {909DE315-2F55-4A20-A9AD-460D4B2A3908}.Release|x86.Deploy.0 = Release|x86 42 | EndGlobalSection 43 | GlobalSection(SolutionProperties) = preSolution 44 | HideSolutionNode = FALSE 45 | EndGlobalSection 46 | EndGlobal 47 | -------------------------------------------------------------------------------- /tests/SQLiteMetroTests/Images/UnitTestLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koush/sqlite-net/18026a6dfa7e5c911fca227aa622a35a79b7ffff/tests/SQLiteMetroTests/Images/UnitTestLogo.png -------------------------------------------------------------------------------- /tests/SQLiteMetroTests/Images/UnitTestSmallLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koush/sqlite-net/18026a6dfa7e5c911fca227aa622a35a79b7ffff/tests/SQLiteMetroTests/Images/UnitTestSmallLogo.png -------------------------------------------------------------------------------- /tests/SQLiteMetroTests/Images/UnitTestSplashScreen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koush/sqlite-net/18026a6dfa7e5c911fca227aa622a35a79b7ffff/tests/SQLiteMetroTests/Images/UnitTestSplashScreen.png -------------------------------------------------------------------------------- /tests/SQLiteMetroTests/Images/UnitTestStoreLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koush/sqlite-net/18026a6dfa7e5c911fca227aa622a35a79b7ffff/tests/SQLiteMetroTests/Images/UnitTestStoreLogo.png -------------------------------------------------------------------------------- /tests/SQLiteMetroTests/Package.appxmanifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 7 | 8 | 9 | SQLiteMetroTests 10 | Frank 11 | Images\UnitTestStoreLogo.png 12 | SQLiteMetroTests 13 | 14 | 15 | 16 | 6.2 17 | 6.2 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 28 | 35 | 36 | 37 | 38 | 39 | 42 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /tests/SQLiteMetroTests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("SQLiteMetroTests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Microsoft")] 12 | [assembly: AssemblyProduct("SQLiteMetroTests")] 13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Version information for an assembly consists of the following four values: 18 | // 19 | // Major Version 20 | // Minor Version 21 | // Build Number 22 | // Revision 23 | // 24 | // You can specify all the values or you can default the Build and Revision Numbers 25 | // by using the '*' as shown below: 26 | // [assembly: AssemblyVersion("1.0.*")] 27 | [assembly: AssemblyVersion("1.0.0.0")] 28 | [assembly: AssemblyFileVersion("1.0.0.0")] 29 | -------------------------------------------------------------------------------- /tests/SQLiteMetroTests/SQLiteMetroTests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 8.0.30703 8 | 2.0 9 | {909DE315-2F55-4A20-A9AD-460D4B2A3908} 10 | Library 11 | Properties 12 | SQLiteMetroTests 13 | SQLiteMetroTests 14 | en-US 15 | 512 16 | {BC8A1FFA-BEE3-4634-8014-F334798102B3};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 17 | SQLiteMetroTests_TemporaryKey.pfx 18 | DFDDA4814A991B0900EDBDBBB55A3C0DC34968D9 19 | 20 | 21 | true 22 | full 23 | false 24 | bin\Debug\ 25 | DEBUG;TRACE;NETFX_CORE 26 | prompt 27 | 4 28 | 29 | 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE;NETFX_CORE 34 | prompt 35 | 4 36 | 37 | 38 | true 39 | bin\ARM\Debug\ 40 | DEBUG;TRACE;NETFX_CORE 41 | ;2008 42 | full 43 | ARM 44 | false 45 | prompt 46 | ExpressRules.ruleset 47 | true 48 | 49 | 50 | bin\ARM\Release\ 51 | TRACE;NETFX_CORE 52 | true 53 | ;2008 54 | pdbonly 55 | ARM 56 | false 57 | prompt 58 | ExpressRules.ruleset 59 | true 60 | 61 | 62 | true 63 | bin\x64\Debug\ 64 | DEBUG;TRACE;NETFX_CORE 65 | ;2008 66 | full 67 | x64 68 | false 69 | prompt 70 | ExpressRules.ruleset 71 | true 72 | 73 | 74 | bin\x64\Release\ 75 | TRACE;NETFX_CORE 76 | true 77 | ;2008 78 | pdbonly 79 | x64 80 | false 81 | prompt 82 | ExpressRules.ruleset 83 | true 84 | 85 | 86 | true 87 | bin\x86\Debug\ 88 | DEBUG;TRACE;NETFX_CORE 89 | ;2008 90 | full 91 | x86 92 | false 93 | prompt 94 | ExpressRules.ruleset 95 | true 96 | 97 | 98 | bin\x86\Release\ 99 | TRACE;NETFX_CORE 100 | true 101 | ;2008 102 | pdbonly 103 | x86 104 | false 105 | prompt 106 | ExpressRules.ruleset 107 | true 108 | 109 | 110 | True 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | SQLite.cs 120 | 121 | 122 | SQLiteAsync.cs 123 | 124 | 125 | AsyncTests.cs 126 | 127 | 128 | BooleanTest.cs 129 | 130 | 131 | ByteArrayTest.cs 132 | 133 | 134 | CollateTest.cs 135 | 136 | 137 | ContainsTest.cs 138 | 139 | 140 | CreateTableTest.cs 141 | 142 | 143 | DropTableTest.cs 144 | 145 | 146 | ExceptionAssert.cs 147 | 148 | 149 | GuidTests.cs 150 | 151 | 152 | InheritanceTest.cs 153 | 154 | 155 | InsertTest.cs 156 | 157 | 158 | LinqTest.cs 159 | 160 | 161 | MappingTest.cs 162 | 163 | 164 | NullableTest.cs 165 | 166 | 167 | SkipTest.cs 168 | 169 | 170 | StringQueryTest.cs 171 | 172 | 173 | TestDb.cs 174 | 175 | 176 | UnicodeTest.cs 177 | 178 | 179 | UniqueTest.cs 180 | 181 | 182 | 183 | 184 | 185 | Designer 186 | 187 | 188 | 189 | 190 | sqlite3.dll 191 | 192 | 193 | PreserveNewest 194 | 195 | 196 | PreserveNewest 197 | 198 | 199 | PreserveNewest 200 | 201 | 202 | PreserveNewest 203 | 204 | 205 | 206 | 207 | 208 | 209 | 11.0 210 | 211 | 212 | 219 | -------------------------------------------------------------------------------- /tests/SQLiteMetroTests/SQLiteMetroTests_TemporaryKey.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koush/sqlite-net/18026a6dfa7e5c911fca227aa622a35a79b7ffff/tests/SQLiteMetroTests/SQLiteMetroTests_TemporaryKey.pfx -------------------------------------------------------------------------------- /tests/SQLiteTouchTests.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLiteTouchTests", "SQLiteTouchTests\SQLiteTouchTests.csproj", "{D61A2E10-513B-4F20-8046-CAA914CEE437}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|iPhoneSimulator = Debug|iPhoneSimulator 9 | Release|iPhoneSimulator = Release|iPhoneSimulator 10 | Debug|iPhone = Debug|iPhone 11 | Release|iPhone = Release|iPhone 12 | Ad-Hoc|iPhone = Ad-Hoc|iPhone 13 | AppStore|iPhone = AppStore|iPhone 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {D61A2E10-513B-4F20-8046-CAA914CEE437}.Ad-Hoc|iPhone.ActiveCfg = Ad-Hoc|iPhone 17 | {D61A2E10-513B-4F20-8046-CAA914CEE437}.Ad-Hoc|iPhone.Build.0 = Ad-Hoc|iPhone 18 | {D61A2E10-513B-4F20-8046-CAA914CEE437}.AppStore|iPhone.ActiveCfg = AppStore|iPhone 19 | {D61A2E10-513B-4F20-8046-CAA914CEE437}.AppStore|iPhone.Build.0 = AppStore|iPhone 20 | {D61A2E10-513B-4F20-8046-CAA914CEE437}.Debug|iPhone.ActiveCfg = Debug|iPhone 21 | {D61A2E10-513B-4F20-8046-CAA914CEE437}.Debug|iPhone.Build.0 = Debug|iPhone 22 | {D61A2E10-513B-4F20-8046-CAA914CEE437}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator 23 | {D61A2E10-513B-4F20-8046-CAA914CEE437}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator 24 | {D61A2E10-513B-4F20-8046-CAA914CEE437}.Release|iPhone.ActiveCfg = Release|iPhone 25 | {D61A2E10-513B-4F20-8046-CAA914CEE437}.Release|iPhone.Build.0 = Release|iPhone 26 | {D61A2E10-513B-4F20-8046-CAA914CEE437}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator 27 | {D61A2E10-513B-4F20-8046-CAA914CEE437}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator 28 | EndGlobalSection 29 | GlobalSection(MonoDevelopProperties) = preSolution 30 | StartupItem = SQLiteTouchTests\SQLiteTouchTests.csproj 31 | EndGlobalSection 32 | EndGlobal 33 | -------------------------------------------------------------------------------- /tests/SQLiteTouchTests/AppDelegate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | using MonoTouch.Foundation; 6 | using MonoTouch.UIKit; 7 | using MonoTouch.NUnit.UI; 8 | 9 | namespace SQLiteTouchTests 10 | { 11 | // The UIApplicationDelegate for the application. This class is responsible for launching the 12 | // User Interface of the application, as well as listening (and optionally responding) to 13 | // application events from iOS. 14 | [Register ("AppDelegate")] 15 | public partial class AppDelegate : UIApplicationDelegate 16 | { 17 | // class-level declarations 18 | UIWindow window; 19 | TouchRunner runner; 20 | 21 | // 22 | // This method is invoked when the application has loaded and is ready to run. In this 23 | // method you should instantiate the window, load the UI into it and then make the window 24 | // visible. 25 | // 26 | // You have 17 seconds to return from this method, or iOS will terminate your application. 27 | // 28 | public override bool FinishedLaunching (UIApplication app, NSDictionary options) 29 | { 30 | // create a new window instance based on the screen size 31 | window = new UIWindow (UIScreen.MainScreen.Bounds); 32 | runner = new TouchRunner (window); 33 | 34 | // register every tests included in the main application/assembly 35 | runner.Add (System.Reflection.Assembly.GetExecutingAssembly ()); 36 | 37 | window.RootViewController = new UINavigationController (runner.GetViewController ()); 38 | 39 | // make the window visible 40 | window.MakeKeyAndVisible (); 41 | 42 | return true; 43 | } 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /tests/SQLiteTouchTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | UIDeviceFamily 6 | 7 | 1 8 | 2 9 | 10 | UISupportedInterfaceOrientations 11 | 12 | UIInterfaceOrientationPortrait 13 | UIInterfaceOrientationLandscapeLeft 14 | UIInterfaceOrientationLandscapeRight 15 | 16 | UISupportedInterfaceOrientations~ipad 17 | 18 | UIInterfaceOrientationPortrait 19 | UIInterfaceOrientationPortraitUpsideDown 20 | UIInterfaceOrientationLandscapeLeft 21 | UIInterfaceOrientationLandscapeRight 22 | 23 | MinimumOSVersion 24 | 3.2 25 | 26 | 27 | -------------------------------------------------------------------------------- /tests/SQLiteTouchTests/Main.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | using MonoTouch.Foundation; 6 | using MonoTouch.UIKit; 7 | 8 | namespace SQLiteTouchTests 9 | { 10 | public class Application 11 | { 12 | // This is the main entry point of the application. 13 | static void Main (string[] args) 14 | { 15 | // if you want to use a different Application Delegate class from "AppDelegate" 16 | // you can specify it here. 17 | UIApplication.Main (args, null, "AppDelegate"); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/SQLiteTouchTests/SQLiteTouchTests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | iPhoneSimulator 6 | 10.0.0 7 | 2.0 8 | {D61A2E10-513B-4F20-8046-CAA914CEE437} 9 | {6BC8ED88-2882-458C-8E55-DFD12B67127B};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 10 | Exe 11 | SQLiteTouchTests 12 | Resources 13 | SQLiteTouchTests 14 | 15 | 16 | True 17 | full 18 | False 19 | bin\iPhoneSimulator\Debug 20 | DEBUG; 21 | prompt 22 | 4 23 | False 24 | True 25 | True 26 | None 27 | 28 | 29 | none 30 | True 31 | bin\iPhoneSimulator\Release 32 | prompt 33 | 4 34 | False 35 | None 36 | 37 | 38 | True 39 | full 40 | False 41 | bin\iPhone\Debug 42 | DEBUG; 43 | prompt 44 | 4 45 | False 46 | iPhone Developer 47 | True 48 | True 49 | 50 | 51 | none 52 | True 53 | bin\iPhone\Release 54 | prompt 55 | 4 56 | False 57 | iPhone Developer 58 | ARMv7 59 | 60 | 61 | 62 | none 63 | True 64 | bin\iPhone\Ad-Hoc 65 | prompt 66 | 4 67 | False 68 | True 69 | iPhone Distribution 70 | 71 | 72 | none 73 | True 74 | bin\iPhone\AppStore 75 | prompt 76 | 4 77 | False 78 | iPhone Distribution 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | AsyncTests.cs 95 | 96 | 97 | BooleanTest.cs 98 | 99 | 100 | ByteArrayTest.cs 101 | 102 | 103 | CollateTest.cs 104 | 105 | 106 | ContainsTest.cs 107 | 108 | 109 | CreateTableTest.cs 110 | 111 | 112 | DateTimeTest.cs 113 | 114 | 115 | DeleteTest.cs 116 | 117 | 118 | DropTableTest.cs 119 | 120 | 121 | ExceptionAssert.cs 122 | 123 | 124 | GuidTests.cs 125 | 126 | 127 | InheritanceTest.cs 128 | 129 | 130 | InsertTest.cs 131 | 132 | 133 | JoinTest.cs 134 | 135 | 136 | LinqTest.cs 137 | 138 | 139 | MappingTest.cs 140 | 141 | 142 | NullableTest.cs 143 | 144 | 145 | ScalarTest.cs 146 | 147 | 148 | SkipTest.cs 149 | 150 | 151 | StringQueryTest.cs 152 | 153 | 154 | TestDb.cs 155 | 156 | 157 | TransactionTest.cs 158 | 159 | 160 | UnicodeTest.cs 161 | 162 | 163 | UniqueTest.cs 164 | 165 | 166 | SQLite.cs 167 | 168 | 169 | SQLiteAsync.cs 170 | 171 | 172 | 173 | -------------------------------------------------------------------------------- /tests/ScalarTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | #if NETFX_CORE 5 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 6 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 7 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 8 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 9 | #else 10 | using NUnit.Framework; 11 | #endif 12 | 13 | namespace SQLite.Tests 14 | { 15 | [TestFixture] 16 | public class ScalarTest 17 | { 18 | class TestTable 19 | { 20 | [PrimaryKey, AutoIncrement] 21 | public int Id { get; set; } 22 | public int Two { get; set; } 23 | } 24 | 25 | const int Count = 100; 26 | 27 | SQLiteConnection CreateDb () 28 | { 29 | var db = new TestDb (); 30 | db.CreateTable (); 31 | var items = from i in Enumerable.Range (0, Count) 32 | select new TestTable { Two = 2 }; 33 | db.InsertAll (items); 34 | Assert.AreEqual (Count, db.Table ().Count ()); 35 | return db; 36 | } 37 | 38 | 39 | [Test] 40 | public void Int32 () 41 | { 42 | var db = CreateDb (); 43 | 44 | var r = db.ExecuteScalar ("SELECT SUM(Two) FROM TestTable"); 45 | 46 | Assert.AreEqual (Count * 2, r); 47 | } 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /tests/SkipTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using SQLite; 7 | 8 | #if NETFX_CORE 9 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 10 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 11 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 12 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 13 | #else 14 | using NUnit.Framework; 15 | #endif 16 | 17 | using System.Diagnostics; 18 | 19 | namespace SQLite.Tests 20 | { 21 | [TestFixture] 22 | public class SkipTest 23 | { 24 | public class TestObj 25 | { 26 | [AutoIncrement, PrimaryKey] 27 | public int Id { get; set; } 28 | public int Order { get; set; } 29 | 30 | public override string ToString () 31 | { 32 | return string.Format("[TestObj: Id={0}, Order={1}]", Id, Order); 33 | } 34 | 35 | } 36 | public class TestDb : SQLiteConnection 37 | { 38 | public TestDb(String path) 39 | : base(path) 40 | { 41 | CreateTable(); 42 | } 43 | } 44 | 45 | [Test] 46 | public void Skip() 47 | { 48 | var n = 100; 49 | 50 | var cq = from i in Enumerable.Range(1, n) 51 | select new TestObj() { 52 | Order = i 53 | }; 54 | var objs = cq.ToArray(); 55 | var db = new TestDb(TestPath.GetTempFileName()); 56 | 57 | var numIn = db.InsertAll(objs); 58 | Assert.AreEqual(numIn, n, "Num inserted must = num objects"); 59 | 60 | var q = from o in db.Table() 61 | orderby o.Order 62 | select o; 63 | 64 | var qs1 = q.Skip(1); 65 | var s1 = qs1.ToList(); 66 | Assert.AreEqual(n - 1, s1.Count); 67 | Assert.AreEqual(2, s1[0].Order); 68 | 69 | var qs5 = q.Skip(5); 70 | var s5 = qs5.ToList(); 71 | Assert.AreEqual(n - 5, s5.Count); 72 | Assert.AreEqual(6, s5[0].Order); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /tests/StringQueryTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Collections.Generic; 4 | 5 | #if NETFX_CORE 6 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 7 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 8 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 9 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 10 | #else 11 | using NUnit.Framework; 12 | #endif 13 | 14 | namespace SQLite.Tests 15 | { 16 | [TestFixture] 17 | public class StringQueryTest 18 | { 19 | TestDb db; 20 | 21 | [SetUp] 22 | public void SetUp () 23 | { 24 | db = new TestDb (); 25 | db.CreateTable (); 26 | 27 | var prods = new[] { 28 | new Product { Name = "Foo" }, 29 | new Product { Name = "Bar" }, 30 | new Product { Name = "Foobar" }, 31 | }; 32 | 33 | db.InsertAll (prods); 34 | } 35 | 36 | [Test] 37 | public void StartsWith () 38 | { 39 | var fs = db.Table ().Where (x => x.Name.StartsWith ("F")).ToList (); 40 | Assert.AreEqual (2, fs.Count); 41 | 42 | var bs = db.Table ().Where (x => x.Name.StartsWith ("B")).ToList (); 43 | Assert.AreEqual (1, bs.Count); 44 | } 45 | 46 | [Test] 47 | public void EndsWith () 48 | { 49 | var fs = db.Table ().Where (x => x.Name.EndsWith ("ar")).ToList (); 50 | Assert.AreEqual (2, fs.Count); 51 | 52 | var bs = db.Table ().Where (x => x.Name.EndsWith ("o")).ToList (); 53 | Assert.AreEqual (1, bs.Count); 54 | } 55 | 56 | [Test] 57 | public void Contains () 58 | { 59 | var fs = db.Table ().Where (x => x.Name.Contains ("o")).ToList (); 60 | Assert.AreEqual (2, fs.Count); 61 | 62 | var bs = db.Table ().Where (x => x.Name.Contains ("a")).ToList (); 63 | Assert.AreEqual (2, bs.Count); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /tests/TestDb.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | 4 | #if NETFX_CORE 5 | class DescriptionAttribute : Attribute 6 | { 7 | public DescriptionAttribute (string desc) 8 | { 9 | } 10 | } 11 | #endif 12 | 13 | namespace SQLite.Tests 14 | { 15 | public class Product 16 | { 17 | [AutoIncrement, PrimaryKey] 18 | public int Id { get; set; } 19 | public string Name { get; set; } 20 | public decimal Price { get; set; } 21 | 22 | public uint TotalSales { get; set; } 23 | } 24 | public class Order 25 | { 26 | [AutoIncrement, PrimaryKey] 27 | public int Id { get; set; } 28 | public DateTime PlacedTime { get; set; } 29 | } 30 | public class OrderHistory { 31 | [AutoIncrement, PrimaryKey] 32 | public int Id { get; set; } 33 | public int OrderId { get; set; } 34 | public DateTime Time { get; set; } 35 | public string Comment { get; set; } 36 | } 37 | public class OrderLine 38 | { 39 | [AutoIncrement, PrimaryKey] 40 | public int Id { get; set; } 41 | [Indexed("IX_OrderProduct", 1)] 42 | public int OrderId { get; set; } 43 | [Indexed("IX_OrderProduct", 2)] 44 | public int ProductId { get; set; } 45 | public int Quantity { get; set; } 46 | public decimal UnitPrice { get; set; } 47 | public OrderLineStatus Status { get; set; } 48 | } 49 | public enum OrderLineStatus { 50 | Placed = 1, 51 | Shipped = 100 52 | } 53 | 54 | public class TestDb : SQLiteConnection 55 | { 56 | public TestDb (bool storeDateTimeAsTicks = false) : base (TestPath.GetTempFileName (), storeDateTimeAsTicks) 57 | { 58 | Trace = true; 59 | } 60 | } 61 | 62 | public class TestPath 63 | { 64 | public static string GetTempFileName () 65 | { 66 | #if NETFX_CORE 67 | var name = Guid.NewGuid () + ".sqlite"; 68 | return Path.Combine (Windows.Storage.ApplicationData.Current.LocalFolder.Path, name); 69 | #else 70 | return Path.GetTempFileName (); 71 | #endif 72 | } 73 | } 74 | } 75 | 76 | -------------------------------------------------------------------------------- /tests/TransactionTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | #if NETFX_CORE 6 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 7 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 8 | using TearDown = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestCleanupAttribute; 9 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 10 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 11 | #else 12 | using NUnit.Framework; 13 | #endif 14 | 15 | namespace SQLite.Tests 16 | { 17 | [TestFixture] 18 | public class TransactionTest 19 | { 20 | private TestDb db; 21 | private List testObjects; 22 | 23 | public class TestObj 24 | { 25 | [AutoIncrement, PrimaryKey] 26 | public int Id { get; set; } 27 | 28 | public override string ToString() 29 | { 30 | return string.Format("[TestObj: Id={0}]", Id); 31 | } 32 | } 33 | 34 | public class TransactionTestException : Exception 35 | { 36 | } 37 | 38 | public class TestDb : SQLiteConnection 39 | { 40 | public TestDb(String path) : base(path) 41 | { 42 | CreateTable(); 43 | } 44 | } 45 | 46 | [SetUp] 47 | public void Setup() 48 | { 49 | testObjects = Enumerable.Range(1, 20).Select(i => new TestObj()).ToList(); 50 | 51 | db = new TestDb(TestPath.GetTempFileName()); 52 | db.InsertAll(testObjects); 53 | } 54 | 55 | [TearDown] 56 | public void TearDown() 57 | { 58 | if (db != null) { 59 | db.Close(); 60 | } 61 | } 62 | 63 | [Test] 64 | public void SuccessfulSavepointTransaction() 65 | { 66 | db.RunInTransaction(() => { 67 | db.Delete(testObjects[0]); 68 | db.Delete(testObjects[1]); 69 | db.Insert(new TestObj()); 70 | }); 71 | 72 | Assert.AreEqual(testObjects.Count - 1, db.Table().Count()); 73 | } 74 | 75 | [Test] 76 | public void FailSavepointTransaction() 77 | { 78 | try { 79 | db.RunInTransaction(() => { 80 | db.Delete(testObjects[0]); 81 | 82 | throw new TransactionTestException(); 83 | }); 84 | } catch (TransactionTestException) { 85 | // ignore 86 | } 87 | 88 | Assert.AreEqual(testObjects.Count, db.Table().Count()); 89 | } 90 | 91 | [Test] 92 | public void SuccessfulNestedSavepointTransaction() 93 | { 94 | db.RunInTransaction(() => { 95 | db.Delete(testObjects[0]); 96 | 97 | db.RunInTransaction(() => { 98 | db.Delete(testObjects[1]); 99 | }); 100 | }); 101 | 102 | Assert.AreEqual(testObjects.Count - 2, db.Table().Count()); 103 | } 104 | 105 | [Test] 106 | public void FailNestedSavepointTransaction() 107 | { 108 | try { 109 | db.RunInTransaction(() => { 110 | db.Delete(testObjects[0]); 111 | 112 | db.RunInTransaction(() => { 113 | db.Delete(testObjects[1]); 114 | 115 | throw new TransactionTestException(); 116 | }); 117 | }); 118 | } catch (TransactionTestException) { 119 | // ignore 120 | } 121 | 122 | Assert.AreEqual(testObjects.Count, db.Table().Count()); 123 | } 124 | } 125 | } 126 | 127 | -------------------------------------------------------------------------------- /tests/UnicodeTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | #if NETFX_CORE 5 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 6 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 7 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 8 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 9 | #else 10 | using NUnit.Framework; 11 | #endif 12 | 13 | 14 | namespace SQLite.Tests 15 | { 16 | [TestFixture] 17 | public class UnicodeTest 18 | { 19 | [Test] 20 | public void Insert () 21 | { 22 | var db = new TestDb (); 23 | 24 | db.CreateTable (); 25 | 26 | string testString = "\u2329\u221E\u232A"; 27 | 28 | db.Insert (new Product { 29 | Name = testString, 30 | }); 31 | 32 | var p = db.Get (1); 33 | 34 | Assert.AreEqual (testString, p.Name); 35 | } 36 | 37 | [Test] 38 | public void Query () 39 | { 40 | var db = new TestDb (); 41 | 42 | db.CreateTable (); 43 | 44 | string testString = "\u2329\u221E\u232A"; 45 | 46 | db.Insert (new Product { 47 | Name = testString, 48 | }); 49 | 50 | var ps = (from p in db.Table () where p.Name == testString select p).ToList (); 51 | 52 | Assert.AreEqual (1, ps.Count); 53 | Assert.AreEqual (testString, ps [0].Name); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /tests/UniqueTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | #if NETFX_CORE 6 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 7 | using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; 8 | using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 9 | using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 10 | #else 11 | using NUnit.Framework; 12 | #endif 13 | 14 | 15 | namespace SQLite.Tests 16 | { 17 | [TestFixture] 18 | public class UniqueIndexTest 19 | { 20 | public class TheOne { 21 | [PrimaryKey, AutoIncrement] 22 | public int ID { get; set; } 23 | 24 | [Unique (Name = "UX_Uno")] 25 | public int Uno { get; set;} 26 | 27 | [Unique (Name = "UX_Dos")] 28 | public int Dos { get; set;} 29 | [Unique (Name = "UX_Dos")] 30 | public int Tres { get; set;} 31 | 32 | [Indexed (Name = "UX_Uno_bool", Unique = true)] 33 | public int Cuatro { get; set;} 34 | 35 | [Indexed (Name = "UX_Dos_bool", Unique = true)] 36 | public int Cinco { get; set;} 37 | [Indexed (Name = "UX_Dos_bool", Unique = true)] 38 | public int Seis { get; set;} 39 | } 40 | 41 | public class IndexColumns { 42 | public int seqno { get; set;} 43 | public int cid { get; set;} 44 | public string name { get; set; } 45 | } 46 | 47 | public class IndexInfo { 48 | public int seq { get; set;} 49 | public string name { get; set;} 50 | public bool unique { get; set;} 51 | } 52 | 53 | [Test] 54 | public void CreateUniqueIndexes () 55 | { 56 | using (var db = new TestDb ()) { 57 | db.CreateTable (); 58 | var indexes = db.Query ("PRAGMA INDEX_LIST (\"TheOne\")"); 59 | Assert.AreEqual (4, indexes.Count, "# of indexes"); 60 | CheckIndex (db, indexes, "UX_Uno", true, "Uno"); 61 | CheckIndex (db, indexes, "UX_Dos", true, "Dos", "Tres"); 62 | CheckIndex (db, indexes, "UX_Uno_bool", true, "Cuatro"); 63 | CheckIndex (db, indexes, "UX_Dos_bool", true, "Cinco", "Seis"); 64 | } 65 | } 66 | 67 | static void CheckIndex (TestDb db, List indexes, string iname, bool unique, params string [] columns) 68 | { 69 | if (columns == null) 70 | throw new Exception ("Don't!"); 71 | var idx = indexes.SingleOrDefault (i => i.name == iname); 72 | Assert.IsNotNull (idx, String.Format ("Index {0} not found", iname)); 73 | Assert.AreEqual (idx.unique, unique, String.Format ("Index {0} unique expected {1} but got {2}", iname, unique, idx.unique)); 74 | var idx_columns = db.Query (String.Format ("PRAGMA INDEX_INFO (\"{0}\")", iname)); 75 | Assert.AreEqual (columns.Length, idx_columns.Count, String.Format ("# of columns: expected {0}, got {1}", columns.Length, idx_columns.Count)); 76 | foreach (var col in columns) { 77 | Assert.IsNotNull (idx_columns.SingleOrDefault (c => c.name == col), String.Format ("Column {0} not in index {1}", col, idx.name)); 78 | } 79 | } 80 | } 81 | } 82 | --------------------------------------------------------------------------------