Advertisement
cydside

Simple package to transform text to SQL statement

Apr 23rd, 2023 (edited)
1,572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.09 KB | None | 0 0
  1. package string2sql
  2.  
  3. import (
  4.     "regexp"
  5.     "strings"
  6. )
  7.  
  8. //______________________________________________________________________________
  9.  
  10. type Str2Sql struct {
  11.     text string
  12. }
  13.  
  14. //______________________________________________________________________________
  15.  
  16. func NewString2Sql(text string) *Str2Sql {
  17.     p := new(Str2Sql)
  18.     p.text = text
  19.  
  20.     return p
  21. }
  22.  
  23. //______________________________________________________________________________
  24.  
  25. func (p *Str2Sql) ZapBlanks() *Str2Sql {
  26.     borders := regexp.MustCompile(`^[\s\p{Zs}]+|[\s\p{Zs}]+$`)
  27.     inner := regexp.MustCompile(`[\s\p{Zs}]{2,}`)
  28.     f := borders.ReplaceAllString(p.text, "")
  29.     f = inner.ReplaceAllString(f, " ")
  30.     p.text = f
  31.  
  32.     return p
  33. }
  34.  
  35. //______________________________________________________________________________
  36.  
  37. func (p *Str2Sql) NormalizeApostrophesAndQuotes() *Str2Sql {
  38.     s := p.text
  39.  
  40.     s = strings.ReplaceAll(s, "\"", "''")
  41.     s = strings.ReplaceAll(s, "'", "''")
  42.  
  43.     p.text = s
  44.  
  45.     return p
  46. }
  47.  
  48. //______________________________________________________________________________
  49.  
  50. func (p *Str2Sql) NormalizeAccents() *Str2Sql {
  51.     s := p.text
  52.  
  53.     s = strings.ReplaceAll(s, "A'", "À")
  54.     s = strings.ReplaceAll(s, "E'", "È")
  55.     s = strings.ReplaceAll(s, "I'", "Ì")
  56.     s = strings.ReplaceAll(s, "O'", "Ò")
  57.     s = strings.ReplaceAll(s, "U'", "Ù")
  58.  
  59.     s = strings.ReplaceAll(s, "a'", "à")
  60.     s = strings.ReplaceAll(s, "e'", "è")
  61.     s = strings.ReplaceAll(s, "i'", "ì")
  62.     s = strings.ReplaceAll(s, "o'", "ò")
  63.     s = strings.ReplaceAll(s, "u'", "ù")
  64.  
  65.     p.text = s
  66.  
  67.     return p
  68. }
  69.  
  70. //______________________________________________________________________________
  71.  
  72. func (p *Str2Sql) RemoveAll(str string) *Str2Sql {
  73.     s := p.text
  74.  
  75.     s = strings.ReplaceAll(s, str, "")
  76.  
  77.     p.text = s
  78.  
  79.     return p
  80. }
  81.  
  82. //______________________________________________________________________________
  83.  
  84. func (p *Str2Sql) Text() string {
  85.     return p.text
  86. }
  87.  
  88. // IN MAIN:
  89.  
  90. // func str2sql(str string) string {
  91. //  s := s2s.NewString2Sql(str)
  92.  
  93. //  s.ZapBlanks().NormalizeAccents().NormalizeApostrophesAndQuotes().RemoveAll("NULL")
  94.  
  95. //  return s.Text()
  96. // }
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement