fsprojects / SQLProvider

A general F# SQL database erasing type provider, supporting LINQ queries, schema exploration, individuals, CRUD operations and much more besides.
https://fsprojects.github.io/SQLProvider
Other
570 stars 144 forks source link

ColumnValues are sorted alphabetically #641

Closed giuliohome closed 4 years ago

giuliohome commented 4 years ago

Description

Can I get - from ColumnValues - the columns in the original order as they are defined in the table?

Repro

Simply refer to the example in the Documentation for Querying

let someQuery =
    query {
        for customer in ctx.Main.Customers do
        //where(...)
        select customer
    } |> Seq.toArray

someQuery |> Array.map(fun c -> c.ColumnValues |> Map.ofSeq)

Expected result

The columns listed as they are defined in the Customers table: in case one needs to sort them, no problem adding an Array.sortBy, but I don't see how I can retrieve the original sequence otherwise.

Thank you.

giuliohome commented 4 years ago

Btw, notice also that it would be awesome if the values in ColumnValues were a union type instead of an obj. Is there anything preventing from it being considered/done? Because the obj might produce some issues in the following RPC serialization, see @Tarmil comment on W# gitter.

Thorium commented 4 years ago

Hmm, I don't know straight away the column order, then again, relational database shouldn't trust to any particular column or row order in general, if not explicitely sorted.

obj...nice idea, but breaking backward compatibility and we would have to know all the supported meta-types by different databases.

giuliohome commented 4 years ago

Just to clarify, of course I trust - e.g. for MS SQL - the column_id of the sys.columns table. And you also trust such table since you are reading the column descriptions from it in the Providers.MsSqlServer.fs: I guess you should simply sort by column_id in a similar query there, in the point where you get the list of the columns from a table...

Edit

Actually, you are already getting the columns and sorting them by ORDINAL_POSITION: it looks like then you are not passing such order to the SQLProvider entities when you map the columns into their internal representation.

giuliohome commented 4 years ago

The type Column in the SqlSchema.fs should have an ordinal: int to keep track of the ORDINAL_POSITION. Maybe it can be tagged as a nice to have and closed for now.