Code Monkey home page Code Monkey logo

sql's Introduction

This repository is now moved to https://github.com/gouniverse/sb

SQL Open in Gitpod

tests

An SQL package that wraps the mainstream DB package to allow transparent working with transactions and simplified SQL builder (with limited functionality).

For a full SQL builder functionality check: https://doug-martin.github.io/goqu

Installation

go get -u github.com/gouniverse/sql

Example Create Table SQL

import sb "github.com/gouniverse/sql"

sql := NewBuilder(DIALECT_MYSQL).
	Table("users").
	Column("id", COLUMN_TYPE_STRING, map[string]string{
		COLUMN_ATTRIBUTE_PRIMARY: "yes",
		COLUMN_ATTRIBUTE_LENGTH:  "40",
	}).
	Column("image", COLUMN_TYPE_BLOB, map[string]string{}).
	Column("price_default", COLUMN_TYPE_DECIMAL, map[string]string{
		COLUMN_ATTRIBUTE_LENGTH:   "12",
		COLUMN_ATTRIBUTE_DECIMALS: "10",
	}).
	Column("price_custom", COLUMN_TYPE_DECIMAL, map[string]string{
		COLUMN_ATTRIBUTE_LENGTH:   "12",
		COLUMN_ATTRIBUTE_DECIMALS: "10",
	}).
	Column("created_at", COLUMN_TYPE_DATETIME, map[string]string{}).
	Column("updated_at", COLUMN_TYPE_DATETIME, map[string]string{}).
	Column("deleted_at", COLUMN_TYPE_DATETIME, map[string]string{
		COLUMN_ATTRIBUTE_NULLABLE: "yes",
	}).
	Create()

Example Table Drop SQL

import sb "github.com/gouniverse/sql"

sql := NewBuilder(DIALECT_MYSQL).
		Table("users").
		Drop()

Example Insert SQL

import sb "github.com/gouniverse/sql"
	
sql := sb.NewBuilder(DIALECT_MYSQL).
	Table("cache").
	Insert(map[string]string{
		"ID":         uid.NanoUid(),
		"CacheKey":   token,
		"CacheValue": string(emailJSON),
		"ExpiresAt":  expiresAt.Format("2006-01-02T15:04:05"),
		"CreatedAt":  time.Now().Format("2006-01-02T15:04:05"),
		"UpdatedAt":  time.Now().Format("2006-01-02T15:04:05"),
	})

Example Delete SQL

sql := sb.NewBuilder(DIALECT_MYSQL).
	Table("user").
	Where(sb.Where{
		Column: "id",
		Operator: "==",
		Value: "1",
	}).
	Limit(1).
	Delete()

Initiating Database Instance

  1. From existing Go DB instance
myDb := sb.NewDatabaseFromDb(sqlDb, DIALECT_MYSQL)
  1. From driver
myDb = sql.NewDatabaseFromDriver("sqlite3", "test.db")

Example SQL Execute

myDb := sb.NewDatabaseFromDb(sqlDb, DIALECT_MYSQL)
err := myDb.Exec(sql)

Example Transaction

import _ "modernc.org/sqlite"
import sb "github.com/gouniverse/sql"

myDb = sql.NewDatabaseFromDriver("sqlite3", "test.db")

myDb.BeginTransaction()

err := Database.Exec(sql1)

if err != nil {
	myDb.RollbackTransaction()
	return err
}

err := Database.Exec(sql2)

if err != nil {
	myDb.RollbackTransaction()
	return err
}

myDB.CommitTransaction()

Example Create View SQL

selectSQL := NewBuilder(DIALECT_POSTGRES).
	Table("users").
	Select([]string{"FirstName", "LastName"})

createViewSql := NewBuilder(DIALECT_POSTGRES).
	View("v_users").
	ViewColumns([]string{"first_name", "last_name"}).
	ViewSQL(selectSQL).
	Create()

Example Create View If Not Exists SQL

selectSQL := NewBuilder(DIALECT_POSTGRES).
	Table("users").
	Select([]string{"FirstName", "LastName"})

createViewSql := NewBuilder(DIALECT_POSTGRES).
	View("v_users").
	ViewColumns([]string{"first_name", "last_name"}).
	ViewSQL(selectSQL).
	CreateIfNotExists()

Example Drop View SQL

dropiewSql := NewBuilder(DIALECT_POSTGRES).
	View("v_users").
	Drop()

Example Select as Map

Executes a select query and returns map[string]any

mapAny := myDb.SelectToMapAny(sql)

Executes a select query and returns map[string]string

mapString := myDb.SelectToMapAny(sql)

Similar

sql's People

Contributors

lesichkovm avatar dependabot[bot] avatar

Stargazers

Sinevia avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

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.