ballerina-platform / ballerina-library

The Ballerina Library
https://ballerina.io/learn/api-docs/ballerina/
Apache License 2.0
137 stars 65 forks source link

Feature Request: Support variable types other than records for JDBC select #175

Closed ramith closed 3 years ago

ramith commented 4 years ago

Description: Following code fragment will not compile.

var result = dbClient->select("SELECT id from mytable where user_id = ?", int, 20);

it gives me an error like this:

incompatible types: expected 'typedesc<record {| anydata...; |}>?', found 'typedesc<int>'

I think this is something we should support.

Affected Versions: v1.2.8

Suggested Assignees (optional): @niveathika @anupama-pathirage

niveathika commented 4 years ago

@ramith Since we support null type descriptors is this improvement necessary?

var result = dbClient->select("SELECT id from mytable where user_id = ?", (), 20);

Usually, the recordType parameter is supported to bind the returned results to a known record type. Thus making it easier down the line

ramith commented 4 years ago

what's the type of result when I specify () or rather how can I get hold of values returned from the sql query?

niveathika commented 4 years ago

The returned table definition will be generated from the metadata from resultset

ramith commented 4 years ago

so in this case its going to be a table<record{int id;}> ?

niveathika commented 4 years ago

Yes

ramith commented 4 years ago

I now realise, that what I requested cannot be supported with current language design (given that we can't have something like table<int>)

So, It would be nice to have your workaround documented ( with examples). I still think there needs to be a way to work with databases with out resorting to tables (probably have an array of tuples as the return type). But that would be a whole new concept.

niveathika commented 3 years ago

@ramith FRom Swanlake version onwards we are returning a stream from DB query. However, as per the stream spec, we can only return a record from the stream and not ballerina basic types