mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.
## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
159 lines
3.9 KiB
Go
159 lines
3.9 KiB
Go
package persistence
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
const (
|
|
queryDelimiter = ';'
|
|
querySliceDefaultSize = 100
|
|
|
|
sqlLeftParenthesis = '('
|
|
sqlRightParenthesis = ')'
|
|
sqlBeginKeyword = "begin"
|
|
sqlEndKeyword = "end"
|
|
sqlLineComment = "--"
|
|
sqlSingleQuote = '\''
|
|
sqlDoubleQuote = '"'
|
|
)
|
|
|
|
// LoadAndSplitQuery loads and split cql / sql query into one statement per string.
|
|
// Comments are removed from the query.
|
|
func LoadAndSplitQuery(
|
|
filePaths []string,
|
|
) ([]string, error) {
|
|
var files []io.Reader
|
|
|
|
for _, filePath := range filePaths {
|
|
f, err := os.Open(filePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error opening file %s: %w", filePath, err)
|
|
}
|
|
files = append(files, f)
|
|
}
|
|
|
|
return LoadAndSplitQueryFromReaders(files)
|
|
}
|
|
|
|
// LoadAndSplitQueryFromReaders loads and split cql / sql query into one statement per string.
|
|
// Comments are removed from the query.
|
|
func LoadAndSplitQueryFromReaders(
|
|
readers []io.Reader,
|
|
) ([]string, error) {
|
|
result := make([]string, 0, querySliceDefaultSize)
|
|
for _, r := range readers {
|
|
content, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading contents: %w", err)
|
|
}
|
|
n := len(content)
|
|
contentStr := string(bytes.ToLower(content))
|
|
for i, j := 0, 0; i < n; i = j {
|
|
// stack to keep track of open parenthesis/blocks
|
|
var st []byte
|
|
var stmtBuilder strings.Builder
|
|
|
|
stmtLoop:
|
|
for ; j < n; j++ {
|
|
switch contentStr[j] {
|
|
case queryDelimiter:
|
|
if len(st) == 0 {
|
|
j++
|
|
break stmtLoop
|
|
}
|
|
|
|
case sqlLeftParenthesis:
|
|
st = append(st, sqlLeftParenthesis)
|
|
|
|
case sqlRightParenthesis:
|
|
if len(st) == 0 || st[len(st)-1] != sqlLeftParenthesis {
|
|
return nil, fmt.Errorf("error reading contents: unmatched right parenthesis")
|
|
}
|
|
st = st[:len(st)-1]
|
|
|
|
case sqlBeginKeyword[0]:
|
|
if hasWordAt(contentStr, sqlBeginKeyword, j) {
|
|
st = append(st, sqlBeginKeyword[0])
|
|
j += len(sqlBeginKeyword) - 1
|
|
}
|
|
|
|
case sqlEndKeyword[0]:
|
|
if hasWordAt(contentStr, sqlEndKeyword, j) {
|
|
if len(st) == 0 || st[len(st)-1] != sqlBeginKeyword[0] {
|
|
return nil, fmt.Errorf("error reading contents: unmatched `END` keyword")
|
|
}
|
|
st = st[:len(st)-1]
|
|
j += len(sqlEndKeyword) - 1
|
|
}
|
|
|
|
case sqlSingleQuote, sqlDoubleQuote:
|
|
quote := contentStr[j]
|
|
j++
|
|
for j < n && contentStr[j] != quote {
|
|
j++
|
|
}
|
|
if j == n {
|
|
return nil, fmt.Errorf("error reading contents: unmatched quotes")
|
|
}
|
|
|
|
case sqlLineComment[0]:
|
|
if j+len(sqlLineComment) <= n && contentStr[j:j+len(sqlLineComment)] == sqlLineComment {
|
|
_, _ = stmtBuilder.Write(bytes.TrimRight(content[i:j], " "))
|
|
for j < n && contentStr[j] != '\n' {
|
|
j++
|
|
}
|
|
i = j
|
|
}
|
|
|
|
default:
|
|
// no-op: generic character
|
|
}
|
|
}
|
|
|
|
if len(st) > 0 {
|
|
switch st[len(st)-1] {
|
|
case sqlLeftParenthesis:
|
|
return nil, fmt.Errorf("error reading contents: unmatched left parenthesis")
|
|
case sqlBeginKeyword[0]:
|
|
return nil, fmt.Errorf("error reading contents: unmatched `BEGIN` keyword")
|
|
default:
|
|
// should never enter here
|
|
return nil, fmt.Errorf("error reading contents: unmatched `%c`", st[len(st)-1])
|
|
}
|
|
}
|
|
|
|
_, _ = stmtBuilder.Write(content[i:j])
|
|
stmt := strings.TrimSpace(stmtBuilder.String())
|
|
if stmt == "" {
|
|
continue
|
|
}
|
|
result = append(result, stmt)
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// hasWordAt is a simple test to check if it matches the whole word:
|
|
// it checks if the adjacent charactes are not alphanumeric if they exist.
|
|
func hasWordAt(s, word string, pos int) bool {
|
|
if pos+len(word) > len(s) || s[pos:pos+len(word)] != word {
|
|
return false
|
|
}
|
|
if pos > 0 && isAlphanumeric(s[pos-1]) {
|
|
return false
|
|
}
|
|
if pos+len(word) < len(s) && isAlphanumeric(s[pos+len(word)]) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func isAlphanumeric(c byte) bool {
|
|
return unicode.IsLetter(rune(c)) || unicode.IsDigit(rune(c))
|
|
}
|