Created
October 5, 2017 09:49
-
-
Save monirz/687c738ffcc76fe8abe3f79c6517cd84 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| type SQLBuilder interface { | |
| Select() SQLBuilder | |
| Star() SQLBuilder | |
| From(table string) SQLBuilder | |
| Where(condition string) SQLBuilder | |
| Build() string | |
| } | |
| type builder struct { | |
| s string | |
| } | |
| func (b *builder) Select() SQLBuilder { | |
| b.s = b.s + "SELECT " | |
| return b | |
| } | |
| func (b *builder) Star() SQLBuilder { | |
| b.s = b.s + " * " | |
| return b | |
| } | |
| func (b *builder) From(table string) SQLBuilder { | |
| b.s = b.s + " FROM " + table + " " | |
| return b | |
| } | |
| func (b *builder) Where(condition string) SQLBuilder { | |
| b.s = b.s + " WHERE " + condition + " " | |
| return b | |
| } | |
| func (b *builder) Build() string { | |
| return b.s | |
| } | |
| func main() { | |
| b := &builder{} | |
| s := b.Select().Star().From("foo") | |
| fmt.Println(s) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment