sqlTs.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2022-2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sql
  15. import (
  16. "bytes"
  17. "database/sql"
  18. "encoding/gob"
  19. "fmt"
  20. kvEncoding "github.com/lf-edge/ekuiper/internal/pkg/store/encoding"
  21. )
  22. type ts struct {
  23. database Database
  24. table string
  25. last int64
  26. }
  27. func init() {
  28. gob.Register(make(map[string]interface{}))
  29. }
  30. func createSqlTs(database Database, table string) (*ts, error) {
  31. store := &ts{
  32. database: database,
  33. table: table,
  34. last: getLast(database, table),
  35. }
  36. err := store.database.Apply(func(db *sql.DB) error {
  37. query := fmt.Sprintf("CREATE TABLE IF NOT EXISTS '%s'('key' INTEGER PRIMARY KEY, 'val' BLOB);", table)
  38. _, err := db.Exec(query)
  39. return err
  40. })
  41. if err != nil {
  42. return nil, err
  43. }
  44. return store, nil
  45. }
  46. func (t *ts) Set(key int64, value interface{}) (bool, error) {
  47. if key <= t.last {
  48. return false, nil
  49. }
  50. b, err := kvEncoding.Encode(value)
  51. if err != nil {
  52. return false, err
  53. }
  54. err = t.database.Apply(func(db *sql.DB) error {
  55. query := fmt.Sprintf("INSERT INTO %s(key,val) values(?,?);", t.table)
  56. stmt, err := db.Prepare(query)
  57. if err != nil {
  58. return err
  59. }
  60. defer stmt.Close()
  61. _, err = stmt.Exec(key, b)
  62. if err != nil {
  63. return err
  64. }
  65. t.last = key
  66. return nil
  67. })
  68. if err != nil {
  69. return false, err
  70. }
  71. return true, nil
  72. }
  73. func (t ts) Get(key int64, value interface{}) (bool, error) {
  74. result := false
  75. err := t.database.Apply(func(db *sql.DB) error {
  76. query := fmt.Sprintf("SELECT val FROM %s WHERE key=%d;", t.table, key)
  77. row := db.QueryRow(query)
  78. var tmp []byte
  79. switch err := row.Scan(&tmp); err {
  80. case sql.ErrNoRows:
  81. return nil
  82. case nil:
  83. default:
  84. return err
  85. }
  86. dec := gob.NewDecoder(bytes.NewBuffer(tmp))
  87. if err := dec.Decode(value); err != nil {
  88. return err
  89. }
  90. result = true
  91. return nil
  92. })
  93. if err != nil {
  94. return false, err
  95. }
  96. return result, nil
  97. }
  98. func (t ts) Last(value interface{}) (int64, error) {
  99. _, err := t.Get(t.last, value)
  100. if err != nil {
  101. return 0, err
  102. }
  103. return t.last, nil
  104. }
  105. func (t ts) Delete(key int64) error {
  106. return t.database.Apply(func(db *sql.DB) error {
  107. query := fmt.Sprintf("DELETE FROM %s WHERE key=%d;", t.table, key)
  108. _, err := db.Exec(query)
  109. return err
  110. })
  111. }
  112. func (t ts) DeleteBefore(key int64) error {
  113. return t.database.Apply(func(db *sql.DB) error {
  114. query := fmt.Sprintf("DELETE FROM %s WHERE key<%d;", t.table, key)
  115. _, err := db.Exec(query)
  116. return err
  117. })
  118. }
  119. func (t ts) Close() error {
  120. return nil
  121. }
  122. func (t ts) Drop() error {
  123. return t.database.Apply(func(db *sql.DB) error {
  124. query := fmt.Sprintf("Drop table %s;", t.table)
  125. _, err := db.Exec(query)
  126. return err
  127. })
  128. }
  129. func getLast(d Database, table string) int64 {
  130. var last int64 = 0
  131. _ = d.Apply(func(db *sql.DB) error {
  132. query := fmt.Sprintf("SELECT key FROM %s Order by key DESC Limit 1;", table)
  133. row := db.QueryRow(query)
  134. err := row.Scan(&last)
  135. return err
  136. })
  137. return last
  138. }