showPapers.m

function showPapers(keyword)
% output a list of papers related to the keyword
% e.g. simulation, thermofluid, benchmark, discrete events

myServer = "127.0.0.1";
myUser = "peter";
myPass = "**********";  % replace password!
myDB = "literature";
mySource = "MySQLDataSource";

% connect to mysql database
opts = databaseConnectionOptions("native", "MySQL");
opts = setoptions(opts, "DataSourceName", mySource, ...
    "DatabaseName", myDB, "Server", myServer, "PortNumber", 3306);
[dbOk, message] = testConnection(opts, myUser, myPass);
if ~dbOk
  fprintf("Connection to database '%s' failed!\n", myDB)
  fprintf("message: %s\n", message)
end
saveAsDataSource(opts);
conn = mysql(mySource, myUser, myPass);

% get list of papers
sqlquery = "SELECT paperId FROM " + ...
  "Paper NATURAL JOIN PaperKeyword NATURAL JOIN Keyword " + ...
  "WHERE word = '" + keyword + "'";
data = fetch(conn, sqlquery);
paperIds = data.paperId';

% create formatted output for each paper
for id = paperIds
  sAuth = getAuthorString(conn, id);
  title = getTitle(conn, id);
  fprintf("%s:\n%s\n\n", sAuth, title)
end

close(conn);

%---------------------------------------------------------------------
function sAuth = getAuthorString(conn, id)
% returns a formatted author list of paper with given id
sqlquery = "SELECT firstName, name FROM " + ...
    "Author NATURAL JOIN PaperAuthor " + ...
    "WHERE paperId = " + string(id);
data = fetch(conn, sqlquery);
sAuth = sprintf("%s.%s, ", [extract(data.firstName,1), data.name]');
sAuth = extractBefore(sAuth, strlength(sAuth)-1);
%---------------------------------------------------------------------
function title = getTitle(conn, id)
% returns the title of paper with given id 
sqlquery = "SELECT title FROM Paper WHERE paperId = " + string(id);
data = fetch(conn, sqlquery);
title = data.title;