github.com/wsand02/massren@v1.5.5-0.20191104203215-f721006d1e4e/history.go (about)

     1  package main
     2  
     3  import (
     4  	"path/filepath"
     5  	"time"
     6  )
     7  
     8  type HistoryItem struct {
     9  	Source           string
    10  	Dest             string
    11  	Timestamp        int64
    12  	Id               string
    13  	IntermediatePath string
    14  }
    15  
    16  func normalizePath(p string) string {
    17  	if p == "" {
    18  		return ""
    19  	}
    20  	output, err := filepath.Abs(filepath.Clean(p))
    21  	if err != nil {
    22  		panic(err)
    23  	}
    24  	return output
    25  }
    26  
    27  func clearHistory() error {
    28  	_, err := profileDb_.Exec("DELETE FROM history")
    29  	return err
    30  }
    31  
    32  func allHistoryItems() ([]HistoryItem, error) {
    33  	var output []HistoryItem
    34  
    35  	rows, err := profileDb_.Query("SELECT id, source, destination, timestamp FROM history ORDER BY id")
    36  	if err != nil {
    37  		return output, err
    38  	}
    39  
    40  	for rows.Next() {
    41  		var item HistoryItem
    42  		rows.Scan(&item.Id, &item.Source, &item.Dest, &item.Timestamp)
    43  		output = append(output, item)
    44  	}
    45  
    46  	return output, nil
    47  }
    48  
    49  func saveHistoryItems(fileActions []*FileAction) error {
    50  	if len(fileActions) == 0 {
    51  		return nil
    52  	}
    53  
    54  	tx, err := profileDb_.Begin()
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	for _, action := range fileActions {
    60  		if action.kind == KIND_DELETE {
    61  			// Current, undo is not supported
    62  			continue
    63  		}
    64  		tx.Exec("INSERT INTO history (source, destination, timestamp) VALUES (?, ?, ?)", action.FullOldPath(), action.FullNewPath(), time.Now().Unix())
    65  	}
    66  
    67  	return tx.Commit()
    68  }
    69  
    70  func deleteHistoryItems(items []HistoryItem) error {
    71  	if len(items) == 0 {
    72  		return nil
    73  	}
    74  
    75  	sqlOr := ""
    76  	for _, item := range items {
    77  		if sqlOr != "" {
    78  			sqlOr += " OR "
    79  		}
    80  		sqlOr += "id = " + item.Id
    81  	}
    82  
    83  	_, err := profileDb_.Exec("DELETE FROM history WHERE " + sqlOr)
    84  
    85  	return err
    86  }
    87  
    88  func deleteOldHistoryItems(minTimestamp int64) {
    89  	if profileDb_ != nil {
    90  		profileDb_.Exec("DELETE FROM history WHERE timestamp < ?", minTimestamp)
    91  	}
    92  }
    93  
    94  func latestHistoryItemsByDestinations(paths []string) ([]HistoryItem, error) {
    95  	var output []HistoryItem
    96  	if len(paths) == 0 {
    97  		return output, nil
    98  	}
    99  
   100  	sqlOr := ""
   101  	var sqlArgs []interface{}
   102  	for _, p := range paths {
   103  		sqlArgs = append(sqlArgs, p)
   104  		if sqlOr != "" {
   105  			sqlOr += " OR "
   106  		}
   107  		sqlOr += "destination = ?"
   108  	}
   109  
   110  	rows, err := profileDb_.Query("SELECT id, source, destination, timestamp FROM history WHERE "+sqlOr+" ORDER BY timestamp DESC", sqlArgs...)
   111  	if err != nil {
   112  		return output, err
   113  	}
   114  
   115  	doneDestinations := make(map[string]bool)
   116  	for rows.Next() {
   117  		var item HistoryItem
   118  		rows.Scan(&item.Id, &item.Source, &item.Dest, &item.Timestamp)
   119  		_, done := doneDestinations[item.Dest]
   120  		if done {
   121  			continue
   122  		}
   123  		output = append(output, item)
   124  		doneDestinations[item.Dest] = true
   125  	}
   126  
   127  	return output, nil
   128  }