#include "dbproc.h"
#include <string.h>
#include <stdlib.h>

@implementation SybaseProcess

- initWith:(const char*) un password:(const char*)pwd server:(const char*)sn
{
  id rval = NULL; 
  if (_login = [[SybaseLogin alloc] initWith:un password:pwd]){
    _ownLogin = 1;
    _serverName = (char*)malloc(sizeof(char)*(strlen(sn)+1));
    strcpy(_serverName, sn);
    if (_proc = dbopen([_login loginRec], _serverName)) rval = self;
  }
  if (!rval) [self free];
  return rval;
}

- initWith: (SybaseLogin *) login server: (const char*) sn
{
  id rval = NULL;
  if (_login=login){
    _serverName = (char*)malloc(sizeof(char)*(strlen(sn)+1));
    strcpy(_serverName, sn);
    if (_proc = dbopen([_login loginRec], _serverName)) rval = self;
  }
  if (!rval) [self free];
  return rval;
}

- (const char*) serverName
{
  return (const char*) _serverName;
}

- (const char*) userName
{
  return [_login userName];
}

- (BOOL)useDBNamed:(const char*)dbName
{
  return (dbuse(_proc ,dbName)==SUCCEED)?YES:NO;
}

- (char*)currentDBName
{
  return dbname(_proc);
}

- free
{
  if (_ownLogin) [_login free];
  if (_serverName) free(_serverName);
  if (_proc) dbclose(_proc);
}

@end



