github.com/code-to-go/safepool.lib@v0.0.0-20221205180519-ee25e63c226e/sql/changes.go2 (about)

     1  package sql
     2  
     3  import (
     4  	"github.com/code-to-go/safepool.lib/core"
     5  	"github.com/code-to-go/safepool.lib/model"
     6  	"time"
     7  )
     8  
     9  // GetChangesForExchange returns all the item in the log filtering out those smaller than start in lexical order
    10  func GetChangesForExchange(domain string, exchange string) ([]model.ChangeFile, error) {
    11  	rows, err := Query("GET_CHANGES", Args{"domain": domain, "exchange": exchange})
    12  	if core.IsErr(err, "cannot get logs from db: %v") {
    13  		return nil, err
    14  	}
    15  
    16  	var changes []model.ChangeFile
    17  	for rows.Next() {
    18  		var change model.ChangeFile
    19  		var timestamp int64
    20  		err = rows.Scan(&change.Name, &timestamp)
    21  		if !core.IsErr(err, "cannot read log row from db: %v") {
    22  			changes = append(changes, change)
    23  		}
    24  	}
    25  	return changes, nil
    26  }
    27  
    28  // GetChanges returns all the item in the log filtering out those smaller than start in lexical order
    29  // func GetChanges(domain string, start string) ([]model.ChangeFile, error) {
    30  // 	rows, err := query("GET_CHANGES", names{"domain": domain, "start": start})
    31  // 	if core.IsErr(err, "cannot get logs from db: %v") {
    32  // 		return nil, err
    33  // 	}
    34  
    35  // 	var changes []model.ChangeFile
    36  // 	for rows.Next() {
    37  // 		var change model.ChangeFile
    38  // 		var timestamp int64
    39  // 		err = rows.Scan(&change.Name, &timestamp)
    40  // 		if !core.IsErr(err, "cannot read log row from db: %v") {
    41  // 			changes = append(changes, change)
    42  // 		}
    43  // 	}
    44  // 	return changes, nil
    45  // }
    46  
    47  // GetChange returns the change
    48  func GetChange(domain string, id uint64) (c model.ChangeFile, ok bool, err error) {
    49  	rows, err := Query("GET_CHANGE", Args{"domain": domain, "id": id})
    50  	if core.IsErr(err, "cannot get logs from db: %v") {
    51  		return model.ChangeFile{}, false, err
    52  	}
    53  
    54  	var changes []model.ChangeFile
    55  	for rows.Next() {
    56  		var change model.ChangeFile
    57  		var timestamp int64
    58  		err = rows.Scan(&change.Name, &timestamp)
    59  		if !core.IsErr(err, "cannot read log row from db: %v") {
    60  			changes = append(changes, change)
    61  		}
    62  	}
    63  	return changes[0], true, nil
    64  }
    65  
    66  func AddChange(domain string, name string) error {
    67  	_, err := Exec("ADD_CHANGE", Args{"domain": domain, "name": name, "timestamp": time.Now().Unix()})
    68  	return err
    69  }