home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / api / api15.c < prev    next >
C/C++ Source or Header  |  2001-01-05  |  7KB  |  221 lines

  1. /*
  2.  *    Program type:  API
  3.  *
  4.  *    Description:
  5.  *        This program demonstrates constructing a database
  6.  *        parameter buffer and attaching to a database.
  7.  *        First manually set sweep interval, then
  8.  *        The user and password is set to "guest" with expand_dpb
  9.  *        Then get back the sweep interval and other useful numbers
  10.  *        with an info call.
  11.  *        This program will accept up to 4 args.  All are positional:
  12.  *        api15 <db_name> <user> <password> <sweep_interval>
  13.  *
  14.  *        Note: The system administrator needs to create the account
  15.  *        "guest" with password "guest" on the server before this
  16.  *        example can be run.
  17.  * The contents of this file are subject to the Interbase Public
  18.  * License Version 1.0 (the "License"); you may not use this file
  19.  * except in compliance with the License. You may obtain a copy
  20.  * of the License at http://www.Inprise.com/IPL.html
  21.  *
  22.  * Software distributed under the License is distributed on an
  23.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  24.  * or implied. See the License for the specific language governing
  25.  * rights and limitations under the License.
  26.  *
  27.  * The Original Code was created by Inprise Corporation
  28.  * and its predecessors. Portions created by Inprise Corporation are
  29.  *
  30.  * Copyright (C) 2000 Inprise Corporation
  31.  * All Rights Reserved.
  32.  * Contributor(s): ______________________________________.
  33.  */
  34.  
  35.  
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <stdio.h>
  39. #include "example.h"
  40. #include <ibase.h>
  41.  
  42. int main (ARG(int, argc), ARG(char **, argv))
  43. ARGLIST(int argc)
  44. ARGLIST(char **argv)
  45. {
  46.     isc_db_handle   db = NULL;      /* database handle */
  47.     isc_tr_handle   trans = NULL;     /* transaction handle */
  48.     isc_stmt_handle stmt = NULL;
  49.     long            status[20];     /* status vector */
  50.     char            dbname[128];
  51.     char            user_name[31], uname[81];
  52.     short           nullind;
  53.     char            password[31];
  54.  
  55.     /* Query to find current user name */
  56.     static char ISC_FAR *query = "SELECT USER FROM RDB$DATABASE";
  57.     char ISC_FAR *  dpb = NULL,    /* DB parameter buffer */
  58.                     *d, *p, *copy;
  59.     XSQLDA ISC_FAR  *sqlda;
  60.  
  61.     short           dpb_length = 0;
  62.     long            l,sweep_interval = 16384;
  63.  
  64.     /* All needed for analyzing an info packet */
  65.     char            buffer[100];    /* Buffer for db info */
  66.     char            item;
  67.     short           length;
  68.     long            value_out;
  69.  
  70.     /* List of items for db_info call */
  71.     static char    db_items [] = {
  72.                         isc_info_page_size,
  73.                         isc_info_allocation,
  74.                         isc_info_sweep_interval,
  75.                         isc_info_end
  76.                     };
  77.  
  78.     strcpy(user_name, "guest");
  79.     strcpy(password, "guest");
  80.     strcpy(dbname, "employee.gdb");
  81.  
  82.     if (argc > 1)
  83.         strcpy(dbname, argv[1]);
  84.     if (argc > 2)
  85.         strcpy(user_name, argv[2]);
  86.     if (argc > 3)
  87.         strcpy(password, argv[3]);
  88.     if (argc > 4)
  89.         sweep_interval = atoi(argv[4]);
  90.  
  91.     /* Adding sweep interval will be done by hand 
  92.     **  First byte is a version (1), next byte is the isc_dpb_sweep
  93.     **  byte, then a length (4) then the byte-swapped int we want
  94.     **  to provide -- 7 bytes total
  95.     */
  96.  
  97.     copy = dpb = (char *) malloc(7);
  98.     p = dpb;
  99.     *p++ = '\1';
  100.     *p++ = isc_dpb_sweep_interval;
  101.     *p++ = '\4';
  102.     l = isc_vax_integer((char ISC_FAR *) &sweep_interval, 4);
  103.     d = (char *) &l;
  104.     *p++ = *d++;
  105.     *p++ = *d++;
  106.     *p++ = *d++;
  107.     *p = *d;
  108.     dpb_length = 7;
  109.  
  110.     /* Add user and password to dpb, much easier.  The dpb will be
  111.     **  new memory.
  112.     */
  113.     isc_expand_dpb(&dpb, (short ISC_FAR *) &dpb_length,
  114.                    isc_dpb_user_name, user_name,
  115.                    isc_dpb_password, password,  NULL);
  116.  
  117.     /*
  118.     **  Connect to the database with the given user and pw.
  119.     */            
  120.     printf("Attaching to %s with user name: %s, password: %s\n",
  121.            dbname, user_name, password);
  122.     printf("Resetting sweep interval to %ld\n", sweep_interval);
  123.  
  124.     if (isc_attach_database(status, 0, dbname, &db, dpb_length, dpb))
  125.         isc_print_status(status);
  126.  
  127.  
  128.     /* Prove we are "guest" . Query a single row with the
  129.     ** key word "USER" to find out who we are
  130.     */     
  131.     if (isc_start_transaction(status, &trans, 1, &db, 0, NULL))
  132.         isc_print_status(status);
  133.     
  134.     /* Prepare sqlda for singleton fetch */
  135.     sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(1));
  136.     sqlda->sqln = sqlda->sqld = 1;
  137.     sqlda->version = 1;
  138.     sqlda->sqlvar[0].sqldata = uname;
  139.     sqlda->sqlvar[0].sqlind = &nullind;
  140.  
  141.     /* Yes, it is possible to execute a singleton select without
  142.     ** a cursor.  You must prepare the sqlda by hand.
  143.     */
  144.     isc_dsql_allocate_statement(status, &db, &stmt);
  145.     if (isc_dsql_prepare(status, &trans, &stmt, 0, query, 1, sqlda))
  146.     {
  147.         ERREXIT(status, 1)
  148.     }
  149.     /* Force to type sql_text */
  150.     sqlda->sqlvar[0].sqltype = SQL_TEXT;
  151.  
  152.     if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  153.     {
  154.         ERREXIT(status, 1)
  155.     }
  156.     /* There will only be one row.  If it isn't there, something is
  157.     **  seriously wrong.
  158.     */
  159.     if (isc_dsql_fetch(status, &stmt, 1, sqlda))
  160.     {
  161.         ERREXIT(status, 1)
  162.     }
  163.  
  164.     isc_dsql_free_statement(status, &stmt, DSQL_close);
  165.  
  166.     uname[sqlda->sqlvar[0].sqllen] = '\0';
  167.     printf ("New user = %s\n", uname);
  168.     if (isc_commit_transaction(status, &trans))
  169.     {
  170.         ERREXIT(status, 1)
  171.     }
  172.  
  173.     /* Look at the database to see some parameters (like sweep
  174.     ** The database_info call returns a buffer full of type, length,
  175.     **  and value sets until info_end is reached
  176.     */
  177.  
  178.     if (isc_database_info(status, &db, sizeof(db_items), db_items,
  179.                           sizeof (buffer), buffer))
  180.     {
  181.         ERREXIT(status, 1)
  182.     }
  183.  
  184.     for (d = buffer; *d != isc_info_end;)
  185.     {
  186.         value_out = 0;
  187.         item = *d++;
  188.         length = (short) isc_vax_integer (d, 2);
  189.         d += 2;
  190.         switch (item)
  191.         {    
  192.             case isc_info_end:
  193.                 break;
  194.  
  195.             case isc_info_page_size:
  196.                 value_out = isc_vax_integer (d, length);
  197.                 printf ("PAGE_SIZE %ld \n", value_out);
  198.                 break;
  199.  
  200.             case isc_info_allocation:
  201.                 value_out = isc_vax_integer (d, length);
  202.                 printf ("Number of DB pages allocated = %ld \n", 
  203.                 value_out);
  204.                 break;
  205.  
  206.             case isc_info_sweep_interval:
  207.                 value_out = isc_vax_integer (d, length);
  208.                 printf ("Sweep interval = %ld \n", value_out);
  209.                 break;
  210.         }
  211.         d += length;
  212.     }    
  213.  
  214.     isc_detach_database(status, &db);
  215.     isc_free(dpb);
  216.     free(copy);
  217.     free(sqlda);
  218.  
  219.     return 0;
  220. }
  221.