Dion presented in Devcon 2020 how to setup Multitenant connection string to use the build-in driver. When I was developing my app I didn't realize there was this option and I rolled my own. But now I am converting to the built in driver. I have added a field to all my tables and queries a new field called tenant_id.
The step that wasn't covered is how to set the session variable for the tenant id in a real world situation. I decided to add the code to my login ux under the Server-side onLogin event and I would like to have someone who has more expertise on Xbasic to look over my code let me know if it is solid.
I get the tenant id from the extended user table.
Thanks
David
The step that wasn't covered is how to set the session variable for the tenant id in a real world situation. I decided to add the code to my login ux under the Server-side onLogin event and I would like to have someone who has more expertise on Xbasic to look over my code let me know if it is solid.
I get the tenant id from the extended user table.
Code:
e.url = "index.a5w" 'DIM a SQL arguments object dim args as sql::arguments 'adds an argument that gets the logged in user id from e.username to be used as a filter in the SQL args.add("arg_user_id",e.username) 'DIM an Xbasic AlphaDAO connection object dim cn as sql::connection dim flag as l ''open the connection object to connect to the Database flag = cn.open("::Name::AlphaServerConn") dim sql as c sql = "SELECT USERID, tenant_id FROM extendeduserinformation WHERE USERID = :arg_user_id" 'turn portable SQL on. (for more information on portable sql search in help (https://www.alphasoftware.com/documentation) 'execute the SQL - pass in the sql::arguments object flag = cn.execute(sql,args) if flag = .f. then 'an error occurred - get the error message from cn.callresult.text 'report the error, close the database and then end the script dim errortext as c errortext = cn.callresult.text cn.close() end if 'DIM a SQL::Resultset object dim rs as sql::resultset rs = cn.resultset 'get the JSON from the query dim json as c json = rs.toJSON() 'parses the json dim p as p p = json_parse(json) dim msg as c 'Sets the session variable session.tenant_id = p[1].tenant_id 'Clean up by freeing the resultset and then closing the database cn.freeresult() cn.close()
David
Comment