/* * NAME * test_conn - test server and database connection routines * * ARGUMENTS * -d database name * -s server name * -h host name * -p port * * DESCRIPTION * Uses the following functions to login to a database: * mi_set_default_connection_info() * mi_server_connect() * mi_get_default_database_info() * mi_login() * * BUILD INSTRUCTIONS: * cc -g -I$MI_HOME/h -o test_conn test_conn.c mi_services.o \ * $MI_HOME/lib/libmi.a -lm * * SAMPLE USAGE: * Uses all defaults: * test_conn * * Overrides defaults: * test_conn -d template1 -h arcadia.cs.berkeley.edu * test_conn -d template1 -h arcadia.cs.berkeley.edu -p 7531 * test_conn -d template1 -s default * test_conn -d template1 -s SanDiego * test_conn -d template1 -s arcadia * * Should fail (unless you have a server named goofy...): * test_conn -s goofy * * MODS: * 5/31/94 jta Compiled for 2.0.12, works with 2.0.9 and 2.0.12. * 7/22/94 jta works with 2.1 * * $Header: /usr/local/devel/montage/samples/programs/RCS/test_conn.c,v 1.3 1994/07/22 23:03:10 jta Exp $ */ #include "mi_services.h" #include main(argc, argv) int argc; char **argv; { char mi_database[20], mi_host[40], mi_server[40], *tmp_port = NULL, library_ver[50], *server_query="return release();"; int c, errflg=0, mi_port=0; /* mi_port value */ MI_CONNECTION *conn = NULL; MI_CONNECTION_INFO cinfo; MI_DATABASE_INFO dinfo; /* ========== Get command line arguments. ========== */ memset(mi_database, (char *)NULL, sizeof(mi_database)); memset(mi_host, (char *)NULL, sizeof(mi_host)); memset(mi_server, (char *)NULL, sizeof(mi_server)); while ((c = getopt(argc, argv, "d:h:p:s:")) != -1) { switch (c) { case 'd': (void) strncpy(mi_database, optarg, sizeof(mi_database)-1); mi_database[sizeof(mi_database)] = '\0'; printf("mi_database = %s\n", mi_database); break; case 'h': (void) strncpy(mi_host, optarg, sizeof(mi_host) -1); mi_host[sizeof(mi_host)] = '\0'; printf("mi_host = %s\n", mi_host); break; case 'p': mi_port = atoi(optarg); printf("mi_port = %d\n", mi_port); break; case 's': (void) strncpy(mi_server, optarg, sizeof(mi_server) -1); mi_server[sizeof(mi_server)] = '\0'; printf("mi_server = %s\n", mi_server); break; case '?': errflg++; break; } } if (errflg || optind < argc) { (void) fprintf(stderr, "usage: %s -d db [-s server] [-h host [-p port]]\n", argv[0]); exit(2); } if (mi_add_callback(MI_All_Events, (MI_VOID) all_callback, NULL) == MI_ERROR) die("mi_add_callback failed.", conn); /*========= connect to server ========== */ cinfo.server_name = '\0'; cinfo.server_host = '\0'; cinfo.server_port = 0; /* If neither are set, get default */ if( (*mi_server == NULL) && (*mi_host == NULL) ) { if (mi_get_default_connection_info(&cinfo) == MI_ERROR) die("mi_get_default_connection_info failed.", conn); } else /* Set either MI_SERVER, or MI_HOST & MI_PORT (but not all 3) */ { if(*mi_server) { /* jta can't get this to work under 2.0.9 or 2.0.12: ** cinfo.server_name = mi_server; */ /* Has no effect: */ if (mi_sysname(mi_server) == (char *)NULL) die("cannot set server name", conn); /* This works: */ cinfo.server_host = mi_getsysparam(mi_server, "MI_HOST"); if (cinfo.server_host == '\0') die("cannot get server connect information", conn); tmp_port = mi_getsysparam(mi_server, "MI_PORT"); if(tmp_port) cinfo.server_port = atoi(tmp_port); } else { cinfo.server_host = mi_host; if(mi_port) /* optional */ cinfo.server_port = mi_port; } if ((mi_set_default_connection_info(&cinfo)) == MI_ERROR) die("mi_set_default_connection_info failed.", conn); } printf("Server info: "); printf("server_name=%s, server_host=%s, server_port=%d\n", cinfo.server_name, cinfo.server_host, cinfo.server_port); if ((conn = mi_server_connect(&cinfo)) == NULL) die("mi_server_connect failed.", conn); /*========= login to database ========== */ if (mi_get_default_database_info(&dinfo) == MI_ERROR) die("mi_get_default_database_info failed - aborting program", conn); if(*mi_database) dinfo.database_name = mi_database; /* override database name */ printf("Database info: "); printf("database_name=%s, user_name=%s\n", dinfo.database_name, dinfo.user_name); if (mi_login(conn, &dinfo) == MI_ERROR) die("mi_login failed.", conn); /*========= Do something =============== */ printf("\nLogged into Illustra server '%s'.\n", mi_sysname((char *)NULL)); if( mi_library_version (library_ver, sizeof(library_ver) - 1) == MI_ERROR) die("client_func: mi_library_version failed", conn); printf("libmi version = '%s'\n", library_ver); printf("server version= "); if (exec_query(conn, server_query, 0) == MI_ERROR) die("select failed.", conn); /*========= Disconnect ================ */ if (mi_close(conn) == MI_ERROR) die("can't close connection", NULL); else exit(0); }