123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- package main
- import (
- "database/sql"
- "fmt"
- "log"
- "os"
- "sync"
- "time"
- _ "xugu_go_driver/xugu"
- )
- func main3() {
- db, err := sql.Open("xugusql", "IP=192.168.2.216;DB=SYSTEM;User=SYSDBA;PWD=SYSDBA;Port=15138;AUTO_COMMIT=on;CHAR_SET=UTF8")
- if err != nil {
- log.Fatal("err ", err)
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- file, err := os.OpenFile("output.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
- if err != nil {
- log.Fatalf("无法打开文件: %v", err)
- }
- defer file.Close()
-
- logger := log.New(file, "", log.LstdFlags)
- var wg sync.WaitGroup
- const numGoroutines = 10000
- for i := 0; i < numGoroutines; i++ {
- wg.Add(1)
- go func(i int) {
- defer wg.Done()
- processQuery(db, i, logger)
- }(i)
- }
- wg.Wait()
- fmt.Println("所有goroutine已完成")
- defer db.Close()
- }
- func processQuery(db *sql.DB, id int, logger *log.Logger) {
- fmt.Printf("goroutine %d 开始\n", id)
-
- rows, err := db.Query("select * from gotab3;")
- if err != nil {
- fmt.Printf("[ERROR] %s\n", err.Error())
- logger.Printf("[ERROR] %s\n", err.Error())
- return
- }
- defer rows.Close()
- column, err := rows.Columns()
- if err != nil {
- fmt.Printf("[ERROR] %s\n", err.Error())
- logger.Printf("[ERROR] %s\n", err.Error())
- return
- }
- rowsValue := make([]interface{}, len(column))
- for key := range rowsValue {
- dest := make([]byte, 100000)
- rowsValue[key] = &dest
- }
- for rows.Next() {
- err := rows.Scan(rowsValue...)
- if err != nil {
- fmt.Printf("[ERROR] %s\n", err.Error())
- logger.Printf("[ERROR] %s\n", err.Error())
- return
- }
- for _, v := range rowsValue {
- sss := []byte(*(v.(*[]byte)))
- v = sss
- }
- }
-
- logger.Println("goroutine %d 完成\n", id)
- fmt.Printf("goroutine %d 完成\n", id)
- }
- type userInfo struct {
- Id int
- Username string
- Password string
- Power sql.NullByte
- }
- type LicenseApplication struct {
- Creator string
- ApplicationDate string
- AssociatedProject string
- SalesPerson string
- SalesEmail string
- SupportPerson string
- SupportEmail string
- TotalNodes string
- Company string
- ProductName string
- Version string
- NodeCount string
- Processor string
- OperatingSystem string
- MasterMacAddress string
- SecondaryMasterMacAddress string
- }
- type LicenseInfo struct {
- ID int `json:"id"`
- UserID int `json:"user_id"`
- UpUser string `json:"up_user"`
- UpTime sql.NullTime `json:"up_time"`
- DelTime sql.NullTime `json:"del_time"`
- License1 string
- License2 string
- LicenseFlage string
- UniqueID string
- LicenseApplication
- }
- func main() {
- db, err := sql.Open("xugusql", "IP=127.0.0.1;DB=SYSTEM;User=SYSDBA;PWD=SYSDBA;Port=5138;AUTO_COMMIT=on;CHAR_SET=UTF8")
- if err != nil {
- log.Fatal("err ", err)
- }
- query := fmt.Sprintf(`
- SELECT la.*,li.lic1, li.lic2
- FROM LicenseApplication la
- JOIN licenseRecord lr ON la.UniqueID = lr.LicenseUniqueID
- JOIN LicenseInfo li ON la.UniqueID = li.LicenseApplicationUniqueID
- WHERE lr.USERS LIKE '%%%s%%'
- LIMIT ? OFFSET ?`, "test3")
- rows, err := db.Query(query, 10, 0)
- if err != nil {
- fmt.Println("query data: %v", err)
-
- }
- defer rows.Close()
- var results []LicenseInfo
- for rows.Next() {
- var li LicenseInfo
- err = rows.Scan(
- &li.ID,
- &li.UniqueID,
- &li.UserID,
- &li.UpUser,
- &li.UpTime,
- &li.DelTime,
- &li.LicenseApplication.Creator,
- &li.LicenseApplication.ApplicationDate,
- &li.LicenseApplication.AssociatedProject,
- &li.LicenseApplication.SalesPerson,
- &li.LicenseApplication.SalesEmail,
- &li.LicenseApplication.SupportPerson,
- &li.LicenseApplication.SupportEmail,
- &li.LicenseApplication.TotalNodes,
- &li.LicenseApplication.Company,
- &li.LicenseApplication.ProductName,
- &li.LicenseApplication.Version,
- &li.LicenseApplication.NodeCount,
- &li.LicenseApplication.Processor,
- &li.LicenseApplication.OperatingSystem,
- &li.LicenseApplication.MasterMacAddress,
- &li.LicenseApplication.SecondaryMasterMacAddress,
- &li.License1,
- &li.License2,
- )
- if err != nil {
- fmt.Println("scan row: %v", err)
-
-
- }
- results = append(results, li)
- }
- if err = rows.Err(); err != nil {
-
- }
- fmt.Printf("results123 %#v", results)
- db.SetConnMaxLifetime(time.Minute * 3)
- }
|