Skip to content

Commit 52f72b5

Browse files
committed
- Changing Open to use byte[] to handle Unicode characters that may be in databasePath
- Exposing Open16 (on DllImport) for those who specifically want to use it - Fixing DllImport statements for string marshal values as correct types
1 parent a729fe5 commit 52f72b5

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/SQLite.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,19 @@ public SQLiteConnection (string databasePath, SQLiteOpenFlags openFlags)
124124
{
125125
DatabasePath = databasePath;
126126
Sqlite3DatabaseHandle handle;
127-
var r = SQLite3.Open (DatabasePath, out handle, (int) openFlags, IntPtr.Zero);
127+
128+
// in the case where the path may include Unicode
129+
// force open to using UTF-8 using sqlite3_open_v2
130+
byte[] databasePathAsBytes;
131+
int databasePathLength;
132+
133+
databasePathLength = System.Text.Encoding.UTF8.GetByteCount(DatabasePath);
134+
databasePathAsBytes = new byte[databasePathLength + 1];
135+
databasePathLength = System.Text.Encoding.UTF8.GetBytes(DatabasePath, 0, DatabasePath.Length, databasePathAsBytes, 0);
136+
137+
// open using the byte[]
138+
var r = SQLite3.Open (databasePathAsBytes, out handle, (int) openFlags, IntPtr.Zero);
139+
128140
Handle = handle;
129141
if (r != SQLite3.Result.OK) {
130142
throw SQLiteException.New (r, String.Format ("Could not open database file: {0} ({1})", DatabasePath, r));
@@ -2017,10 +2029,16 @@ public enum ConfigOption : int
20172029

20182030
#if !USE_CSHARP_SQLITE
20192031
[DllImport("sqlite3", EntryPoint = "sqlite3_open", CallingConvention=CallingConvention.Cdecl)]
2020-
public static extern Result Open (string filename, out IntPtr db);
2032+
public static extern Result Open ([MarshalAs(UnmanagedType.LPStr)] string filename, out IntPtr db);
20212033

20222034
[DllImport("sqlite3", EntryPoint = "sqlite3_open_v2", CallingConvention=CallingConvention.Cdecl)]
2023-
public static extern Result Open (string filename, out IntPtr db, int flags, IntPtr zvfs);
2035+
public static extern Result Open ([MarshalAs(UnmanagedType.LPStr)] string filename, out IntPtr db, int flags, IntPtr zvfs);
2036+
2037+
[DllImport("sqlite3", EntryPoint = "sqlite3_open_v2", CallingConvention = CallingConvention.Cdecl)]
2038+
public static extern Result Open(byte[] filename, out IntPtr db, int flags, IntPtr zvfs);
2039+
2040+
[DllImport("sqlite3", EntryPoint = "sqlite3_open16", CallingConvention = CallingConvention.Cdecl)]
2041+
public static extern Result Open16([MarshalAs(UnmanagedType.LPWStr)] string filename, out IntPtr db);
20242042

20252043
[DllImport("sqlite3", EntryPoint = "sqlite3_close", CallingConvention=CallingConvention.Cdecl)]
20262044
public static extern Result Close (IntPtr db);
@@ -2035,7 +2053,7 @@ public enum ConfigOption : int
20352053
public static extern int Changes (IntPtr db);
20362054

20372055
[DllImport("sqlite3", EntryPoint = "sqlite3_prepare_v2", CallingConvention=CallingConvention.Cdecl)]
2038-
public static extern Result Prepare2 (IntPtr db, string sql, int numBytes, out IntPtr stmt, IntPtr pzTail);
2056+
public static extern Result Prepare2 (IntPtr db, [MarshalAs(UnmanagedType.LPStr)] string sql, int numBytes, out IntPtr stmt, IntPtr pzTail);
20392057

20402058
public static IntPtr Prepare2 (IntPtr db, string query)
20412059
{
@@ -2068,7 +2086,7 @@ public static string GetErrmsg (IntPtr db)
20682086
}
20692087

20702088
[DllImport("sqlite3", EntryPoint = "sqlite3_bind_parameter_index", CallingConvention=CallingConvention.Cdecl)]
2071-
public static extern int BindParameterIndex (IntPtr stmt, string name);
2089+
public static extern int BindParameterIndex (IntPtr stmt, [MarshalAs(UnmanagedType.LPStr)] string name);
20722090

20732091
[DllImport("sqlite3", EntryPoint = "sqlite3_bind_null", CallingConvention=CallingConvention.Cdecl)]
20742092
public static extern int BindNull (IntPtr stmt, int index);
@@ -2083,7 +2101,7 @@ public static string GetErrmsg (IntPtr db)
20832101
public static extern int BindDouble (IntPtr stmt, int index, double val);
20842102

20852103
[DllImport("sqlite3", EntryPoint = "sqlite3_bind_text16", CallingConvention=CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
2086-
public static extern int BindText (IntPtr stmt, int index, string val, int n, IntPtr free);
2104+
public static extern int BindText (IntPtr stmt, int index, [MarshalAs(UnmanagedType.LPWStr)] string val, int n, IntPtr free);
20872105

20882106
[DllImport("sqlite3", EntryPoint = "sqlite3_bind_blob", CallingConvention=CallingConvention.Cdecl)]
20892107
public static extern int BindBlob (IntPtr stmt, int index, byte[] val, int n, IntPtr free);

0 commit comments

Comments
 (0)