Coder Social home page Coder Social logo

gosql's Introduction

gosql 一个简单的MySql DML语句封装, 包含(空闲连接自动断开, sql封装使用, 读写分离)


列子

创建连接,连接池配置,设置表名称和数据写入

package main

import (
    "github.com/hfdend/gosql"
    "fmt"
)

func main() {
    // 建立连接
    m := gosql.NewDbMysql("127.0.0.1", 3306, "root", "", "test")
    // 设置最大连接数
    m.SetMaxOpenConns(30)
    // SetMaxIdleConns sets the maximum number of connections in the idle
    m.SetMaxIdleConns(10)
    // 设置空闲连接池的生存时间
    m.SetAutoCloseTime(100)
    // 设置表名
    m.SetTableName("user")

    // 数据插入
    data := map[string]interface{} {
        "user": "张三",
        "sex": "1",
        "age": 56,
        "hobbies": "乒乓球",
    }
    id, e := m.Insert(data);
    fmt.Println(e)
    fmt.Println(id)
    fmt.Println(m.LastSql)
}

简单查询操作与查询结果数据转换

condition := m.NewCondition()
condition.SetFilter("Id", 3)
condition.SetFilter("sex", 1)
condition.SetFilterEx("age", ">", 20)

// sql: select * from `user` where `Id` = 3 and `sex` = 1 and `age` > 20
r, e := m.SetCondition(condition).FindAll()

fmt.Println("错误", e)
fmt.Println("查询结果", r)
// 将数据转换成map
fmt.Println("数据转换成map", r.Result())
// 将数据Scan到结构体
type User struct {
    User        string  `field:"user"`
    Sex         string   `field:"sex"`
    Age         string  `field:"age"`
    Hobbies     string  `field:"Hobbies"`
}
var userAry []User
var userAryPtr []*User
r.Scan(&userAry)
r.Scan(&userAryPtr)
fmt.Println("数据转换成结构体", userAry, userAryPtr)
// 转换单个数据到结构体
for _, v := range r.ResultValue() {
    var user User
    v.Scan(&user)
    fmt.Println("单个结构体转换", user)
}
// 自定义结构转换
handlerUserAry := &[]*User{}
r.Scan(handlerUserAry, func(v interface{}, row gosql.Value) {
    u := v.(*User)
    u.Age = "1010"
})
fmt.Println("自定义转换结果 start")
for _, v := range *handlerUserAry {
    fmt.Println(v)
}
fmt.Println("自定义转换结果 end")
fmt.Println("执行的sql", m.LastSql)

输出

错误 <nil>
查询结果 &[0xc82002c038]
数据转换成map [map[Id:3 user:张三 sex:1 age:28 hobbies:乒乓球]]
数据转换成结构体 [{张三 1 28 }] [0xc820010480]
单个结构体转换 {张三 1 28 }
自定义转换结果 start
&{张三 1 1010 }
自定义转换结果 end
执行的sql &{select * from `user` where `Id` = ? and `sex` = ? and `age` > ? [3 1 20]}

OR查询,连表查询与分页查询

// OR查询条件设置
conditon1 := m.NewCondition()
conditon2 := m.NewCondition()
condition := m.NewCondition()
conditon1.SetFilter("id", 1)
conditon2.SetFilter("id", 2)
condition.SetFilterOr(conditon1, conditon2)
// sql: where id = 1 or id = 2
m.SetCondition(condition)

// in 查询
conditon2.SetFilter("id", []int{1, 3, 4})

// 关联查询
m.LeftJoin("user2", "user2.user_id = user.user_id")

// 分页
pager := m.NewPager()
// 设置每页条数
pager.Limit = 20
// 如果打开将使用子查询查询出总数
pager.IsSubqueries = false
// 设置偏移量
pager.Offset = 5
// 实现分页查询
values, err := m.PagerFindAll(pager)
fmt.Println("错误", err)
fmt.Println("总条数与分页情况", pager)
fmt.Println("当前页数的数据", values)

读写分离

conf := &gosql.Config{
    Master: &gosql.ConfigModel{"127.0.0.1", 3306, "root", "pwd", "test", 100, 0, 0},
    Slave: []*gosql.ConfigModel{
        &gosql.ConfigModel{"127.0.0.1", 3306, "root", "pwd", "test", 100, 0, 0},
        &gosql.ConfigModel{"192.168.101.241", 3306, "root", "pwd", "test", 100, 0, 0},
    },
}
m := conf.NewDbMysql() // 实例化的时候已经把写库的配置和随机一条读库的配置记录到DbMysql中
m.SetTableName("demo").FindAll()        // 默认连接读库执行查询操作    
m.SetTableName("demo").Insert(dbData)   // 默认连接写库执行写入操作  Update一样的逻辑
// 手动指定读写库
m.SetTableName("demo").UseMaster().FindAll() 
m.SetTableName("demo").UseSlave().FindAll() 
```just

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.