utils.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //go:build !windows
  2. // +build !windows
  3. package postgresql
  4. import (
  5. nodes "github.com/pganalyze/pg_query_go/v2"
  6. )
  7. func isArray(n *nodes.TypeName) bool {
  8. if n == nil {
  9. return false
  10. }
  11. return len(n.ArrayBounds) > 0
  12. }
  13. func isNotNull(n *nodes.ColumnDef) bool {
  14. if n.IsNotNull {
  15. return true
  16. }
  17. for _, c := range n.Constraints {
  18. switch inner := c.Node.(type) {
  19. case *nodes.Node_Constraint:
  20. if inner.Constraint.Contype == nodes.ConstrType_CONSTR_NOTNULL {
  21. return true
  22. }
  23. if inner.Constraint.Contype == nodes.ConstrType_CONSTR_PRIMARY {
  24. return true
  25. }
  26. }
  27. }
  28. return false
  29. }
  30. func IsNamedParamFunc(node *nodes.Node) bool {
  31. fun, ok := node.Node.(*nodes.Node_FuncCall)
  32. return ok && joinNodes(fun.FuncCall.Funcname, ".") == "sqlc.arg"
  33. }
  34. func IsNamedParamSign(node *nodes.Node) bool {
  35. expr, ok := node.Node.(*nodes.Node_AExpr)
  36. return ok && joinNodes(expr.AExpr.Name, ".") == "@"
  37. }
  38. func makeByte(s string) byte {
  39. var b byte
  40. if s == "" {
  41. return b
  42. }
  43. return []byte(s)[0]
  44. }
  45. func makeUint32Slice(in []uint64) []uint32 {
  46. out := make([]uint32, len(in))
  47. for i, v := range in {
  48. out[i] = uint32(v)
  49. }
  50. return out
  51. }
  52. func makeString(s string) *string {
  53. if s == "" {
  54. return nil
  55. }
  56. return &s
  57. }