Skip to content

Commit

Permalink
Merge pull request doug-martin#205 from vlanse/feature/sqlserver-support
Browse files Browse the repository at this point in the history
add support for sqlserver dialect, doug-martin#197
  • Loading branch information
doug-martin authored Mar 16, 2020
2 parents 38a583f + 17531e7 commit 6cb3453
Show file tree
Hide file tree
Showing 80 changed files with 74,906 additions and 39 deletions.
19 changes: 11 additions & 8 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ on:
pull_request: # Specify a second event with pattern matching
jobs:
test:
name: Test go - ${{ matrix.go_version }} mysql - ${{ matrix.db_versions.mysql_version}} postgres - ${{ matrix.db_versions.postgres_version}}
name: Test go - ${{ matrix.go_version }} mysql - ${{ matrix.db_versions.mysql_version}} postgres - ${{ matrix.db_versions.postgres_version}} sqlserver- ${{ matrix.db_versions.sqlserver_version}}
runs-on: ubuntu-latest
strategy:
matrix:
go_version: ["1.10", "1.11", "latest"]
db_versions:
- mysql_version: 5
postgres_version: 9.6
- mysql_version: 5
postgres_version: "10.10"
- mysql_version: 8
postgres_version: 11.5
- mysql_version: 5
postgres_version: 9.6
sqlserver_version: "2017-CU8-ubuntu"
- mysql_version: 5
postgres_version: "10.10"
sqlserver_version: "2017-CU8-ubuntu"
- mysql_version: 8
postgres_version: 11.5
sqlserver_version: "2017-CU8-ubuntu"
steps:
- name: checkout
uses: actions/checkout@v1
Expand All @@ -27,7 +30,7 @@ jobs:
GO_VERSION: ${{ matrix.go_version }}
MYSQL_VERSION: ${{ matrix.db_versions.mysql_version }}
POSTGRES_VERSION: ${{ matrix.db_versions.postgres_version }}
SQLSERVER_VERSION: ${{ matrix.db_versions.sqlserver_version }}
run: docker-compose run goqu-coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1

7 changes: 5 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ linters:
- gochecknoglobals
- gochecknoinits
- dupl
- gomnd
- funlen
- wsl

run:
skip-dirs:
Expand All @@ -59,6 +62,6 @@ issues:
# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
golangci-lint-version: 1.15.x # use the fixed version to not introduce new linters unexpectedly
golangci-lint-version: 1.23.x # use the fixed version to not introduce new linters unexpectedly
prepare:
- echo "here I can run custom commands, but no preparation needed for this repo"
- echo "here I can run custom commands, but no preparation needed for this repo"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
phony:

lint:
docker run --rm -v ${CURDIR}:/app -w /app golangci/golangci-lint:v1.21.0 golangci-lint run -v
docker run --rm -v ${CURDIR}:/app -w /app golangci/golangci-lint:v1.23.8 golangci-lint run -v
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ We tried a few other sql builders but each was a thin wrapper around sql fragmen

## Docs

* [Dialect](./docs/dialect.md) - Introduction to different dialects (`mysql`, `postgres`, `sqlite3` etc)
* [Dialect](./docs/dialect.md) - Introduction to different dialects (`mysql`, `postgres`, `sqlite3`, `sqlserver` etc)
* [Expressions](./docs/expressions.md) - Introduction to `goqu` expressions and common examples.
* [Select Dataset](./docs/selecting.md) - Docs and examples about creating and executing SELECT sql statements.
* [Insert Dataset](./docs/inserting.md) - Docs and examples about creating and executing INSERT sql statements.
Expand Down Expand Up @@ -288,7 +288,7 @@ If you find an issue you want to work on please comment on it letting other peop
If want to work on an issue but dont know where to start just leave a comment and I'll be more than happy to point you in the right direction.

### Running tests
The test suite requires a postgres and mysql database. You can override the mysql/postgres connection strings with the [`MYSQL_URI` and `PG_URI` environment variables](https://github.com/doug-martin/goqu/blob/2fe3349/docker-compose.yml#L26)*
The test suite requires a postgres, mysql and sqlserver databases. You can override the connection strings with the [`MYSQL_URI`, `PG_URI`, `SQLSERVER_URI` environment variables](https://github.com/doug-martin/goqu/blob/2fe3349/docker-compose.yml#L26)*

```sh
go test -v -race ./...
Expand Down
2 changes: 1 addition & 1 deletion dialect/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func DialectOptions() *goqu.SQLDialectOptions {

opts.UseFromClauseForMultipleUpdateTables = false

opts.PlaceHolderRune = '?'
opts.PlaceHolderFragment = []byte("?")
opts.IncludePlaceholderNum = false
opts.QuoteRune = '`'
opts.DefaultValuesFragment = []byte("")
Expand Down
2 changes: 1 addition & 1 deletion dialect/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func DialectOptions() *goqu.SQLDialectOptions {
do := goqu.DefaultDialectOptions()
do.PlaceHolderRune = '$'
do.PlaceHolderFragment = []byte("$")
do.IncludePlaceholderNum = true
return do
}
Expand Down
2 changes: 1 addition & 1 deletion dialect/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func DialectOptions() *goqu.SQLDialectOptions {
opts.SupportsWindowFunction = false
opts.SupportsLateral = false

opts.PlaceHolderRune = '?'
opts.PlaceHolderFragment = []byte("?")
opts.IncludePlaceholderNum = false
opts.QuoteRune = '`'
opts.DefaultValuesFragment = []byte("")
Expand Down
90 changes: 90 additions & 0 deletions dialect/sqlserver/sqlserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package sqlserver

import (
"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/exp"
"github.com/doug-martin/goqu/v9/sqlgen"
)

func DialectOptions() *goqu.SQLDialectOptions {
opts := goqu.DefaultDialectOptions()

opts.UseLiteralIsBools = false

opts.SupportsReturn = false
opts.SupportsOrderByOnUpdate = false
opts.SupportsLimitOnUpdate = false
opts.SupportsLimitOnDelete = false
opts.SupportsOrderByOnDelete = true
opts.SupportsConflictUpdateWhere = false
opts.SupportsInsertIgnoreSyntax = false
opts.SupportsConflictTarget = false
opts.SupportsWithCTE = false
opts.SupportsWithCTERecursive = false
opts.SupportsDistinctOn = false
opts.SupportsWindowFunction = false

opts.PlaceHolderFragment = []byte("@p")
opts.LimitFragment = []byte(" TOP ")
opts.IncludePlaceholderNum = true
opts.DefaultValuesFragment = []byte("")
opts.True = []byte("1")
opts.False = []byte("0")
opts.TimeFormat = "2006-01-02 15:04:05"
opts.BooleanOperatorLookup = map[exp.BooleanOperation][]byte{
exp.EqOp: []byte("="),
exp.NeqOp: []byte("!="),
exp.GtOp: []byte(">"),
exp.GteOp: []byte(">="),
exp.LtOp: []byte("<"),
exp.LteOp: []byte("<="),
exp.InOp: []byte("IN"),
exp.NotInOp: []byte("NOT IN"),
exp.IsOp: []byte("="),
exp.IsNotOp: []byte("IS NOT"),
exp.LikeOp: []byte("LIKE"),
exp.NotLikeOp: []byte("NOT LIKE"),
exp.ILikeOp: []byte("LIKE"),
exp.NotILikeOp: []byte("NOT LIKE"),
exp.RegexpLikeOp: []byte("REGEXP BINARY"),
exp.RegexpNotLikeOp: []byte("NOT REGEXP BINARY"),
exp.RegexpILikeOp: []byte("REGEXP"),
exp.RegexpNotILikeOp: []byte("NOT REGEXP"),
}

opts.FetchFragment = []byte(" FETCH FIRST ")

opts.SelectSQLOrder = []sqlgen.SQLFragmentType{
sqlgen.CommonTableSQLFragment,
sqlgen.SelectWithLimitSQLFragment,
sqlgen.FromSQLFragment,
sqlgen.JoinSQLFragment,
sqlgen.WhereSQLFragment,
sqlgen.GroupBySQLFragment,
sqlgen.HavingSQLFragment,
sqlgen.WindowSQLFragment,
sqlgen.CompoundsSQLFragment,
sqlgen.OrderWithOffsetFetchSQLFragment,
sqlgen.ForSQLFragment,
}

opts.EscapedRunes = map[rune][]byte{
'\'': []byte("\\'"),
'"': []byte("\\\""),
'\\': []byte("\\\\"),
'\n': []byte("\\n"),
'\r': []byte("\\r"),
0: []byte("\\x00"),
0x1a: []byte("\\x1a"),
}

opts.ConflictFragment = []byte("")
opts.ConflictDoUpdateFragment = []byte("")
opts.ConflictDoNothingFragment = []byte("")

return opts
}

func init() {
goqu.RegisterDialect("sqlserver", DialectOptions())
}
Loading

0 comments on commit 6cb3453

Please sign in to comment.