# quotations - create a database of quotations
# new and improved to use subroutines and functions
# 2012-11-25 j.m.reneau

# create a new database file or open it or create it with a few good quotes
dbfile$ = "quotations.sqlite3"
e = exists(dbfile$)		# set e to true if database already exists
# open the database - open databases are global to program
# dboopen without a database number opens the database as 0
dbopen dbfile$
# if the database did not exist then create the tables and populate
# with initial set og quotaions
if (not e) then call createquotetable()

print "Database has " + quotecount() + " quotes."

# display a menu until the user chooses a zero
do
   print
   print "1 - view a random quote "
   print "2 - list all quotes"
   print "3 - add a quote"
   print "0 - quit"
   input "0,1,2,or 3 >", choice
   
   if choice = 1 then call showrandomquote()
   if choice = 2 then call showallquotes()
   if choice = 3 then call addnewquote()
until choice = 0

# wrap everything up
print "Good bye."
dbclose
end

subroutine showrandomquote()
   # generate a random integer from 1 to the number of quotes
   # and display that quotation
   quote = 1 + int(rand * quotecount())
   call showquote(quote)
end subroutine

subroutine showquote(n)
   # given a quote number n - display a single quotation
   stmt$ = "select author, saying from quotes where ROWID = " + n + ";"
   dbopenset stmt$
   if dbrow() then
      print dbstring("saying")
      print " - " + dbstring("author")
   else
      print "Quotation " + n + " is not on file."
   end if
   dbcloseset
end subroutine

subroutine showallquotes()
   # show all quotes in the table
   dbopenset "select rowid, author, saying from quotes order by rowid;"
   while dbrow()
      print dbint("rowid") + " " + dbstring("saying") + " (" + dbstring("author") + ")"
   end while
   dbcloseset
end subroutine

subroutine addnewquote()
   # ask the user to enter in a new quote
   # do not allow
   input "Quote>", quote$
   input "Author>", author$
   if quote$ = "" or author$ = "" then
      print "You must supply both a quote and an author to add."
   else
      id = insertquote(author$, quote$)
      print "Quote " + id + " added."
   end if
end subroutine

function quotecount()
   # return the quotation count
   # get an unused set number for database 0 for this operation
   quotecount = 0
   tempset = freedbset(0)
   dbopenset 0, tempset, "select count(*) as k from quotes"
   if dbrow(0, tempset) then
      quotecount =  dbint(0, tempset, "k")
   end if
   dbcloseset 0,tempset
end function

subroutine createquotetable()
   # create the quotes table a populate an initial group of quotations into it
   # sqlite automatically creates a primary key column called ROWID and assigns a unique value
   dbexecute "create table quotes (author text, saying text);"
   id = insertquote('Abraham Lincoln (1809 - 1865)','Most folks are about as happy as they make up their minds to be.')
   id = insertquote('George Burns (1896 - 1996)','Happiness is having a large, loving, caring, close-knit family in another city.')
   id = insertquote('Jean Houston','At the height of laughter, the universe is flung into a kaleidoscope of new possibilities.')
   id = insertquote('Doug Larson', 'Home computers are being called upon to perform many new functions, including the consumption of homework formerly eaten by the dog.')
   id = insertquote( 'Isaac Asimov', 'I do not fear computers. I fear the lack of them.')
   id = insertquote( 'Pierre Gallois', 'If you put tomfoolery into a computer, nothing comes out of it but tomfoolery. But this tomfoolery, having passed through a very expensive machine, is somehow ennobled and no-one dares criticize it.')
   id = insertquote('Robert Orben', 'To err is human--and to blame it on a computer is even more so.')
   id = insertquote('Herm Albright (1876 - 1944)', 'A positive attitude may not solve all your problems, but it will annoy enough people to make it worth the effort.')
   id = insertquote('William James (1842 - 1910)', 'The greatest discovery of my generation is that a human being can alter his life by altering his attitudes of mind.')
end subroutine

function insertquote(author$, saying$)
   # insert a single quotation into the database table
   dbexecute "insert into quotes (author, saying) values (" + q$(author$) +", " + q$(saying$) + ");"
   # now get the primary key (ROWID) that was assigned to the entry
   dbopenset "select last_insert_rowid()"
   if dbrow() then
      insertquote =  dbint(0)
   else
      insertquote = -1
   end if
   dbcloseset
end function

function q$(a$)
   # put single quotes around a string after changing single quotes to two single quotes
   q$ = "'" + replace(a$,"'","''") + "'"
end function
