add test db
This commit is contained in:
Generated
+9
-1
@@ -1,7 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="ab419b68-984b-4661-bb9a-26bcba1547b3" name="Default Changelist" comment="" />
|
||||
<list default="true" id="ab419b68-984b-4661-bb9a-26bcba1547b3" name="Default Changelist" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/cmd/root.go" beforeDir="false" afterPath="$PROJECT_DIR$/cmd/root.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/cmd/test.go" beforeDir="false" afterPath="$PROJECT_DIR$/cmd/test.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/database/sqlite.go" beforeDir="false" afterPath="$PROJECT_DIR$/database/sqlite.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/go.mod" beforeDir="false" afterPath="$PROJECT_DIR$/go.mod" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/go.sum" beforeDir="false" afterPath="$PROJECT_DIR$/go.sum" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/telegram.yaml" beforeDir="false" afterPath="$PROJECT_DIR$/telegram.yaml" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
|
||||
@@ -29,6 +29,9 @@ var (
|
||||
baudrate int
|
||||
bytesize byte
|
||||
|
||||
dbFile string
|
||||
dbInit bool
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "tele-recv",
|
||||
@@ -90,5 +93,7 @@ func initConfig() {
|
||||
device = viper.GetString("serial.device")
|
||||
baudrate = viper.GetInt("serial.baudrate")
|
||||
bytesize = byte(viper.GetInt("serial.bytesize"))
|
||||
dbFile = viper.GetString("sqlite.file")
|
||||
dbInit = viper.GetBool("sqlite.init")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/tarm/serial"
|
||||
"os"
|
||||
"it2000.com.cn/tele-recv/database"
|
||||
)
|
||||
|
||||
// testCmd represents the test command
|
||||
@@ -39,6 +40,8 @@ Test serial port`,
|
||||
}
|
||||
fmt.Println(c , "opened")
|
||||
_ = s.Close()
|
||||
|
||||
database.Init(dbFile, dbInit)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
+10
-2
@@ -2,20 +2,21 @@ package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
DbDriver = "sqlite3"
|
||||
TableCreate =`'''
|
||||
TableCreate =`
|
||||
create table IF NOT EXISTS telegram (
|
||||
[tele_id] INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
[tele_recv_time] TIMESTAMP NOT NULL DEFAULT (datetime('now', 'localtime')),
|
||||
[tele_processed] int(1) NOT NULL DEFAULT 0,
|
||||
[tele_text] TEXT NOT NULL
|
||||
)
|
||||
'''
|
||||
|
||||
`
|
||||
)
|
||||
var (
|
||||
@@ -37,5 +38,12 @@ func getDb(file string, init bool) (*sql.DB, error) {
|
||||
if err != nil {
|
||||
log.Fatal("error in create telegram table", err)
|
||||
}
|
||||
log.Println("database ", db, " opened")
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func Init(file string, init bool) error {
|
||||
var dbErr error
|
||||
db, dbErr = getDb(file, init)
|
||||
return dbErr
|
||||
}
|
||||
@@ -5,6 +5,7 @@ go 1.15
|
||||
require (
|
||||
github.com/fsnotify/fsnotify v1.4.9 // indirect
|
||||
github.com/magiconair/properties v1.8.4 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.5
|
||||
github.com/mitchellh/mapstructure v1.4.0 // indirect
|
||||
github.com/pelletier/go-toml v1.8.1 // indirect
|
||||
github.com/spf13/afero v1.5.1 // indirect
|
||||
|
||||
@@ -118,6 +118,8 @@ github.com/magiconair/properties v1.8.4 h1:8KGKTcQQGm0Kv7vEbKFErAoAOFyyacLStRtQS
|
||||
github.com/magiconair/properties v1.8.4/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
|
||||
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||
github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ=
|
||||
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
|
||||
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
|
||||
|
||||
@@ -6,3 +6,4 @@ serial:
|
||||
|
||||
sqlite:
|
||||
file: telegram.db
|
||||
init: true
|
||||
|
||||
Reference in New Issue
Block a user