sqlite.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package dbmanager
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/google/uuid"
  7. _ "github.com/mattn/go-sqlite3" // SQLite driver
  8. )
  9. type SQLiteManager struct {
  10. db *sql.DB
  11. }
  12. // NewSQLiteManager initializes a new SQLiteManager
  13. func NewSQLiteManager(dataSourceName string) (*SQLiteManager, error) {
  14. db, err := sql.Open("sqlite3", dataSourceName)
  15. if err != nil {
  16. return nil, fmt.Errorf("failed to connect to SQLite database: %w", err)
  17. }
  18. // Enable foreign key constraints
  19. _, err = db.Exec("PRAGMA foreign_keys = ON;")
  20. if err != nil {
  21. return nil, fmt.Errorf("failed to enable foreign keys: %w", err)
  22. }
  23. return &SQLiteManager{db: db}, nil
  24. }
  25. // Connect establishes a connection to the SQLite database
  26. func (m *SQLiteManager) Connect() error {
  27. // No action needed for SQLite
  28. return nil
  29. }
  30. // Disconnect closes the connection to the SQLite database
  31. func (m *SQLiteManager) Disconnect() error {
  32. return m.db.Close()
  33. }
  34. // Create inserts a new record into the specified table
  35. func (m *SQLiteManager) Create(table string, data interface{}) error {
  36. jsonData, err := json.Marshal(data)
  37. if err != nil {
  38. return fmt.Errorf("failed to marshal data: %w", err)
  39. }
  40. id := uuid.New().String()
  41. query := fmt.Sprintf("INSERT INTO %s (id, data) VALUES (?, ?)", table)
  42. _, err = m.db.Exec(query, id, jsonData)
  43. if err != nil {
  44. return fmt.Errorf("failed to insert data into table %s: %w", table, err)
  45. }
  46. return nil
  47. }
  48. // GetByID retrieves a record by ID from the specified table
  49. func (m *SQLiteManager) GetByID(table string, id string) (interface{}, error) {
  50. query := fmt.Sprintf("SELECT data FROM %s WHERE id = ?", table)
  51. row := m.db.QueryRow(query, id)
  52. var jsonData string
  53. if err := row.Scan(&jsonData); err != nil {
  54. if err == sql.ErrNoRows {
  55. return nil, nil // No record found
  56. }
  57. return nil, fmt.Errorf("failed to retrieve data from table %s: %w", table, err)
  58. }
  59. var result map[string]interface{}
  60. if err := json.Unmarshal([]byte(jsonData), &result); err != nil {
  61. return nil, fmt.Errorf("failed to unmarshal data: %w", err)
  62. }
  63. return result, nil
  64. }
  65. // Update updates an existing record in the specified table
  66. func (m *SQLiteManager) Update(table string, data interface{}) error {
  67. jsonData, err := json.Marshal(data)
  68. if err != nil {
  69. return fmt.Errorf("failed to marshal data: %w", err)
  70. }
  71. // Assuming the data contains an "id" field
  72. id := data.(map[string]interface{})["id"]
  73. query := fmt.Sprintf("UPDATE %s SET data = ? WHERE id = ?", table)
  74. _, err = m.db.Exec(query, jsonData, id)
  75. if err != nil {
  76. return fmt.Errorf("failed to update data in table %s: %w", table, err)
  77. }
  78. return nil
  79. }
  80. // Delete removes a record by ID from the specified table
  81. func (m *SQLiteManager) Delete(table string, id string) error {
  82. query := fmt.Sprintf("DELETE FROM %s WHERE id = ?", table)
  83. _, err := m.db.Exec(query, id)
  84. if err != nil {
  85. return fmt.Errorf("failed to delete data from table %s: %w", table, err)
  86. }
  87. return nil
  88. }
  89. // List retrieves all records from the specified table
  90. func (m *SQLiteManager) List(table string, filter map[string]interface{}) ([]interface{}, error) {
  91. query := fmt.Sprintf("SELECT data FROM %s", table)
  92. rows, err := m.db.Query(query)
  93. if err != nil {
  94. return nil, fmt.Errorf("failed to list data from table %s: %w", table, err)
  95. }
  96. defer rows.Close()
  97. var results []interface{}
  98. for rows.Next() {
  99. var jsonData string
  100. if err := rows.Scan(&jsonData); err != nil {
  101. return nil, fmt.Errorf("failed to scan row: %w", err)
  102. }
  103. var result map[string]interface{}
  104. if err := json.Unmarshal([]byte(jsonData), &result); err != nil {
  105. return nil, fmt.Errorf("failed to unmarshal data: %w", err)
  106. }
  107. results = append(results, result)
  108. }
  109. return results, nil
  110. }
  111. // Exec executes a raw SQL query
  112. func (m *SQLiteManager) Exec(query string, args ...interface{}) (interface{}, error) {
  113. result, err := m.db.Exec(query, args...)
  114. if err != nil {
  115. return nil, fmt.Errorf("failed to execute query: %w", err)
  116. }
  117. return result, nil
  118. }
  119. // Query executes a raw SQL query and returns the rows
  120. func (m *SQLiteManager) Query(query string, args ...interface{}) (*sql.Rows, error) {
  121. rows, err := m.db.Query(query, args...)
  122. if err != nil {
  123. return nil, fmt.Errorf("failed to execute query: %w", err)
  124. }
  125. return rows, nil
  126. }
  127. // Close closes the database connection
  128. func (m *SQLiteManager) Close() error {
  129. return m.db.Close()
  130. }