Git Product home page Git Product logo

chenquan / sqlbreaker Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 200 KB

A low-code intrusion library that provides SQL breaker capabilities, suitable for any relational database (Sqlite3, MySQL, Oracle, SQL Server, PostgreSQL, TiDB, TDengine, etc.) and ORM libraries for various relational database (gorm, xorm, sqlx, ent, etc.)

License: Apache License 2.0

Go 100.00%
go gorm mysql oracle postgresql sql sqlite3 sqlserver tidb xorm

sqlbreaker's Introduction

sqlbreaker

Godoc Go Report Card Release codecov Download GitHub

A low-code intrusion library that provides SQL breaker capabilities, suitable for any relational database (Sqlite3, MySQL, Oracle, SQL Server, PostgreSQL, TiDB, TDengine, etc.) and ORM libraries for various relational database (gorm, xorm, sqlx, etc.)

Unstable, use with caution in production environment

😜installation

go get -u github.com/chenquan/sqlbreaker

👏how to use

package main

import (
	"context"
	"database/sql"
	"fmt"

	"github.com/chenquan/sqlbreaker"
	"github.com/chenquan/sqlbreaker/pkg/breaker"
	"github.com/chenquan/sqlplus"
	"github.com/chenquan/sqltrace"
	"github.com/mattn/go-sqlite3"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/trace"
)

func main() {
	// Create a sqlite3 driver with tracing and  breaker
	hook := sqlplus.NewMultiHook(
		sqltrace.NewTraceHook(sqltrace.Config{
			Name:           "sqlite3_trace",
			DataSourceName: "sqlite3",
			Endpoint:       "http://localhost:14268/api/traces",
			Sampler:        1,
			Batcher:        "jaeger",
		}),
		sqlbreaker.NewBreakerHook(breaker.NewBreaker()),
	)

	// register new driver
	sql.Register("sqlite3_trace", sqlplus.New(&sqlite3.SQLiteDriver{}, hook))
	defer sqltrace.StopAgent()

	// open database
	db, err := sql.Open("sqlite3_trace", "identifier.sqlite")
	if err != nil {
		panic(err)
	}

	tracer := otel.GetTracerProvider().Tracer("sqlite3_trace")
	ctx, span := tracer.Start(context.Background(),
		"test",
		trace.WithSpanKind(trace.SpanKindClient),
	)
	defer span.End()

	db.ExecContext(ctx, `CREATE TABLE  IF NOT EXISTS t
(
    age  integer,
    name TEXT
)`)
	db.ExecContext(ctx, "insert into t values (?,?)", 1, "chenquan")

	// transaction
	tx, err := db.BeginTx(ctx, nil)
	stmt, err := tx.PrepareContext(ctx, "select age+1 as age,name from t where age = ?;")
	stmt.QueryContext(ctx, 1)
	tx.Commit()

	rows, err := db.QueryContext(ctx, "select  age+1 as age,name from t;")
	for rows.Next() {
		var age int
		var name string
		err := rows.Scan(&age, &name)
		if err != nil {
			fmt.Println(err)
		}

		fmt.Println(age, name)
	}
}

⭐star

If you like or are using this project to learn or start your solution, please give it a star⭐. Thanks!

👐ecosystem

  • sqltrace:A low-code intrusion library that provides SQL tracing capabilities, suitable for any relational database (Sqlite3, MySQL, Oracle, SQL Server, PostgreSQL, TiDB, TDengine, etc.) and ORM libraries for various relational database (gorm, xorm, sqlx, etc.)
  • sqlplus: A sql enhancement tool library based on database/sql/driver

sqlbreaker's People

Contributors

chenquan avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 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.