Code Monkey home page Code Monkey logo

sqlgen's Introduction

sqlgen

English | 中文

Go codecov Go Reference Go Report Card Release GitHub license

sqlgen is a tool to generate bun, gorm, sql, sqlx and xorm sql code from SQL file which is inspired by

Installation

go install github.com/anqiansong/sqlgen@latest

Example

See example

Queries rule

1. Function Name

You can define a function via fn keyword in line comment, for example:

-- fn: my_func
SELECT *
FROM user;

it will be generated as:

func (m *UserModel) my_func (...) {
...
}

2. Get One Record

The expression limit 1 must be explicitly defined if you want to get only one record, for example:

-- fn: FindOne
select *
from user
where id = ? limit 1;

3. Marker or Values?

For arguments of SQL, you can use ? or explicitly values to mark them, in sqlgen, the arguments will be converted into variables, for example, the following query are equivalent:

NOTES: It does not apply to rule 2

-- fn: FindLimit
select *
from user
where id = ?;

-- fn: FindLimit
select *
from user
where id = 1;

4. SQL Function

sqlgen supports aggregate function queries in sql, other than that, other functions are not supported so far. All the aggregate function query expressions must contain AS expression, for example:

-- fn: CountAll
select count(*) as count
from user;

For most query cases, you can see example.sql for details.

How it works

  1. Create a SQL file
  2. Write your SQL code in the SQL file
  3. Run sqlgen to generate code

Notes

  1. Only support MYSQL code generation.
  2. Do not support multiple tables in one SQL file.
  3. Do not support join query.

sqlgen's People

Contributors

kesonan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sqlgen's Issues

Maybe not support <> or != in sql

just like this sql:

update user set name = ?, password = ?, mobile = ?, gender = ?, nickname = ?, type = ?, create_at = ?, update_at = ? where id = ? and name != '';
update user set name = ?, password = ?, mobile = ?, gender = ?, nickname = ?, type = ?, create_at = ?, update_at = ? where id = ? and name <> '';

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.