I want to do the following two insert operations using the postgresql driver for dart. The code below works!
INSERT INTO posts(
title, description, posted_at, last_edited, "user",
editor, up_votes, down_votes, flag, links_to)
VALUES ('test title', 'test description', now(), now(), 'dartUser', 'dartUser',
0, 0, 'healthy', 'http://google.com');
INSERT INTO atatched_tags(
post_id, tag)
VALUES (currval('posts_post_id_seq'), 'testTag');
The second insert should only be done if an optional parameter is given to the dart method. The code I have right now looks like this.
addPost(Map post_values, [Map tag_values]){
Connection conn;
connect(uri)
.then((_conn){
conn = _conn;
})
.then((_){
conn.execute('''insert into posts(title, description, posted_at, last_edited, "user", editor, up_votes, down_votes, flag, links_to)
values(@title, @description, now(), now(), @user, @editor, @upVotes, @downVotes, @flag, @links_to)''', post_values)
.catchError((err){
print('Execute error in addPost: $err');
})
.then((_){
if(tag_values != null){
_addTagToPost(conn, tag_values);
}
})
.whenComplete(() => conn.close());
})
.catchError((err) => print('Error in addPost: $err'));
}
_addTagToPost(Connection conn, Map values) {
print('trying to add tag to attached tags');
conn.execute("insert into atatched_tags values(currval('posts_post_id_seq'), @tag)", values)
.catchError((err){
print('Execute error in addTagToPost: $err');
});
}
I run the method as follows.
dbUtil.addPost({'title': 'Sample title',
'description': 'This is a description for a sample post',
'user': 'dartUser', 'editor': 'dartUser', 'upVotes': 0,
'downVotes': 0, 'flag': 'healthy', 'links_to':
'http://google.com'}, {'tag':'testTag'});
dbUtil
is an instance of the class where the two methods above reside.
The code does not throw any errors. But even though it writes to the console trying to add tag to attached tags
the second insert operation is not executed (no rows are added to the table). The first works as expected.
Any help is much appreciated.
Edit: If somebody wants to add mulitple tags in the _addTagToPost
function then this code might be helpfull.
Edit2: The code below is incorrect I leave it, but check the comments in the accepted answer.
Future _addTagToPost(Connection conn, List<Map> values) {
var completer = new Completer();
values.forEach((value){
conn.execute("insert into attached_tags values(currval('posts_post_id_seq'), @tag)", value)
.catchError((err){
print('Execute error in addTagToPost: $err');
});
});
return completer.future;
}
You need to add a return
here
return _addTagToPost(conn, tag_values);
and
Future _addTagToPost(Connection conn, Map values) {
print('trying to add tag to attached tags');
return conn.execute(
"insert into atatched_tags values(currval('posts_post_id_seq'),
@tag)", values)
.catchError((err){
print('Execute error in addTagToPost: $err');
});
}
to keep the async operations connected. Otherwise the connection is closed before _addTagToPost()
is finished which results in not completing this SQL statement.
Update to the edit in the question. This could also be done like
Future _addTagToPost(Connection conn, List<Map> values) {
return Future.wait(values.map((value){
return conn.execute("insert into attached_tags values(currval('posts_post_id_seq'), @tag)", value)
.catchError((err){
print('Execute error in addTagToPost: $err');
});
}));
}
or
Future _addTagToPost(Connection conn, List<Map> values) =>
Future.wait(values.map((value) => conn
.execute(
"insert into attached_tags values(currval('posts_post_id_seq'), @tag)",
value)
.catchError((err) => print('Execute error in addTagToPost: $err'))));