Migrate the rest of everything from rowid to id

This commit is contained in:
2024-10-25 16:37:37 +02:00
parent b89e27d5b4
commit 24546a4ef5
6 changed files with 36 additions and 37 deletions

View File

@@ -19,7 +19,7 @@ drop view if exists weightMonthly;
drop view if exists weightYearly;
create view weightView as
select rowid,
select id,
date,
round(weight, 2) as weight
from weight
@@ -152,7 +152,7 @@ drop view if exists foodYearly;
drop view if exists foodRecent;
create view foodView as
select rowid,
select id,
date,
food,
description,
@@ -198,8 +198,7 @@ group by strftime('%Y', date)
order by date desc;
create view foodRecent as
select rowid,
*
select *
from food
order by date desc
limit 10;
@@ -220,7 +219,7 @@ set per100 = coalesce(
limit 1
)
)
where rowid = new.rowid;
where id = new.id;
end;
create table settings(

View File

@@ -11,7 +11,7 @@ type (
db *DB
}
Food struct {
Rowid int64 `json:"rowid"`
Id int64 `json:"id"`
Date string `json:"date"`
Food string `json:"food"`
Descripton string `json:"description"`
@@ -31,7 +31,7 @@ type (
}
)
const foodColumns = "rowid, date, food, description, amount, per100, energy"
const foodColumns = "id, date, food, description, amount, per100, energy"
const foodAggregatedColumns = "period, amount, avgPer100, energy"
func (s *FoodService) GetRecent() ([]Food, error) {
@@ -48,7 +48,7 @@ func (s *FoodService) GetRecent() ([]Food, error) {
for row.Next() {
var food Food
err := row.Scan(&food.Rowid, &food.Date, &food.Food, &food.Descripton, &food.Amount, &food.Per100, &food.Energy)
err := row.Scan(&food.Id, &food.Date, &food.Food, &food.Descripton, &food.Amount, &food.Per100, &food.Energy)
if err != nil {
log.Printf("error scanning row: %v", err)
continue
@@ -89,7 +89,7 @@ limit %d
for rows.Next() {
var f FoodSearch
err := rows.Scan(&f.Rowid, &f.Date, &f.Food.Food, &f.Descripton, &f.Amount, &f.Per100, &f.Energy, &f.Score)
err := rows.Scan(&f.Id, &f.Date, &f.Food.Food, &f.Descripton, &f.Amount, &f.Per100, &f.Energy, &f.Score)
if err != nil {
log.Printf("error scanning row: %v", err)
continue
@@ -129,20 +129,20 @@ func (s *FoodService) Create(food Food) (Food, error) {
}
}
rowid, err := res.LastInsertId()
id, err := res.LastInsertId()
if err != nil {
return food, fmt.Errorf("error getting last insert id: %v", err)
}
return s.GetByRowid(rowid)
return s.GetById(id)
}
func (s *FoodService) Update(food Food) (Food, error) {
if s.db == nil || !s.db.Ready {
return food, fmt.Errorf("cannot update food, db is nil or is not ready")
}
if food.Rowid <= 0 {
return food, fmt.Errorf("cannot update food, rowid is less than or equal to 0")
if food.Id <= 0 {
return food, fmt.Errorf("cannot update food, id is less than or equal to 0")
}
if food.Food == "" {
return food, fmt.Errorf("cannot update food, food is empty")
@@ -152,28 +152,28 @@ func (s *FoodService) Update(food Food) (Food, error) {
}
if food.Per100 > 0 {
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ?, per100 = ? WHERE rowid = ?", food.Food, food.Descripton, food.Amount, food.Per100, food.Rowid)
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ?, per100 = ? WHERE Id = ?", food.Food, food.Descripton, food.Amount, food.Per100, food.Id)
if err != nil {
return food, fmt.Errorf("error updating food: %v", err)
}
} else {
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ? WHERE rowid = ?", food.Food, food.Descripton, food.Amount, food.Rowid)
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ? WHERE Id = ?", food.Food, food.Descripton, food.Amount, food.Id)
if err != nil {
return food, fmt.Errorf("error updating food: %v", err)
}
}
return s.GetByRowid(food.Rowid)
return s.GetById(food.Id)
}
func (s *FoodService) GetByRowid(rowid int64) (Food, error) {
func (s *FoodService) GetById(id int64) (Food, error) {
var res Food
if s.db == nil || !s.db.Ready {
return res, fmt.Errorf("cannot get food by rowid, db is nil or is not ready")
return res, fmt.Errorf("cannot get food by id, db is nil or is not ready")
}
row := s.db.readConn.QueryRow(fmt.Sprintf("SELECT %s from foodView WHERE rowid = ?", foodColumns), rowid)
err := row.Scan(&res.Rowid, &res.Date, &res.Food, &res.Descripton, &res.Amount, &res.Per100, &res.Energy)
row := s.db.readConn.QueryRow(fmt.Sprintf("SELECT %s from foodView WHERE id = ?", foodColumns), id)
err := row.Scan(&res.Id, &res.Date, &res.Food, &res.Descripton, &res.Amount, &res.Per100, &res.Energy)
if err != nil {
return res, fmt.Errorf("error scanning row: %v", err)
}

View File

@@ -9,7 +9,7 @@
food: "",
amount: 0,
description: "",
rowid: 0,
id: 0,
date: "",
per100: 0,
energy: 0,

View File

@@ -6,7 +6,7 @@
let item: main.Weight = {
weight: 0,
rowid: 0,
id: 0,
date: ''
}
let weight: string | null = null

View File

@@ -33,7 +33,7 @@ export namespace main {
}
}
export class Food {
rowid: number;
id: number;
date: string;
food: string;
description: string;
@@ -47,7 +47,7 @@ export namespace main {
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.rowid = source["rowid"];
this.id = source["id"];
this.date = source["date"];
this.food = source["food"];
this.description = source["description"];
@@ -57,7 +57,7 @@ export namespace main {
}
}
export class FoodSearch {
rowid: number;
id: number;
date: string;
food: string;
description: string;
@@ -72,7 +72,7 @@ export namespace main {
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.rowid = source["rowid"];
this.id = source["id"];
this.date = source["date"];
this.food = source["food"];
this.description = source["description"];
@@ -267,7 +267,7 @@ export namespace main {
}
}
export class Weight {
rowid: number;
id: number;
date: string;
weight: number;
@@ -277,7 +277,7 @@ export namespace main {
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.rowid = source["rowid"];
this.id = source["id"];
this.date = source["date"];
this.weight = source["weight"];
}

View File

@@ -10,7 +10,7 @@ type (
db *DB
}
Weight struct {
Rowid int64 `json:"rowid"`
Id int64 `json:"id"`
Date string `json:"date"`
Weight float32 `json:"weight"`
}
@@ -20,7 +20,7 @@ type (
}
)
const weightcolumns = "rowid, date, weight"
const weightcolumns = "id, date, weight"
const weightAggregatedColumns = "period, amount"
func (w *WeightService) GetRecent() ([]Weight, error) {
@@ -37,7 +37,7 @@ func (w *WeightService) GetRecent() ([]Weight, error) {
for row.Next() {
var weight Weight
err := row.Scan(&weight.Rowid, &weight.Date, &weight.Weight)
err := row.Scan(&weight.Id, &weight.Date, &weight.Weight)
if err != nil {
log.Printf("error scanning row: %v", err)
continue
@@ -62,22 +62,22 @@ func (w *WeightService) Create(weight Weight) (Weight, error) {
return weight, fmt.Errorf("error inserting weight: %v", err)
}
rowid, err := res.LastInsertId()
id, err := res.LastInsertId()
if err != nil {
return weight, fmt.Errorf("error getting last insert id: %v", err)
}
return w.GetByRowid(rowid)
return w.GetById(id)
}
func (w *WeightService) GetByRowid(rowid int64) (Weight, error) {
func (w *WeightService) GetById(id int64) (Weight, error) {
var res Weight
if w.db == nil || !w.db.Ready {
return res, fmt.Errorf("cannot get weight by rowid, db is nil or is not ready")
return res, fmt.Errorf("cannot get weight by id, db is nil or is not ready")
}
row := w.db.readConn.QueryRow(fmt.Sprintf("SELECT %s from weightView WHERE rowid = ?", weightcolumns), rowid)
err := row.Scan(&res.Rowid, &res.Date, &res.Weight)
row := w.db.readConn.QueryRow(fmt.Sprintf("SELECT %s from weightView WHERE id = ?", weightcolumns), id)
err := row.Scan(&res.Id, &res.Date, &res.Weight)
if err != nil {
return res, fmt.Errorf("error scanning row: %v", err)
}