Deno Mysql - Query VS Execute
What's the difference between them?
When you look at the Deno Mysql documentation you can use two functions to run your query :
client.execute(/*SQL*/)
client.query(/*SQL*/)
I wasn't sure when to use what, so I consulted the code to find out.
Both function work the same as the query function is internally using the execute function. But they have not the same return value:
The
executefunction return anExecuteResultobject.While the
queryfunction return either anExecuteResultobject or anarray.
Basicaly it's means that query is more interesting if you want to retrieve the content of the "rows" directly (wich is mostly the case on a SELECT query)
export type ExecuteResult = {
affectedRows?: number;
lastInsertId?: number;
fields?: FieldInfo[];
rows?: any[];
iterator?: any;
};
async query(sql: string, params?: any[]): Promise<ExecuteResult | any[]> {
const result = await this.execute(sql, params);
if (result && result.rows) {
return result.rows;
} else {
return result;
}
}
When to use them?
For HTTP calls:
Use
queryforGETendpointUse
executeforPOST/PUT/UPDATE/DELETEendpoint
In terms of SQL:
Use
queryonSELECTstatements or any other SQL operation that returns rows from the database.Use
executeonINSERT, UPDATE, DELETE, or any other SQL operation that modifies data in the database