Skip to content

Commit 79da46e

Browse files
committed
add param binding to fdb push write
Signed-off-by: Emelia Lei <wlei29@bloomberg.net>
1 parent b62700d commit 79da46e

3 files changed

Lines changed: 76 additions & 5 deletions

File tree

db/fdb_push.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,34 @@ int fdb_push_write_setup(Parse *pParse, enum ast_type type, Table *pTab)
164164
if (!push)
165165
return -1;
166166

167-
clnt->fdb_push = push;
167+
// Copy params from clnt plugin into push connector
168+
int nparams = param_count(clnt);
169+
if (nparams > 0) {
170+
struct param_data *params = calloc(nparams, sizeof(struct param_data));
171+
if (!params)
172+
goto err;
173+
174+
for (int i = 0; i < nparams; i++) {
175+
if (param_value(clnt, &params[i], i) != 0) {
176+
free(params);
177+
goto err;
178+
}
179+
}
168180

181+
if (dohsql_clone_params(nparams, params, &push->nparams, &push->params)) {
182+
free(params);
183+
goto err;
184+
}
185+
free(params);
186+
}
187+
188+
clnt->fdb_push = push;
169189
return 0;
190+
191+
err:
192+
free(push->remotedb);
193+
free(push);
194+
return -1;
170195
}
171196

172197
/**
@@ -638,7 +663,7 @@ int handle_fdb_push_write(sqlclntstate *clnt, struct errstat *err,
638663
}
639664
}
640665

641-
rc = cdb2_run_statement(tran->fcon.hndl, "begin");
666+
rc = cdb2_run_statement(tran->fcon.hndl, "begin");
642667
while (rc == CDB2_OK) {
643668
rc = cdb2_next_record(tran->fcon.hndl);
644669
}
@@ -805,9 +830,7 @@ int handle_fdb_push_write(sqlclntstate *clnt, struct errstat *err,
805830
/* This will get retried as non-cdb2api, free fdb_push
806831
* so that the postpone message works
807832
*/
808-
clnt->fdb_push = NULL;
809-
free(push->remotedb);
810-
free(push);
833+
fdb_push_free(&clnt->fdb_push);
811834
if (set_intrans) {
812835
/* if this tried to short call to local sqlite3BtreeBeginTrans
813836
* first time we try to push, and we set clnt->intrans,

tests/fdb_push.test/output.log

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ Test parameters
2929
(i=NULL, r=NULL, s=NULL, b=x'deadbeaf', d=NULL, d2=NULL)
3030
(i=NULL, r=NULL, s=NULL, b=NULL, d="2023-09-13T000000.000 America/New_York", d2=NULL)
3131
(i=NULL, r=NULL, s=NULL, b=NULL, d=NULL, d2="2023-09-13T000000.000001 America/New_York")
32+
Test write with bound parameters
33+
(rows inserted=1)
34+
(id=50, b1='Bound1')
35+
(rows updated=1)
36+
(id=50, b1='Updated')
37+
(rows deleted=1)
38+
(rows inserted=1)
39+
(i=99, r=3.140000, s='bound', b=NULL, d=NULL, d2=NULL)
40+
(rows deleted=1)
3241
Test set statement
3342
(i=NULL, r=NULL, s=NULL, b=NULL, d="2023-09-13T130000.000 Asia/Tokyo", d2=NULL)
3443
(i=NULL, r=NULL, s=NULL, b=NULL, d="2023-09-13T130000.000 Asia/Tokyo", d2=NULL)

tests/fdb_push.test/test_fdb_push.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,45 @@ cdb2sql -s ${SRC_CDB2_OPTIONS} $a_dbname default - >> $output 2>&1 << EOF
8282
select * from LOCAL_${a_remdbname}.t2 where d2=@d2
8383
EOF
8484

85+
echo "Test write with bound parameters" >> $output
86+
# INSERT with bound integer and cstring params
87+
cdb2sql -s ${SRC_CDB2_OPTIONS} $a_dbname default - >> $output 2>&1 << EOF
88+
@bind CDB2_INTEGER id 50
89+
@bind CDB2_CSTRING b1 Bound1
90+
insert into LOCAL_${a_remdbname}.t values (@id, @b1)
91+
EOF
92+
cdb2sql -s ${SRC_CDB2_OPTIONS} $a_dbname default "select * from LOCAL_${a_remdbname}.t where id=50" >> $output 2>&1
93+
94+
# UPDATE with bound params
95+
cdb2sql -s ${SRC_CDB2_OPTIONS} $a_dbname default - >> $output 2>&1 << EOF
96+
@bind CDB2_CSTRING b1 Updated
97+
@bind CDB2_INTEGER id 50
98+
update LOCAL_${a_remdbname}.t set b1=@b1 where id=@id
99+
EOF
100+
cdb2sql -s ${SRC_CDB2_OPTIONS} $a_dbname default "select * from LOCAL_${a_remdbname}.t where id=50" >> $output 2>&1
101+
102+
# DELETE with bound param
103+
cdb2sql -s ${SRC_CDB2_OPTIONS} $a_dbname default - >> $output 2>&1 << EOF
104+
@bind CDB2_INTEGER id 50
105+
delete from LOCAL_${a_remdbname}.t where id=@id
106+
EOF
107+
cdb2sql -s ${SRC_CDB2_OPTIONS} $a_dbname default "select * from LOCAL_${a_remdbname}.t where id=50" >> $output 2>&1
108+
109+
# INSERT into t2 with multiple typed params
110+
cdb2sql -s ${SRC_CDB2_OPTIONS} $a_dbname default - >> $output 2>&1 << EOF
111+
@bind CDB2_INTEGER i 99
112+
@bind CDB2_REAL r 3.14
113+
@bind CDB2_CSTRING s bound
114+
insert into LOCAL_${a_remdbname}.t2(i, r, s) values (@i, @r, @s)
115+
EOF
116+
cdb2sql -s ${SRC_CDB2_OPTIONS} $a_dbname default "select * from LOCAL_${a_remdbname}.t2 where i=99" >> $output 2>&1
117+
118+
# DELETE from t2 with bound param to clean up
119+
cdb2sql -s ${SRC_CDB2_OPTIONS} $a_dbname default - >> $output 2>&1 << EOF
120+
@bind CDB2_INTEGER i 99
121+
delete from LOCAL_${a_remdbname}.t2 where i=@i
122+
EOF
123+
85124
echo "Test set statement" >> $output
86125
cdb2sql -s ${SRC_CDB2_OPTIONS} $a_dbname default - >> $output 2>&1 << EOF
87126
set timezone Asia/Tokyo

0 commit comments

Comments
 (0)