Skip to content

The search box in the website knows all the secrets—try it!

For any queries, join our Discord Channel to reach us faster.

JasperFx Logo

JasperFx provides formal support for Marten and other JasperFx libraries. Please check our Support Plans for more details.

Execute custom SQL in session

Use QueueSqlCommand(string sql, params object[] parameterValues) method to register and execute any custom/arbitrary SQL commands with the underlying unit of work, as part of the batched commands within IDocumentSession.

? placeholders can be used to denote parameter values. Postgres type casts :: can be applied to the parameter if needed. If the ? character is not suitable as a placeholder because you need to use ? in your sql query, you can change the placeholder by providing an alternative. Pass this in before the sql argument.

cs
theSession.QueueSqlCommand("insert into names (name) values ('Jeremy')");
theSession.QueueSqlCommand("insert into names (name) values ('Babu')");
theSession.Store(Target.Random());
theSession.QueueSqlCommand("insert into names (name) values ('Oskar')");
theSession.Store(Target.Random());
var json = "{ \"answer\": 42 }";
theSession.QueueSqlCommand("insert into data (raw_value) values (?::jsonb)", json);
// Use ^ as the parameter placeholder
theSession.QueueSqlCommand('^', "insert into data (raw_value) values (^::jsonb)", json);

snippet source | anchor

Released under the MIT License.