morphismtech / squeal

Squeal, a deep embedding of SQL in Haskell
352 stars 32 forks source link

insert entire row with a specifc datatype #344

Open MangoIV opened 11 months ago

MangoIV commented 11 months ago

Is there an intented way to express this generically? image

In my case the names are exactly like the ones specified with the overloaded labels so it should be possible to just get the values from the types in the input type?

MangoIV commented 11 months ago

I guess it's a bit more complicated because you'd have to carry along the labels split from the input type in a wrapper around Manipulation

echatav commented 11 months ago

I've tried at times to make a params term that could be used in this type of situation but I never found a way to get it to work right to figure out the induction on type level natural numbers. For inlining there's inlineColumns which might be used for similar purposes.

MangoIV commented 11 months ago

I don’t think going via the numbers is a good idea tbh. I don’t have a solution written out though so I can be wrong

MangoIV commented 11 months ago

Thank you for writing this library. It’s awesome. Compile times are of course ass (21s spent in the simplifier) but that’s just GHCs fault.

MangoIV commented 11 months ago

image (this is actually hilarious)

echatav commented 11 months ago
class All KnownNat ixs
  => HasParams (ixs :: [Nat]) (params :: [NullType]) (tys :: [NullType]) where
    parameters :: NP (TypeExpression db) tys -> NP (Expression grp lat with db from) tys
instance HasParams '[] params '[] where
  parameters Nil = Nil
instance (HasParam ix params ty, HasParams ixs params tys)
  => HasParams (ix:ixs) params (ty:tys) where
    parameters (ty :* tys) = parameter @ix @params ty :* parameters @ixs @params tys

params
  :: forall ns tys lat with db params from grp
   . (All (NullTyped db) tys, HasParameters ns params tys)
  => NP (Expression grp lat with db params from) tys
params = parameters @ns (hcpure (Proxy @(NullTyped db)) (nulltype @db))

Maybe we can do something built on this? Haven't checked it.

echatav commented 11 months ago
(Values_ (hmap ((`as` Alias) . Set) (params @'[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])))
MangoIV commented 11 months ago

what I'm trying to say is can we just have something like GManipulation which instead of taking a list of NullType takes a list of NullType paired with their original name (as computed with the squeals rowtype type family) and then wrap around a Manipulation and then we can avoid the whole natural numbers stuff altogether?

MangoIV commented 11 months ago

so similarly to the Manipulation_ type family but it doesn't throw away the label types in the parameter list of NullType passed to Manipulation Sorry if that's a bit unclear, I haven't dug in too deep yet :D

MangoIV commented 11 months ago

thank you for your support!

echatav commented 11 months ago

Oh those label types aren't thrown away.

In my case the names are exactly like the ones specified with the overloaded labels so it should be possible to just get the values from the types in the input type?

Sure, you don't need to include the labels, #label is just short for Alias @label and the label can be inferred by GHC so you can just use Alias if you want, i.e. Set (param @1) `as` Alias :* Set (param @2) `as` Alias :* ...

The kind of the last type parameter of a QueryClause is a ColumnsType so includes column labels as well as optionalities (default value or not) in addition to NullTypes.

echatav commented 11 months ago

We cannot avoid natural numbers because these actually correspond to PostgreSQL positional parameter numbers.

MangoIV commented 11 months ago

Ah right that makes sense. Unfortunate. Well then we have to hack the natural numbers into place i guess

mwotton commented 11 months ago

@MangoIV something I've done in my last workplace was to aggressively use schemas and multiple Schema.hs files to cut down on squeal compile times, might be worth a go.