github.com/kotovmak/go-admin@v1.1.1/modules/db/sqlite.go (about)

     1  // Copyright 2019 GoAdmin Core Team. All rights reserved.
     2  // Use of this source code is governed by a Apache-2.0 style
     3  // license that can be found in the LICENSE file.
     4  
     5  package db
     6  
     7  import (
     8  	"database/sql"
     9  
    10  	"github.com/kotovmak/go-admin/modules/config"
    11  )
    12  
    13  // Sqlite is a Connection of sqlite.
    14  type Sqlite struct {
    15  	Base
    16  }
    17  
    18  // GetSqliteDB return the global sqlite connection.
    19  func GetSqliteDB() *Sqlite {
    20  	return &Sqlite{
    21  		Base: Base{
    22  			DbList: make(map[string]*sql.DB),
    23  		},
    24  	}
    25  }
    26  
    27  // Name implements the method Connection.Name.
    28  func (db *Sqlite) Name() string {
    29  	return "sqlite"
    30  }
    31  
    32  // GetDelimiter implements the method Connection.GetDelimiter.
    33  func (db *Sqlite) GetDelimiter() string {
    34  	return "`"
    35  }
    36  
    37  // GetDelimiter2 implements the method Connection.GetDelimiter2.
    38  func (db *Sqlite) GetDelimiter2() string {
    39  	return "`"
    40  }
    41  
    42  // GetDelimiters implements the method Connection.GetDelimiters.
    43  func (db *Sqlite) GetDelimiters() []string {
    44  	return []string{"`", "`"}
    45  }
    46  
    47  // QueryWithConnection implements the method Connection.QueryWithConnection.
    48  func (db *Sqlite) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error) {
    49  	return CommonQuery(db.DbList[con], query, args...)
    50  }
    51  
    52  // ExecWithConnection implements the method Connection.ExecWithConnection.
    53  func (db *Sqlite) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error) {
    54  	return CommonExec(db.DbList[con], query, args...)
    55  }
    56  
    57  // Query implements the method Connection.Query.
    58  func (db *Sqlite) Query(query string, args ...interface{}) ([]map[string]interface{}, error) {
    59  	return CommonQuery(db.DbList["default"], query, args...)
    60  }
    61  
    62  // Exec implements the method Connection.Exec.
    63  func (db *Sqlite) Exec(query string, args ...interface{}) (sql.Result, error) {
    64  	return CommonExec(db.DbList["default"], query, args...)
    65  }
    66  
    67  func (db *Sqlite) QueryWith(tx *sql.Tx, conn, query string, args ...interface{}) ([]map[string]interface{}, error) {
    68  	if tx != nil {
    69  		return db.QueryWithTx(tx, query, args...)
    70  	}
    71  	return db.QueryWithConnection(conn, query, args...)
    72  }
    73  
    74  func (db *Sqlite) ExecWith(tx *sql.Tx, conn, query string, args ...interface{}) (sql.Result, error) {
    75  	if tx != nil {
    76  		return db.ExecWithTx(tx, query, args...)
    77  	}
    78  	return db.ExecWithConnection(conn, query, args...)
    79  }
    80  
    81  // InitDB implements the method Connection.InitDB.
    82  func (db *Sqlite) InitDB(cfgList map[string]config.Database) Connection {
    83  	db.Configs = cfgList
    84  	db.Once.Do(func() {
    85  		for conn, cfg := range cfgList {
    86  			sqlDB, err := sql.Open("sqlite3", cfg.GetDSN())
    87  
    88  			if err != nil {
    89  				panic(err)
    90  			}
    91  
    92  			sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
    93  			sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
    94  			sqlDB.SetConnMaxLifetime(cfg.ConnMaxLifetime)
    95  			sqlDB.SetConnMaxIdleTime(cfg.ConnMaxIdleTime)
    96  
    97  			db.DbList[conn] = sqlDB
    98  
    99  			if err := sqlDB.Ping(); err != nil {
   100  				panic(err)
   101  			}
   102  		}
   103  	})
   104  	return db
   105  }
   106  
   107  // BeginTxWithReadUncommitted starts a transaction with level LevelReadUncommitted.
   108  func (db *Sqlite) BeginTxWithReadUncommitted() *sql.Tx {
   109  	return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelReadUncommitted)
   110  }
   111  
   112  // BeginTxWithReadCommitted starts a transaction with level LevelReadCommitted.
   113  func (db *Sqlite) BeginTxWithReadCommitted() *sql.Tx {
   114  	return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelReadCommitted)
   115  }
   116  
   117  // BeginTxWithRepeatableRead starts a transaction with level LevelRepeatableRead.
   118  func (db *Sqlite) BeginTxWithRepeatableRead() *sql.Tx {
   119  	return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelRepeatableRead)
   120  }
   121  
   122  // BeginTx starts a transaction with level LevelDefault.
   123  func (db *Sqlite) BeginTx() *sql.Tx {
   124  	return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelDefault)
   125  }
   126  
   127  // BeginTxWithLevel starts a transaction with given transaction isolation level.
   128  func (db *Sqlite) BeginTxWithLevel(level sql.IsolationLevel) *sql.Tx {
   129  	return CommonBeginTxWithLevel(db.DbList["default"], level)
   130  }
   131  
   132  // BeginTxWithReadUncommittedAndConnection starts a transaction with level LevelReadUncommitted and connection.
   133  func (db *Sqlite) BeginTxWithReadUncommittedAndConnection(conn string) *sql.Tx {
   134  	return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelReadUncommitted)
   135  }
   136  
   137  // BeginTxWithReadCommittedAndConnection starts a transaction with level LevelReadCommitted and connection.
   138  func (db *Sqlite) BeginTxWithReadCommittedAndConnection(conn string) *sql.Tx {
   139  	return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelReadCommitted)
   140  }
   141  
   142  // BeginTxWithRepeatableReadAndConnection starts a transaction with level LevelRepeatableRead and connection.
   143  func (db *Sqlite) BeginTxWithRepeatableReadAndConnection(conn string) *sql.Tx {
   144  	return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelRepeatableRead)
   145  }
   146  
   147  // BeginTxAndConnection starts a transaction with level LevelDefault and connection.
   148  func (db *Sqlite) BeginTxAndConnection(conn string) *sql.Tx {
   149  	return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelDefault)
   150  }
   151  
   152  // BeginTxWithLevelAndConnection starts a transaction with given transaction isolation level and connection.
   153  func (db *Sqlite) BeginTxWithLevelAndConnection(conn string, level sql.IsolationLevel) *sql.Tx {
   154  	return CommonBeginTxWithLevel(db.DbList[conn], level)
   155  }
   156  
   157  // QueryWithTx is query method within the transaction.
   158  func (db *Sqlite) QueryWithTx(tx *sql.Tx, query string, args ...interface{}) ([]map[string]interface{}, error) {
   159  	return CommonQueryWithTx(tx, query, args...)
   160  }
   161  
   162  // ExecWithTx is exec method within the transaction.
   163  func (db *Sqlite) ExecWithTx(tx *sql.Tx, query string, args ...interface{}) (sql.Result, error) {
   164  	return CommonExecWithTx(tx, query, args...)
   165  }