SERVICE
This class is a "service" and it is automatically injected if declared as a member in the constructor of the consuming class.
- See the constructor section below for details
- See the Automatic Instance Injection page for details on the topic
The SqlQueryService class provides methods to execute raw sql queries against the local (on-device) database.
Don't instantiate directly instances of the SqlQueryServiceclass.
SqlQueryService instances are automatically injected by Dynamics Mobile framework.
import { View, SqlQueryService } from'$dms';import { Customer } from'$dms-bo';@View()exportclassMyView {constructor(//we need to only declare the memberprivate SqlQueryService:SqlQueryService ){ }asyncload(){ //we can now use the injected instance let value =awaitthis.SqlQueryService.execute(`select * from ${Customer.boTableName}}`); }}
📳 Mobile
Avoid using SqlQueryService directly as it uses raw (low-level) sql query and does not return business entities.
Use DbQuery class for more cases. See also BusinessObjectBase class
▶️ execute(sql: string): Promise<Array<any>>
execute method performs query execution of the sql statement provided as argument against the local on-device database and returns array of flat typescript objects.
array of "flat" javascript objects, where each javascript object represent the affected records , when the sql parameter represents "select" sql clause. If the sql parameter represents sql clause other than "select", the method returns undefined.
mport { SqlQueryService } from '@dms';import { Customer } from'@dms-bo';...var records =awaitthis.SqlQueryService.execute(`select * from ${Customer.boTableName}`);records.forEach((record)=>{console.log(`Customer name is ${records.name}`); });
How to use
import { View, SqlQueryService } from'@dms';import { Customer } from'@dms-bo';// it is supposed that there is defined business object Customer in the Business Object folder@View()exportclassMyView {constructor(private SqlQueryService:SqlQueryService){ }asyncload(){ var records =awaitthis.SqlQueryService.execute(`select * from ${Customer.boTableName}`);records.forEach(function(records){console.log(`Customer name is ${record.name}`); }); }}