buggins / ddbc

DDBC is DB Connector for D language (similar to JDBC)
78 stars 45 forks source link

How to access prepared statemetns on the DB Server? #15

Closed KoreyPeters closed 8 years ago

KoreyPeters commented 8 years ago

Hi. I'm having trouble figuring out how to access a prepared statement already on the PostGres server.

My d code is:

    auto stmt = conn.createStatement();
    scope(exit) stmt.close();
    auto rs = stmt.executeQuery(format("SELECT create_step(%d, '%s', %d);", parentId, name, rank));
    scope(exit) rs.close();

My stored procedure is:

-- Function: create_step(integer, character varying, integer)

-- DROP FUNCTION create_step(integer, character varying, integer);

CREATE OR REPLACE FUNCTION create_step(new_parent_id integer, new_name character varying, new_rank integer)
  RETURNS step AS
$BODY$

DECLARE
    created_step step%ROWTYPE;
BEGIN
    UPDATE steps SET rank = rank + 1 WHERE parent_id = new_parent_id AND rank >= new_rank;

    INSERT INTO steps (parent_id, name, rank)
    VALUES (new_parent_id, new_name, new_rank) 
    RETURNING id, parent_id, name, rank INTO created_step;
    RETURN created_step;
END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION create_step(integer, character varying, integer)
  OWNER TO postgres;

And the stack trace is:

ddbc.core.SQLException@../../.dub/packages/ddbc-0.2.24/source/ddbc/drivers/pgsqlddbc.d(401): Unsupported column type 16563
----------------
../../.dub/packages/ddbc-0.2.24/source/ddbc/drivers/pgsqlddbc.d:401 void ddbc.drivers.pgsqlddbc.PGSQLStatement.fillData(ddbc.drivers.pgsql.PGresult*, ref std.variant.VariantN!(32uL).VariantN[][]) [0x72576c]
../../.dub/packages/ddbc-0.2.24/source/ddbc/drivers/pgsqlddbc.d:443 ddbc.core.ResultSet ddbc.drivers.pgsqlddbc.PGSQLStatement.executeQuery(immutable(char)[]) [0x725a6c]
source/storage.d:79 models.Step storage.PostGresStorage.createStep(int, immutable(char)[], int) [0x71ef98]
source/app.d:38 models.Step app.MyAPIImplementation.createStep(int, immutable(char)[], int) [0x6e748e]
source/app.d:44 void app.MyAPIImplementation.__unittestL41_1() [0x6e7539]

I feel like there's some example or documentation I'm missing. I can call a non-parameterized stored procedure with

auto rs = stmt.executeQuery("SELECT create_step_table()");
buggins commented 8 years ago

Stored procedures currently are not supported in DDBC, In JDBC, stored procedures are being accessed using conn.prepareCall() There is no such method in DDBC.

Using of stored procedure inside SELECT statement seems not working because create_step() returns result set - such column type support is not implemented in DDBC PGSQL driver.

KoreyPeters commented 8 years ago

Okay. Thanks for letting me know (and sorry I didn't close earlier!)