From dbab50ee51e59ce98caefdc71c6d25dcfe12ac32 Mon Sep 17 00:00:00 2001 From: Ilya Sinelnikov Date: Thu, 9 Apr 2020 14:15:34 +0300 Subject: [PATCH] lectures: 07-sql draft v2 --- go.mod | 6 +- go.sum | 98 +++++++++++++++++++++++++++ lectures/07-sql/alive/ping.go | 22 +++++- lectures/07-sql/conn/conn.go | 22 ++++-- lectures/07-sql/exec/exec.go | 26 ++++--- lectures/07-sql/exec/result.go | 6 +- lectures/07-sql/named/insert.go | 21 ++++-- lectures/07-sql/named/named.go | 6 +- lectures/07-sql/nulls/insert.go | 28 ++++++++ lectures/07-sql/nulls/insert_null.go | 6 -- lectures/07-sql/nulls/insert_value.go | 6 -- lectures/07-sql/nulls/nulls.go | 6 +- lectures/07-sql/nulls/rows.go | 36 +++++++--- lectures/07-sql/open/pgx.go | 18 +++-- lectures/07-sql/open/sql.go | 19 ++++-- lectures/07-sql/prepare/prepare.go | 41 ++++++----- lectures/07-sql/query/query.go | 40 +++++++---- lectures/07-sql/query/queryrow.go | 33 ++++++--- lectures/07-sql/sql.slide | 81 ++++++++++++++-------- lectures/07-sql/sql/driver.go | 11 ++- lectures/07-sql/sqlx/example.go | 21 ------ lectures/07-sql/sqlx/ext.go | 13 +++- lectures/07-sql/sqlx/named.go | 21 ++++++ lectures/07-sql/sqlx/open.go | 18 +++-- lectures/07-sql/sqlx/structscan.go | 33 ++++++++- lectures/07-sql/tx/tx.go | 30 +++++--- 26 files changed, 489 insertions(+), 179 deletions(-) create mode 100644 lectures/07-sql/nulls/insert.go delete mode 100644 lectures/07-sql/nulls/insert_null.go delete mode 100644 lectures/07-sql/nulls/insert_value.go delete mode 100644 lectures/07-sql/sqlx/example.go create mode 100644 lectures/07-sql/sqlx/named.go diff --git a/go.mod b/go.mod index 069904d..da344ba 100644 --- a/go.mod +++ b/go.mod @@ -10,10 +10,12 @@ require ( github.com/gorilla/handlers v1.4.2 github.com/gorilla/mux v1.7.4 github.com/gorilla/websocket v1.4.2 + github.com/jackc/pgx/v4 v4.6.0 + github.com/jmoiron/sqlx v1.2.0 github.com/spf13/cobra v0.0.5 - github.com/stretchr/testify v1.4.0 + github.com/stretchr/testify v1.5.1 go.uber.org/goleak v1.0.0 - golang.org/x/net v0.0.0-20190628185345-da137c7871d7 + golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 golang.org/x/perf v0.0.0-20191209155426-36b577b0eb03 golang.org/x/sync v0.0.0-20190423024810-112230192c58 golang.org/x/tools v0.0.0-20200125223703-d33eef8e6825 diff --git a/go.sum b/go.sum index e7f6555..227bc5a 100644 --- a/go.sum +++ b/go.sum @@ -4,17 +4,23 @@ github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20190129172621-c8b1d7a94ddf github.com/aclements/go-gg v0.0.0-20170118225347-6dbb4e4fefb0/go.mod h1:55qNq4vcpkIuHowELi5C8e+1yUHtoLoOUR9QU5j7Tes= github.com/aclements/go-moremath v0.0.0-20161014184102-0ff62e0875ff/go.mod h1:idZL3yvz4kzx1dsBOAC+oYv6L92P1oFEhUXUB1A/lwQ= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= +github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/go-resty/resty/v2 v2.1.0 h1:Z6IefCpUMfnvItVJaJXWv/pMiiD11So35QgwEELsldE= github.com/go-resty/resty/v2 v2.1.0/go.mod h1:dZGr0i9PLlaaTD4H/hoZIDjQ+r6xq8mgbRzHZf7f2J8= +github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE= github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/golang/mock v1.4.1 h1:ocYkMQY5RrXTYgXl7ICpV0IXwlEQGwKIsery4gyXa1U= @@ -37,19 +43,82 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0= +github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= +github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= +github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= +github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= +github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3/go.mod h1:jkELnwuX+w9qN5YIfX0fl88Ehu4XC3keFuOJJk9pcnA= +github.com/jackc/pgconn v0.0.0-20190824142844-760dd75542eb/go.mod h1:lLjNuW/+OfW9/pnVKPazfWOgNfH2aPem8YQ7ilXGvJE= +github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsUgOEh9hBm+xYTstcNHg7UPMVJqRfQxq4s= +github.com/jackc/pgconn v1.5.0 h1:oFSOilzIZkyg787M1fEmyMfOUUvwj0daqYMfaWwNL4o= +github.com/jackc/pgconn v1.5.0/go.mod h1:QeD3lBfpTFe8WUnPZWN5KY/mB8FGMIYRdd8P8Jr0fAI= +github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= +github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= +github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A= +github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78= +github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA= +github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg= +github.com/jackc/pgproto3/v2 v2.0.0-rc3/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= +github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= +github.com/jackc/pgproto3/v2 v2.0.1 h1:Rdjp4NFjwHnEslx2b66FfCI2S0LhO4itac3hXz6WX9M= +github.com/jackc/pgproto3/v2 v2.0.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= +github.com/jackc/pgservicefile v0.0.0-20200307190119-3430c5407db8 h1:Q3tB+ExeflWUW7AFcAhXqk40s9mnNYLk1nOkKNZ5GnU= +github.com/jackc/pgservicefile v0.0.0-20200307190119-3430c5407db8/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= +github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= +github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc= +github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw= +github.com/jackc/pgtype v1.3.0 h1:l8JvKrby3RI7Kg3bYEeU9TA4vqC38QDpFCfcrC7KuN0= +github.com/jackc/pgtype v1.3.0/go.mod h1:b0JqxHvPmljG+HQ5IsvQ0yqeSi4nGcDTVjFoiLDb0Ik= +github.com/jackc/pgx v3.6.2+incompatible h1:2zP5OD7kiyR3xzRYMhOcXVvkDZsImVXfj+yIyTQf3/o= +github.com/jackc/pgx v3.6.2+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I= +github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y= +github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM= +github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc= +github.com/jackc/pgx/v4 v4.6.0 h1:Fh0O9GdlG4gYpjpwOqjdEodJUQM9jzN3Hdv7PN0xmm0= +github.com/jackc/pgx/v4 v4.6.0/go.mod h1:vPh43ZzxijXUVJ+t/EmXBtFmbFVO72cuneCT9oAlxAg= +github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA= +github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-sqlite3 v0.0.0-20161215041557-2d44decb4941/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= +github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s= @@ -60,16 +129,30 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= +go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/goleak v1.0.0 h1:qsup4IcBdlmsnGfqyLl4Ntn3C2XCCuKAE7DwHpScyUo= go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= +golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 h1:3zb4D3T4G8jdExgVU/95+vQXfpEPiMdCaZgmGVxjNHM= +golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= @@ -80,22 +163,36 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwL golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190628185345-da137c7871d7 h1:rTIdg5QFRR7XCaK4LCjBiPbx8j4DQRpdYMnGn/bJUEU= golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20170207211851-4464e7848382/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/perf v0.0.0-20191209155426-36b577b0eb03 h1:KBM1Z6efANgwwa1Rns+b3KRyuKhLRVC/OeHhFMFJqOA= golang.org/x/perf v0.0.0-20191209155426-36b577b0eb03/go.mod h1:FrqOtQDO3iMDVUtw5nNTDFpR1HUCGh00M3kj2wiSzLQ= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190823170909-c4a336ef6a2f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200125223703-d33eef8e6825 h1:aNQeSIHKi0RWpKA5NO0CqyLjx6Beh5l0LLUEnndEjz0= golang.org/x/tools v0.0.0-20200125223703-d33eef8e6825/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -108,6 +205,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= diff --git a/lectures/07-sql/alive/ping.go b/lectures/07-sql/alive/ping.go index b4871de..3b259d2 100644 --- a/lectures/07-sql/alive/ping.go +++ b/lectures/07-sql/alive/ping.go @@ -1,3 +1,21 @@ -if err := db.Ping(ctx); err != nil { - // do something with error +package alive + +import ( + "context" + "database/sql" + "log" + + _ "github.com/jackc/pgx/v4/stdlib" +) + +func IsItAliveQuestionMark() { + db, err := sql.Open("pgx", "postgres://pgx_md5:secret@localhost:5432/pgx_test") + if err != nil { + log.Fatal(err) + } + defer db.Close() + + if err := db.PingContext(context.Background()); err != nil { + log.Fatal(err) + } } diff --git a/lectures/07-sql/conn/conn.go b/lectures/07-sql/conn/conn.go index 631dcdc..14095c8 100644 --- a/lectures/07-sql/conn/conn.go +++ b/lectures/07-sql/conn/conn.go @@ -1,7 +1,17 @@ -c, err := db.Conn(ctx) -if err != nil { - log.Fatal(err) -} +package query -_ := c.PingContext(ctx) -_, _ := c.QueryContext(...) +import ( + "context" + "database/sql" + "log" +) + +func Conn(ctx context.Context, db *sql.DB) { + c, err := db.Conn(ctx) + if err != nil { + log.Fatal(err) + } + defer c.Close() + + _ = c.PingContext(ctx) +} diff --git a/lectures/07-sql/exec/exec.go b/lectures/07-sql/exec/exec.go index 3d82c13..2c56532 100644 --- a/lectures/07-sql/exec/exec.go +++ b/lectures/07-sql/exec/exec.go @@ -1,9 +1,19 @@ -res, err := db.ExecContext(ctx, "UPDATE users SET name = $1 WHERE id = $2", "William Mandella", 1) -if err != nil { - log.Fatal(err) +package exec + +import ( + "context" + "database/sql" + "log" +) + +func Exec(ctx context.Context, db *sql.DB) { + res, err := db.ExecContext(ctx, "UPDATE users SET name = $1 WHERE id = $2", "William Mandella", 1) + if err != nil { + log.Fatal(err) + } + + lastID, _ := res.LastInsertId() + rowsAffected, _ := res.RowsAffected() + + log.Println(lastID, rowsAffected) } - -lastID, _ := res.LastInsertId() -rowsAffeced, := res.RowsAffected() - -log.Println(lastID, rowsAffected) diff --git a/lectures/07-sql/exec/result.go b/lectures/07-sql/exec/result.go index 273cf1f..72c1b26 100644 --- a/lectures/07-sql/exec/result.go +++ b/lectures/07-sql/exec/result.go @@ -1,4 +1,6 @@ +package exec + type Result interface { - LastInsertId() (int64, error) - RowsAffected() (int64, error) + LastInsertId() (int64, error) + RowsAffected() (int64, error) } diff --git a/lectures/07-sql/named/insert.go b/lectures/07-sql/named/insert.go index 1b56b4f..b6e02c1 100644 --- a/lectures/07-sql/named/insert.go +++ b/lectures/07-sql/named/insert.go @@ -1,5 +1,18 @@ -db.ExecContext( - ctx, - "INSERT INTO users(name) VALUES(@name)"), - sql.Named("name", "Rocinante"), +package named + +import ( + "context" + "database/sql" + "log" ) + +func Insert(ctx context.Context, db *sql.DB) { + _, err := db.ExecContext( + ctx, + "INSERT INTO users(name) VALUES(@name)", + sql.Named("name", "Amos Burton"), + ) + if err != nil { + log.Fatal(err) + } +} diff --git a/lectures/07-sql/named/named.go b/lectures/07-sql/named/named.go index 1b05c5f..fecd13b 100644 --- a/lectures/07-sql/named/named.go +++ b/lectures/07-sql/named/named.go @@ -1,4 +1,6 @@ +package named + type NamedArg struct { - Name string - Value interface{} + Name string + Value interface{} } diff --git a/lectures/07-sql/nulls/insert.go b/lectures/07-sql/nulls/insert.go new file mode 100644 index 0000000..049b6bf --- /dev/null +++ b/lectures/07-sql/nulls/insert.go @@ -0,0 +1,28 @@ +package nulls + +import ( + "context" + "database/sql" + "log" +) + +func Insert(ctx context.Context, db *sql.DB, name interface{}) { + _, err := db.ExecContext( + ctx, + "INSERT INTO users(name) VALUES(@name)", + sql.Named("name", name), + ) + if err != nil { + log.Fatal(err) + } +} + +func DoStuff(ctx context.Context, db *sql.DB) { + // Nulls + Insert(ctx, db, nil) + Insert(ctx, db, sql.NullString{}) + + // Values + Insert(ctx, db, "The Shrike") + Insert(ctx, db, sql.NullString{String: "The Shrike", Valid: true}) +} diff --git a/lectures/07-sql/nulls/insert_null.go b/lectures/07-sql/nulls/insert_null.go deleted file mode 100644 index 6cb24c8..0000000 --- a/lectures/07-sql/nulls/insert_null.go +++ /dev/null @@ -1,6 +0,0 @@ -var name sql.NullString -db.ExecContext( - ctx, - "INSERT INTO users(name) VALUES(@name)"), - sql.Named("name", name), -) diff --git a/lectures/07-sql/nulls/insert_value.go b/lectures/07-sql/nulls/insert_value.go deleted file mode 100644 index 422c6da..0000000 --- a/lectures/07-sql/nulls/insert_value.go +++ /dev/null @@ -1,6 +0,0 @@ -name := sql.NullString{Value: "The Shrike",Valid: true} -db.ExecContext( - ctx, - "INSERT INTO users(name) VALUES(@name)"), - sql.Named("name", name), -) diff --git a/lectures/07-sql/nulls/nulls.go b/lectures/07-sql/nulls/nulls.go index d239508..7b69302 100644 --- a/lectures/07-sql/nulls/nulls.go +++ b/lectures/07-sql/nulls/nulls.go @@ -1,4 +1,6 @@ +package nulls + type NullString struct { - String string - Valid bool + String string + Valid bool } diff --git a/lectures/07-sql/nulls/rows.go b/lectures/07-sql/nulls/rows.go index fe810b1..7597557 100644 --- a/lectures/07-sql/nulls/rows.go +++ b/lectures/07-sql/nulls/rows.go @@ -1,12 +1,28 @@ -for rows.Next() { - var s sql.NullString - if err := rows.Scan(&s); err != nil { - log.Fatal(err) - } +package nulls - if s.Valid { - // - } else { - // - } +import ( + "context" + "database/sql" + "log" +) + +func Results(ctx context.Context, db *sql.DB) { + rows, err := db.QueryContext(ctx, "SELECT id, name FROM users WHERE id = $1", 1) + if err != nil { + log.Fatal(err) + } + defer rows.Close() + + for rows.Next() { + var s sql.NullString + if err := rows.Scan(&s); err != nil { + log.Fatal(err) + } + + if s.Valid { + // + } else { + // + } + } } diff --git a/lectures/07-sql/open/pgx.go b/lectures/07-sql/open/pgx.go index 259020e..b71557b 100644 --- a/lectures/07-sql/open/pgx.go +++ b/lectures/07-sql/open/pgx.go @@ -1,7 +1,17 @@ -import ( - "context" +package open - "github.com/jackc/pgx/v4" +import ( + "context" + "log" + + "github.com/jackc/pgx/v4" ) -conn, err := pgx.Connect(context.Background(), "postgres://pgx_md5:secret@localhost:5432/pgx_test") +func PGXOpen() { + ctx := context.Background() + conn, err := pgx.Connect(ctx, "postgres://pgx_md5:secret@localhost:5432/pgx_test") + if err != nil { + log.Fatal(err) + } + defer conn.Close(ctx) +} diff --git a/lectures/07-sql/open/sql.go b/lectures/07-sql/open/sql.go index bd41aa6..94ff648 100644 --- a/lectures/07-sql/open/sql.go +++ b/lectures/07-sql/open/sql.go @@ -1,11 +1,16 @@ -import ( - "database/sql" +package open - _ "github.com/jackc/pgx/v4/stdlib" +import ( + "database/sql" + "log" + + _ "github.com/jackc/pgx/v4/stdlib" ) -db, err := sql.Open("pgx", "postgres://pgx_md5:secret@localhost:5432/pgx_test") -if err != nil { - return err +func SQLOpen() { + db, err := sql.Open("pgx", "postgres://pgx_md5:secret@localhost:5432/pgx_test") + if err != nil { + log.Fatal(err) + } + defer db.Close() } -defer db.Close() diff --git a/lectures/07-sql/prepare/prepare.go b/lectures/07-sql/prepare/prepare.go index 98c126d..fb92e97 100644 --- a/lectures/07-sql/prepare/prepare.go +++ b/lectures/07-sql/prepare/prepare.go @@ -1,19 +1,24 @@ -stmt, err := db.PrepareContext(ctx, "SELECT name FROM users WHERE id = $1") -if err != nil { - log.Fatal(err) -} -defer stmt.Close() - -for i := 1; ; i++ { - row, err := stmt.QueryRowContext(ctx, i) - if err != nil { - log.Fatal(err) - } - - var name string - if err = row.Scan(&name); err != nil { - log.Fatal(err) - } - - log.Println(name) +package prepare + +import ( + "context" + "database/sql" + "log" +) + +func Prepare(ctx context.Context, db *sql.DB) { + stmt, err := db.PrepareContext(ctx, "SELECT name FROM users WHERE id = $1") + if err != nil { + log.Fatal(err) + } + defer stmt.Close() + + for i := 1; ; i++ { + var name string + if err = stmt.QueryRowContext(ctx, i).Scan(&name); err != nil { + log.Fatal(err) + } + + log.Println(name) + } } diff --git a/lectures/07-sql/query/query.go b/lectures/07-sql/query/query.go index 576f9a9..7dac48b 100644 --- a/lectures/07-sql/query/query.go +++ b/lectures/07-sql/query/query.go @@ -1,19 +1,29 @@ -rows, err := db.QueryContext(ctx, "SELECT id, name FROM users WHERE id = $1", 1) -if err != nil { - log.Fatal(err) -} -defer rows.Close() +package query -for rows.Next() { - var id int - var name string - if err := rows.Scan(&id, &name); err != nil { - log.Fatal(err) - } +import ( + "context" + "database/sql" + "log" +) - log.Println(id, name) -} +func Query(ctx context.Context, db *sql.DB) { + rows, err := db.QueryContext(ctx, "SELECT id, name FROM users WHERE id = $1", 1) + if err != nil { + log.Fatal(err) + } + defer rows.Close() -if err = rows.Err(); err != nil { - log.Fatal(err) + for rows.Next() { + var id int + var name string + if err := rows.Scan(&id, &name); err != nil { + log.Fatal(err) + } + + log.Println(id, name) + } + + if err = rows.Err(); err != nil { + log.Fatal(err) + } } diff --git a/lectures/07-sql/query/queryrow.go b/lectures/07-sql/query/queryrow.go index 1a6fe3d..0128b04 100644 --- a/lectures/07-sql/query/queryrow.go +++ b/lectures/07-sql/query/queryrow.go @@ -1,13 +1,24 @@ -var id int -var name string -err = db.QueryRowContext(ctx, "SELECT id, name FROM users WHERE id = $1", 1).Scan(&id, &name); -if err != nil { - if errors.Is(err, sql.ErrNoRows) { - log.Println("nothing found") - return - } +package query - log.Fatal(err) +import ( + "context" + "database/sql" + "errors" + "log" +) + +func QueryRow(ctx context.Context, db *sql.DB) { + var id int + var name string + err := db.QueryRowContext(ctx, "SELECT id, name FROM users WHERE id = $1", 1).Scan(&id, &name) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + log.Println("nothing found") + return + } + + log.Fatal(err) + } + + log.Println(name) } - -log.Println(name) diff --git a/lectures/07-sql/sql.slide b/lectures/07-sql/sql.slide index 36711ca..8489d7b 100644 --- a/lectures/07-sql/sql.slide +++ b/lectures/07-sql/sql.slide @@ -20,10 +20,10 @@ database/sql * Подключение - через database/sql -.code open/sql.go +.code open/sql.go /^func SQLOpen/,/^}/ - драйвером на прямую -.code open/pgx.go +.code open/pgx.go /^func PGXOpen/,/^}/ * Основные сущности @@ -37,45 +37,45 @@ database/sql * Проверка доступности -.code alive/ping.go +.code alive/ping.go /^func IsItAliveQuestionMark/,/^}/ * Запрос с получением результатов -.code query/query.go +.code query/query.go /^func Query/,/^}/ * Запрос с получением одного результата -.code query/queryrow.go +.code query/queryrow.go /^func QueryRow/,/^}/ * Запрос без результатов -.code exec/exec.go +.code exec/exec.go /^func Exec/,/^}/ Возвращаемый тип -.code exec/result.go - -* Nulls - -.code nulls/nulls.go - -* Null в аргументах - -Передаём null -.code nulls/insert_null.go -Передаём значение -.code nulls/insert_value.go - -* Null в результатах -.code nulls/rows.go +.code exec/result.go /^type Result/,/^}/ * Именованые аргументы - реализуются на уровне драйвера -.code named/named.go - +.code named/named.go /^type NamedArg/,/^}/ * Именованые аргументы - использование -.code named/insert.go + +.code named/insert.go /^func Insert/,/^}/ + +* Nulls + +.code nulls/nulls.go /^type NullString/,/^}/ + +* Null в аргументах + +.code nulls/insert.go /^func Insert/,/^}/ + +Передаём значения +.code nulls/insert.go /^func DoStuff/,/^}/ + +* Null в результатах +.code nulls/rows.go /^func Results/,/^}/ * Prepared Statements @@ -89,15 +89,15 @@ database/sql * Prepare -.code prepare/prepare.go +.code prepare/prepare.go /^func Prepare/,/^}/ * Одно подключение -.code conn/conn.go +.code conn/conn.go /^func Conn/,/^}/ * Транзакции -.code tx/tx.go +.code tx/tx.go /^func Begin/,/^}/ * Типовые ошибки и подводные камни @@ -129,15 +129,36 @@ database/sql * sqlx - обобщенный интерфейс -.code sqlx/ext.go +.code sqlx/ext.go /^type QueryerContext/,/^}/ +.code sqlx/ext.go /^type ExecerContext/,/^}/ +.code sqlx/ext.go /^type ExtContext/,/^}/ * sqlx - StructScan -.code sqlx/structscan.go + func (r *Rows) StructScan(dest interface{}) error + + func (r *Row) StructScan(dest interface{}) error + + func StructScan(rows rowsi, dest interface{}) error + + type rowsi interface { + Close() error + Columns() ([]string, error) + Err() error + Next() bool + Scan(...interface{}) error + } * sqlx - StructScan на практике -.code sqlx/example.go +.code sqlx/structscan.go /^func Example/,/^}/ + +* sqlx - именованые аргументы + +- работают для любого драйвера +- тоже защищают от SQL-injection + +.code sqlx/named.go /^func Insert/,/^}/ * GORM diff --git a/lectures/07-sql/sql/driver.go b/lectures/07-sql/sql/driver.go index 5664851..443d5be 100644 --- a/lectures/07-sql/sql/driver.go +++ b/lectures/07-sql/sql/driver.go @@ -1,7 +1,14 @@ +package sql + +import ( + "context" + "database/sql/driver" +) + type Driver interface { - Open(name string) (Conn, error) + Open(name string) (driver.Conn, error) } type QueryerContext interface { - QueryContext(ctx context.Context, query string, args []NamedValue) (Rows, error) + QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) } diff --git a/lectures/07-sql/sqlx/example.go b/lectures/07-sql/sqlx/example.go deleted file mode 100644 index d212433..0000000 --- a/lectures/07-sql/sqlx/example.go +++ /dev/null @@ -1,21 +0,0 @@ -rows, err := db.QueryContext(ctx, "SELECT id, name FROM users WHERE id = $1", 1) -if err != nil { - log.Fatal(err) -} -defer rows.Close() - -for rows.Next() { - var value struct { - ID int `db:"id"` - Name string `db:"name"` - } - if err := rows.StructScan(&value); err != nil { - log.Fatal(err) - } - - log.Println(value) -} - -if err = rows.Err(); err != nil { - log.Fatal(err) -} diff --git a/lectures/07-sql/sqlx/ext.go b/lectures/07-sql/sqlx/ext.go index b0cc6c8..8cf6953 100644 --- a/lectures/07-sql/sqlx/ext.go +++ b/lectures/07-sql/sqlx/ext.go @@ -1,7 +1,16 @@ +package sqlx + +import ( + "context" + "database/sql" + + "github.com/jmoiron/sqlx" +) + type QueryerContext interface { QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) - QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) - QueryRowxContext(ctx context.Context, query string, args ...interface{}) *Row + QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) + QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row } type ExecerContext interface { diff --git a/lectures/07-sql/sqlx/named.go b/lectures/07-sql/sqlx/named.go new file mode 100644 index 0000000..114669a --- /dev/null +++ b/lectures/07-sql/sqlx/named.go @@ -0,0 +1,21 @@ +package sqlx + +import ( + "context" + "log" + + "github.com/jmoiron/sqlx" +) + +func Insert(ctx context.Context, db *sqlx.DB) { + _, err := db.NamedExecContext( + ctx, + "INSERT INTO users(name) VALUES(:name)", + map[string]interface{}{ + "name": "Jukka Sarasti", + }, + ) + if err != nil { + log.Fatal(err) + } +} diff --git a/lectures/07-sql/sqlx/open.go b/lectures/07-sql/sqlx/open.go index 41e8859..0d701ff 100644 --- a/lectures/07-sql/sqlx/open.go +++ b/lectures/07-sql/sqlx/open.go @@ -1,10 +1,16 @@ +package sqlx + import ( - "github.com/jmoiron/sqlx" - _ "github.com/jackc/pgx/v4/stdlib" + "log" + + _ "github.com/jackc/pgx/v4/stdlib" + "github.com/jmoiron/sqlx" ) -db, err := sqlx.Open("pgx", "postgres://pgx_md5:secret@localhost:5432/pgx_test") -if err != nil { - return err +func Open() { + db, err := sqlx.Open("pgx", "postgres://pgx_md5:secret@localhost:5432/pgx_test") + if err != nil { + log.Fatal(err) + } + defer db.Close() } -defer db.Close() diff --git a/lectures/07-sql/sqlx/structscan.go b/lectures/07-sql/sqlx/structscan.go index 530d541..35fba9a 100644 --- a/lectures/07-sql/sqlx/structscan.go +++ b/lectures/07-sql/sqlx/structscan.go @@ -1,5 +1,32 @@ -func StructScan(rows rowsi, dest interface{}) error +package sqlx -func (r *Rows) StructScan(dest interface{}) error +import ( + "context" + "log" -func (r *Row) StructScan(dest interface{}) error + "github.com/jmoiron/sqlx" +) + +func Example(ctx context.Context, db *sqlx.DB) { + rows, err := db.QueryContext(ctx, "SELECT id, name FROM users WHERE id = $1", 1) + if err != nil { + log.Fatal(err) + } + defer rows.Close() + + for rows.Next() { + var value struct { + ID int `db:"id"` + Name string `db:"name"` + } + if err := sqlx.StructScan(rows, &value); err != nil { + log.Fatal(err) + } + + log.Println(value) + } + + if err = rows.Err(); err != nil { + log.Fatal(err) + } +} diff --git a/lectures/07-sql/tx/tx.go b/lectures/07-sql/tx/tx.go index ccb8227..f7da2e5 100644 --- a/lectures/07-sql/tx/tx.go +++ b/lectures/07-sql/tx/tx.go @@ -1,13 +1,23 @@ -tx, err := db.BeginTx(ctx, nil) -if err != nil { - log.Fatal(err) -} -defer tx.Rollback() +package tx -if _, err = tx.ExecContext(ctx, ...); err != nil { - log.Fatal(err) -} +import ( + "context" + "database/sql" + "log" +) -if err = tx.Commit(); err != nil { - log.Fatal(err) +func Begin(ctx context.Context, db *sql.DB) { + tx, err := db.BeginTx(ctx, nil) + if err != nil { + log.Fatal(err) + } + defer tx.Rollback() + + if _, err = tx.ExecContext(ctx, `UPDATE users SET name = "Tyador Borlú" WHERE id = 1`); err != nil { + log.Fatal(err) + } + + if err = tx.Commit(); err != nil { + log.Fatal(err) + } }