odbcimporter.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package importer
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. "git.wecise.com/wecise/cgimport/odbc"
  10. "git.wecise.com/wecise/odb-go/odb"
  11. "git.wecise.com/wecise/util/cast"
  12. "git.wecise.com/wecise/util/cmap"
  13. "git.wecise.com/wecise/util/merrs"
  14. )
  15. type classdatainfo struct {
  16. *classinfo
  17. insertcount int64
  18. lastlogtime time.Time
  19. lastlogicount int64
  20. mutex sync.Mutex
  21. }
  22. var classdatainfos = cmap.New[string, *classdatainfo]()
  23. type ODBCImporter struct {
  24. client odb.Client
  25. }
  26. func NewODBCImporter() *ODBCImporter {
  27. odbci := &ODBCImporter{}
  28. if odbc.DevPhase&(odbc.DP_CREATECLASS|odbc.DP_INSERTDATA) != 0 {
  29. odbci.client = odbc.ODBC()
  30. }
  31. return odbci
  32. }
  33. // 根据数据修正类定义
  34. func (odbci *ODBCImporter) ReviseClassStruct() (err error) {
  35. for _, classname := range classnames {
  36. ci := classinfos.GetIFPresent(classname)
  37. if ci == nil {
  38. return merrs.NewError("classinfo not found " + classname)
  39. }
  40. _, err = classdatainfos.GetWithNew(classname, func() (cdi *classdatainfo, err error) {
  41. logger.Info("create class " + ci.classname)
  42. if odbci.client != nil {
  43. _, err = odbci.client.Query(ci.createmql).Do()
  44. if err != nil {
  45. return
  46. }
  47. }
  48. cdi = &classdatainfo{classinfo: ci}
  49. return
  50. })
  51. }
  52. if odbci.client != nil {
  53. for _, createedgemql := range createedgemqls {
  54. _, e := odbci.client.Query(createedgemql).Do()
  55. if e != nil && !strings.Contains(e.Error(), "already exist") {
  56. err = e
  57. return
  58. }
  59. }
  60. }
  61. return
  62. }
  63. func (odbci *ODBCImporter) InsertEdge(data map[string]any) (err error) {
  64. return
  65. }
  66. // 插入数据
  67. func (odbci *ODBCImporter) InsertData(classname string, data map[string]any) (err error) {
  68. cdi := classdatainfos.GetIFPresent(classname)
  69. if cdi == nil {
  70. return merrs.NewError("class not defined " + classname)
  71. }
  72. if cdi.insertmql == "" {
  73. return merrs.NewError("class no fields to insert " + classname)
  74. }
  75. values := []any{}
  76. for _, fn := range cdi.fieldslist {
  77. fi := cdi.fieldinfos[fn]
  78. if fi.datakey == "" {
  79. td := map[string]any{}
  80. for k, v := range data {
  81. if cdi.datakey_fieldinfos[k] == nil {
  82. td[k] = v
  83. }
  84. }
  85. tdbs, e := json.Marshal(td)
  86. if e != nil {
  87. return merrs.NewError(e)
  88. }
  89. values = append(values, string(tdbs))
  90. continue
  91. }
  92. v := data[fi.datakey]
  93. switch fi.fieldtype {
  94. case "set<varchar>":
  95. v = cast.ToStringSlice(v)
  96. case "timestamp":
  97. tv, e := cast.ToDateTimeE(v, "2006-01-02-15.04.05.000000")
  98. if e != nil {
  99. return merrs.NewError(fmt.Sprint("can't parse datetime value '", v, "'"))
  100. }
  101. v = tv.Format("2006-01-02 15:04:05.000000")
  102. }
  103. values = append(values, v)
  104. }
  105. if odbci.client != nil {
  106. _, err = odbci.client.Query(cdi.insertmql, values...).Do()
  107. if err != nil {
  108. err = merrs.NewError(err, merrs.SSMaps{{"mql": cdi.insertmql}, {"values": fmt.Sprint(values)}})
  109. logger.Error(err)
  110. return
  111. }
  112. }
  113. atomic.AddInt64(&cdi.insertcount, 1)
  114. cdi.mutex.Lock()
  115. if time.Since(cdi.lastlogtime) > 5*time.Second && cdi.lastlogicount != cdi.insertcount {
  116. cdi.lastlogtime = time.Now()
  117. cdi.lastlogicount = cdi.insertcount
  118. logger.Info("class", cdi.classname, "import", cdi.insertcount, "records")
  119. }
  120. cdi.mutex.Unlock()
  121. return
  122. }
  123. func (odbci *ODBCImporter) done() {
  124. classdatainfos.Fetch(func(cn string, cdi *classdatainfo) bool {
  125. cdi.mutex.Lock()
  126. if cdi.lastlogicount != cdi.insertcount {
  127. cdi.lastlogtime = time.Now()
  128. cdi.lastlogicount = cdi.insertcount
  129. logger.Info("class", cdi.classname, "import", cdi.insertcount, "records")
  130. }
  131. cdi.mutex.Unlock()
  132. return true
  133. })
  134. }
  135. func (odbci *ODBCImporter) alldone() {
  136. classdatainfos.Fetch(func(cn string, cdi *classdatainfo) bool {
  137. cdi.mutex.Lock()
  138. if cdi.insertcount != 0 {
  139. cdi.lastlogtime = time.Now()
  140. cdi.lastlogicount = cdi.insertcount
  141. logger.Info("class", cdi.classname, "import", cdi.insertcount, "records")
  142. }
  143. cdi.mutex.Unlock()
  144. return true
  145. })
  146. }
  147. func (odbci *ODBCImporter) reload() error {
  148. if odbci.client != nil {
  149. for i := len(classnames) - 1; i >= 0; i-- {
  150. classname := classnames[i]
  151. ci := classinfos.GetIFPresent(classname)
  152. if ci == nil {
  153. continue
  154. }
  155. for retry := 2; retry >= 0; retry-- {
  156. _, e := odbci.client.Query(`delete from "` + ci.classfullname + `" with version`).Do()
  157. _ = e
  158. _, e = odbci.client.Query(`drop class if exists "` + ci.classfullname + `"`).Do()
  159. if e != nil {
  160. if retry > 0 {
  161. continue
  162. }
  163. return e
  164. }
  165. }
  166. }
  167. }
  168. return nil
  169. }