| | |
| | | "strings" |
| | | "sync" |
| | | "github.com/jinzhu/gorm" |
| | | "basic.com/valib/logger.git" |
| | | ) |
| | | |
| | | const ( |
| | | PersonSqliteDBPath = "~/workspace/gitblit/dbserver/config/testdb.db" |
| | | DbT_TableName = "dbtables" |
| | | DBP_TableName = "dbtablepersons" |
| | | ) |
| | | |
| | | var syncMut sync.Mutex |
| | |
| | | dbPath = PersonSqliteDBPath |
| | | } |
| | | |
| | | fmt.Println("self: ========>", dbPath) |
| | | logger.Info("self: ========>", dbPath) |
| | | db, err := New(dbPath, "", false) |
| | | if err != nil { |
| | | fmt.Println("new db database: ", err) |
| | | logger.Error("new db database: ", err) |
| | | return err |
| | | } |
| | | dbConn, err := db.Connect() |
| | | if err != nil { |
| | | fmt.Println("new db conn error; ", err) |
| | | logger.Error("new db conn error; ", err) |
| | | return err |
| | | } |
| | | |
| | |
| | | |
| | | path, err := getCurrentPath() |
| | | if err != nil { |
| | | fmt.Println("getCurrentPath error; ", err) |
| | | logger.Error("getCurrentPath error; ", err) |
| | | return "", err |
| | | } |
| | | |
| | | filepath := path + "tmp.db" |
| | | fmt.Println("filepath:", filepath) |
| | | logger.Info("filepath:", filepath) |
| | | db, err := New(filepath, "", false) |
| | | if err != nil { |
| | | fmt.Println("new db database: ", err) |
| | | logger.Error("new db database: ", err) |
| | | return "", err |
| | | } |
| | | |
| | | tmpconn, err := db.Connect() |
| | | if err != nil { |
| | | fmt.Println("new db conn error; ", err) |
| | | logger.Error("new db conn error; ", err) |
| | | return "", err |
| | | } |
| | | defer tmpconn.Close() |
| | |
| | | defer syncMut.Unlock() |
| | | allResults, err := SerfDbConn.Execute(sqlString, false, false) |
| | | if err != nil { |
| | | fmt.Println("execute error!", err) |
| | | logger.Error("execute error!", err) |
| | | return nil, err |
| | | } |
| | | return allResults, nil |
| | |
| | | defer syncMut.Unlock() |
| | | rows, err := SerfDbConn.Query(sqlString, false, false) |
| | | if err != nil { |
| | | fmt.Println("execute error!", err) |
| | | logger.Error("execute error!", err) |
| | | return nil, err |
| | | } |
| | | return rows, nil |
| | |
| | | return false,result.Error |
| | | } |
| | | if result.RowsAffected == 0{ |
| | | fmt.Println("ExecuteSqlByGorm fail") |
| | | logger.Error("ExecuteSqlByGorm fail") |
| | | return false,errors.New("ExecuteSqlByGorm fail") |
| | | } |
| | | } |
| | |
| | | if err !=nil { |
| | | return nil,errors.New("tableDesc err") |
| | | } |
| | | fmt.Println(table,"'Columns is:",tDescArr) |
| | | logger.Info(table,"'Columns is:",tDescArr) |
| | | if tDescArr == nil || len(tDescArr) == 0 { |
| | | return nil,errors.New(table+" has no column") |
| | | } |
| | |
| | | for _,col :=range tDescArr { |
| | | columnNames = append(columnNames, fmt.Sprintf(`'||quote("%s")||'`, col.Name)) |
| | | } |
| | | if table == "dbTables" { |
| | | if table == DbT_TableName { |
| | | tSql = fmt.Sprintf(`SELECT 'INSERT INTO "%s" VALUES(%s)' as sql FROM "%s" where (analyServerId='' or analyServerId is NULL);`, |
| | | table, |
| | | strings.Join(columnNames, ","), |
| | | table) |
| | | } else if table == "dbtablepersons" { |
| | | } else if table == DBP_TableName { |
| | | tSql = fmt.Sprintf(`SELECT 'INSERT INTO "%s" VALUES(%s)' as sql FROM "%s" where tableId in (select id from dbTables where (analyServerId='' or analyServerId is NULL));`, |
| | | table, |
| | | strings.Join(columnNames, ","), |
| | |
| | | table) |
| | | } |
| | | |
| | | fmt.Println("tSql:",tSql) |
| | | logger.Info("tSql:",tSql) |
| | | |
| | | err = localDb.Raw(tSql).Scan(&dumpSql).Error |
| | | if err !=nil { |
| | |
| | | |
| | | var b strings.Builder |
| | | if err := SerfDbConn.Dump(&b); err != nil { |
| | | fmt.Println("dump file ", err.Error()) |
| | | logger.Error("dump file ", err.Error()) |
| | | } |
| | | fmt.Printf("%T\n", b) |
| | | logger.Info("%T\n", b) |
| | | } |
| | | |
| | | // get current path |