deprecated funcs fix

This commit is contained in:
ycc 2023-08-22 21:35:14 +02:00
parent c362b6bc33
commit fb6955465a
4 changed files with 16 additions and 43 deletions

24
db.go
View File

@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
"io/ioutil"
"log" "log"
"os" "os"
"reflect" "reflect"
@ -402,16 +401,13 @@ func (t *TableInfo) DeleteColumn(name string) error {
} }
func (db *Db) ImportSchema(filename string) { func (db *Db) ImportSchema(filename string) {
jsonFile, err := os.Open(filename) byteValue, _ := os.ReadFile(filename)
var jsonSource []TableInfo
err := json.Unmarshal([]byte(byteValue), &jsonSource)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var jsonSource []TableInfo
json.Unmarshal([]byte(byteValue), &jsonSource)
for _, ti := range jsonSource { for _, ti := range jsonSource {
ti.db = db ti.db = db
err = db.CreateTable(ti) err = db.CreateTable(ti)
@ -422,19 +418,13 @@ func (db *Db) ImportSchema(filename string) {
} }
func (db *Db) ClearImportSchema(filename string) { func (db *Db) ClearImportSchema(filename string) {
jsonFile, err := os.Open(filename) byteValue, _ := os.ReadFile(filename)
if err != nil {
log.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var jsonSource []TableInfo var jsonSource []TableInfo
json.Unmarshal([]byte(byteValue), &jsonSource) json.Unmarshal([]byte(byteValue), &jsonSource)
for _, ti := range jsonSource { for _, ti := range jsonSource {
ti.db = db ti.db = db
err = ti.DeleteTable() err := ti.DeleteTable()
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
} }
@ -600,7 +590,7 @@ func (db *Db) SaveSchema(generatedFilename string) error {
} }
// file, _ := json.Marshal(schema) // file, _ := json.Marshal(schema)
file, _ := json.MarshalIndent(schema, "", " ") file, _ := json.MarshalIndent(schema, "", " ")
_ = ioutil.WriteFile(generatedFilename, file, 0644) _ = os.WriteFile(generatedFilename, file, 0644)
return nil return nil
} }

View File

@ -3,7 +3,6 @@ package sqldb
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"testing" "testing"
) )
@ -12,18 +11,12 @@ func TestMyCreateTable(t *testing.T) {
db := Open("mysql", "test:test@tcp(127.0.0.1:3306)/test?parseTime=true") db := Open("mysql", "test:test@tcp(127.0.0.1:3306)/test?parseTime=true")
defer db.Close() defer db.Close()
jsonFile, err := os.Open("test_table.json") byteValue, _ := os.ReadFile("test_table.json")
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var jsonSource TableInfo var jsonSource TableInfo
json.Unmarshal([]byte(byteValue), &jsonSource) json.Unmarshal([]byte(byteValue), &jsonSource)
err = db.CreateTable(jsonSource) err := db.CreateTable(jsonSource)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
@ -88,10 +81,9 @@ func TestMyInsert(t *testing.T) {
if len(jsonStringOld) == len(jsonStringNew) { if len(jsonStringOld) == len(jsonStringNew) {
t.Errorf("Error row not created") t.Errorf("Error row not created")
} }
jsonFile, err := os.Open("insert.json")
defer jsonFile.Close()
var result map[string]interface{} var result map[string]interface{}
byteValue, _ := ioutil.ReadAll(jsonFile) byteValue, _ := os.ReadFile("insert.json")
json.Unmarshal(byteValue, &result) json.Unmarshal(byteValue, &result)
} }

View File

@ -3,7 +3,6 @@ package sqldb
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"testing" "testing"
) )
@ -12,18 +11,12 @@ func TestPgCreateTable(t *testing.T) {
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable") db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
defer db.Close() defer db.Close()
jsonFile, err := os.Open("test_table.json") byteValue, _ := os.ReadFile("test_table.json")
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var jsonSource TableInfo var jsonSource TableInfo
json.Unmarshal([]byte(byteValue), &jsonSource) json.Unmarshal([]byte(byteValue), &jsonSource)
err = db.CreateTable(jsonSource) err := db.CreateTable(jsonSource)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
@ -88,10 +81,8 @@ func TestPgInsert(t *testing.T) {
if len(jsonStringOld) == len(jsonStringNew) { if len(jsonStringOld) == len(jsonStringNew) {
t.Errorf("Error row not created") t.Errorf("Error row not created")
} }
jsonFile, err := os.Open("insert.json")
defer jsonFile.Close()
var result map[string]interface{} var result map[string]interface{}
byteValue, _ := ioutil.ReadAll(jsonFile) byteValue, _ := os.ReadFile("insert.json")
json.Unmarshal(byteValue, &result) json.Unmarshal(byteValue, &result)
} }

View File

@ -5,8 +5,8 @@
"id":"integer", "id":"integer",
"name":"varchar(255)|comment", "name":"varchar(255)|comment",
"description":"varchar(1000)", "description":"varchar(1000)",
"startdate":"timestamp", "startdate":"date",
"enddate":"timestamp", "endtime":"timestamp",
"latitude":"float|map", "latitude":"float|map",
"longitude":"float|map", "longitude":"float|map",
"intvalue":"integer", "intvalue":"integer",