Skip to content

Xzya/sqlhandler

Repository files navigation

SQL Handlers Typewriter

This is a gen typewriter for creating sql operation handlers.

Dependencies

This package uses gorm as the ORM.

Supported operations

Operation Description
Create Adds a Create handler for inserting values.
Find Adds a Find handler which accepts dynamic where clause as parameter.
FindAll Adds a FindAll handler which returns all rows of that model.
FindByID Adds a FindByID handler which returns the row with the given ID
FindAssociation[T1,T2,...] Adds a Find<TYPE><T>Association handler which gets the given association for the model (see example usage below).
FindAssociations[T1,T2,...] Adds a Find<TYPE>Associations handler which gets all specified associations (see example usage below). Note: This requires you to also use FindAssociation for each specified type.
Delete Adds a Delete handler which accepts dynamic where clause as parameter.
DeleteByID Adds a DeleteByID handler which deletes the row with the given ID

Usage

package main

import "github.com/jinzhu/gorm"

//go:generate gen -f

// +gen sqlhandler:"Create,Find,FindByID,FindAll,FindAssociation[PersonalInfo,Projects],FindAssociations[PersonalInfo,Projects],Delete,DeleteByID"
type Account struct {
	gorm.Model

	Username string
	Password string

	PersonalInfo PersonalInfo
	Projects     []Project
}

type PersonalInfo struct {
	gorm.Model

	AccountID uint

	FirstName string
	LastName  string
}

type Project struct {
	gorm.Model

	Name string
}

type Projects []Project

Produces the following code

// Generated by: main.exe
// TypeWriter: sqlhandler
// Directive: +gen on Account

package main

import (
	"github.com/jinzhu/gorm"
)

// AccountHandler handles all Account database operations
type AccountHandler struct {
	db *gorm.DB
}

func NewAccountHandler(db *gorm.DB) *AccountHandler {
	return &AccountHandler{
		db,
	}
}

// Create inserts a Account into the database
func (h *AccountHandler) Create(value *Account) error {
	return h.db.Create(value).Error
}

// Find finds a Account that matches the given conditions
func (h *AccountHandler) Find(where ...interface{}) (*Account, error) {
	var res Account

	if err := h.db.Find(&res, where...).Limit(1).Error; err != nil {
		return nil, err
	}

	return &res, nil
}

// FindByID finds a Account with the given ID
func (h *AccountHandler) FindByID(ID uint) (*Account, error) {
	var res Account

	if err := h.db.Find(&res, "id = ?", ID).Limit(1).Error; err != nil {
		return nil, err
	}

	return &res, nil
}

// FindAll finds all Account
func (h AccountHandler) FindAll() ([]Account, error) {
	var results []Account

	if err := h.db.Model(&Account{}).Find(&results).Error; err != nil {
		return nil, err
	}

	return results, nil
}

// FindAccountPersonalInfoAssociation finds the Account's PersonalInfo association
func FindAccountPersonalInfoAssociation(db *gorm.DB, item *Account) error {
	return db.Model(item).Association("PersonalInfo").Find(&item.PersonalInfo).Error
}

// FindAccountProjectsAssociation finds the Account's Projects association
func FindAccountProjectsAssociation(db *gorm.DB, item *Account) error {
	return db.Model(item).Association("Projects").Find(&item.Projects).Error
}

// FindAccountAssociations finds the Account's associations
func FindAccountAssociations(db *gorm.DB, item *Account) error {

	if err := FindAccountPersonalInfoAssociation(db, item); err != nil {
		return err
	}

	if err := FindAccountProjectsAssociation(db, item); err != nil {
		return err
	}

	return nil
}

// Delete deletes a Account that matches the given conditions
func (h *AccountHandler) Delete(value interface{}, where ...interface{}) error {
	return h.db.Delete(value, where...).Error
}

// Delete deletes a Account with the given ID
func (h *AccountHandler) DeleteByID(ID uint) error {
	return h.db.Delete(&Account{}, "id = ?", ID).Limit(1).Error
}

License

Open sourced under the MIT license.

About

This is a typewriter for creating sql operation handlers for https://github.com/clipperhouse/gen

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages