mirror of https://github.com/usememos/memos
feat: `store/db` module with sqlite
parent
c34cbb19bc
commit
8e01eb8702
@ -0,0 +1,64 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MigrationHistory struct {
|
||||
CreatedTs int64
|
||||
Version string
|
||||
}
|
||||
|
||||
func findMigrationHistoyList(db *DB) ([]*MigrationHistory, error) {
|
||||
rows, err := db.Db.Query(`
|
||||
SELECT
|
||||
version,
|
||||
created_ts
|
||||
FROM
|
||||
migration_history
|
||||
ORDER BY created_ts DESC
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
migrationHistoryList := make([]*MigrationHistory, 0)
|
||||
for rows.Next() {
|
||||
var migrationHistory MigrationHistory
|
||||
if err := rows.Scan(
|
||||
&migrationHistory.Version,
|
||||
&migrationHistory.CreatedTs,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
migrationHistoryList = append(migrationHistoryList, &migrationHistory)
|
||||
}
|
||||
|
||||
return migrationHistoryList, nil
|
||||
}
|
||||
|
||||
func createMigrationHistoy(db *DB, version string) error {
|
||||
result, err := db.Db.Exec(`
|
||||
INSERT INTO migration_history (
|
||||
version,
|
||||
created_ts
|
||||
)
|
||||
VALUES (?, ?)
|
||||
`,
|
||||
version,
|
||||
time.Now().Unix(),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rows, _ := result.RowsAffected()
|
||||
if rows == 0 {
|
||||
return fmt.Errorf("failed to create migration history with %s", version)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Table struct {
|
||||
Name string
|
||||
SQL string
|
||||
}
|
||||
|
||||
func findTable(db *DB, tableName string) (*Table, error) {
|
||||
where, args := []string{"1 = 1"}, []interface{}{}
|
||||
|
||||
where, args = append(where, "type = ?"), append(args, "table")
|
||||
where, args = append(where, "name = ?"), append(args, tableName)
|
||||
|
||||
rows, err := db.Db.Query(`
|
||||
SELECT
|
||||
tbl_name,
|
||||
sql
|
||||
FROM sqlite_schema
|
||||
WHERE `+strings.Join(where, " AND "),
|
||||
args...,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, FormatError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
tableList := make([]*Table, 0)
|
||||
for rows.Next() {
|
||||
var table Table
|
||||
if err := rows.Scan(
|
||||
&table.Name,
|
||||
&table.SQL,
|
||||
); err != nil {
|
||||
return nil, FormatError(err)
|
||||
}
|
||||
|
||||
tableList = append(tableList, &table)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, FormatError(err)
|
||||
}
|
||||
|
||||
if len(tableList) == 0 {
|
||||
return nil, nil
|
||||
} else {
|
||||
return tableList[0], nil
|
||||
}
|
||||
}
|
||||
|
||||
func createTable(db *DB, sql string) error {
|
||||
result, err := db.Db.Exec(sql)
|
||||
|
||||
rows, _ := result.RowsAffected()
|
||||
if rows == 0 {
|
||||
return fmt.Errorf("failed to create table with %s", sql)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
)
|
||||
|
||||
func FormatError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch err {
|
||||
case sql.ErrNoRows:
|
||||
return errors.New("data not found")
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
@ -1,13 +1,20 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"memos/common"
|
||||
)
|
||||
|
||||
// Store provides database access to all raw objects
|
||||
type Store struct {
|
||||
db *DB
|
||||
db *sql.DB
|
||||
profile *common.Profile
|
||||
}
|
||||
|
||||
// New creates a new instance of Store
|
||||
func New(db *DB) *Store {
|
||||
func New(db *sql.DB, profile *common.Profile) *Store {
|
||||
return &Store{
|
||||
db: db,
|
||||
db: db,
|
||||
profile: profile,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue