view m-toolbox/classes/@repogui2/cb_connect.m @ 52:daf4eab1a51e database-connection-manager tip

Fix. Default password should be [] not an empty string
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 07 Dec 2011 17:29:47 +0100
parents f0afece42f48
children
line wrap: on
line source

% CB_CONNECT callback when the user clicks connect button.
%
% M Hewitson 22-09-08
%
% $Id: cb_connect.m,v 1.3 2011/04/08 08:56:36 hewitson Exp $
%

function cb_connect(varargin)

  myh     = varargin{1};
  mainfig = varargin{end};
  status  = findobj(mainfig.handle, 'Tag', 'RepoguiStatusTxt');
  panels  = mainfig.panels;

  % Get conn
  conn = mainfig.connection;

  if isempty(conn)

    % get hostname
    hostname = get(findobj(mainfig.handle, 'Tag','RepoguiServerEdit'), 'String');

    % get the database
    db = get(findobj(mainfig.handle, 'Tag','RepoguiDatabaseEdit'), 'String');

    if isempty(db)
      error('### Please select a database first.');
    end
    
    % check for username and password
    dbuser = mainfig.dbuser;
    dbpass = mainfig.dbpass;

    % connect
    if ~isempty(dbpass) && ~isempty(dbuser)
      conn = utils.mysql.connect(hostname, db, dbuser, dbpass);
    else
      [conn, dbpass] = utils.mysql.connect(hostname, db);
    end
    if isa(conn, 'database')
      mainfig.connection = conn;
      mainfig.username   = conn.Username;
      set(myh, 'String', 'disconnect');
      set(status, 'String', sprintf('connected to %s on %s as %s', db, hostname, conn.Username));

      % Set tables on query panel
      buildConditions(mainfig);

      % store the connection
      mainfig.dbuser = conn.Username;
      mainfig.dbpass = dbpass;
    else
      set(status, 'String', 'not connected');
      error('Invalid connection.');
    end
  else
    % disconnect
    close(conn);
    conn = [];
    disp('* disconnected.');
    mainfig.connection = conn;
    mainfig.username   = '';
    set(myh, 'String', 'connect');
    set(status, 'String', 'not connected');
    mainfig.dbuser = '';
    mainfig.dbpass = '';
  end

end