Comments (6)
Here's what I've come up with, which shares a signature with the functions I removed (i.e., (obj -> 'a) -> IDbCommand -> DbResult<'a>
).
Hopefully they will help your use cases. Apologies if there's been any frustration, my goal wasn't to make a library that made someone's job harder.
An example usage:
dbCommand conn { cmdText "SELECT 1" }
|> Db.scalar Convert.ToInt32 // or, Db.Async.scalar Convert.ToInt32
|> printfn "%i" // outputs: 1
Once I get the thumbs up from you, I'll release a new minor version of the package to include this in the API.
from donald.
Wonderful 🎉
During testing got a database is locked
exception that wasn't handled inside of Donald
. Not sure why yet. Probably I wasn't careful when rewrited some parts.
from donald.
Hi @FoggyFinder,
Thanks for reaching out. Appreciate the interest.
I had the same line of thinking in the past. In fact, original iterations of this project had a invocation pathway for ExecuteScalar()
which boxed return value based on the generic type parameter provided. This is obviously not an ideal situation for a number of reasons. So I modified the invocation to receive a mapping function to be applied against the obj
returned. Again, less than ideal because an entire different suite of helpers had to be created to support that (in addition to those responsible for consuming IDataReader
values) and it ultimately was clunky to deal with nulls.
So I decided that the optimal route was to leverage Db.QuerySingle
which elegantly deals with all of the above. In addition to solving the problems above it elegantly deals with miscasts (yielding sensible exceptions) and has the benefit of streaming result data.
I had thought about creating a Db.Scalar
function which simply calls Db.QuerySingle
under the covers. But I haven't really found using QuerySingle to be clunky, and I use the library a lot for both work and personal projects.
Anyway, I'm open to hearing if there's a use case that I'm missing. But ultimately, I don't imagine I'll be adding an explicit wrapper around ExecuteScalar(). Make sense?
from donald.
Anyway, I'm open to hearing if there's a use case that I'm missing. But ultimately, I don't imagine I'll be adding an explicit wrapper around ExecuteScalar()
Well, right, I really don't care how it would work internally.
I had thought about creating a Db.Scalar function which simply calls Db.QuerySingle under the covers. But I haven't really found using QuerySingle to be clunky
It's just a bit annoying to check result first and then check whether the query is actually returned something.
I wrote a simple function though
module Db =
let execSingle cmd =
match Db.querySingle (fun r -> r.GetValue(0) |> unbox) cmd with
| DbResult.Ok r when r.IsSome ->
Result.Ok r.Value
| DbResult.Ok _ ->
Result.Error "A query didn't return any value"
| DbResult.Error err ->
Result.Error err.Statement
which I haven't tested yet but I believe it would work.
from donald.
perfect, thank you
from donald.
You can test this out in 6.1.0-beta1.
from donald.
Related Issues (20)
- GetDateTimeOffset Signature is string -> DateTime HOT 1
- Unable to execute query without a transaction HOT 6
- TransactionBuilder HOT 9
- CommandBehavior.SequentialAccess causes error with SqlHydra readers HOT 5
- Cast exception when reading DateTimeOffset with System.Data.SqlClient HOT 4
- [Help] How to parse results with sequential access? HOT 2
- Db.Async.execMany throws "A command is already in progress" with NPGSQL HOT 5
- v.7.0.0 HOT 9
- FsDto lib review HOT 2
- Who is @dysme on the landing page? HOT 3
- CancellationToken support
- Support of transaction in newCommand in Fluent syntax HOT 3
- Getting error while using Db.Async.query method HOT 6
- Require help on query HOT 7
- Support or Transition to Microsoft.Data.SqlClient HOT 3
- SQLite: What SQL Type should I choose for a JSON column? HOT 2
- Possible issue with Db.Async.exec throwing instead of returning a Result HOT 1
- How to deal with DB enums in Donald? HOT 1
- DateOnly and TimeOnly parameters
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from donald.