function deleteJDBCDataSource(datasourcename)
%DELETEJDBCDATASOURCE delete a JDBC data source
%
% deleteJDBCDataSource(datasourcename)
% datasourcename is the jdbc data source to delete

%   Copyright 2019-2020 The MathWorks, Inc.

validateattributes(datasourcename,["string","char"],{'scalartext'},"deleteJDBCDataSource","datasourcename");

database.internal.utilities.repairOldJDBCDataSources

if ~ispref('DatabaseToolbox',database.options.SQLConnectionOptions.PREFERENCE_NAME)
    error(message('database:database:dataSourceNameNotFoundUnix'))
end

%Get the configurations from the preferences
allSources = listDataSources;
jdbcNames = allSources.Name(allSources.DriverType == "JDBC");

if ~ismember(datasourcename,jdbcNames)
    error(message('database:configureJDBCDataSource:InvalidMatch',prettyPrint(jdbcNames),datasourcename));
end


database.internal.utilities.hdeleteDataSource(datasourcename);
end

function str = prettyPrint(existing_datasources)
        
    str = '';
    for i = 1:numel(existing_datasources)
        if i == numel(existing_datasources)
            str = [str '' sprintf('''%s'' ', existing_datasources{i})];   
            break;
        end
        if mod(i,4) == 0
            str = [str '' sprintf('''%s'',\n ',existing_datasources{i})];
        else
            str = [str '' sprintf('''%s'', ', existing_datasources{i})];
        end

    end
end