1+ using SqlKata . Compilers ;
2+ using Xunit ;
3+ using SqlKata . Execution ;
4+ using MySql . Data . MySqlClient ;
5+ using System ;
6+ using System . Linq ;
7+ using static SqlKata . Expressions ;
8+ using System . Collections . Generic ;
9+ using Microsoft . Data . Sqlite ;
10+
11+ namespace SqlKata . Tests
12+ {
13+ public class SqliteExecutionTest
14+ {
15+ [ Fact ]
16+ public void EmptySelect ( )
17+ {
18+
19+ var db = DB ( ) . Create ( "Cars" , new [ ] {
20+ "Id INTEGER PRIMARY KEY AUTOINCREMENT" ,
21+ "Brand TEXT NOT NULL" ,
22+ "Year INT NOT NULL" ,
23+ "Color TEXT NULL" ,
24+ } ) ;
25+
26+
27+ var tables = db . Select ( @"SELECT name FROM sqlite_schema WHERE type ='table' AND name NOT LIKE 'sqlite_%'" ) ;
28+
29+ var rows = db . Query ( "Cars" ) . Get ( ) ;
30+
31+ Assert . Empty ( rows ) ;
32+
33+ db . Drop ( "Cars" ) ;
34+ }
35+
36+ [ Fact ]
37+ public void SelectWithLimit ( )
38+ {
39+ var db = DB ( ) . Create ( "Cars" , new [ ] {
40+ "Id INTEGER PRIMARY KEY AUTOINCREMENT" ,
41+ "Brand TEXT NOT NULL" ,
42+ "Year INT NOT NULL" ,
43+ "Color TEXT NULL" ,
44+ } ) ;
45+
46+ db . Statement ( "INSERT INTO `Cars`(Brand, Year) VALUES ('Honda', 2020)" ) ;
47+
48+ var rows = db . Query ( "Cars" ) . Get ( ) . ToList ( ) ;
49+
50+ Assert . Single ( rows ) ;
51+
52+ db . Drop ( "Cars" ) ;
53+ }
54+
55+ [ Fact ]
56+ public void InsertGetId ( )
57+ {
58+ var db = DB ( ) . Create ( "Cars" , new [ ] {
59+ "Id INTEGER PRIMARY KEY AUTOINCREMENT" ,
60+ "Brand TEXT NOT NULL" ,
61+ "Year INT NOT NULL" ,
62+ } ) ;
63+
64+ var id = db . Query ( "Cars" ) . InsertGetId < int > ( new
65+ {
66+ Brand = "Toyota" ,
67+ Year = 1900
68+ } ) ;
69+
70+ Assert . Equal ( 1 , id ) ;
71+
72+ id = db . Query ( "Cars" ) . InsertGetId < int > ( new
73+ {
74+ Brand = "Toyota 2" ,
75+ Year = 1901
76+ } ) ;
77+
78+ Assert . Equal ( 2 , id ) ;
79+
80+ id = db . Query ( "Cars" ) . InsertGetId < int > ( new
81+ {
82+ Brand = "Toyota 2" ,
83+ Year = 1901
84+ } ) ;
85+
86+ Assert . Equal ( 3 , id ) ;
87+
88+
89+ db . Drop ( "Cars" ) ;
90+ }
91+
92+
93+
94+ [ Fact ]
95+ public void Count ( )
96+ {
97+ var db = DB ( ) . Create ( "Cars" , new [ ] {
98+ "Id INTEGER PRIMARY KEY AUTOINCREMENT" ,
99+ "Brand TEXT NOT NULL" ,
100+ "Year INT NOT NULL" ,
101+ "Color TEXT NULL" ,
102+ } ) ;
103+
104+ db . Statement ( "INSERT INTO `Cars`(Brand, Year) VALUES ('Honda', 2020)" ) ;
105+ var count = db . Query ( "Cars" ) . Count < int > ( ) ;
106+ Assert . Equal ( 1 , count ) ;
107+
108+ db . Statement ( "INSERT INTO `Cars`(Brand, Year) VALUES ('Toyota', 2021)" ) ;
109+ count = db . Query ( "Cars" ) . Count < int > ( ) ;
110+ Assert . Equal ( 2 , count ) ;
111+
112+ int affected = db . Query ( "Cars" ) . Delete ( ) ;
113+ Assert . Equal ( 2 , affected ) ;
114+
115+ count = db . Query ( "Cars" ) . Count < int > ( ) ;
116+ Assert . Equal ( 0 , count ) ;
117+
118+ db . Drop ( "Cars" ) ;
119+ }
120+
121+ [ Fact ]
122+ public void CloneThenCount ( )
123+ {
124+ var db = DB ( ) . Create ( "Cars" , new [ ] {
125+ "Id INTEGER PRIMARY KEY AUTOINCREMENT" ,
126+ "Brand TEXT NOT NULL" ,
127+ "Year INT NOT NULL" ,
128+ "Color TEXT NULL" ,
129+ } ) ;
130+
131+ for ( int i = 0 ; i < 10 ; i ++ )
132+ {
133+ db . Query ( "Cars" ) . Insert ( new
134+ {
135+ Brand = "Brand " + i ,
136+ Year = "2020" ,
137+ } ) ;
138+ }
139+
140+ var query = db . Query ( "Cars" ) . Where ( "Id" , "<" , 5 ) ;
141+ var count = query . Count < int > ( ) ;
142+ var cloneCount = query . Clone ( ) . Count < int > ( ) ;
143+
144+ Assert . Equal ( 4 , count ) ;
145+ Assert . Equal ( 4 , cloneCount ) ;
146+
147+ db . Drop ( "Cars" ) ;
148+ }
149+
150+ [ Fact ]
151+ public void QueryWithVariable ( )
152+ {
153+ var db = DB ( ) . Create ( "Cars" , new [ ] {
154+ "Id INTEGER PRIMARY KEY AUTOINCREMENT" ,
155+ "Brand TEXT NOT NULL" ,
156+ "Year INT NOT NULL" ,
157+ "Color TEXT NULL" ,
158+ } ) ;
159+
160+ for ( int i = 0 ; i < 10 ; i ++ )
161+ {
162+ db . Query ( "Cars" ) . Insert ( new
163+ {
164+ Brand = "Brand " + i ,
165+ Year = "2020" ,
166+ } ) ;
167+ }
168+
169+
170+ var count = db . Query ( "Cars" )
171+ . Define ( "Threshold" , 5 )
172+ . Where ( "Id" , "<" , SqlKata . Expressions . Variable ( "Threshold" ) )
173+ . Count < int > ( ) ;
174+
175+ Assert . Equal ( 4 , count ) ;
176+
177+ db . Drop ( "Cars" ) ;
178+ }
179+
180+ [ Fact ]
181+ public void InlineTable ( )
182+ {
183+ var db = DB ( ) . Create ( "Transaction" , new [ ] {
184+ "Id INTEGER PRIMARY KEY AUTOINCREMENT" ,
185+ "Amount int NOT NULL" ,
186+ "Date DATE NOT NULL" ,
187+ } ) ;
188+
189+ db . Query ( "Transaction" ) . Insert ( new
190+ {
191+ Date = "2022-01-01" ,
192+ Amount = 10
193+ } ) ;
194+
195+
196+ var rows = db . Query ( "Transaction" )
197+ . With ( "Rates" , new [ ] { "Date" , "Rate" } , new object [ ] [ ] {
198+ new object [ ] { "2022-01-01" , 0.5 } ,
199+ } )
200+ . Join ( "Rates" , "Rates.Date" , "Transaction.Date" )
201+ . SelectRaw ( "([Transaction].[Amount] * [Rates].[Rate]) as AmountConverted" )
202+ . Get ( ) ;
203+
204+ Assert . Single ( rows ) ;
205+ Assert . Equal ( 5 , rows . First ( ) . AmountConverted ) ;
206+
207+ db . Drop ( "Transaction" ) ;
208+ }
209+
210+ QueryFactory DB ( )
211+ {
212+ var cs = $ "Data Source=file::memory:;Cache=Shared";
213+
214+ var connection = new SqliteConnection ( cs ) ;
215+
216+ var db = new QueryFactory ( connection , new SqliteCompiler ( ) ) ;
217+
218+ return db ;
219+ }
220+
221+
222+
223+ }
224+ }
0 commit comments