%% Reset Connection Options to Default Values
% Create, configure, and test a PostgreSQL native interface data source for a PostgreSQL database. Reset the database connection options to their default values. Then configure, test, and save the data source with different database connection options.
%
% Create a PostgreSQL native interface data source for a PostgreSQL database connection.

vendor = "PostgreSQL";
opts = databaseConnectionOptions("native",vendor)

% opts is an SQLConnectionOptions object with these properties:
% DataSourceName — Name of the data source
% Vendor — Database vendor name
% DatabaseName — Name of the database
% Server — Name of the database server
% PortNumber — Port number
%
% Configure the data source by setting the database connection options for the data source PostgreSQLDataSource, database name toystore_doc, database server dbtb00, and port number 5432.

opts = setoptions(opts, ...
    'DataSourceName',"PostgreSQLDataSource", ...
    'DatabaseName',"toystore_doc",'Server',"dbtb00", ...
    'PortNumber',5432)

% The setoptions function sets the DataSourceName, DatabaseName, Server, and PortNumber properties in the SQLConnectionOptions object.
%
% Test the database connection with a user name and password. The testConnection function returns the logical 1, which indicates the database connection is successful.

username = "dbdev";
password = "matlab";
status = testConnection(opts,username,password)

%
% Reset the database connection options to their default values.

opts = reset(opts)

%
% Configure the data source again by setting the database connection options for the data source PostgreSQLDataSource, database name toystore_doc, database server dbtb00, and port number 5432. Also, set a driver-specific connection option to specify a timeout value for establishing the database connection.

opts = setoptions(opts, ...
    "DataSourceName","PostgreSQLDataSource", ...
    "DatabaseName","toystore_doc", ...
    "Server","dbtb00","PortNumber",5432, ...
    "connect_timeout",20)

% The setoptions function sets the DataSourceName, DatabaseName, Server, and PortNumber properties in the SQLConnectionOptions object. The driver-specific connection option appears below the other connection options.
%
% Test the database connection again.

username = "dbdev";
password = "matlab";
status = testConnection(opts,username,password)

%
% Save the configured data source.

saveAsDataSource(opts)

% Copyright 2020 The MathWorks, Inc.