sqlClient.querySingleWithParams("SELECT id,birthday,full_name,nick_name,email FROM user WHERE uname=? and pwd=?",JsonArray(uname,pwd)){ if(it.failed()){ //DO some }else{ val uid = it.result().getInteger(0) sqlClient.queryWithParams("SELECT * FROM friends WHERE uid=?",JsonArray(uid)){ if(it.failed()){ //Do some }else{ sqlClient.queryWithParams("SELECT * FROM posts WHERE uid=?",JsonArray(uid)){ if(it.failed()){ //Do some }else{ context.response().end(/*All infomations*/) } } } } } }
/*未使用协程*/ sql.querySingle("SELECT id FROM example"){ if(it.failed()){ context.response().end("{status:-1}") }else{ contezt.response().end("{status:1,id:${it.result().getInteger(0)}") } } /* 使用协程 */ val result = sql.querySingle("SELECT id FROM example").awit() if(it.failed()){ context.response().end("{status:-1}") }else{ contezt.response().end("{status:1,id:${it.result().getInteger(0)}") }
router.route("/login/:username/:password").handler{ if(it.session().get<String>("username")=="admin"){ it.response().end("{status:1,msg:\"You have logged in \"}") return@handler } val username = it.request().getParam("username") val password = it.request().getParam("password") Async{ val result = sqlClient.querySingleWithParamsCurry("SELECT id,level FROM user WHERE username=? and password=?",JsonArray(username,password)).awit() if(result.failed() || result.result()==null){ it.response().end("{status:-1}") }else{ it.session().put("username",username) it.session().put("uid",result.result().getInteger(0)) it.session().put("level",result.result().getInteger(1)) it.response().end("{status:0}") } } }