github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/providers/mysqlxorm/sqlite3/interface.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     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  
    15  package sqlite3
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  
    23  	_ "github.com/mattn/go-sqlite3"
    24  	"xorm.io/xorm"
    25  	"xorm.io/xorm/names"
    26  
    27  	"github.com/erda-project/erda-infra/providers/mysqlxorm"
    28  )
    29  
    30  type Sqlite3 struct {
    31  	db *xorm.Engine
    32  }
    33  
    34  func (s *Sqlite3) DB() *xorm.Engine {
    35  	return s.db
    36  }
    37  
    38  func (s *Sqlite3) DataSourceName() string {
    39  	return s.DB().DataSourceName()
    40  }
    41  
    42  func (s *Sqlite3) NewSession(ops ...mysqlxorm.SessionOption) *mysqlxorm.Session {
    43  	tx := &mysqlxorm.Session{}
    44  	for _, opt := range ops {
    45  		opt(tx)
    46  	}
    47  
    48  	if tx.Session == nil {
    49  		tx.Session = s.db.NewSession()
    50  	}
    51  
    52  	return tx
    53  }
    54  
    55  // NewSqlite3 Use for unit-test
    56  func NewSqlite3(dbSourceName string, opts ...OptionFunc) (*Sqlite3, error) {
    57  	if dbSourceName == "" {
    58  		return nil, errors.New("empty dbSourceName")
    59  	}
    60  
    61  	o := &Options{}
    62  	var err error
    63  
    64  	for _, opt := range opts {
    65  		opt(o)
    66  	}
    67  
    68  	if o.RandomName {
    69  		dbSourceName, err = randomName(dbSourceName)
    70  		if err != nil {
    71  			return nil, err
    72  		}
    73  	}
    74  
    75  	engine, err := xorm.NewEngine("sqlite3", dbSourceName)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	// set journal_mode in sqlite3
    81  	// the default journal_mode in sqlite is `delete`
    82  	if o.JournalMode != "" {
    83  		_, err = engine.Exec(fmt.Sprintf("PRAGMA journal_mode = %s", o.JournalMode))
    84  		if err != nil {
    85  			return nil, err
    86  		}
    87  	}
    88  
    89  	engine.SetMapper(names.GonicMapper{})
    90  
    91  	sqlite3Engine := &Sqlite3{
    92  		db: engine,
    93  	}
    94  
    95  	return sqlite3Engine, nil
    96  }
    97  
    98  func (s *Sqlite3) Close() error {
    99  	err := s.DB().Close()
   100  	if err != nil {
   101  		return err
   102  	}
   103  	return os.Remove(s.DataSourceName())
   104  }
   105  
   106  // randomName accepts a path with pattern and returns a random name
   107  // such as `/var/user/test-*.db => /var/user/test-3125863660.db`
   108  func randomName(path string) (string, error) {
   109  	dir, file := filepath.Split(path)
   110  	temp, err := os.CreateTemp(dir, file)
   111  	if err != nil {
   112  		return "", err
   113  	}
   114  	return temp.Name(), nil
   115  }