Skip to content

Commit d44b31c

Browse files
committed
Don't throw an exception when ExecuteNonQuery is called and the call returns some rows. Update doc.
1 parent f74dbda commit d44b31c

File tree

9 files changed

+80
-78
lines changed

9 files changed

+80
-78
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ TestResults
1313
/SQLite.Net.sln.ide
1414
/.vs
1515
/nuget/SQLite.Net*
16+
/.idea

README.md

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
# Setup
22

3-
Add this package to your netstandard project:
3+
Add this package to a netstandard compatible project:
44

55
[![NuGet](https://img.shields.io/nuget/v/sqlite-net2.svg)](https://www.nuget.org/packages/sqlite-net2/)
66
![Nuget](https://img.shields.io/nuget/dt/sqlite-net2)
77

8-
Also add one of the https://github.com/ericsink/SQLitePCL.raw package of your choice to the netstandard project:
9-
- SQLitePCLRaw.bundle_e_sqlite3 for a normal database file
10-
- SQLitePCLRaw.bundle_e_sqlcipher for a crypted database file
8+
**Required**: add ONLY ONE of the followin packages to your common project:
9+
- [SQLitePCLRaw.bundle_e_sqlite3](https://www.nuget.org/packages/SQLitePCLRaw.bundle_e_sqlite3) for a normal database file
10+
- [SQLitePCLRaw.bundle_e_sqlcipher](https://www.nuget.org/packages/SQLitePCLRaw.bundle_e_sqlcipher) for an encrypted database file
1111

12-
And call this statup function in each of your platform projects:
12+
Then call the init method once. That can be in your App.cs:
1313

14-
```csharp
14+
```c#
1515
SQLitePCL.Batteries_V2.Init()
1616
```
1717

18-
For a simple key/value store based on sqlite, or a drop-in replacement (alternative) to the unstable Akavache, check https://github.com/softlion/KeyValueLite
19-
2018
# Features
2119

2220
* Netstandard 2+
2321
* Uses SQLitePCLRaw for sqlite raw communication
2422
* Compatible with SQLitePCLRaw standard and cypher
2523
* Stable and used in tons of apps
2624

25+
For a key/value store based on sqlite, or a drop-in replacement (alternative) to the unstable Akavache, check [KeyValueLite](https://github.com/softlion/KeyValueLite)
26+
2727
# Other Features (compared to oysteinkrog)
2828

2929
* Multiple primary key support
3030
Ex:
31-
```csharp
32-
public class PrivacyGroupItem
33-
{
34-
[PrimaryKey]
35-
public int GroupId {get;set;}
36-
37-
[PrimaryKey]
38-
public int ContactId {get;set;}
39-
}
40-
41-
db.Delete<PrivacyGroupItem>(groupId, contactId);
42-
```
31+
```c#
32+
public class PrivacyGroupItem
33+
{
34+
[PrimaryKey]
35+
public int GroupId {get;set;}
36+
37+
[PrimaryKey]
38+
public int ContactId {get;set;}
39+
}
40+
41+
db.Delete<PrivacyGroupItem>(groupId, contactId);
42+
```
4343

4444
* Projections now have the expected result type
4545
Ex: `IEnumerable<int> ids = from pgi in db.Table<PrivacyGroupItem>() where pgi.PrivacyGroupId == groupId select pgi.ContactId;`
@@ -64,7 +64,7 @@ Note: see unit tests for more examples.
6464

6565
## Define the database schema using a code first approach.
6666

67-
```csharp
67+
```c#
6868
public class DbStock
6969
{
7070
[PrimaryKey, AutoIncrement]
@@ -92,7 +92,7 @@ Note: see unit tests for more examples.
9292

9393
## Create the schema
9494

95-
```csharp
95+
```c#
9696
var dbFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
9797
dbFilePath = Path.Combine(dbFilePath, "store.db3");
9898
var exists = File.Exists(dbFilePath);
@@ -122,7 +122,7 @@ Note: see unit tests for more examples.
122122

123123
Simple add, update and delete:
124124

125-
```csharp
125+
```c#
126126
var stock = new DbStock() { Symbol = "EUR" };
127127
db.Insert(stock);
128128
stock.Symbol = "USD";
@@ -140,7 +140,7 @@ After the Insert call, stock.Id will be set, because Id has the AutoIncrement at
140140

141141
Simple query using LINQ. Most linq operators work:
142142

143-
```csharp
143+
```c#
144144
var stocksStartingWithA = db.Table<DbStock>()
145145
.Where(stock => stock.Symbol.StartsWith("A"))
146146
.OrderBy(stock => stock.Symbol)
@@ -151,14 +151,14 @@ Simple query using LINQ. Most linq operators work:
151151

152152
Advanced queries using SQL:
153153

154-
```csharp
154+
```c#
155155
var dbValuation = db.Query<DbValuation> ("select * from DbValuation where StockId = ?", stock.Id);
156156
db.Execute("delete * from DbValuation where StockId = ?", stock.Id);
157157
```
158158

159159
The T in `db.Query<T>` specifies the object to create for each row. It can be a table class, or any other class whose public properties match the query columns.
160160

161-
```csharp
161+
```c#
162162
public class Val {
163163
public decimal Money { get; set; }
164164
public DateTime Date { get; set; }
@@ -169,15 +169,17 @@ The T in `db.Query<T>` specifies the object to create for each row. It can be a
169169
}
170170
```
171171

172-
## Encrypting the database file
173-
174-
Add the nuget `SQLitePCLRaw.bundle_e_sqlcipher` to your project containing `sqlite-net2`.
172+
## Using an encrypted database file
175173

176-
Call this right after opening or creating the db, as the 1st instruction.
174+
Add the nuget [SQLitePCLRaw.bundle_e_sqlcipher](https://www.nuget.org/packages/SQLitePCLRaw.bundle_e_sqlcipher) to your common project.
175+
Then add this code right after opening or creating the db, and before any other db call:
177176

178-
```csharp
177+
```c#
178+
string key = "yourCryptingKey";
179179
var db = new SQLiteConnection(filePath);
180-
db.Execute($"PRAGMA key = '{key}';");
180+
var ok = db.Execute<string>($"PRAGMA key = '{key}';");
181+
if(ok != "ok")
182+
throw new Exception("Bad key");
181183
```
182184

183-
And use the db as usual.
185+
Then use the db as usual.

SQLite.Net.Tests/SQLite.Net2.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
<ItemGroup>
1010
<PackageReference Include="nunit" Version="3.13.2" />
11-
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0">
11+
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1">
1212
<PrivateAssets>all</PrivateAssets>
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
1515
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
16-
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.0.6" />
16+
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.0.7" />
1717
</ItemGroup>
1818

1919
<ItemGroup>

examples/Stocks/Stocks.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<OutputType>Exe</OutputType>
88
<RootNamespace>Stocks</RootNamespace>
99
<AssemblyName>Stocks</AssemblyName>
10-
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
10+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
1111
<FileUpgradeFlags>
1212
</FileUpgradeFlags>
1313
<UpgradeBackupLocation>
@@ -80,7 +80,7 @@
8080
</ItemGroup>
8181
<ItemGroup>
8282
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlcipher">
83-
<Version>2.0.6</Version>
83+
<Version>2.0.7</Version>
8484
</PackageReference>
8585
</ItemGroup>
8686
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

examples/Stocks/app.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?xml version="1.0"?>
22
<configuration>
3-
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/></startup></configuration>
3+
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup></configuration>

nuget/pack.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if ($IsMacOS) {
66
$msbuild = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath
77
$msbuild = join-path $msbuild 'MSBuild\Current\Bin\MSBuild.exe'
88
}
9-
$version="2.0.6"
9+
$version="2.0.7"
1010
$versionSuffix=""
1111

1212
#####################

src/SQLite.Net/PreparedSqlLiteInsertCommand.cs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal PreparedSqlLiteInsertCommand(SQLiteConnection conn, string commandText)
4545
this.commandText = commandText;
4646
}
4747

48-
public int ExecuteNonQuery(object[] source)
48+
public int ExecuteNonQuery(object[]? source)
4949
{
5050
Connection.TraceListener.WriteLine("Executing: {0}", commandText);
5151

@@ -71,28 +71,27 @@ public int ExecuteNonQuery(object[] source)
7171
r = sqlite.Step(Statement);
7272
}
7373

74-
if (r == Result.Done)
74+
switch (r)
7575
{
76-
var rowsAffected = sqlite.Changes(Connection.Handle);
77-
sqlite.Reset(Statement);
78-
return rowsAffected;
76+
case Result.Done or Result.Row:
77+
{
78+
var rowsAffected = sqlite.Changes(Connection.Handle);
79+
sqlite.Reset(Statement);
80+
return rowsAffected;
81+
}
82+
case Result.Error:
83+
{
84+
var msg = sqlite.Errmsg16(Connection.Handle);
85+
sqlite.Reset(Statement);
86+
throw new SQLiteException(r, msg);
87+
}
88+
case Result.Constraint when sqlite.ExtendedErrCode(Connection.Handle) == ExtendedResult.ConstraintNotNull:
89+
sqlite.Reset(Statement);
90+
throw new NotNullConstraintViolationException(r, sqlite.Errmsg16(Connection.Handle));
91+
default:
92+
sqlite.Reset(Statement);
93+
throw new SQLiteException(r, sqlite.Errmsg16(Connection.Handle));
7994
}
80-
81-
if (r == Result.Error)
82-
{
83-
var msg = sqlite.Errmsg16(Connection.Handle);
84-
sqlite.Reset(Statement);
85-
throw new SQLiteException(r, msg);
86-
}
87-
88-
if (r == Result.Constraint && sqlite.ExtendedErrCode(Connection.Handle) == ExtendedResult.ConstraintNotNull)
89-
{
90-
sqlite.Reset(Statement);
91-
throw new NotNullConstraintViolationException(r, sqlite.Errmsg16(Connection.Handle));
92-
}
93-
94-
sqlite.Reset(Statement);
95-
throw new SQLiteException(r, sqlite.Errmsg16(Connection.Handle));
9695
}
9796

9897

src/SQLite.Net/SQLite.Net2.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
<DebugType>portable</DebugType>
99
<AssemblyProduct>SQLite.Net2</AssemblyProduct>
10-
<AssemblyCopyright>Copyright ©2021 Benjamin Mayrargue</AssemblyCopyright>
10+
<AssemblyCopyright>Copyright ©2022 Benjamin Mayrargue</AssemblyCopyright>
1111
<LangVersion>Latest</LangVersion>
12+
<Nullable>enable</Nullable>
1213
</PropertyGroup>
1314

1415
<!-- nuget configurable properties -->
@@ -46,7 +47,7 @@
4647
</PropertyGroup>
4748

4849
<ItemGroup>
49-
<PackageReference Include="SQLitePCLRaw.core" Version="2.0.6" />
50+
<PackageReference Include="SQLitePCLRaw.core" Version="2.0.7" />
5051
<None Include="..\..\LICENSE.md" Pack="true" PackagePath="" />
5152
</ItemGroup>
5253

src/SQLite.Net/SQLiteCommand.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Copyright (c) 2012 Krueger Systems, Inc.
3-
// Copyright (c) 2013 Øystein Krog ([email protected])
4-
// Copyright (c) 2014 Benjamin Mayrarague
3+
// Copyright (c) 2013 Oystein Krog ([email protected])
4+
// Copyright (c) 2014 Benjamin Mayrargue
55
// - support for new types: XElement, TimeSpan
66
// - ExecuteSimpleQuery
77
// - ExecuteDeferredQuery: support primitive types in T
@@ -63,24 +63,23 @@ public int ExecuteNonQuery()
6363
var stmt = Prepare();
6464
var r = sqlite.Step(stmt);
6565
Finalize(stmt);
66-
if (r == Result.Done)
66+
switch (r)
6767
{
68-
var rowsAffected = sqlite.Changes(_conn.Handle);
69-
return rowsAffected;
70-
}
71-
if (r == Result.Error)
72-
{
73-
var msg = sqlite.Errmsg16(_conn.Handle);
74-
throw new SQLiteException(r, msg);
75-
}
76-
if (r == Result.Constraint)
77-
{
78-
if (sqlite.ExtendedErrCode(_conn.Handle) == ExtendedResult.ConstraintNotNull)
68+
case Result.Done or Result.Row:
7969
{
80-
throw new NotNullConstraintViolationException(r, sqlite.Errmsg16(_conn.Handle));
70+
var rowsAffected = sqlite.Changes(_conn.Handle);
71+
return rowsAffected;
72+
}
73+
case Result.Error:
74+
{
75+
var msg = sqlite.Errmsg16(_conn.Handle);
76+
throw new SQLiteException(r, msg);
8177
}
78+
case Result.Constraint when sqlite.ExtendedErrCode(_conn.Handle) == ExtendedResult.ConstraintNotNull:
79+
throw new NotNullConstraintViolationException(r, sqlite.Errmsg16(_conn.Handle));
80+
default:
81+
throw new SQLiteException(r, r.ToString());
8282
}
83-
throw new SQLiteException(r, r.ToString());
8483
}
8584

8685

0 commit comments

Comments
 (0)