From 98d16250b48a47bb5d273e62611ff28cffbf3809 Mon Sep 17 00:00:00 2001 From: Samuel N Cui Date: Tue, 3 Oct 2023 23:09:23 +0800 Subject: [PATCH] feat: add cross compile --- .github/workflows/build.yml | 104 ++++++++++++++++++++++++++++++------ build.sh | 2 +- go.mod | 13 ++++- go.sum | 34 ++++++++---- resource/db.go | 29 ++++++---- resource/sqlite.go | 12 +++++ resource/sqlite_cgo.go | 12 +++++ 7 files changed, 167 insertions(+), 39 deletions(-) create mode 100644 resource/sqlite.go create mode 100644 resource/sqlite_cgo.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 991a7a2..134c016 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,11 +11,92 @@ permissions: pull-requests: read jobs: - build: - runs-on: ubuntu-20.04 + build-linux-amd64: + strategy: + matrix: + # Include amd64 on all platforms. remove windows for mount mechanism + goos: [freebsd, openbsd, linux, darwin] + goarch: [amd64, 386] + + exclude: + # Exclude i386 on darwin. + - goarch: 386 + goos: darwin + include: + # BEGIN Linux ARM 5 6 7 + - goos: linux + goarch: arm + goarm: 7 + - goos: linux + goarch: arm + goarm: 6 + - goos: linux + goarch: arm + goarm: 5 + # END Linux ARM 5 6 7 + # BEGIN Windows ARM 7 + # - goos: windows + # goarch: arm + # goarm: 7 + # END Windows ARM 7 + # BEGIN FreeBSD ARM 6 7 + - goos: freebsd + goarch: arm + goarm: 6 + - goos: freebsd + goarch: arm + goarm: 7 + # END FreeBSD ARM 6 7 + # BEGIN OpenBSD ARM 6 7 + - goos: openbsd + goarch: arm + goarm: 6 + - goos: openbsd + goarch: arm + goarm: 7 + # END OpenBSD ARM 6 7 + # BEGIN Other architectures + - goos: darwin + goarch: arm64 + - goos: linux + goarch: arm64 + - goos: linux + goarch: riscv64 + - goos: linux + goarch: loong64 + # - goos: windows + # goarch: arm64 + - goos: android + goarch: arm64 + - goos: freebsd + goarch: arm64 + - goos: openbsd + goarch: arm64 + # BEGIN MIPS + - goos: linux + goarch: mips64 + - goos: linux + goarch: mips64le + - goos: linux + goarch: mipsle + - goos: linux + goarch: mips + # END MIPS + # END Other architectures + fail-fast: false + + runs-on: ubuntu-latest + env: + RELEASE_VERSION: ${{ github.event.release.tag_name }} + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + GOARM: ${{ matrix.goarm }} + EXPERIMENTAL: ${{ matrix.goos == 'linux' && matrix.goarch == 'amd64' && '' || '-experimental' }} + CGO_ENABLED: 0 + steps: - name: Set env - run: echo "RELEASE_VERSION=${{ github.event.release.tag_name }}" >> $GITHUB_ENV + run: echo "TARGET_NAME='${{ env.GOOS }}-${{ env.GOARCH }}${{ env.GOARM }}${{ env.EXPERIMENTAL }}'" >> $GITHUB_ENV - uses: actions/checkout@v4 with: @@ -24,7 +105,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.20' + go-version: '1.21' - name: Setup Node.js environment uses: actions/setup-node@v3.8.1 @@ -40,17 +121,6 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ github.event.release.upload_url }} - asset_path: ./yatm-linux-amd64-${{ env.RELEASE_VERSION }}.tar.gz - asset_name: yatm-linux-amd64-${{ env.RELEASE_VERSION }}.tar.gz + asset_path: ./yatm-${{ env.TARGET_NAME }}-${{ env.RELEASE_VERSION }}.tar.gz + asset_name: yatm-${{ env.TARGET_NAME }}-${{ env.RELEASE_VERSION }}.tar.gz asset_content_type: application/x-tgz - - # - uses: "marvinpinto/action-automatic-releases@latest" - # with: - # repo_token: "${{ secrets.GITHUB_TOKEN }}" - # automatic_release_tag: "${{ env.RELEASE_VERSION }}" - # prerelease: false - # title: "Automatic Build" - # files: | - # LICENSE - # README.md - # yatm-linux-amd64-${{ env.RELEASE_VERSION }}.tar.gz diff --git a/build.sh b/build.sh index f6f6aa1..3101b6d 100755 --- a/build.sh +++ b/build.sh @@ -4,7 +4,7 @@ set -ex; CURDIR=$(cd $(dirname $0); pwd); cd ${CURDIR}; -export TARGET_FILE="yatm-linux-amd64-${RELEASE_VERSION}.tar.gz" +export TARGET_FILE="yatm-${TARGET_NAME}-${RELEASE_VERSION}.tar.gz" rm -rf output; mkdir -p output; diff --git a/go.mod b/go.mod index f947991..a7293d8 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/davecgh/go-spew v1.1.1 github.com/deckarep/golang-set/v2 v2.3.1 github.com/gin-gonic/gin v1.9.0 + github.com/glebarez/sqlite v1.9.0 github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.5 github.com/hashicorp/go-multierror v1.1.1 github.com/improbable-eng/grpc-web v0.15.0 @@ -25,7 +26,7 @@ require ( gopkg.in/yaml.v2 v2.4.0 gorm.io/driver/mysql v1.3.6 gorm.io/driver/sqlite v1.3.6 - gorm.io/gorm v1.23.8 + gorm.io/gorm v1.25.2 ) require ( @@ -33,13 +34,16 @@ require ( github.com/cenkalti/backoff/v4 v4.1.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect + github.com/dustin/go-humanize v1.0.1 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/glebarez/go-sqlite v1.21.2 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.11.2 // indirect github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/goccy/go-json v0.10.0 // indirect github.com/golang/protobuf v1.5.2 // indirect + github.com/google/uuid v1.3.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect @@ -49,13 +53,14 @@ require ( github.com/lestrrat-go/strftime v1.0.6 // indirect github.com/mattn/go-isatty v0.0.17 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect - github.com/mattn/go-sqlite3 v1.14.12 // indirect + github.com/mattn/go-sqlite3 v1.14.16 // indirect github.com/minio/sha256-simd v1.0.1 // indirect github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/moby/sys/mountinfo v0.6.2 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/rivo/uniseg v0.4.4 // indirect github.com/rs/cors v1.7.0 // indirect github.com/samuelncui/godf v0.0.0-20230927093204-37ea5acb9fc1 // indirect @@ -71,5 +76,9 @@ require ( golang.org/x/text v0.8.0 // indirect google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + modernc.org/libc v1.22.5 // indirect + modernc.org/mathutil v1.5.0 // indirect + modernc.org/memory v1.5.0 // indirect + modernc.org/sqlite v1.23.1 // indirect nhooyr.io/websocket v1.8.6 // indirect ) diff --git a/go.sum b/go.sum index cc855a7..2c8c61d 100644 --- a/go.sum +++ b/go.sum @@ -63,6 +63,8 @@ github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f h1:U5y3Y5UE0w7amNe github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f/go.mod h1:xH/i4TFMt8koVQZ6WFms69WAsDWr2XsYL3Hkl7jkoLE= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= @@ -82,6 +84,10 @@ github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= github.com/gin-gonic/gin v1.9.0 h1:OjyFBKICoexlu99ctXNR2gg+c5pKrKMuyjgARg9qeY8= github.com/gin-gonic/gin v1.9.0/go.mod h1:W1Me9+hsUSyj3CePGrd1/QrKJMSJ1Tu/0hFEH89961k= +github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo= +github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k= +github.com/glebarez/sqlite v1.9.0 h1:Aj6bPA12ZEx5GbSF6XADmCkYXlljPNUY+Zf1EQxynXs= +github.com/glebarez/sqlite v1.9.0/go.mod h1:YBYCoyupOao60lzp1MVBLEjZfgkq0tdB1voAQ09K9zw= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= @@ -147,8 +153,11 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= @@ -252,8 +261,9 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0= github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= @@ -341,6 +351,9 @@ github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+Gx github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.3.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= +github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 h1:mZHayPoR0lNmnHyvtYjDeq0zlVHn9K/ZXoy17ylucdo= github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5/go.mod h1:GEXHk5HgEKCvEIIrSpFI3ozzG5xOKA2DVlEX/gGnewM= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= @@ -356,14 +369,6 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= -github.com/samuelncui/acp v0.0.0-20230927101214-786cd32f8d38 h1:TYVXiMTG04wAWZy1mPdEwqd75FeYJoEZYpCUuiLopw8= -github.com/samuelncui/acp v0.0.0-20230927101214-786cd32f8d38/go.mod h1:HDBJGNFN6yd3kWuCU5eKaCICvmCwVWb6AzFS+wSKyWQ= -github.com/samuelncui/acp v0.0.0-20230927173814-44b54705fc4b h1:wfi5H9nbag1rnUM5wVr/E7apVsvznbZu8TwZsQr8yOQ= -github.com/samuelncui/acp v0.0.0-20230927173814-44b54705fc4b/go.mod h1:HDBJGNFN6yd3kWuCU5eKaCICvmCwVWb6AzFS+wSKyWQ= -github.com/samuelncui/acp v0.0.0-20230927193628-457f88d5268d h1:/xwkO9zlY8TMcG+asORTXWEqKY9tD4wEx4kb3q/7TNY= -github.com/samuelncui/acp v0.0.0-20230927193628-457f88d5268d/go.mod h1:HDBJGNFN6yd3kWuCU5eKaCICvmCwVWb6AzFS+wSKyWQ= -github.com/samuelncui/acp v0.0.0-20230928143329-dd07ebc94c58 h1:Mgc3xitaiqsbL6hNEUzic5JCESmEQ3Ll+KdJEwMniGs= -github.com/samuelncui/acp v0.0.0-20230928143329-dd07ebc94c58/go.mod h1:HDBJGNFN6yd3kWuCU5eKaCICvmCwVWb6AzFS+wSKyWQ= github.com/samuelncui/acp v0.0.0-20230929123032-b9f8584ad50c h1:xJVq1UOaqjI3JVGUQvT+w6584UdEBGzxy7WN8XXuSnk= github.com/samuelncui/acp v0.0.0-20230929123032-b9f8584ad50c/go.mod h1:HDBJGNFN6yd3kWuCU5eKaCICvmCwVWb6AzFS+wSKyWQ= github.com/samuelncui/godf v0.0.0-20230927093204-37ea5acb9fc1 h1:K2m4b66nzupWlkfUPJKIw2tgz4aDociv5XwtlynwbzI= @@ -617,12 +622,21 @@ gorm.io/driver/mysql v1.3.6/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10 gorm.io/driver/sqlite v1.3.6 h1:Fi8xNYCUplOqWiPa3/GuCeowRNBRGTf62DEmhMDHeQQ= gorm.io/driver/sqlite v1.3.6/go.mod h1:Sg1/pvnKtbQ7jLXxfZa+jSHvoX8hoZA8cn4xllOMTgE= gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= -gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE= gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.25.2 h1:gs1o6Vsa+oVKG/a9ElL3XgyGfghFfkKA2SInQaCyMho= +gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= +modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE= +modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY= +modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ= +modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= +modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds= +modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU= +modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM= +modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk= nhooyr.io/websocket v1.8.6 h1:s+C3xAMLwGmlI31Nyn/eAehUlZPwfYZu2JXM621Q5/k= nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/resource/db.go b/resource/db.go index 5912fb9..9ae2edf 100644 --- a/resource/db.go +++ b/resource/db.go @@ -1,8 +1,9 @@ package resource import ( + "fmt" + "gorm.io/driver/mysql" - "gorm.io/driver/sqlite" "gorm.io/gorm" ) @@ -12,10 +13,26 @@ func NewDBConn(dialect, dsn string) (*gorm.DB, error) { case "mysql": dialector = mysql.Open(dsn) case "sqlite": - dialector = sqlite.Open(dsn) + dialector = openSQLite(dsn) + } + + db, err := gorm.Open(dialector) + if err != nil { + return nil, fmt.Errorf("new db conn fail, dialect= '%s' dsn= '%s', %w", dialect, dsn, err) + } + + switch dialect { + case "sqlite": + sqlDB, err := db.DB() + if err != nil { + return nil, fmt.Errorf("sqlite set config fail, dialect= '%s' dsn= '%s', %w", dialect, dsn, err) + } + + // Prevent "database locked" errors + sqlDB.SetMaxOpenConns(1) } - return gorm.Open(dialector) + return db, nil } func SQLEscape(sql string) string { @@ -29,22 +46,16 @@ func SQLEscape(sql string) string { switch c { case 0: /* Must be escaped for 'mysql' */ escape = '0' - break case '\n': /* Must be escaped for logs */ escape = 'n' - break case '\r': escape = 'r' - break case '\\': escape = '\\' - break case '\'': escape = '\'' - break case '"': /* Better safe than sorry */ escape = '"' - break case '\032': //十进制26,八进制32,十六进制1a, /* This gives problems on Win32 */ escape = 'Z' } diff --git a/resource/sqlite.go b/resource/sqlite.go new file mode 100644 index 0000000..1357027 --- /dev/null +++ b/resource/sqlite.go @@ -0,0 +1,12 @@ +//go:build (darwin && amd64) || (darwin && arm64) || (freebsd && amd64) || (linux && arm) || (linux && arm64) || (linux && 386) || (linux && amd64) || (linux && s390x) || (windows && amd64) + +package resource + +import ( + "github.com/glebarez/sqlite" + "gorm.io/gorm" +) + +func openSQLite(dsn string) gorm.Dialector { + return sqlite.Open(dsn) +} diff --git a/resource/sqlite_cgo.go b/resource/sqlite_cgo.go new file mode 100644 index 0000000..9348d73 --- /dev/null +++ b/resource/sqlite_cgo.go @@ -0,0 +1,12 @@ +//go:build !((darwin && amd64) || (darwin && arm64) || (freebsd && amd64) || (linux && arm) || (linux && arm64) || (linux && 386) || (linux && amd64) || (linux && s390x) || (windows && amd64)) + +package resource + +import ( + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +func openSQLite(dsn string) gorm.Dialector { + return sqlite.Open(dsn) +}