GithubHelp home page GithubHelp logo

sqlbuild's Introduction

Generate SQL code only with your Golang structs

The main idea of this package is to generate the most standard SQL queries that exist. It is very easily understood with an example, the typical queries would be the following:

package main

import (
 "fmt"

 "github.com/arturo-source/sqlbuild"
)

type Person struct {
 Id   int `db:"id"`
 Name string
 Age  *int `db:"age"`
}

func main() {
 age := 20
 p := Person{
  Id:   0,
  Name: "John",
  Age:  &age,
 }

 fmt.Println(sqlbuild.Create(p))
 fmt.Println(sqlbuild.SelectAll(p))
 fmt.Println(sqlbuild.SelectById(p))
 fmt.Println(sqlbuild.Insert(p))
 fmt.Println(sqlbuild.Update(p))
 fmt.Println(sqlbuild.DeleteAll(p))
 fmt.Println(sqlbuild.DeleteById(p))
 fmt.Println(sqlbuild.Drop(p))

 people := []Person{
  p,
  {Id: 1, Name: "Mike", Age: nil},
  {Id: 2, Name: "Cris", Age: nil},
 }
 fmt.Println(sqlbuild.InsertMultiple(people))
}

And the result of this code would be the following (I omit the err=nil):

CREATE TABLE "Person" ("id" INT NOT NULL PRIMARY KEY, "Name" TEXT NOT NULL, "age" INT)
SELECT * FROM "Person"
SELECT * FROM "Person" WHERE "id" = 0
INSERT INTO "Person" ("id", "Name", "age") VALUES (0, 'John', 20)
UPDATE "Person" SET "id" = 0, "Name" = 'John', "age" = 20 WHERE "id" = 0
DELETE FROM "Person"
DELETE FROM "Person" WHERE "id" = 0
DROP TABLE "Person"
INSERT INTO "Person" ("id", "Name", "age") VALUES (0, 'John', 20), (1, 'Mike', null), (2, 'Cris', null)

TO DO

These are some problems I have to think how to face them:

  • AUTO_INCREMENT is written differently in each database engine.
  • References between tables. CreateWithReferences ??
  • I don't really know how to avoid SQL injections. The package uses sanitize but I think it is not enough.

Possible uses of this package

The reality is that I have created this package to practice using reflection, without a clear use case. But something came to my mind while I was programming it. If you can think others, don't hesitate to add it in an issue or PR:

  1. Creation of SQL scripts using Go, for databases in development environments.

sqlbuild's People

Contributors

arturo-source avatar

Stargazers

Abdulhafiz KAŞGARLI avatar

Watchers

 avatar

Forkers

avary

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.