np

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

README

NP

NP - Null types and Pointer to comma separated string

Create comma separated names and values from struct or map for raw SQL query.

This is an experimental and for my own usage.

Which types of

  • database/sql Nullnnnn
  • github.com/guregu/null
  • Map - Convert kebab case keys to upper case with underscore
  • Pointer

Example

See np_test.go

package main

import (
	"github.com/practice-golang/np"
)

// Human
type Human struct {
	Name         null.String `json:"name" db:"NAME"`
	Age          null.Int    `json:"age"  db:"AGE"`
	EmailAddress null.String `json:"email-address,omitempty" db:"EMAIL_ADDRESS"`
}

func main() {
	john := Human{
		null.NewString("John", true),
		null.NewInt(777, true),
		null.NewString("john@human.io", true),
	}

	np.TagName = "db"
	np.Separator = ","

	colString := np.MakeString(john)

	selectQuery := "SELECT " + colString.Names + " FROM table_name;"
	insertQuery := "INSERT INTO table_name (" + colString.Names + ") VALUES(" + colString.Values + ");"

	fmt.Println(selectQuery)
	fmt.Println(insertQuery)
}

// Result:
// SELECT NAME,AGE,EMAIL_ADDRESS FROM table_name;
// INSERT INTO table_name (NAME,AGE,EMAIL_ADDRESS) VALUES(John,777,john@human.io)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TagName   = "db"
	TagNameNP = "np"
	Separator = ","
)

Functions

func MakeMap

func MakeMap(o interface{}) map[string]string

MakeMap - make map from struct, map

func MakeSlice

func MakeSlice(o interface{}) map[string][]string

MakeSlice - make slices from struct, map

Types

type ColumnStrings

type ColumnStrings struct {
	Names  string
	Values string
}

ColumnStrings - strings for names and values

func MakeString

func MakeString(o interface{}) ColumnStrings

MakeString - make string from struct, map

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL