예제 #1
0
        public static Task <string> GetJsonData(string streamId, int streamVersion)
        {
            using (var connection = new SqliteConnection(_settings.GetConnectionString(true)))
                using (var command = connection.CreateCommand())
                {
                    connection.Open();

                    command.CommandText = @"SELECT messages.json_data
FROM messages
WHERE messages.stream_id_internal = 
(
SELECT streams.id_internal
FROM streams
WHERE streams.id = @streamId)
AND messages.stream_version = @streamVersion";
                    command.Parameters.Clear();
                    command.Parameters.AddWithValue("@streamId", streamId);
                    command.Parameters.AddWithValue("@streamVersion", streamVersion);

                    using (var reader = command.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleRow))
                    {
                        return(Task.FromResult(reader.Read()
                        ? reader.GetTextReader(0).ReadToEnd()
                        : default(string)));
                    }
                }
        }
예제 #2
0
        internal SqliteConnection OpenConnection(bool isReadOnly = true)
        {
            var connection = new SqliteConnection(_settings.GetConnectionString(isReadOnly));

            connection.Open();
            using (var command = connection.CreateCommand())
            {
                command.CommandText = @"PRAGMA foreign_keys = true;
                                        PRAGMA journal_mode = WAL;";
                command.ExecuteNonQuery();
            }

            // register functions.
            connection.CreateFunction("split", (string source) => source.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
            connection.CreateFunction("contains",
                                      (string allIdsBeingSought, string idToSeek) =>
            {
                var ids = allIdsBeingSought.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                return(ids.Contains(idToSeek, StringComparer.OrdinalIgnoreCase));
            });
            return(connection);
        }