ソースを参照

combine separate racket files into a single utility. fixes #8

George C. Privon 1 年間 前
コミット
d765c0ff04
3 ファイル変更40 行追加41 行削除
  1. 2 2
      README.md
  2. 0 37
      create_database.rkt
  3. 38 2
      proposal_database.rkt

+ 2 - 2
README.md

@@ -3,8 +3,8 @@
 Tools to create and manipulate a sqlite3 database with information on telescope and funding proposals.
 
 Before using, copy `config.rkt.example` to `config.rkt` and edit it with your desired database location and, optionally, your last name.
-Then run `racket create_database.rkt` to create the sqlite3 file and create the `proposals` table.
-Run `racket update_proposals.rkt help` for information on how to add/update entries.
+Then run `racket proposal_database.rkt create-database` to create the sqlite3 file and create the `proposals` table.
+Run `racket proposal_database.rkt help` for information on how to add/update entries.
 
 ## Requirements
 

+ 0 - 37
create_database.rkt

@@ -1,37 +0,0 @@
-#lang racket/base
-
-;; This program creates a sqlite3 database and then creates an empty table
-;; for information on proposals.
-
-(require db
-         "config.rkt") ; load configuration file
-
-; create the database and create the table
-(define (createdb dbloc)
-  (write-string (string-append "Creating database " dbloc "\n"))
-  (define conn (sqlite3-connect #:database dbloc
-                                #:mode 'create))
-  (query-exec conn "CREATE TABLE proposals (ID INTEGER PRIMARY KEY,
-type TEXT NOT NULL,
-organization TEXT NOT NULL,
-solicitation TEXT NOT NULL,
-telescope TEXT DEFAULT '',
-orgpropID TEXT NOT NULL,
-PI TEXT NOT NULL,
-title TEXT NOT NULL,
-CoI TEXT NOT NULL,
-status TEXT NOT NULL,
-submitdate TEXT NOT NULL,
-resultdate TEXT DEFAULT '')")
-  (disconnect conn)
-  (write-string (string-append "Database created at " dbloc "\n")))
-
-
-; make sure we can use the sqlite3 connection
-(cond (not (sqlite3-available?))
-      (error "Sqlite3 library not available."))
-
-; create the database and add the `proposals` table if it doesn't exist
-(if (not (file-exists? dbloc))
-    (createdb dbloc)
-    (write-string "Database exists. Exiting."))

+ 38 - 2
update_proposals.rkt → proposal_database.rkt

@@ -41,6 +41,7 @@
                             progname " MODE"))
   (newline)
   (displayln "Where MODE is one of:")
+  (displayln " create-database - initialize the proposal database.")
   (displayln " add\t\t - add new proposal to database.")
   (displayln " update\t\t - update a proposal with results.")
   (displayln " stats\t\t - print summary statistics.")
@@ -50,7 +51,7 @@
   (displayln " list-rejected\t - Show rejected proposals.")
   (displayln " help\t\t - Show this help message.")
   (newline)
-  (displayln "Copyright 2019-2020, 2022-2023 George C. Privon"))
+  (displayln "Copyright 2019-2020, 2022-2024 George C. Privon"))
 
 ; set up a condensed prompt for getting information
 (define (getinput prompt)
@@ -84,6 +85,37 @@
 (define (last-proposal-call conn)
   (vector->list (query-row conn "SELECT type, organization, solicitation, telescope FROM proposals ORDER BY id DESC LIMIT 1")))
 
+; create the database and create the table
+(define (createdb dbloc)
+  ; make sure we can use the sqlite3 connection
+  (cond [(not (sqlite3-available?)) (error "Sqlite3 library not available.")])
+
+  ; create the database and add the `proposals` table if it doesn't exist
+  (cond [(file-exists? dbloc) (error "Database exists. Exiting.")])
+  (write-string (string-append "Creating database " dbloc "\n"))
+  (define conn (sqlite3-connect #:database dbloc
+                                #:mode 'create))
+  (query-exec conn "CREATE TABLE proposals (ID INTEGER PRIMARY KEY,
+type TEXT NOT NULL,
+organization TEXT NOT NULL,
+solicitation TEXT NOT NULL,
+telescope TEXT DEFAULT '',
+orgpropID TEXT NOT NULL,
+PI TEXT NOT NULL,
+title TEXT NOT NULL,
+CoI TEXT NOT NULL,
+status TEXT NOT NULL,
+submitdate TEXT NOT NULL,
+resultdate TEXT DEFAULT '')")
+  (disconnect conn)
+  (write-string (string-append "Database created at " dbloc "\n")))
+
+; check to see if we can access the database
+(define (checkdb conn)
+  (cond [(connected? conn) (write-string "Database created successfully.")]
+        [else (write-string "Could not connect to database.")]))
+
+
 ; add a new proposal to the database
 (define (addnew conn)
   ; full list of input fileds that we will need (these will be the prompts
@@ -294,7 +326,10 @@
 
 ; catch-all routine for when we need to access the database
 (define (querysys mode)
-  ; first see if we need write access or if we can use read only
+  ; check if the user would like to create the database or not
+  (cond
+    [(regexp-match "create-database" mode) (createdb dbloc)])
+  ; see if we need write access or if we can use read only
   (define dbmode (if (or (regexp-match "add" mode)
                          (regexp-match "update" mode))
                      'read/write
@@ -304,6 +339,7 @@
                                 #:mode dbmode))
   ; now handle the user's request
   (cond
+    [(regexp-match "create-database" mode) (checkdb conn)]
     [(regexp-match "add" mode) (addnew conn)]
     [(regexp-match "update" mode) (findpending conn)]
     [(regexp-match "stats" mode) (proposal-stats conn)]