/*******************************************************************************
**
** Source File Name = sws.c
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 2000 
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
** PURPOSE : to demonstrate the use of the DATABASE MONITOR SWITCH API.
**
** OTHER APIs USED :
**          INSTANCE ATTACH               sqleatin()
**
** EXTERNAL FUNCTIONS USED IN CODE SAMPLE:
**          check_error :     Checks for SQLCODE error, and prints out any
**          [in UTIL.C]          related information available.
**
**    EXTERNAL DEPENDENCIES :
**       - Ensure existence of database for precompile purposes.
**       - Compile and link with the IBM Cset++ compiler (AIX and OS/2)
**         or the Microsoft Visual C++ compiler (Windows) 
**         or the compiler supported on your platform.
**
** For more information about these samples see the README file.
**
** For more information on programming in C, see the:
**   -  "Programming in C and C++" section of the Application Development Guide
** For more information on Building C Applications, see the:
**   -  "Building C Applications" section of the Application Building Guide.
**
** For more information on the SQL language see the SQL Reference.
**
*******************************************************************************/
 /******************************************************************************
     The Database Monitor Switch API

#include "sqlca.h"
#include "sqlutilapi.h"
#include "sqlmon.h"
#include "utilapi.h"
int SQL_API_FN sqlmon(sqluint32             version,
                      char                 *reserved,
                      sqlm_recording_group group_states[],
                      struct sqlca         *sqlca
                     );

The 'group_states' is an input/output structure used to specify
the switches to set or query, it returns their settings.

INPUT:

version: SQLM_DBMON_VERSION1
         SQLM_DBMON_VERSION2
         Both will return the same output.

INPUT/OUTPUT:

group_states: An array of sqlm_recording_group, one for each monitor switch:
    input_state:  - SQLM_HOLD  -  Request setting for this switch
                    SQLM_OFF   -  Turn the switch OFF
                    SQLM_ON    -  Turn the switch ON
    output_state  - The current state of the switch: SQLM_OFF, or SQLM_ON
    start_time    - A timestamp, the time a which the switch was turned ON,
                    0 if the switch is OFF.

*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "sqlca.h"
#include "sqlutil.h"
#include "sqlmon.h"
#include "utilapi.h"


 /******************************************************************************
* Database Monitor Switch API Sample
*******************************************************************************/
char* sw_status_string(int val) {
    switch (val) {
    case SQLM_OFF: return "OFF";
    case SQLM_ON:  return "ON";
    }
    return "";
}
void print_sws(sqlm_recording_group group_states[SQLM_NUM_GROUPS]);
void print_sw_set_times(sqlm_recording_group group_states[SQLM_NUM_GROUPS]);

int main (int argc, char* argv[]) {
    /********************************************************************
     Set Table switch ON, UOW switch OFF, and query the current (default)
     values for the other switches.  (see Database Manager Configuration)
    ********************************************************************/
    sqlm_recording_group group_states[SQLM_NUM_GROUPS];
    struct sqlca sqlca;

    char userid[9];
    char passwd[19];
    char nodename[9];

    if (argc == 4) {
        strcpy (userid, argv[1]);
        strcpy (passwd, argv[2]);
        strcpy (nodename, argv[3]);

       
       /*************************/
       /* ATTACH API called     */
       /*************************/
        sqleatin (nodename, userid, passwd, &sqlca);
    }
    else if (argc != 1) {
        printf("\nUSAGE: sws [userid password remote_nodename]\n");
        return 1;
    }

    group_states[SQLM_TABLE_SW].input_state =       SQLM_ON;
    group_states[SQLM_UOW_SW].input_state =         SQLM_OFF;
    group_states[SQLM_STATEMENT_SW].input_state =   SQLM_HOLD;
    group_states[SQLM_BUFFER_POOL_SW].input_state = SQLM_HOLD;
    group_states[SQLM_LOCK_SW].input_state =        SQLM_HOLD;
    group_states[SQLM_SORT_SW].input_state =        SQLM_HOLD;

    /************************************\
    * DATABASE MONITOR SWITCH API called *
    \************************************/
    sqlmon(SQLM_DBMON_VERSION2, NULL, group_states, &sqlca);
    API_SQL_CHECK("MONITOR SWITCH");

    /* Print the output */
    print_sws(group_states);          /* Print the switch values */
    print_sw_set_times(group_states); /* Print their switch set time (if ON) */
    return 0;

} /* end of Database Monitor Switch API sample */

 /******************************************************************************
* print switch values
*******************************************************************************/
void print_sws(sqlm_recording_group group_states[SQLM_NUM_GROUPS]) {

    printf("SQLM_UOW_SW:         %s\n",
         sw_status_string(group_states[SQLM_UOW_SW].output_state));
    printf("SQLM_STATEMENT_SW:   %s\n",
         sw_status_string(group_states[SQLM_STATEMENT_SW].output_state));
    printf("SQLM_TABLE_SW:       %s\n",
         sw_status_string(group_states[SQLM_TABLE_SW].output_state));
    printf("SQLM_BUFFER_POOL_SW: %s\n",
         sw_status_string(group_states[SQLM_BUFFER_POOL_SW].output_state));
    printf("SQLM_LOCK_SW:        %s\n",
         sw_status_string(group_states[SQLM_LOCK_SW].output_state));
    printf("SQLM_SORT_SW:        %s\n",
         sw_status_string(group_states[SQLM_SORT_SW].output_state));
} /* end print_sws */

 /******************************************************************************
* print switch set times (if ON)
*******************************************************************************/
void print_sw_set_times(
           sqlm_recording_group group_states[SQLM_NUM_GROUPS]) {

    if (group_states[SQLM_UOW_SW].start_time.seconds) {
        printf("SQLM_UOW_SW start_time:         %s\n", ctime((time_t *)
        &group_states[SQLM_UOW_SW].start_time.seconds));
    }
    if (group_states[SQLM_STATEMENT_SW].start_time.seconds) {
        printf("SQLM_STATEMENT_SW start_time:   %s\n", ctime((time_t *)
        &group_states[SQLM_STATEMENT_SW].start_time.seconds));
    }
    if (group_states[SQLM_TABLE_SW].start_time.seconds) {
        printf("SQLM_TABLE_SW start_time:       %s\n", ctime((time_t *)
        &group_states[SQLM_TABLE_SW].start_time.seconds));
    }
    if (group_states[SQLM_BUFFER_POOL_SW].start_time.seconds) {
        printf("SQLM_BUFFER_POOL_SW start_time: %s\n", ctime((time_t *)
        &group_states[SQLM_BUFFER_POOL_SW].start_time.seconds));
    }
    if (group_states[SQLM_LOCK_SW].start_time.seconds) {
        printf("SQLM_LOCK_SW start_time:        %s\n", ctime((time_t *)
        &group_states[SQLM_LOCK_SW].start_time.seconds));
    }
    if (group_states[SQLM_SORT_SW].start_time.seconds) {
        printf("SQLM_SORT_SW start_time:        %s\n", ctime((time_t *)
        &group_states[SQLM_SORT_SW].start_time.seconds));
    }
} /* end print_sw_set_times (if ON) */