Skip to content

Commit ff63d8e

Browse files
committed
Merge pull request #294 from Yukinobu-Mine/non_ascii_query
Fix an issue that miscount length of a query including non-ascii characters Fix #147 Fix #101
2 parents 377cc65 + 1c2b3c0 commit ff63d8e

File tree

14 files changed

+74
-57
lines changed

14 files changed

+74
-57
lines changed

src/SQLite.Net.Platform.Generic/SQLiteApiGeneric.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public IDbStatement Prepare2(IDbHandle db, string query)
7171
{
7272
var internalDbHandle = (DbHandle) db;
7373
IntPtr stmt;
74-
Result r = SQLiteApiGenericInternal.sqlite3_prepare_v2(internalDbHandle.DbPtr, query, query.Length, out stmt, IntPtr.Zero);
74+
Result r = SQLiteApiGenericInternal.sqlite3_prepare16_v2(internalDbHandle.DbPtr, query, -1, out stmt, IntPtr.Zero);
7575
if (r != Result.OK)
7676
{
7777
throw SQLiteException.New(r, Errmsg16(internalDbHandle));

src/SQLite.Net.Platform.Generic/SQLiteApiGenericInternal.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ public static extern int sqlite3_bind_text16(IntPtr stmt,
126126
[DllImport("sqlite3", EntryPoint = "sqlite3_open_v2", CallingConvention = CallingConvention.Cdecl)]
127127
public static extern Result sqlite3_open_v2(byte[] filename, out IntPtr db, int flags, IntPtr zvfs);
128128

129-
[DllImport("sqlite3", EntryPoint = "sqlite3_prepare_v2", CallingConvention = CallingConvention.Cdecl)]
130-
public static extern Result sqlite3_prepare_v2(IntPtr db,
131-
[MarshalAs(UnmanagedType.LPStr)] string sql,
129+
[DllImport("sqlite3", EntryPoint = "sqlite3_prepare16_v2", CallingConvention = CallingConvention.Cdecl)]
130+
public static extern Result sqlite3_prepare16_v2(IntPtr db,
131+
[MarshalAs(UnmanagedType.LPWStr)] string sql,
132132
int numBytes,
133133
out IntPtr stmt,
134134
IntPtr pzTail);

src/SQLite.Net.Platform.OSX/SQLiteApiOSX.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public IDbStatement Prepare2(IDbHandle db, string query)
7373
{
7474
var internalDbHandle = (DbHandle) db;
7575
IntPtr stmt;
76-
Result r = SQLiteApiOSXInternal.sqlite3_prepare_v2(internalDbHandle.DbPtr, query, query.Length, out stmt, IntPtr.Zero);
76+
Result r = SQLiteApiOSXInternal.sqlite3_prepare16_v2(internalDbHandle.DbPtr, query, -1, out stmt, IntPtr.Zero);
7777
if (r != Result.OK)
7878
{
7979
throw SQLiteException.New(r, Errmsg16(internalDbHandle));

src/SQLite.Net.Platform.OSX/SQliteApiOSXInternal.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public static extern Result sqlite3_open([MarshalAs(UnmanagedType.LPStr)] string
5050
[DllImport("libsqlite3_for_net", EntryPoint = "sqlite3_changes", CallingConvention = CallingConvention.Cdecl)]
5151
public static extern int sqlite3_changes(IntPtr db);
5252

53-
[DllImport("libsqlite3_for_net", EntryPoint = "sqlite3_prepare_v2", CallingConvention = CallingConvention.Cdecl)
53+
[DllImport("libsqlite3_for_net", EntryPoint = "sqlite3_prepare16_v2", CallingConvention = CallingConvention.Cdecl)
5454
]
55-
public static extern Result sqlite3_prepare_v2(IntPtr db, [MarshalAs(UnmanagedType.LPStr)] string sql,
55+
public static extern Result sqlite3_prepare16_v2(IntPtr db, [MarshalAs(UnmanagedType.LPWStr)] string sql,
5656
int numBytes,
5757
out IntPtr stmt, IntPtr pzTail);
5858

src/SQLite.Net.Platform.Win32/SQLiteApiWin32.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public IDbStatement Prepare2(IDbHandle db, string query)
7979
{
8080
var internalDbHandle = (DbHandle) db;
8181
IntPtr stmt;
82-
Result r = SQLiteApiWin32Internal.sqlite3_prepare_v2(internalDbHandle.DbPtr, query, query.Length, out stmt, IntPtr.Zero);
82+
Result r = SQLiteApiWin32Internal.sqlite3_prepare16_v2(internalDbHandle.DbPtr, query, -1, out stmt, IntPtr.Zero);
8383
if (r != Result.OK)
8484
{
8585
throw SQLiteException.New(r, Errmsg16(internalDbHandle));

src/SQLite.Net.Platform.Win32/SQliteApiWin32Internal.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ public static extern Result sqlite3_open([MarshalAs(UnmanagedType.LPStr)] string
9090
[DllImport("SQLite.Interop.dll", EntryPoint = "sqlite3_changes", CallingConvention = CallingConvention.Cdecl)]
9191
public static extern int sqlite3_changes(IntPtr db);
9292

93-
[DllImport("SQLite.Interop.dll", EntryPoint = "sqlite3_prepare_v2", CallingConvention = CallingConvention.Cdecl)
93+
[DllImport("SQLite.Interop.dll", EntryPoint = "sqlite3_prepare16_v2", CallingConvention = CallingConvention.Cdecl)
9494
]
95-
public static extern Result sqlite3_prepare_v2(IntPtr db, [MarshalAs(UnmanagedType.LPStr)] string sql,
95+
public static extern Result sqlite3_prepare16_v2(IntPtr db, [MarshalAs(UnmanagedType.LPWStr)] string sql,
9696
int numBytes,
9797
out IntPtr stmt, IntPtr pzTail);
9898

src/SQLite.Net.Platform.WinRT/SQLiteApiWinRT.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public IDbStatement Prepare2(IDbHandle db, string query)
204204
{
205205
var dbHandle = (DbHandle)db;
206206
var stmt = default(Sqlite3Statement);
207-
var r = SQLite3.Prepare2(dbHandle.InternalDbHandle, query, query.Length, out stmt, IntPtr.Zero);
207+
var r = SQLite3.Prepare2(dbHandle.InternalDbHandle, query, -1, out stmt, IntPtr.Zero);
208208
if (r != Result.OK)
209209
{
210210
throw SQLiteException.New(r, SQLite3.GetErrmsg(dbHandle.InternalDbHandle));
@@ -355,8 +355,8 @@ public static class SQLite3
355355
[DllImport("sqlite3", EntryPoint = "sqlite3_changes", CallingConvention = CallingConvention.Cdecl)]
356356
public static extern int Changes(IntPtr db);
357357

358-
[DllImport("sqlite3", EntryPoint = "sqlite3_prepare_v2", CallingConvention = CallingConvention.Cdecl)]
359-
public static extern Result Prepare2(IntPtr db, [MarshalAs(UnmanagedType.LPStr)] string sql, int numBytes, out IntPtr stmt, IntPtr pzTail);
358+
[DllImport("sqlite3", EntryPoint = "sqlite3_prepare16_v2", CallingConvention = CallingConvention.Cdecl)]
359+
public static extern Result Prepare2(IntPtr db, [MarshalAs(UnmanagedType.LPWStr)] string sql, int numBytes, out IntPtr stmt, IntPtr pzTail);
360360

361361
[DllImport("sqlite3", EntryPoint = "sqlite3_step", CallingConvention = CallingConvention.Cdecl)]
362362
public static extern Result Step(IntPtr stmt);

src/SQLite.Net.Platform.XamarinAndroid/SQLiteApiAndroid.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public IDbStatement Prepare2(IDbHandle db, string query)
7272
{
7373
var internalDbHandle = (DbHandle) db;
7474
IntPtr stmt;
75-
Result r = SQLiteApiAndroidInternal.sqlite3_prepare_v2(internalDbHandle.DbPtr, query, query.Length, out stmt, IntPtr.Zero);
75+
Result r = SQLiteApiAndroidInternal.sqlite3_prepare16_v2(internalDbHandle.DbPtr, query, -1, out stmt, IntPtr.Zero);
7676
if (r != Result.OK)
7777
{
7878
throw SQLiteException.New(r, Errmsg16(internalDbHandle));

src/SQLite.Net.Platform.XamarinAndroid/SQLiteApiAndroidInternal.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ public static extern int sqlite3_bind_text16(IntPtr stmt,
128128
[DllImport(DllName, EntryPoint = "sqlite3_open_v2", CallingConvention = CallingConvention.Cdecl)]
129129
public static extern Result sqlite3_open_v2(byte[] filename, out IntPtr db, int flags, IntPtr zvfs);
130130

131-
[DllImport(DllName, EntryPoint = "sqlite3_prepare_v2", CallingConvention = CallingConvention.Cdecl)]
132-
public static extern Result sqlite3_prepare_v2(IntPtr db,
133-
[MarshalAs(UnmanagedType.LPStr)] string sql,
131+
[DllImport(DllName, EntryPoint = "sqlite3_prepare16_v2", CallingConvention = CallingConvention.Cdecl)]
132+
public static extern Result sqlite3_prepare16_v2(IntPtr db,
133+
[MarshalAs(UnmanagedType.LPWStr)] string sql,
134134
int numBytes,
135135
out IntPtr stmt,
136136
IntPtr pzTail);

src/SQLite.Net.Platform.XamarinIOS.Unified/SQLiteApiIOS.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public IDbStatement Prepare2(IDbHandle db, string query)
7272
{
7373
var internalDbHandle = (DbHandle) db;
7474
IntPtr stmt;
75-
Result r = SQLiteApiIOSInternal.sqlite3_prepare_v2(internalDbHandle.DbPtr, query, query.Length, out stmt, IntPtr.Zero);
75+
Result r = SQLiteApiIOSInternal.sqlite3_prepare16_v2(internalDbHandle.DbPtr, query, -1, out stmt, IntPtr.Zero);
7676
if (r != Result.OK)
7777
{
7878
throw SQLiteException.New(r, Errmsg16(internalDbHandle));

src/SQLite.Net.Platform.XamarinIOS.Unified/SQLiteApiIOSInternal.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ public static extern int sqlite3_bind_text16(IntPtr stmt,
128128
[DllImport(DllName, EntryPoint = "sqlite3_open_v2", CallingConvention = CallingConvention.Cdecl)]
129129
public static extern Result sqlite3_open_v2(byte[] filename, out IntPtr db, int flags, IntPtr zvfs);
130130

131-
[DllImport(DllName, EntryPoint = "sqlite3_prepare_v2", CallingConvention = CallingConvention.Cdecl)]
132-
public static extern Result sqlite3_prepare_v2(IntPtr db,
133-
[MarshalAs(UnmanagedType.LPStr)] string sql,
131+
[DllImport(DllName, EntryPoint = "sqlite3_prepare16_v2", CallingConvention = CallingConvention.Cdecl)]
132+
public static extern Result sqlite3_prepare16_v2(IntPtr db,
133+
[MarshalAs(UnmanagedType.LPWStr)] string sql,
134134
int numBytes,
135135
out IntPtr stmt,
136136
IntPtr pzTail);

src/SQLite.Net.Platform.XamarinIOS/SQLiteApiIOS.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public int Changes(IDbHandle db) {
6060
public IDbStatement Prepare2(IDbHandle db, string query) {
6161
var internalDbHandle = (DbHandle)db;
6262
IntPtr stmt;
63-
Result r = SQLiteApiIOSInternal.sqlite3_prepare_v2(internalDbHandle.DbPtr,
63+
Result r = SQLiteApiIOSInternal.sqlite3_prepare16_v2(internalDbHandle.DbPtr,
6464
query,
65-
query.Length,
65+
-1,
6666
out stmt,
6767
IntPtr.Zero);
6868
if(r != Result.OK) {

src/SQLite.Net.Platform.XamarinIOS/SQLiteApiIOSInternal.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ public static extern int sqlite3_bind_text16(IntPtr stmt,
128128
[DllImport(DllName, EntryPoint = "sqlite3_open_v2", CallingConvention = CallingConvention.Cdecl)]
129129
public static extern Result sqlite3_open_v2(byte[] filename, out IntPtr db, int flags, IntPtr zvfs);
130130

131-
[DllImport(DllName, EntryPoint = "sqlite3_prepare_v2", CallingConvention = CallingConvention.Cdecl)]
132-
public static extern Result sqlite3_prepare_v2(IntPtr db,
133-
[MarshalAs(UnmanagedType.LPStr)] string sql,
131+
[DllImport(DllName, EntryPoint = "sqlite3_prepare16_v2", CallingConvention = CallingConvention.Cdecl)]
132+
public static extern Result sqlite3_prepare16_v2(IntPtr db,
133+
[MarshalAs(UnmanagedType.LPWStr)] string sql,
134134
int numBytes,
135135
out IntPtr stmt,
136136
IntPtr pzTail);

tests/UnicodeTest.cs

+48-31
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,66 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using NUnit.Framework;
4+
using SQLite.Net.Attributes;
45

56
namespace SQLite.Net.Tests
67
{
7-
[TestFixture]
8-
public class UnicodeTest
9-
{
10-
[Test]
11-
public void Insert()
12-
{
13-
var db = new TestDb();
8+
[TestFixture]
9+
public class UnicodeTest
10+
{
11+
[Table("\u7523\u54C1")]
12+
public class UnicodeProduct
13+
{
14+
[AutoIncrement, PrimaryKey, Column("\u6A19\u8B58")]
15+
public int Id { get; set; }
1416

15-
db.CreateTable<Product>();
17+
[Column("\u540D")]
18+
public string Name { get; set; }
1619

17-
string testString = "\u2329\u221E\u232A";
20+
[Column("\u5024")]
21+
public decimal Price { get; set; }
1822

19-
db.Insert(new Product
20-
{
21-
Name = testString,
22-
});
23+
[Column("\u53CE\u76CA")]
24+
public uint TotalSales { get; set; }
25+
}
2326

24-
var p = db.Get<Product>(1);
27+
[Test]
28+
public void Insert()
29+
{
30+
var db = new TestDb();
2531

26-
Assert.AreEqual(testString, p.Name);
27-
}
32+
db.CreateTable<UnicodeProduct>();
2833

29-
[Test]
30-
public void Query()
31-
{
32-
var db = new TestDb();
34+
string testString = "\u2329\u221E\u232A";
3335

34-
db.CreateTable<Product>();
36+
db.Insert(new UnicodeProduct
37+
{
38+
Name = testString,
39+
});
3540

36-
string testString = "\u2329\u221E\u232A";
41+
var p = db.Get<UnicodeProduct>(1);
3742

38-
db.Insert(new Product
39-
{
40-
Name = testString,
41-
});
43+
Assert.AreEqual(testString, p.Name);
44+
}
4245

43-
List<Product> ps = (from p in db.Table<Product>() where p.Name == testString select p).ToList();
46+
[Test]
47+
public void Query()
48+
{
49+
var db = new TestDb();
4450

45-
Assert.AreEqual(1, ps.Count);
46-
Assert.AreEqual(testString, ps[0].Name);
47-
}
48-
}
51+
db.CreateTable<UnicodeProduct>();
52+
53+
string testString = "\u2329\u221E\u232A";
54+
55+
db.Insert(new UnicodeProduct
56+
{
57+
Name = testString,
58+
});
59+
60+
var ps = (from p in db.Table<UnicodeProduct>() where p.Name == testString select p).ToList();
61+
62+
Assert.AreEqual(1, ps.Count);
63+
Assert.AreEqual(testString, ps[0].Name);
64+
}
65+
}
4966
}

0 commit comments

Comments
 (0)