diff options
| author | Nikolaus Gotsche <n@softwarefools.com> | 2018-10-26 15:36:21 +0200 |
|---|---|---|
| committer | Nikolaus Gotsche <n@softwarefools.com> | 2018-10-26 15:36:21 +0200 |
| commit | 3fb09744cc5264fdf3740364a9c6f6f5b64707cc (patch) | |
| tree | ca3a8a716a163783febd1859fe37f9217331a79e /sqlite.go | |
| parent | d06db7af6014e8498672899218a05adf36ab1aa3 (diff) | |
Export tested, structs made Public0.3.4
Diffstat (limited to 'sqlite.go')
| -rw-r--r-- | sqlite.go | 311 |
1 files changed, 188 insertions, 123 deletions
@@ -15,33 +15,36 @@ import ( _ "github.com/mattn/go-sqlite3" ) -type project struct { - id int - name string - comment string - first time.Time - last time.Time - finished int // id of last paid bill - customer int -} - -type task struct { - id int - projectid int - start time.Time - stop time.Time - taskname string - comment string - checkout int // id of bill -} - -type customer struct { - id int - company string - name string - address string - satz float64 - lastbill time.Time // Last time a bill was paid +// Datatype of SQL projects table +type Project struct { + Id int + Name string + Comment string + First time.Time + Last time.Time + Finished int // id of last paid bill + Customer int +} + +// Datatype of SQL timetable table +type Task struct { + Id int + Projectid int + Start time.Time + Stop time.Time + Taskname string + Comment string + Checkout int // id of bill +} + +// Datatype of SQL customer table +type Customer struct { + Id int + Company string + Name string + Address string + Satz float64 + Lastbill time.Time // Last time a bill was paid } type billitem struct { @@ -64,8 +67,8 @@ type bill struct { var db *sql.DB var err error -var currproject project -var opentask task +var currproject Project +var opentask Task var pausetask int // Database Operations @@ -200,7 +203,7 @@ func newTaskTime(tim string) { boldRed := color.New(color.FgRed, color.Bold).SprintFunc() fmt.Println(frame(boldGreen("Starting Task at ", tim),true)) bonus := "" - if opentask.id == 0 { + if opentask.Id == 0 { timstr := "1791-09-30 19:07" //zone, _ := time.Now().Zone() if isDateTime(tim) { @@ -221,22 +224,22 @@ func newTaskTime(tim string) { fmt.Println(nli+timstr) task := getInterInput(sli+"Specify Task: ") if task == "" { - nm,st := GetTaskSums(currproject.id) + nm,st := GetTaskSums(currproject.Id) ch := Multichoice("What Task should be Started at "+timstr+"?",st) task = nm[ch] bonus = line("xxx",true) } //if proj == 0 { - _, err = stmt.Exec(currproject.id, timstr, task, 0) + _, err = stmt.Exec(currproject.Id, timstr, task, 0) //} else { // _, err = stmt.Exec(proj, timstr, task, 0) //} checkErr(err) - fmt.Println(bonus+nli+"...new task inserted into", currproject.name, ": ", task) + fmt.Println(bonus+nli+"...new task inserted into", currproject.Name, ": ", task) fmt.Println(bonus+frame(posR(),false)) getOpenTask() - updateProject(currproject.id) + updateProject(currproject.Id) } else { fmt.Println(nli+boldRed("Another Task is already Open")) //fmt.Println(frame("Close Task First",false)) @@ -255,7 +258,7 @@ func newTask(resume bool) { }else{ fmt.Println(frame(boldGreen("Starting Task Now"),true)) } - if opentask.id > 0 { + if opentask.Id > 0 { fmt.Println(nli+boldRed("Another Task is already Open")) //showOpenTask() fmt.Println(frame(negR(),false)) @@ -265,7 +268,7 @@ func newTask(resume bool) { if pausetask == 0 { fmt.Println(nli+boldRed("No Task was Paused")) if isInterSure(nli+"Resume older task?"){ - nm,st := GetTaskSums(currproject.id) + nm,st := GetTaskSums(currproject.Id) ch := Multichoice("What Task should be resumed?",st) task = nm[ch] bonus = line("xxx",true) @@ -277,15 +280,15 @@ func newTask(resume bool) { idx := []int{pausetask} tsks := GetSelectedTasks(idx) fulltask := tsks[0] - fmt.Println(nli+"Resuming Task ", pausetask, " - ", fulltask.taskname) + fmt.Println(nli+"Resuming Task ", pausetask, " - ", fulltask.Taskname) //fmt.Println() - task = fulltask.taskname + task = fulltask.Taskname } } else { //fmt.Println(boldGreen("Starting new Task")) task = getInterInput(sli+"Specify Task: ") if task == "" { - nm,st := GetTaskSums(currproject.id) + nm,st := GetTaskSums(currproject.Id) ch := Multichoice("What Task should be restarted?",st) task = nm[ch] bonus = line("xxx",true) @@ -293,14 +296,14 @@ func newTask(resume bool) { } stmt, err := db.Prepare("INSERT INTO timetable(project, task, checkout) values(?, ?, ?)") checkErr(err) - _, err = stmt.Exec(currproject.id, task, 0) + _, err = stmt.Exec(currproject.Id, task, 0) checkErr(err) if !resume { - fmt.Println(bonus+nli+"...New Task inserted into", currproject.name, ": ", task) + fmt.Println(bonus+nli+"...New Task inserted into", currproject.Name, ": ", task) } fmt.Println(bonus+frame(posR(),false)) getOpenTask() - updateProject(currproject.id) + updateProject(currproject.Id) } func newBill(proj int) (int, string) { @@ -437,12 +440,12 @@ func closeTaskTime(tim string) { boldRed := color.New(color.FgRed, color.Bold).SprintFunc() //fmt.Println(boldGreen("Stoping Task ", opentask.id, ":", opentask.taskname)) fmt.Println(frame(boldGreen("Stoping Task"),true)) - if opentask.id == 0 { + if opentask.Id == 0 { fmt.Println(nli+boldRed("There is no Open Task")) fmt.Println(frame(negR(),false)) return }else{ - fmt.Println(nli,"ID ",opentask.id, ":", opentask.taskname) + fmt.Println(nli,"ID ",opentask.Id, ":", opentask.Taskname) } //timt,err := time.Parse("2006-01-02 15:04",tim) timst, timstr := "1791-09-30 19:07", "1791-09-30 19:07" @@ -464,19 +467,19 @@ func closeTaskTime(tim string) { checkErr(err) //fmt.Println(timst,timt,opentask.start) - if timt.After(opentask.start) { + if timt.After(opentask.Start) { //timstr := timt.UTC().Format("2006-01-02 15:04") com := "" if isInterSure(sli+"Do You Want to enter a Comment?") { com = getInterMultiInput(nli+"Comment:") } - fmt.Println(nli,"...Closing Task", opentask.id, "at", timst) + fmt.Println(nli,"...Closing Task", opentask.Id, "at", timst) stmt, err := db.Prepare("UPDATE timetable SET stop = datetime(?,'utc'), comment = ? WHERE id = ?") checkErr(err) - _, err = stmt.Exec(timstr, com, opentask.id) + _, err = stmt.Exec(timstr, com, opentask.Id) checkErr(err) - opentask.id = 0 - updateProject(opentask.projectid) + opentask.Id = 0 + updateProject(opentask.Projectid) fmt.Println(frame(posR(),false)) } else { fmt.Println(nli,boldRed("Cannot Stop before the Beginning!")) @@ -491,34 +494,34 @@ func closeTask(loud bool) { boldRed := color.New(color.FgRed, color.Bold).SprintFunc() if loud { fmt.Println(frame(boldGreen("Stoping Task"),true)) - if opentask.id == 0 { + if opentask.Id == 0 { fmt.Println(nli,boldRed("There is no Open Task")) fmt.Println(frame(negR(),false)) return }else{ - fmt.Println(nli,"ID ",opentask.id, ":", opentask.taskname) + fmt.Println(nli,"ID ",opentask.Id, ":", opentask.Taskname) } //fmt.Println(boldGreen("Stoping Task ", opentask.id, ":", opentask.taskname)) }else{ - if opentask.id == 0 { + if opentask.Id == 0 { fmt.Println(boldRed("There is no Open Task")) } } - if time.Now().After(opentask.start.Local()) { + if time.Now().After(opentask.Start.Local()) { com := "" if loud { if isInterSure(sli+"Do You Want to enter a Comment?") { com = getInterMultiInput(nli+"Comment:") } - fmt.Println(nli+"...Closing Task", opentask.id) + fmt.Println(nli+"...Closing Task", opentask.Id) fmt.Println(frame(posR(),false)) } stmt, err := db.Prepare("UPDATE timetable SET stop = datetime('now'), comment = ? WHERE id = ?") checkErr(err) - _, err = stmt.Exec(com, opentask.id) + _, err = stmt.Exec(com, opentask.Id) checkErr(err) - opentask.id = 0 - updateProject(opentask.projectid) + opentask.Id = 0 + updateProject(opentask.Projectid) } else { if loud { fmt.Println(nli,boldRed("Cannot Stop before the Beginning!")) @@ -692,7 +695,9 @@ func checkCustomerProjects(in []int) (multicust, multiproj bool, projid []int) { return } -func getProject(id int) (outpr project, outcu customer) { +// return a project and a customer struct that corresponds +// to a project id. +func GetProject(id int) (outpr Project, outcu Customer) { rows, err := db.Query("SELECT * FROM projects WHERE id = ?", id) checkErr(err) var pid, customer, finished int @@ -702,13 +707,13 @@ func getProject(id int) (outpr project, outcu customer) { for rows.Next() { err = rows.Scan(&pid, &name, &comm, &first, &last, &finished, &customer) checkErr(err) - outpr.id = pid - outpr.name = name - outpr.comment = comm - outpr.first = first - outpr.last = last - outpr.finished = finished - outpr.customer = customer + outpr.Id = pid + outpr.Name = name + outpr.Comment = comm + outpr.First = first + outpr.Last = last + outpr.Finished = finished + outpr.Customer = customer } row, err := db.Query("SELECT * FROM customers WHERE id = ?", customer) checkErr(err) @@ -720,18 +725,56 @@ func getProject(id int) (outpr project, outcu customer) { for row.Next() { err = row.Scan(&cid, &comp, &cuname, &addy, &satz, &lastb) checkErr(err) - outcu.id = cid - outcu.company = comp - outcu.name = cuname - outcu.address = addy - outcu.satz = satz - outcu.lastbill = lastb + outcu.Id = cid + outcu.Company = comp + outcu.Name = cuname + outcu.Address = addy + outcu.Satz = satz + outcu.Lastbill = lastb + } + return +} +// Return the []customers corresponding to an []int of customer ids +func GetSelectedCustomers(in []int) (out []Customer) { + ins := strings.Trim(strings.Replace(fmt.Sprint(in), " ", " , ", -1), "[]") + que := fmt.Sprintf("SELECT * FROM customers WHERE id IN (%s)", ins) + rows, err := db.Query(que) + checkErr(err) + + var id int + var rat float64 + var last time.Time + var nam, com, add string + + defer rows.Close() + for rows.Next() { + err = rows.Scan(&id, &com, &nam, &add, &rat, &last) + out = append(out, Customer{id,com,nam,add,rat,last}) + } + return +} + +// Return the []projects corresponding to an []int of project ids +func GetSelectedProjects(in []int) (out []Project) { + ins := strings.Trim(strings.Replace(fmt.Sprint(in), " ", " , ", -1), "[]") + que := fmt.Sprintf("SELECT * FROM projects WHERE id IN (%s)", ins) + rows, err := db.Query(que) + checkErr(err) + + var id,fin, cust int + var first, last time.Time + var nam, com string + + defer rows.Close() + for rows.Next() { + err = rows.Scan(&id, &nam, &com, &first, &last, &fin, &cust) + out = append(out, Project{id,nam,com,first,last,fin,cust}) } return } -// Return the []tasks correspintding to an []int of task ids -func GetSelectedTasks(in []int) (outtask []task) { +// Return the []tasks corresponding to an []int of task ids +func GetSelectedTasks(in []int) (outtask []Task) { ins := strings.Trim(strings.Replace(fmt.Sprint(in), " ", " , ", -1), "[]") que := fmt.Sprintf("SELECT * FROM timetable WHERE id IN (%s)", ins) rows, err := db.Query(que) @@ -744,27 +787,27 @@ func GetSelectedTasks(in []int) (outtask []task) { defer rows.Close() for rows.Next() { err = rows.Scan(&id, &project, &start, &stop, &tsk, &com, &checkout) - outtask = append(outtask, task{id, project, start, stop, tsk, com, checkout}) + outtask = append(outtask, Task{id, project, start, stop, tsk, com, checkout}) } return } - -//Return sum of Hours and Dateranges -func AnalyzeTasks(in []task) (count int, hours float64, duration string) { +// Loop through all Tasks in a Slice and +// Return count, sum of Hours and Dateranges +func AnalyzeTasks(in []Task) (count int, hours float64, duration string) { var lstart, hstop time.Time for i, t := range in { if i == 0 { - lstart = t.start - hstop = t.stop + lstart = t.Start + hstop = t.Stop } count++ - dur := float64(t.stop.Sub(t.start)) / (1000000000 * 60 * 60) + dur := float64(t.Stop.Sub(t.Start)) / (1000000000 * 60 * 60) hours = hours + dur - if lstart.After(t.start) { - lstart = t.start + if lstart.After(t.Start) { + lstart = t.Start } - if hstop.Before(t.stop) { - hstop = t.stop + if hstop.Before(t.Stop) { + hstop = t.Stop } //txt := fmt.Sprintf("%s - (%v) - %.2f h",task, durstr, dur) } @@ -794,6 +837,7 @@ func getProjectList(in []int) ([]int, []string) { } return outids, outstr } + // Returns two Lists of Task ids and corresponding strings for Multichoice selection. // Depending on showcust the name of the customer will be displayed too // Depending on exclude Tasks of project 0 will be in the Return Lists @@ -811,9 +855,9 @@ func GetTaskList(in []int, showcust,exclude bool) ([]int, []string) { //rows,err := db.Query("SELECT id, project, start, stop, task FROM timetable WHERE stop != '1791-09-30 19:07' AND checkout = 0 AND id IN ? ORDER BY project DESC, stop DESC",in) if len(in) == 1 && in[0] == 0 { rows, err = db.Query("SELECT id, project, start, stop, task FROM timetable WHERE stop != '1791-09-30 19:07' AND checkout = 0 ORDER BY project DESC, stop DESC") - if exclude { +/* if exclude { rows, err = db.Query("SELECT id, project, start, stop, task FROM timetable WHERE stop != '1791-09-30 19:07' AND checkout = 0 AND project != 0 ORDER BY project DESC, stop DESC") - } + }*/ } //else{ // rows,err = db.Query("SELECT id, project, start, stop, task FROM timetable WHERE stop != '1791-09-30 19:07' AND checkout = 0 AND id IN ? ORDER BY project DESC, stop DESC",in) //} @@ -825,8 +869,8 @@ func GetTaskList(in []int, showcust,exclude bool) ([]int, []string) { for rows.Next() { err = rows.Scan(&id, &project, &start, &stop, &task) checkErr(err) + pr, cu := getProjectName(project) if project != lastpr { - pr, cu := getProjectName(project) if showcust { pre = fmt.Sprintf("%v| %v: ", cu, pr) } else { @@ -842,12 +886,16 @@ func GetTaskList(in []int, showcust,exclude bool) ([]int, []string) { durstr := fmt.Sprintf("%v - %v", start.Local().Format("Mon Jan _2 2006 15:04"), stop.Local().Format("15:04")) txt := fmt.Sprintf("%s - (%v) - %.2f h", task, durstr, dur) - outids = append(outids, id) - outstr = append(outstr, fmt.Sprintf("%s%s", pre, txt)) + if !(exclude && cu == "No one") { + outids = append(outids, id) + outstr = append(outstr, fmt.Sprintf("%s%s", pre, txt)) + } } return outids, outstr } -func getProjectIds() []int { + +// Returns slice of ids of all Projects except Project 0 +func GetProjectIds() []int { var ids []int rows, err := db.Query("SELECT id FROM projects WHERE id != 0") // ORDER BY id DESC") checkErr(err) @@ -862,7 +910,24 @@ func getProjectIds() []int { return ids } -func getTaskIds() []int { +// Returns slice of ids of all Customers except Customer 0 +func GetCustomerIds() []int { + var ids []int + rows, err := db.Query("SELECT id FROM customers WHERE id != 0") // ORDER BY id DESC") + checkErr(err) + var id int + + defer rows.Close() + for rows.Next() { + err = rows.Scan(&id) + checkErr(err) + ids = append(ids, id) + } + return ids +} + +// Returns Slice of All Task ids except open ones +func GetTaskIds() []int { var ids []int rows, err := db.Query("SELECT id FROM timetable WHERE stop != '1791-09-30 19:07'") // ORDER BY id DESC") checkErr(err) @@ -928,11 +993,11 @@ func getOpenTask() { //fmt.Println(id, "Open Task:", task, project, start) } //rows.Close() //good habit to close - opentask.id = id - opentask.projectid = project - opentask.start = start - opentask.taskname = task - opentask.checkout = checkout + opentask.Id = id + opentask.Projectid = project + opentask.Start = start + opentask.Taskname = task + opentask.Checkout = checkout //opentask.checkout = checkout != 0 } @@ -970,14 +1035,14 @@ func showStatus(full bool) { // Get all Tasks of the current Project and display them with simmilar name func ShowProjectSum() { - _,st := GetTaskSums(currproject.id) + _,st := GetTaskSums(currproject.Id) fmt.Println(sub("Tasks")) fmt.Println(StrLines(st,nli)) fmt.Println(sub("")) } func showCurrentTask() { - if opentask.id == 0 { + if opentask.Id == 0 { if pausetask > 0 { //fmt.Printf("___Task %v Paused___________\n", pausetask) st := fmt.Sprintf("Task %v Paused", pausetask) @@ -989,10 +1054,10 @@ func showCurrentTask() { } else { //fmt.Println("___Open Task________________") fmt.Println(sub("Open Task")) - dur := float64(time.Now().Sub(opentask.start)) / (1000000000 * 60 * 60) + dur := float64(time.Now().Sub(opentask.Start)) / (1000000000 * 60 * 60) //fmt.Printf(" %v: %v - (%v) - %.2f h\n", opentask.id, opentask.taskname, opentask.start.Local().Format("Mon Jan _2 2006 15:04"), dur) //fmt.Println(opentask.id,":", opentask.taskname,"-", opentask.start.Local().Format("Mon Jan _2 2006 15:04"),dur,"h") - fmt.Printf("%s %v: %v - (%v) - %.2f h\n", nli, opentask.id, opentask.taskname, opentask.start.Local().Format("Mon Jan _2 2006 15:04"), dur) + fmt.Printf("%s %v: %v - (%v) - %.2f h\n", nli, opentask.Id, opentask.Taskname, opentask.Start.Local().Format("Mon Jan _2 2006 15:04"), dur) fmt.Println(frame("",false)) } } @@ -1003,8 +1068,8 @@ func showOpenProject(alone bool) { if alone { fmt.Println(frame(boldGreen("Current Project"),true)) } - fmt.Println(nli,currproject.id, ":", currproject.name, "- Started:", currproject.first.Local().Format("Mon _2 Jan 2006")) - fmt.Println(nli," Last Changes", currproject.last.Local().Format("2006 Mon Jan _2 15:04")) + fmt.Println(nli,currproject.Id, ":", currproject.Name, "- Started:", currproject.First.Local().Format("Mon _2 Jan 2006")) + fmt.Println(nli," Last Changes", currproject.Last.Local().Format("2006 Mon Jan _2 15:04")) //fmt.Println(frame("Current Project",true)) } @@ -1039,7 +1104,7 @@ func newProject() { boldRed := color.New(color.FgRed, color.Bold).SprintFunc() boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() fmt.Println(frame(boldGreen("Creating new Project"),true)) - if opentask.id > 0 { + if opentask.Id > 0 { fmt.Println(nli,boldRed("There is an Open Task")) fmt.Println(frame(negR(),false)) //showOpenTask() @@ -1077,13 +1142,13 @@ func newProject() { } func getClosedTasks(num int) { - rows, err := db.Query("SELECT * FROM timetable WHERE stop != '1791-09-30 19:07' ORDER BY datetime(start)", currproject.id) + rows, err := db.Query("SELECT * FROM timetable WHERE stop != '1791-09-30 19:07' ORDER BY datetime(start)", currproject.Id) checkErr(err) if num > 0 { - rows, err = db.Query("SELECT * FROM timetable WHERE project = $1 AND checkout > 0 AND stop != '1791-09-30 19:07' ORDER BY datetime(start) DESC LIMIT $2", currproject.id, num) + rows, err = db.Query("SELECT * FROM timetable WHERE project = $1 AND checkout > 0 AND stop != '1791-09-30 19:07' ORDER BY datetime(start) DESC LIMIT $2", currproject.Id, num) checkErr(err) } else { - rows, err = db.Query("SELECT * FROM timetable WHERE project = $1 AND checkout > 0 AND stop != '1791-09-30 19:07' ORDER BY datetime(start)", currproject.id) + rows, err = db.Query("SELECT * FROM timetable WHERE project = $1 AND checkout > 0 AND stop != '1791-09-30 19:07' ORDER BY datetime(start)", currproject.Id) checkErr(err) } var id, proj, check int @@ -1118,10 +1183,10 @@ func getClosedTasks(num int) { rows.Close() if num > 0 { - rows, err = db.Query("SELECT * FROM timetable WHERE project = $1 AND checkout = 0 AND stop != '1791-09-30 19:07'ORDER BY datetime(start) DESC LIMIT $2", currproject.id, num) + rows, err = db.Query("SELECT * FROM timetable WHERE project = $1 AND checkout = 0 AND stop != '1791-09-30 19:07'ORDER BY datetime(start) DESC LIMIT $2", currproject.Id, num) checkErr(err) } else { - rows, err = db.Query("SELECT * FROM timetable WHERE project = $1 AND checkout = 0 AND stop != '1791-09-30 19:07'ORDER BY datetime(start)", currproject.id) + rows, err = db.Query("SELECT * FROM timetable WHERE project = $1 AND checkout = 0 AND stop != '1791-09-30 19:07'ORDER BY datetime(start)", currproject.Id) checkErr(err) } sum2 := 0.0 @@ -1178,14 +1243,14 @@ func getLastProject() { } rows.Close() //good habit to close - currproject.id = nuid - currproject.name = nprname - currproject.comment = nprcom - currproject.first = nfirst - currproject.last = nlast - currproject.finished = nfinish + currproject.Id = nuid + currproject.Name = nprname + currproject.Comment = nprcom + currproject.First = nfirst + currproject.Last = nlast + currproject.Finished = nfinish //currproject.finish = nfinish != 0 - currproject.customer = ncustom + currproject.Customer = ncustom } @@ -1193,7 +1258,7 @@ func setProject(nid int) { boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() boldRed := color.New(color.FgRed, color.Bold).SprintFunc() fmt.Println(frame(boldGreen("Opening Project ", nid),true)) - if opentask.id > 0 { + if opentask.Id > 0 { fmt.Println(nli,boldRed("There is an Open Task")) //fmt.Println(frame("",false)) showCurrentTask() @@ -1227,14 +1292,14 @@ func setProject(nid int) { checkErr(err) } rows.Close() //good habit to close - currproject.id = uid - currproject.name = prname - currproject.comment = comm - currproject.first = first - currproject.last = last - currproject.finished = finish + currproject.Id = uid + currproject.Name = prname + currproject.Comment = comm + currproject.First = first + currproject.Last = last + currproject.Finished = finish //currproject.finish = finish != 0 - currproject.customer = custo + currproject.Customer = custo updateProject(uid) showOpenProject(false) |
