Giter Site home page Giter Site logo

sqlsqbdr's Introduction

Table of Content

  1. Name
  2. Getting started
  3. Usage

SQLABST

sqlsqbdr is the acronym for SQL Simple Query Builder. This is a simple query builder that only build the placeholder for updating field, inserting field, and filtering field. No more creating a long placeholder of query for inserting, and updating data

Getting started

This is an example to how to use this project locally

Installation

go get github.com/nurcahyaari/sqlsqbdr

Motivasion

when I created a query that have a long placeholder for inserting and updating data. it's painful, because there's many field, placeholder, and the value that need to assign to the query. especially when I need to add new field (it means new field from DB, or missing field from query). I feel confused by the query

here the example when I create query

INSERT INTO product 
(
    id, 
    name,
    image, 
    price, 
    discount_price, 
    active, 
    created_at, 
    created_by, 
    updated_at, 
    updated_by
) VALUES (
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
)

So, I want to create the query without defining the field, and their placeholder

Usage

This is a guideline to use sqlsqbdr on your projects

example

// let's say you have a defined struct
type Product struct {
    Id int64 `db:"id"`
    Name string `db:"name"`
    Image string `db:"image"`
    Price int64 `db:"price"`
    DiscountPrice int64 `db:"discount_price"`
    Active bool `db:"active"`
    CreatedAt string `db:"created_at"`
    CreatedBy string `db:"created_by"`
    UpdatedAt string `db:"updated_at"`
    UpdatedBy string `db:"updated_by"`
}
// here is my new data
products := []Product{
    Product{
        Id: 1,
        Name: "Test",
        Image: "test.com/1.png",
        Price: 1000,
        DiscountPrice: 1000,
        Active: true,
        CreatedAt: "2022-01-01 01:00:00"
        CreatedBy: "Tester",
        UpdatedAt: "2022-01-01 01:00:00"
        UpdatedBy: "Tester",
    }
}

// here is my updated data
updatedProduct := Product{
    Name: "Test2",
    Price: 2000,
    DiscountPrice: 1500,
}

Type of FieldSelect

sqlsqbdr have 2 field type

name usage
IncludeField Select field based on the selected field on the params
ExcludeField Select field that not in selected field on the params

build insert placeholder

BuildInsertField have 3 params first is the model/ entity's struct, the second is FieldSelectType, the the last is a variadic this params is defined the list of field that you want to insert if you choose the FieldSelectType as IncludeField. but if you chose the FieldSelectedType as ExcludeField the last params is the field that you don't want to insert

insertField, err := sqlsqbdr.BuildInsertField(products, sqlsqbdr.IncludeField)
if err != nil {
    return
}

query := fmt.Sprintf("INSERT INTO product (%s) VALUES %s", strings.Join(insertField.Name, ","), strings.Join(insertField.Placeholder, ","))

// the query will be like this
/*
    INSERT INTO product 
    (
        id, 
        name,
        image, 
        price, 
        discount_price, 
        active, 
        created_at, 
        created_by, 
        updated_at, 
        updated_by
    ) VALUES (
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
    )
*/

build update placeholder

BuildUpdatedField have 3 params. first is the model/ entity's struct, the second is FieldSelectType, the the last is a variadic this params is defined the list of field that you want to update if you choose the FieldSelectType as IncludeField. but if you chose the FieldSelectedType as ExcludeField the last params is the field that you don't want to update

updatedField, err := sqlsqbdr.BuildUpdatedField(updatedProduct, sqlsqbdr.IncludeField, "name", "price", "discount_price")
if err != nil {
    return
}

query := fmt.Sprintf("UPDATE product SET %s", strings.Join(updatedField.Name, ","))

build filtering field

BuildWhereFilter has 1 param. and it's a defined struct

the parameter

Filters is an array, and here is the description about the struct

name usage
Field it means the field on your database
Value It means the value of the field
field, values := sqlsqbdr.BuildWhereFilter(sqlsqbdr.Filters{
    &sqlsqbdr.Filter{
        Field: "id",
        Value: 1,
    },
})
// result
// field: id = ?
// values: []interface{}{1}

sqlsqbdr's People

Contributors

nurcahyaari avatar

Stargazers

 avatar

Watchers

James Cloos 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.