Skip to content

Commit

Permalink
fix: convert windows CRLF(\r\n) to LF(\n)
Browse files Browse the repository at this point in the history
  • Loading branch information
ystyle committed Sep 5, 2022
1 parent ed7598d commit ea4154b
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 105 deletions.
145 changes: 72 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
a library for manager sql with markdown or constant. and you can custom sql store plugin.

**Not A Go ORM Library**

### feature
- manage sql with markdown
- render sql with go template
- support load sql with custom plugin

### usage

create a markdown in `sql/test.md`
#### create a markdown in
> markdown content in sql/test.md
```markdown
### GetStudentByID
>get student by id, required id
Expand All @@ -23,78 +24,76 @@ create a markdown in `sql/test.md`
```
```

in golang
```go
package main
#### Use Available plugins
1. File Driver: using *.md file
```go
package main

import (
"fmt"
"github.com/ystyle/sqlmanager"
)
import (
"fmt"
"github.com/ystyle/sqlmanager"
)

func main() {
sm := sqlmanager.New()
sm.Use(sqlmanager.NewMarkdownDriver())
// load sql with custom dir
// sm.Use(sqlmanager.NewMarkdownDriverWithDir("./prod-sql"))
// register go template func
// sm.RegisterFunc(template.FuncMap{
// "test": func(v string) string {
// return strings.ToUpper(v)
// },
// })
sm.Load()
sql, err := sm.RenderTPL("GetStudentByID", 1)
if err != nil {
panic(err)
}
fmt.Println(sql)
// select * from student where id = 1

// using gorm
// db, err := gorm.Open("databaseurl")
// if err != nil {
// panic("failed to connect database")
// }
// db.Raw(sql)

// using database/sql
// db, err := sql.Open("driver-name", "database=test1")
// if err != nil {
// log.Fatal(err)
// }
// db.Query(sql)
}
```
func main() {
sm := sqlmanager.New()
sm.Use(sqlmanager.NewMarkdownDriver())
// load sql with custom dir
// sm.Use(sqlmanager.NewMarkdownDriverWithDir("./prod-sql"))
// register go template func
// sm.RegisterFunc(template.FuncMap{
// "test": func(v string) string {
// return strings.ToUpper(v)
// },
// })
sm.Load()
sql, err := sm.RenderTPL("GetStudentByID", 1)
if err != nil {
panic(err)
}
fmt.Println(sql)
// select * from student where id = 1

### Available plugins
- File Driver: using file, See above.
- Dynamic Driver: using variable.
```go
store := sqlmanager.NewDynamicDriver()
// using gorm
// db, err := gorm.Open("databaseurl")
// if err != nil {
// panic("failed to connect database")
// }
// db.Raw(sql)

const GetStudentByID = "select * from student where id = {{.}}"
store.RegisterWithDescs("GetStudentByID", "Query Student by ID", GetStudentByID)
// using database/sql
// db, err := sql.Open("driver-name", "database=test1")
// if err != nil {
// log.Fatal(err)
// }
// db.Query(sql)
}
```
2. Embed markdown Driver: using go embed.
```go
//go:embed test-sql
var Assets embed.FS
func main() {
sm = sqlmanager.New()
sm.Use(sqlmanager.NewMarkdownDriverWithEmbedDir(Assets, "test-sql"))
// sm.Use(sqlmanager.NewMarkdownDriverWithEmbed(Assets)) // default dir is sql
sm.load()
sql, _ := sm.RenderTPL("test/GetStudentByID", 1)
}
```
>when the sql in `test-sql/admin/report.md/###GetStudentByRoot` the sql id is: `admin/report/GetStudentByRoot`
sm := sqlmanager.New()
sm.Use(store)
sm.load()
sql, _ := sm.RenderTPL("GetStudentByID", 1)
```
- Embed markdown Driver: using go embed.
```go
//go:embed test-sql
var Assets embed.FS
func main() {
sm = sqlmanager.New()
sm.Use(sqlmanager.NewMarkdownDriverWithEmbedDir(Assets, "test-sql"))
// sm.Use(sqlmanager.NewMarkdownDriverWithEmbed(Assets)) // default dir is sql
sm.load()
sql, _ := sm.RenderTPL("test/GetStudentByID", 1)
}
```
> when the sql in `test-sql/admin/report.md/###GetStudentByRoot` the sql id is: `admin/report/GetStudentByRoot`
3. Dynamic Driver: using variable.
```go
store := sqlmanager.NewDynamicDriver()

const GetStudentByID = "select * from student where id = {{.}}"
store.RegisterWithDescs("GetStudentByID", "Query Student by ID", GetStudentByID)

sm := sqlmanager.New()
sm.Use(store)
sm.load()
sql, _ := sm.RenderTPL("GetStudentByID", 1)
```

### custom puglin
> implement sqlmanager.Driver
Expand All @@ -103,16 +102,16 @@ type CustomeDriver struct {
}
func NewCustomeDriver() *CustomeDriver {
return &CustomeDriver{}
return &CustomeDriver{}
}
func (mdd *CustomeDriver ) DriverName() string {
return "CustomeDriver"
return "CustomeDriver"
}
func (mdd *CustomeDriver ) Load() ([]sqlmanager.SqlTemple, error) {
var list []sqlmanager.SqlTemple
// db.table("sql_store").Find(&list)
return list, nil
var list []sqlmanager.SqlTemple
// db.table("sql_store").Find(&list)
return list, nil
}
```
7 changes: 5 additions & 2 deletions embed_markdown_driver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sqlmanager

import (
"bytes"
"embed"
"errors"
"fmt"
Expand Down Expand Up @@ -33,7 +34,7 @@ func (mdd *EmbedMarkdownDriver) DriverName() string {

func (mdd *EmbedMarkdownDriver) Load() ([]SqlTemple, error) {
var sqls []SqlTemple
err := fs.WalkDir(mdd.fs, "sql", func(subpath string, d fs.DirEntry, err error) error {
err := fs.WalkDir(mdd.fs, mdd.dir, func(subpath string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
Expand Down Expand Up @@ -62,7 +63,9 @@ func (mdd *EmbedMarkdownDriver) parseMarkdown(filename string) ([]SqlTemple, err
log.Printf("sqlmanager - ERROR: %s loading failed...\n", filename)
return nil, err
}

if bytes.ContainsRune(buf, '\r') {
buf = bytes.ReplaceAll(buf, []byte{'\r'}, nil)
}
psr := parser.New()
node := markdown.Parse(buf, psr)
list := getAll(node)
Expand Down
19 changes: 19 additions & 0 deletions embed_markdown_driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sqlmanager

import (
"embed"
"fmt"
"testing"
)

//go:embed test-sql
var Assets embed.FS

func TestEmbedMarkdownDriver(t *testing.T) {
sm := New()
sm.Use(NewMarkdownDriverWithEmbedDir(Assets, "test-sql"))
// sm.Use(sqlmanager.NewMarkdownDriverWithEmbed(Assets)) // default dir is sql
sm.Load()
sql, _ := sm.RenderTPL("test/GetStudentByID2", 1)
fmt.Println(sql)
}
31 changes: 6 additions & 25 deletions file_driver.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package sqlmanager

import (
"bytes"
"errors"
"fmt"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/parser"
"io/ioutil"
"log"
"os"
"path"
Expand Down Expand Up @@ -63,14 +62,16 @@ func (mdd *MarkdownDriver) Load() ([]SqlTemple, error) {

func parseMarkdown(filename string) ([]SqlTemple, error) {
var sqls []SqlTemple
buf, err := ioutil.ReadFile(filename)
buf, err := os.ReadFile(filename)
if err != nil {
log.Printf("sqlmanager - ERROR: %s loading failed...\n", filename)
return nil, err
}

if bytes.ContainsRune(buf, '\r') {
buf = bytes.ReplaceAll(buf, []byte{'\r'}, nil)
}
psr := parser.New()
node := markdown.Parse(buf, psr)
node := markdown.Parse(bytes.ReplaceAll(buf, []byte("\r"), nil), psr)
list := getAll(node)
i := 0
for {
Expand All @@ -97,23 +98,3 @@ func parseMarkdown(filename string) ([]SqlTemple, error) {
}
return sqls, nil
}

func getAll(node ast.Node) []item {
var list []item
ast.WalkFunc(node, func(node ast.Node, entering bool) ast.WalkStatus {
switch v := node.(type) {
case *ast.Text:
list = append(list, item{
Type: "text",
Content: string(v.Literal),
})
case *ast.CodeBlock:
list = append(list, item{
Type: "code",
Content: string(v.Literal),
})
}
return 0
})
return list
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/ystyle/sqlmanager

go 1.13
go 1.16

require github.com/gomarkdown/markdown v0.0.0-20191123064959-2c17d62f5098
require github.com/gomarkdown/markdown v0.0.0-20220310201231-552c6011c0b8
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
github.com/gomarkdown/markdown v0.0.0-20191123064959-2c17d62f5098 h1:Qxs3bNRWe8GTcKMxYOSXm0jx6j0de8XUtb/fsP3GZ0I=
github.com/gomarkdown/markdown v0.0.0-20191123064959-2c17d62f5098/go.mod h1:aii0r/K0ZnHv7G0KF7xy1v0A7s2Ljrb5byB7MO5p6TU=
golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ=
github.com/gomarkdown/markdown v0.0.0-20220310201231-552c6011c0b8 h1:YVvt637ygnOO9qjLBVmPOvrUmCz/i8YECSu/8UlOQW0=
github.com/gomarkdown/markdown v0.0.0-20220310201231-552c6011c0b8/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
25 changes: 25 additions & 0 deletions markdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sqlmanager

import (
"github.com/gomarkdown/markdown/ast"
)

func getAll(node ast.Node) []item {
var list []item
ast.WalkFunc(node, func(node ast.Node, entering bool) ast.WalkStatus {
switch v := node.(type) {
case *ast.Text:
list = append(list, item{
Type: "text",
Content: string(v.Literal),
})
case *ast.CodeBlock:
list = append(list, item{
Type: "code",
Content: string(v.Literal),
})
}
return 0
})
return list
}

0 comments on commit ea4154b

Please sign in to comment.