sqlconv

package module
v0.0.0-...-e705385 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2021 License: MIT Imports: 7 Imported by: 0

README

SQLConv - Convert Go Structs to SQL Statements

Basically, library converts from Go structs to "Simple SQL" statements.

This would be a good helper, if you use bare SQL (w/o ORM) to handle data operations for accessing to the database side.

To be specific, wonderful sqxlx library would be base one to provide SQL statements to be executed.

Samples

Here are some samples for different type of SQL statements to be converted from Go structs:

Create Table SQL Statements

import (
    "github.com/emrahayanoglu/sqlconv"
    "github.com/emrahayanoglu/sqlconv/adapters"
)

type HelloWorld struct {
	Name string
	Surname string
}

// As default, SQLConv would take PostgreSQL
createSQL, err := sqlconv.GenerateCreateTableSQL(nil, HelloWorld{})

createSQL, err = sqlconv.GenerateCreateTableSQL(adapters.MySQL{}, HelloWorld{})

Drop Table SQL Statements

import (
    "github.com/emrahayanoglu/sqlconv"
    "github.com/emrahayanoglu/sqlconv/adapters"
)

type HelloWorld struct {
	Name string
	Surname string
}

// As default, SQLConv would take PostgreSQL
dropSQL, err := sqlconv.GenerateDropTableSQL(nil, HelloWorld{})

dropSQL, err = sqlconv.GenerateDropTableSQL(adapters.MySQL{}, HelloWorld{})

Insert into Table SQL Statements

import (
    "github.com/emrahayanoglu/sqlconv"
)

type HelloWorld struct {
	Name string
	Surname string
}

insertSQL, err := sqlconv.GenerateInsertIntoTableSQL(nil, HelloWorld{})

TODO List:

  • Basic Documentation about "How to use and Samples"
  • Database Specific Field Type Implementation
  • Database Specific Extra Fields Validation
  • Rename repository to sqlconv
  • Generate "DROP Table" SQL Statements
  • Generate "INSERT" SQL Statements

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CreateTableTemplate = `` /* 130-byte string literal not displayed */

	DropTableTemplate = `DROP TABLE {{ .TableName }};`

	InsertIntoTableTemplate = `INSERT INTO {{ .TableName }}({{ .FieldList }}) VALUES({{ .ValueList }});`
)

Functions

func GenerateCreateTableSQL

func GenerateCreateTableSQL(dbType adapters.DBInterface, allStructs ...interface{}) (string, error)

GenerateCreateTableSQL basically generates "CREATE TABLE" SQL statements with given structs (single or multiple). Mainly converts namings(table name and field names) into the snake case and handles tags to provide SQL specific options. Example:

type HelloWorld struct {
	Name string
	Surname string
}

createSQL, err := GenerateCreateTableSQL(nil, HelloWorld{})

func GenerateCreateTableSQLForSingleTable

func GenerateCreateTableSQLForSingleTable(tableName string, fieldMap map[string]string) (string, error)

GenerateCreateTableSQLForSingleTable is very similar function to main one; however, it does the main job to traverse on the fields and converts to the corresponding SQL statements.

func GenerateDropTableSQL

func GenerateDropTableSQL(dbType adapters.DBInterface, allStructs ...interface{}) (string, error)

GenerateDropTableSQL is simply generates "DROP TABLE" SQL statements with given multiple structs.

func GenerateDropTableSQLForSingleTable

func GenerateDropTableSQLForSingleTable(tableName string) (string, error)

GenerateDropTableSQLForSingleTable is very similar to the general function about drop table; however, it does the main job to generate "DROP TABLE" statement for the single table

func GenerateInsertIntoTableSQL

func GenerateInsertIntoTableSQL(s interface{}) (string, error)

GenerateInsertIntoTableSQL basically generates "INSERT INTO TABLE" SQL statements with given single struct.

func ParseTagsAndTypes

func ParseTagsAndTypes(dbType adapters.DBInterface, field *structs.Field) (string, error)

ParseTagsAndTypes: in short, checks the field tags and types on the struct and converts into the right SQL statements.

Most of the basic Types are supported currently in the dbtypes section.

Current Supported Tags: length: 100 -> Only applicable to string type autoincrement -> Converts field data type to corresponding auto increment data type in supported database extra -> Feed your custom SQL Statements for related field

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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