home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / interb / InterBase_WI-V6.0-server.exe / server / examples / api / api15.c < prev    next >
C/C++ Source or Header  |  2000-06-23  |  7KB  |  224 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.Interbase.com/IPL/
  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 Interbase Software Corporation
  28.  * and its successors. Portions created by Borland/Inprise are
  29.  * Copyright (C) 1992-1998 and 1999-2000 Borland/Inprise. Portions
  30.  * created by InterBase Software Corporation are Copyright (C)
  31.  * 1998-1999 InterBase Software Corporation.
  32.  *
  33.  * Copyright (C) 2000 InterBase Software Corporation
  34.  * All Rights Reserved.
  35.  * Contributor(s): ______________________________________.
  36.  */
  37.  
  38.  
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <stdio.h>
  42. #include "example.h"
  43. #include <ibase.h>
  44.  
  45. int main (ARG(int, argc), ARG(char **, argv))
  46. ARGLIST(int argc)
  47. ARGLIST(char **argv)
  48. {
  49.     isc_db_handle   db = NULL;      /* database handle */
  50.     isc_tr_handle   trans = NULL;     /* transaction handle */
  51.     isc_stmt_handle stmt = NULL;
  52.     long            status[20];     /* status vector */
  53.     char            dbname[128];
  54.     char            user_name[31], uname[81];
  55.     short           nullind;
  56.     char            password[31];
  57.  
  58.     /* Query to find current user name */
  59.     static char ISC_FAR *query = "SELECT USER FROM RDB$DATABASE";
  60.     char ISC_FAR *  dpb = NULL,    /* DB parameter buffer */
  61.                     *d, *p, *copy;
  62.     XSQLDA ISC_FAR  *sqlda;
  63.  
  64.     short           dpb_length = 0;
  65.     long            l,sweep_interval = 16384;
  66.  
  67.     /* All needed for analyzing an info packet */
  68.     char            buffer[100];    /* Buffer for db info */
  69.     char            item;
  70.     short           length;
  71.     long            value_out;
  72.  
  73.     /* List of items for db_info call */
  74.     static char    db_items [] = {
  75.                         isc_info_page_size,
  76.                         isc_info_allocation,
  77.                         isc_info_sweep_interval,
  78.                         isc_info_end
  79.                     };
  80.  
  81.     strcpy(user_name, "guest");
  82.     strcpy(password, "guest");
  83.     strcpy(dbname, "employee.gdb");
  84.  
  85.     if (argc > 1)
  86.         strcpy(dbname, argv[1]);
  87.     if (argc > 2)
  88.         strcpy(user_name, argv[2]);
  89.     if (argc > 3)
  90.         strcpy(password, argv[3]);
  91.     if (argc > 4)
  92.         sweep_interval = atoi(argv[4]);
  93.  
  94.     /* Adding sweep interval will be done by hand 
  95.     **  First byte is a version (1), next byte is the isc_dpb_sweep
  96.     **  byte, then a length (4) then the byte-swapped int we want
  97.     **  to provide -- 7 bytes total
  98.     */
  99.  
  100.     copy = dpb = (char *) malloc(7);
  101.     p = dpb;
  102.     *p++ = '\1';
  103.     *p++ = isc_dpb_sweep_interval;
  104.     *p++ = '\4';
  105.     l = isc_vax_integer((char ISC_FAR *) &sweep_interval, 4);
  106.     d = (char *) &l;
  107.     *p++ = *d++;
  108.     *p++ = *d++;
  109.     *p++ = *d++;
  110.     *p = *d;
  111.     dpb_length = 7;
  112.  
  113.     /* Add user and password to dpb, much easier.  The dpb will be
  114.     **  new memory.
  115.     */
  116.     isc_expand_dpb(&dpb, (short ISC_FAR *) &dpb_length,
  117.                    isc_dpb_user_name, user_name,
  118.                    isc_dpb_password, password,  NULL);
  119.  
  120.     /*
  121.     **  Connect to the database with the given user and pw.
  122.     */            
  123.     printf("Attaching to %s with user name: %s, password: %s\n",
  124.            dbname, user_name, password);
  125.     printf("Resetting sweep interval to %ld\n", sweep_interval);
  126.  
  127.     if (isc_attach_database(status, 0, dbname, &db, dpb_length, dpb))
  128.         isc_print_status(status);
  129.  
  130.  
  131.     /* Prove we are "guest" . Query a single row with the
  132.     ** key word "USER" to find out who we are
  133.     */     
  134.     if (isc_start_transaction(status, &trans, 1, &db, 0, NULL))
  135.         isc_print_status(status);
  136.     
  137.     /* Prepare sqlda for singleton fetch */
  138.     sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(1));
  139.     sqlda->sqln = sqlda->sqld = 1;
  140.     sqlda->version = 1;
  141.     sqlda->sqlvar[0].sqldata = uname;
  142.     sqlda->sqlvar[0].sqlind = &nullind;
  143.  
  144.     /* Yes, it is possible to execute a singleton select without
  145.     ** a cursor.  You must prepare the sqlda by hand.
  146.     */
  147.     isc_dsql_allocate_statement(status, &db, &stmt);
  148.     if (isc_dsql_prepare(status, &trans, &stmt, 0, query, 1, sqlda))
  149.     {
  150.         ERREXIT(status, 1)
  151.     }
  152.     /* Force to type sql_text */
  153.     sqlda->sqlvar[0].sqltype = SQL_TEXT;
  154.  
  155.     if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  156.     {
  157.         ERREXIT(status, 1)
  158.     }
  159.     /* There will only be one row.  If it isn't there, something is
  160.     **  seriously wrong.
  161.     */
  162.     if (isc_dsql_fetch(status, &stmt, 1, sqlda))
  163.     {
  164.         ERREXIT(status, 1)
  165.     }
  166.  
  167.     isc_dsql_free_statement(status, &stmt, DSQL_close);
  168.  
  169.     uname[sqlda->sqlvar[0].sqllen] = '\0';
  170.     printf ("New user = %s\n", uname);
  171.     if (isc_commit_transaction(status, &trans))
  172.     {
  173.         ERREXIT(status, 1)
  174.     }
  175.  
  176.     /* Look at the database to see some parameters (like sweep
  177.     ** The database_info call returns a buffer full of type, length,
  178.     **  and value sets until info_end is reached
  179.     */
  180.  
  181.     if (isc_database_info(status, &db, sizeof(db_items), db_items,
  182.                           sizeof (buffer), buffer))
  183.     {
  184.         ERREXIT(status, 1)
  185.     }
  186.  
  187.     for (d = buffer; *d != isc_info_end;)
  188.     {
  189.         value_out = 0;
  190.         item = *d++;
  191.         length = (short) isc_vax_integer (d, 2);
  192.         d += 2;
  193.         switch (item)
  194.         {    
  195.             case isc_info_end:
  196.                 break;
  197.  
  198.             case isc_info_page_size:
  199.                 value_out = isc_vax_integer (d, length);
  200.                 printf ("PAGE_SIZE %ld \n", value_out);
  201.                 break;
  202.  
  203.             case isc_info_allocation:
  204.                 value_out = isc_vax_integer (d, length);
  205.                 printf ("Number of DB pages allocated = %ld \n", 
  206.                 value_out);
  207.                 break;
  208.  
  209.             case isc_info_sweep_interval:
  210.                 value_out = isc_vax_integer (d, length);
  211.                 printf ("Sweep interval = %ld \n", value_out);
  212.                 break;
  213.         }
  214.         d += length;
  215.     }    
  216.  
  217.     isc_detach_database(status, &db);
  218.     isc_free(dpb);
  219.     free(copy);
  220.     free(sqlda);
  221.  
  222.     return 0;
  223. }
  224.