Home > database > mysql_make_conn.m

mysql_make_conn

PURPOSE ^

Opens a connection to a mysql server.

SYNOPSIS ^

function [conn_id, mysql_info] = mysql_make_conn(mysql_info,database,conn_id)

DESCRIPTION ^

 Opens a connection to a mysql server.
 conn_id = mysql_make_conn(mysql_info);

 Opens a connection to a mysql server and returns a connection ID
 (conn_id) that can be used in subsequent calls to the database. The
 connection information is specified in the input structure (mysql_info)
 which has to have the following fields:

 mysql_info.host - name of host with the database
 mysql_info.database - name of database to open
 mysql_info.user - user who to connect to the database with
 mysql_info.passwd - the database password for the specific user

 The mysql_info structure can be obtained by calling
 mysql_login() solely with the host and database information.

 If no connection ID is given, a default ID of zero is used, and a conn_id
 field is attached to mysql_info and returned as an optional 2nd output argument.
 If a connection with the specified conn_id is already open, it is left open.

 NOTE: In contrast to earlier versions, mysql_make_conn now REQUIRES a structure
 that specifies the host, database, user, and password in order to
 establish a connection.  Failure to pass in any of this information will
 no longer result in utilization of default values.

 See also: mysql_login

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [conn_id, mysql_info] = mysql_make_conn(mysql_info,database,conn_id)
0002 % Opens a connection to a mysql server.
0003 % conn_id = mysql_make_conn(mysql_info);
0004 %
0005 % Opens a connection to a mysql server and returns a connection ID
0006 % (conn_id) that can be used in subsequent calls to the database. The
0007 % connection information is specified in the input structure (mysql_info)
0008 % which has to have the following fields:
0009 %
0010 % mysql_info.host - name of host with the database
0011 % mysql_info.database - name of database to open
0012 % mysql_info.user - user who to connect to the database with
0013 % mysql_info.passwd - the database password for the specific user
0014 %
0015 % The mysql_info structure can be obtained by calling
0016 % mysql_login() solely with the host and database information.
0017 %
0018 % If no connection ID is given, a default ID of zero is used, and a conn_id
0019 % field is attached to mysql_info and returned as an optional 2nd output argument.
0020 % If a connection with the specified conn_id is already open, it is left open.
0021 %
0022 % NOTE: In contrast to earlier versions, mysql_make_conn now REQUIRES a structure
0023 % that specifies the host, database, user, and password in order to
0024 % establish a connection.  Failure to pass in any of this information will
0025 % no longer result in utilization of default values.
0026 %
0027 % See also: mysql_login
0028 
0029 % 01/04/07 PJ Modified to return conn_id
0030 % 12/19/07 PJ added checking for open connection
0031 % 10/18/09 PJ added option of passing in the first argument as a struct
0032 %             that contains all of the information
0033 % 10/30/09 PJ minor fix to handle empty first argument
0034 % 11/3/09  ST minor fix (use host_or_struct in first try instead of host)
0035 % 06/08/10 ST abstracted function to work with any host or database
0036 %             branching based on host and database moved to mysql_researcher_login.m
0037 % 06/15/10 PJ Modified to force passing in of connection information
0038   
0039 if nargin < 1
0040   error('%s: Structure with following fields required:\n\t.host\n\t.database\n\t.user\n\t.passwd\n', mfilename);
0041 end
0042 
0043 if nargin == 3
0044   warning(sprintf(['The host,database,conn_id scheme for calling mysql_make_conn has been changed.\n' ...
0045     'The single input parameter is now a structure with the following required fields:\n' ...
0046     '\t.host\n\t.database\n\t.user\n\t.passwd\n\t.conn_id - for connections other than 0']))
0047   if ~isempty(conn_id) && ~mysql(conn_id,'status')
0048     fprintf('Connection with ID=%d is already open\n', conn_id)
0049     return
0050   else
0051     disp('Given separate login possibilities for researcher and subject, there is no way to handle your request.');
0052   end
0053 end
0054 
0055 if ~isfield(mysql_info, 'conn_id') || isempty(mysql_info.conn_id)
0056   mysql_info.conn_id = 0;
0057 end;
0058 
0059 if mysql(mysql_info.conn_id,'status')
0060   % Need to open a mysql connection
0061   
0062   % Check to make sure we have the requisite variables to try
0063   if ~all(isfield(mysql_info,{'host','database','user','passwd'})) || ...
0064       isempty(mysql_info.host) || ...
0065       isempty(mysql_info.user) || ...
0066       isempty(mysql_info.passwd) || ...
0067       isempty(mysql_info.database)
0068     error('%s: insufficient information specified to establish connection')
0069   else
0070     status = mysql(mysql_info.conn_id,'open',mysql_info.host,mysql_info.user,mysql_info.passwd);
0071     database_str = sprintf('use %s', mysql_info.database);
0072     mysql(mysql_info.conn_id,database_str);
0073   end
0074 end
0075 
0076 conn_id = mysql_info.conn_id;
0077 
0078 return

Generated on Sat 24-Aug-2019 04:00:39 by m2html © 2003