Active Query Builder support area

Call the InitializeDatabaseSchemaTree asynchronously

Avatar
  • updated
  • Completed

Hi,
I have a tabbed form: one tab with the text box (opens first) and the other tab has the query builder. usually, when the user opens the form he gets the text box and immediately begins to type the SQL. later he may or may not move to the visual query builder.

This means it doesn't make sense to call InitializeDatabaseSchemaTree() and make him wait while the meta data is loading. But then, I do want the auto-complete on the text box, which is dependent upon InitializeDatabaseSchemaTree() finishing.

My question is: can it be done asynchronously? Can I just let him type as the form opens, then call InitializeDatabaseSchemaTree in the background (on some thread I guess) and then have the user see the autocomplete as soon as it's done, while he's already busy typing the SQL?

and if so, is there a code sample anywhere? I couldn't find it

Avatar
Andrey Zavyalov, PM

 

The code sample below can help you to cope with this task, but it is a kind of workaround. The current version of Active Query Builder 3 doesn't have methods to pass another connection object to perform metadata retrieval in another thread reliably. Thus, in this code sample, we turned off other queries that may be initiated by the end-user dragging objects to the Design pane. So, during the metadata loading process, the component will not be able to retrieve lists of fields for tables on the design pane. The code sample additionally calls the RefreshDatasourcesMetadata method to update the design pane when metadata loading is finished.

We are planning to improve the API in one of the next minor versions and provide this functionality off the shelf.

// create and connect metadata provider
var metadataProvider = new MSSQLMetadataProvider {
    Connection = new SqlConnection
    {
        ConnectionString =
            "Data Source=sql2008;Initial Catalog=AdventureWorks;Integrated Security=true;"
    }};
metadataProvider.Connect();

// create and init syntax provider
var syntaxProvider = new MSSQLSyntaxProvider();
syntaxProvider.DoDetectServerVersion(metadataProvider);

// set up query builder
queryBuilder1.SyntaxProvider = syntaxProvider;
queryBuilder1.MetadataProvider = metadataProvider;
// disable on-demand metadata queries
queryBuilder1.MetadataLoadingOptions.OfflineMode = true;

new Thread(() => {
    // preload database objects from background thread
    queryBuilder1.SQLContext.MetadataContainer.LoadAll(false);

    // init visual interface (in GUI thread)
    queryBuilder1.Invoke((MethodInvoker) delegate
    {
        // load database schema tree on completion
        queryBuilder1.InitializeDatabaseSchemaTree();
        // disable on-demand metadata queries
        queryBuilder1.MetadataLoadingOptions.OfflineMode = false;
    });
}).Start();