Handling a Language Event

This example describes the lang_execute event handler provided in the sample gateway application. This event handler is implemented as a callback function that sends a client's language command to the SQL Server example remote database, receives the results, and returns the results to the client.

  1. Define the lang_execute function and the following parameters:
    int lang_execute(srvproc)
    SRV_PROC *srvproc;
    {
    REMOTE_DBMS *rmt_dbms;        // Remote database pointer             
    DBPROCESS *rmtproc;            // Open Data Services DBPROCESS pointer
    DBCHAR *query;                // Pointer to language buffer          
    int query_len;                // Length of query                     
    int status;                    // Status of DB-Library calls          
    
  2. Get the remote DBPROCESS pointer *rmtproc in the SRV_PROC connection structure with srv_getuserdata. This is a pointer that was saved using srv_setuserdata after the client connected to a remote database.
    rmt_dbms = (REMOTE_DBMS *)srv_getuserdata(srvproc);
    rmtproc = rmt_dbms->dbproc;
    
  3. Get the pointer to the client's language request command buffer using the srv_langptr function, and put the pointer in the DBPROCESS structure of the SQL Server database:
    // Get the pointer to the client language request command buffer. 
    
    query = srv_langptr (srvproc);
    
    // Place the pointer in the remote DBPROCESS pointer rmtproc.     
    
    status = dbcmd (rmtproc, query);
    
  4. Send the SQL command to the remote database using the DB-Library dbsqlexec function if the previous DB-Library dbcmd function call was successful:
    if (status == SUCCEED)    // Previous DB-Library call successful   
        status = dbsqlexec (rmtproc);
    else
    
      // If an attention event was issued or if the previous DB-Library
      // call was not successful, acknowledge and send a results       
      // completion message to the client.                             
    
        srv_senddone (srvproc, SRV_DONE_FINAL, (DBUSMALLINT) 0,
             (DBINT) 0);
    
  5. Get the remote database results and pass them back to the client using the handle_results function:
    handle_results (rmtproc, srvproc);
    

    (For more information about this function, see Receiving Results from a Remote Database.

  6. Return the srv_continue constant to continue processing events:
    return (SRV_CONTINUE);