/*
 * Copyright 1990 by Baylor College of Medicine ALL RIGHTS RESERVED. 
 *
 * This program is subject to a license agreement between 
 * Baylor College of Medicine and MIT. Any use inconsistent with
 * said license and any use by persons other than the faculty, 
 * students and staff at MIT or any use on a computer not operated 
 * as part of the Athena Computing Environment (ACE) is expressly 
 * prohibited.
 */
/****************************************************************
 * File: database.c
 * Date: 05/05/91
 *
 * Description:
 *   This file contains functions for manipulatin the VnsDatabase
 *   structure.
 *
 * Revisions:
 ****************************************************************/
#include <stdio.h>
#include <string.h>
#include <malloc.h>

#include "VnsP.h"

/****************************************************************
 *              Public Functions
 ****************************************************************/

/*
 * This function adds a database to the internal list of databases
 * but does not make a connection with that database.
 */
PUBLIC
VnsDatabase *
VnsAddDatabase(vns,host,port,name)
	VnsContext *vns;
	char *host;
	int port;
	char *name;
{
	VnsDatabase *db = (VnsDatabase *)malloc(sizeof(VnsDatabase));

	db->srv = NULL;            /* initially not connected */
	db->host = strdup(host);
	db->port = port;
	db->name = strdup(name);
	db->vns = vns;
	db->first_note = NULL;
	db->first_author = NULL;
	db->next = NULL;

	/* add it to the list of databases */
	db->next = vns->first_db;
	vns->first_db = db;

	/* now set the new database as the "current" database */
	vns->current_db = db;

	return db;
}

PUBLIC
void
VnsDeleteDatabase(vns,db)
	VnsContext *vns;
	VnsDatabase *db;
{
	/* we want to close the connection if connected and delete from list */
}

PUBLIC
Boolean
VnsConnect(db)
	VnsDatabase *db;
{
	if (db == NULL)
	{
		fprintf(stderr,"VnsConnect: db is NULL\n");
		return FALSE;
	}

	db->srv = srv_init(db->host,db->port,db->name);
	if (db->srv == NULL)
	{
		return FALSE;
	}

	/* add event handlers for this connection (see events.c) */
	add_event_handlers(db);

	return TRUE;
}

PUBLIC
void
VnsDisconnect(db)
	VnsDatabase *db;
{
	if (db == NULL)
	{
		fprintf(stderr,"VnsDisconnect: db is NULL\n");
		return;
	}

	if (db->srv == NULL) return;

	srv_close(db->srv);

	db->srv = NULL;
}
