From dc2a86bb6fe2ce78cf47b14302e72f76fe5de193 Mon Sep 17 00:00:00 2001 From: seb-bartholomew-amcs Date: Tue, 15 Nov 2016 12:42:50 +0000 Subject: [PATCH] Adds identifier keyword character trimming on column name prior to attempting to map to object properties Adds TrimIdentifierKeywordEscapeCharacters method to trim column name returned by _sqlitePlatform.SQLiteApi.ColumnName16. --- src/SQLite.Net/SQLiteCommand.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/SQLite.Net/SQLiteCommand.cs b/src/SQLite.Net/SQLiteCommand.cs index 9db0d705b..53e6fa2c5 100755 --- a/src/SQLite.Net/SQLiteCommand.cs +++ b/src/SQLite.Net/SQLiteCommand.cs @@ -40,6 +40,7 @@ public class SQLiteCommand private readonly SQLiteConnection _conn; private readonly ISQLitePlatform _sqlitePlatform; private const string DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ"; + private readonly char[] identifierEscapeCharacters = new [] { '"', '`', '[', ']' }; internal SQLiteCommand(ISQLitePlatform platformImplementation, SQLiteConnection conn) { @@ -128,7 +129,8 @@ public IEnumerable ExecuteDeferredQuery(TableMapping map) for (var i = 0; i < cols.Length; i++) { var name = _sqlitePlatform.SQLiteApi.ColumnName16(stmt, i); - cols[i] = map.FindColumn(name); + var trimmedName = TrimIdentifierKeywordEscapeCharacters(name); + cols[i] = map.FindColumn(trimmedName); } while (_sqlitePlatform.SQLiteApi.Step(stmt) == Result.Row) @@ -585,6 +587,21 @@ private object ReadCol(IDbStatement stmt, int index, ColType type, Type clrType) throw new NotSupportedException("Don't know how to read " + clrType); } + private string TrimIdentifierKeywordEscapeCharacters(string identifier) + { + string trimmedIdentifier = identifier; + if (identifierEscapeCharacters.Contains(trimmedIdentifier[0])) + { + trimmedIdentifier = trimmedIdentifier.Remove(0, 1); + } + int lastCharIndex = trimmedIdentifier.Length - 1; + if (identifierEscapeCharacters.Contains(trimmedIdentifier[lastCharIndex])) + { + trimmedIdentifier = trimmedIdentifier.Remove(lastCharIndex); + } + return trimmedIdentifier; + } + private class Binding { [CanBeNull]