yugabytedb-ysql 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +7 -0
  2. data/.appveyor.yml +42 -0
  3. data/.gems +6 -0
  4. data/.gemtest +0 -0
  5. data/.github/workflows/binary-gems.yml +117 -0
  6. data/.github/workflows/source-gem.yml +143 -0
  7. data/.gitignore +24 -0
  8. data/.hgsigs +34 -0
  9. data/.hgtags +41 -0
  10. data/.irbrc +23 -0
  11. data/.pryrc +23 -0
  12. data/.tm_properties +21 -0
  13. data/.travis.yml +49 -0
  14. data/BSDL +22 -0
  15. data/Contributors.rdoc +46 -0
  16. data/Gemfile +18 -0
  17. data/History.md +901 -0
  18. data/LICENSE +56 -0
  19. data/Manifest.txt +73 -0
  20. data/POSTGRES +23 -0
  21. data/README-OS_X.rdoc +68 -0
  22. data/README-Windows.rdoc +56 -0
  23. data/README.ja.md +302 -0
  24. data/README.md +381 -0
  25. data/Rakefile +118 -0
  26. data/Rakefile.cross +299 -0
  27. data/certs/ged.pem +24 -0
  28. data/certs/kanis@comcard.de.pem +20 -0
  29. data/certs/larskanis-2022.pem +26 -0
  30. data/certs/larskanis-2023.pem +24 -0
  31. data/certs/larskanis-2024.pem +24 -0
  32. data/ext/errorcodes.def +1044 -0
  33. data/ext/errorcodes.rb +45 -0
  34. data/ext/errorcodes.txt +497 -0
  35. data/ext/extconf.rb +174 -0
  36. data/ext/gvl_wrappers.c +21 -0
  37. data/ext/gvl_wrappers.h +264 -0
  38. data/ext/pg.c +692 -0
  39. data/ext/pg.h +392 -0
  40. data/ext/pg_binary_decoder.c +308 -0
  41. data/ext/pg_binary_encoder.c +387 -0
  42. data/ext/pg_coder.c +624 -0
  43. data/ext/pg_connection.c +4681 -0
  44. data/ext/pg_copy_coder.c +917 -0
  45. data/ext/pg_errors.c +95 -0
  46. data/ext/pg_record_coder.c +522 -0
  47. data/ext/pg_result.c +1766 -0
  48. data/ext/pg_text_decoder.c +1005 -0
  49. data/ext/pg_text_encoder.c +827 -0
  50. data/ext/pg_tuple.c +572 -0
  51. data/ext/pg_type_map.c +200 -0
  52. data/ext/pg_type_map_all_strings.c +130 -0
  53. data/ext/pg_type_map_by_class.c +271 -0
  54. data/ext/pg_type_map_by_column.c +355 -0
  55. data/ext/pg_type_map_by_mri_type.c +313 -0
  56. data/ext/pg_type_map_by_oid.c +388 -0
  57. data/ext/pg_type_map_in_ruby.c +333 -0
  58. data/ext/pg_util.c +149 -0
  59. data/ext/pg_util.h +65 -0
  60. data/ext/vc/pg.sln +26 -0
  61. data/ext/vc/pg_18/pg.vcproj +216 -0
  62. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  63. data/lib/pg/basic_type_map_based_on_result.rb +67 -0
  64. data/lib/pg/basic_type_map_for_queries.rb +202 -0
  65. data/lib/pg/basic_type_map_for_results.rb +104 -0
  66. data/lib/pg/basic_type_registry.rb +303 -0
  67. data/lib/pg/binary_decoder/date.rb +9 -0
  68. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  69. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  70. data/lib/pg/coder.rb +106 -0
  71. data/lib/pg/connection.rb +997 -0
  72. data/lib/pg/exceptions.rb +25 -0
  73. data/lib/pg/load_balance_service.rb +406 -0
  74. data/lib/pg/result.rb +43 -0
  75. data/lib/pg/text_decoder/date.rb +18 -0
  76. data/lib/pg/text_decoder/inet.rb +9 -0
  77. data/lib/pg/text_decoder/json.rb +14 -0
  78. data/lib/pg/text_decoder/numeric.rb +9 -0
  79. data/lib/pg/text_decoder/timestamp.rb +30 -0
  80. data/lib/pg/text_encoder/date.rb +12 -0
  81. data/lib/pg/text_encoder/inet.rb +28 -0
  82. data/lib/pg/text_encoder/json.rb +14 -0
  83. data/lib/pg/text_encoder/numeric.rb +9 -0
  84. data/lib/pg/text_encoder/timestamp.rb +24 -0
  85. data/lib/pg/tuple.rb +30 -0
  86. data/lib/pg/type_map_by_column.rb +16 -0
  87. data/lib/pg/version.rb +5 -0
  88. data/lib/ysql.rb +130 -0
  89. data/misc/openssl-pg-segfault.rb +31 -0
  90. data/misc/postgres/History.txt +9 -0
  91. data/misc/postgres/Manifest.txt +5 -0
  92. data/misc/postgres/README.txt +21 -0
  93. data/misc/postgres/Rakefile +21 -0
  94. data/misc/postgres/lib/postgres.rb +16 -0
  95. data/misc/ruby-pg/History.txt +9 -0
  96. data/misc/ruby-pg/Manifest.txt +5 -0
  97. data/misc/ruby-pg/README.txt +21 -0
  98. data/misc/ruby-pg/Rakefile +21 -0
  99. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  100. data/rakelib/task_extension.rb +46 -0
  101. data/sample/array_insert.rb +20 -0
  102. data/sample/async_api.rb +102 -0
  103. data/sample/async_copyto.rb +39 -0
  104. data/sample/async_mixed.rb +56 -0
  105. data/sample/check_conn.rb +21 -0
  106. data/sample/copydata.rb +71 -0
  107. data/sample/copyfrom.rb +81 -0
  108. data/sample/copyto.rb +19 -0
  109. data/sample/cursor.rb +21 -0
  110. data/sample/disk_usage_report.rb +177 -0
  111. data/sample/issue-119.rb +94 -0
  112. data/sample/losample.rb +69 -0
  113. data/sample/minimal-testcase.rb +17 -0
  114. data/sample/notify_wait.rb +72 -0
  115. data/sample/pg_statistics.rb +285 -0
  116. data/sample/replication_monitor.rb +222 -0
  117. data/sample/test_binary_values.rb +33 -0
  118. data/sample/wal_shipper.rb +434 -0
  119. data/sample/warehouse_partitions.rb +311 -0
  120. data/yugabytedb-ysql.gemspec +33 -0
  121. metadata +232 -0
data/LICENSE ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a) distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/Manifest.txt ADDED
@@ -0,0 +1,73 @@
1
+ .gemtest
2
+ BSDL
3
+ Contributors.rdoc
4
+ History.rdoc
5
+ LICENSE
6
+ Manifest.txt
7
+ POSTGRES
8
+ README-OS_X.rdoc
9
+ README-Windows.rdoc
10
+ README.ja.rdoc
11
+ README.rdoc
12
+ Rakefile
13
+ Rakefile.cross
14
+ ext/errorcodes.def
15
+ ext/errorcodes.rb
16
+ ext/errorcodes.txt
17
+ ext/extconf.rb
18
+ ext/gvl_wrappers.c
19
+ ext/gvl_wrappers.h
20
+ ext/pg.c
21
+ ext/pg.h
22
+ ext/pg_binary_decoder.c
23
+ ext/pg_binary_encoder.c
24
+ ext/pg_coder.c
25
+ ext/pg_connection.c
26
+ ext/pg_copy_coder.c
27
+ ext/pg_errors.c
28
+ ext/pg_record_coder.c
29
+ ext/pg_result.c
30
+ ext/pg_text_decoder.c
31
+ ext/pg_text_encoder.c
32
+ ext/pg_tuple.c
33
+ ext/pg_type_map.c
34
+ ext/pg_type_map_all_strings.c
35
+ ext/pg_type_map_by_class.c
36
+ ext/pg_type_map_by_column.c
37
+ ext/pg_type_map_by_mri_type.c
38
+ ext/pg_type_map_by_oid.c
39
+ ext/pg_type_map_in_ruby.c
40
+ ext/pg_util.c
41
+ ext/pg_util.h
42
+ ext/vc/pg.sln
43
+ ext/vc/pg_18/pg.vcproj
44
+ ext/vc/pg_19/pg_19.vcproj
45
+ lib/pg.rb
46
+ lib/pg/basic_type_mapping.rb
47
+ lib/pg/binary_decoder.rb
48
+ lib/pg/coder.rb
49
+ lib/pg/connection.rb
50
+ lib/pg/constants.rb
51
+ lib/pg/exceptions.rb
52
+ lib/pg/result.rb
53
+ lib/pg/text_decoder.rb
54
+ lib/pg/text_encoder.rb
55
+ lib/pg/tuple.rb
56
+ lib/pg/type_map_by_column.rb
57
+ lib/pg/load_balance_service.rb
58
+ spec/data/expected_trace.out
59
+ spec/data/random_binary_data
60
+ spec/helpers.rb
61
+ spec/pg/basic_type_mapping_spec.rb
62
+ spec/pg/connection_spec.rb
63
+ spec/pg/connection_sync_spec.rb
64
+ spec/pg/result_spec.rb
65
+ spec/pg/tuple_spec.rb
66
+ spec/pg/type_map_by_class_spec.rb
67
+ spec/pg/type_map_by_column_spec.rb
68
+ spec/pg/type_map_by_mri_type_spec.rb
69
+ spec/pg/type_map_by_oid_spec.rb
70
+ spec/pg/type_map_in_ruby_spec.rb
71
+ spec/pg/type_map_spec.rb
72
+ spec/pg/type_spec.rb
73
+ spec/pg_spec.rb
data/POSTGRES ADDED
@@ -0,0 +1,23 @@
1
+ PostgreSQL Database Management System
2
+ (formerly known as Postgres, then as Postgres95)
3
+
4
+ Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
5
+
6
+ Portions Copyright (c) 1994, The Regents of the University of California
7
+
8
+ Permission to use, copy, modify, and distribute this software and its
9
+ documentation for any purpose, without fee, and without a written agreement
10
+ is hereby granted, provided that the above copyright notice and this
11
+ paragraph and the following two paragraphs appear in all copies.
12
+
13
+ IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
14
+ DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
15
+ LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
16
+ DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
17
+ POSSIBILITY OF SUCH DAMAGE.
18
+
19
+ THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
20
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
+ AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22
+ ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
23
+ PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
data/README-OS_X.rdoc ADDED
@@ -0,0 +1,68 @@
1
+ = Compiling on MacOS X
2
+
3
+ The EnterpriseDB packages are the recommended PostgreSQL installations to use
4
+ with MacOS X. They eliminate most or all of the issues with getting 'pg'
5
+ installed, linked correctly, and running.
6
+
7
+ == Segfaults and SSL Support
8
+
9
+ If you need a custom installation of PostgreSQL, you should ensure that you
10
+ either compile it against the same version of OpenSSL as the OpenSSL extension
11
+ of the Ruby you'll be using, or compile it without SSL support. If you fail to
12
+ do this, you will likely see segfaults when you use 'pg' and the 'openssl'
13
+ extension at the same time. You can see what library it's linked against using
14
+ 'otool -L'; for example, on my 10.7 machine I use for 'pg' development:
15
+
16
+ $ otool -L /System/Library/Frameworks/Ruby.framework/Versions\
17
+ /1.8/usr/lib/ruby/1.8/universal-darwin11.0/openssl.bundle
18
+
19
+ /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/\
20
+ lib/ruby/1.8/universal-darwin11.0/openssl.bundle:
21
+ /System/Library/Frameworks/Ruby.framework/Versions/1.8/\
22
+ usr/lib/libruby.1.dylib (compatibility version 1.8.0, \
23
+ current version 1.8.7)
24
+ /usr/lib/libssl.0.9.8.dylib (compatibility version 0.9.8, \
25
+ current version 0.9.8)
26
+ /usr/lib/libcrypto.0.9.8.dylib (compatibility version 0.9.8, \
27
+ current version 0.9.8)
28
+ /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, \
29
+ current version 159.0.0)
30
+
31
+
32
+ == Dealing with Installation Problems
33
+
34
+ If you are building/installing pg on MacOS X, and the installation doesn't
35
+ work at first, here are a few things you can try.
36
+
37
+ === pg_config
38
+
39
+ The first thing you should do is ensure that the 'pg_config' tool that comes
40
+ with Postgres is in your path. If it isn't, or the one that's first in your
41
+ path isn't the one that was installed with the Postgres you want to build
42
+ against, you can specify the path to it with the --with-pg-config option.
43
+
44
+ For example, if you're using the Ruby binary that comes with OSX, and
45
+ PostgreSQL 9.0.x installed from MacPorts, do:
46
+
47
+ gem install -- --with-pg-config=/opt/local/lib/postgresql90/bin/pg_config
48
+
49
+ === ARCHFLAGS and Universal Binaries
50
+
51
+ OS X supports both architecture-specific binaries (e.g. i386), as well as
52
+ universal binaries (i.e. i386 & ppc). If Ruby is built as a universal binary
53
+ and PostgreSQL is not, you need to specify the path to the appropriate
54
+ pg_config binary or set the environment variable ARCHFLAGS appropriately.
55
+
56
+ Alternatively, if the build system can't figure out which architectures it
57
+ should include, you may need to set the 'ARCHFLAGS' environment variable
58
+ explicitly:
59
+
60
+ sudo env ARCHFLAGS='-arch x86_64' gem install pg
61
+
62
+ or, if you're building from source:
63
+
64
+ rake compile ARCHFLAGS="-arch x86_64"
65
+
66
+ Note that the recommended EnterpriseDB packages are correctly compiled as
67
+ universal binaries, and don't need any of these workarounds.
68
+
@@ -0,0 +1,56 @@
1
+ = Compiling 'pg' on MS Windows
2
+
3
+ In order to build this extension on MS Windows you will need a couple things.
4
+
5
+ First, a compiler. For the one click installer this means you should use
6
+ the DevKit or the compiler that comes with cygwin if you're building on that
7
+ platform.
8
+
9
+ If you've built Ruby yourself, you should use the same compiler to build
10
+ this library that you used to build Ruby.
11
+
12
+ Second, PostgreSQL. Be sure you installed it with the development header
13
+ files if you installed it using the standard PostgreSQL installer for
14
+ Windows. If you didn't, you can run the installer again, select "modify",
15
+ and then select the 'development headers' option to install them.
16
+
17
+ I recommend making sure that 'pg_config.exe' is in your PATH. The PostgreSQL
18
+ installer for Windows does not necessarily update your PATH when it installs
19
+ itself, so you may need to do this manually. This isn't strictly necessary,
20
+ however.
21
+
22
+ In order to build ruby-pg, just run 'rake'. If the pg_config.exe executable
23
+ is not in your PATH, you'll need to explicitly point ruby-pg to where your
24
+ PostgreSQL headers and libraries are with something like this:
25
+
26
+ rake --with-pg-dir=c:/progra~1/postgr~1/8.3
27
+
28
+ Adjust your path accordingly. BE SURE TO USE THE SHORT PATH NAMES! If you
29
+ try to use a path with spaces in it, the nmake.exe program will choke.
30
+
31
+
32
+ == Building binary 'pg' gems for MS Windows
33
+
34
+ Binary gems for windows can be built on Linux, OS-X and even on Windows
35
+ with the help of docker. This is how regular windows gems are built for
36
+ rubygems.org .
37
+
38
+ To do this, install boot2docker {on Windows}[https://github.com/boot2docker/windows-installer/releases]
39
+ or {on OS X}[https://github.com/boot2docker/osx-installer/releases] and make
40
+ sure it is started. A native Docker installation is best on Linux.
41
+
42
+ Then run:
43
+
44
+ rake gem:windows
45
+
46
+ This will download a docker image suited for building windows gems, and it
47
+ will download and build OpenSSL and PostgreSQL. Finally the gem is built
48
+ containing binaries for all supported ruby versions.
49
+
50
+
51
+ == Reporting Problems
52
+
53
+ If you have any problems you can submit them via {the project's
54
+ issue-tracker}[https://github.com/ged/ruby-pg/issues]. And submit questions, problems, or
55
+ solutions, so that it can be improved.
56
+
data/README.ja.md ADDED
@@ -0,0 +1,302 @@
1
+ # yugabytedb-ysql
2
+
3
+ * ホーム :: https://github.com/ged/ruby-pg
4
+ * ドキュメント :: http://deveiate.org/code/pg (英語)、 https://deveiate.org/code/pg/README_ja_md.html (日本語)
5
+ * 変更履歴 :: link:/History.md
6
+
7
+ [![https://gitter.im/ged/ruby-pg
8
+ でチャットに参加](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/ged/ruby-pg?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
9
+
10
+
11
+ ## 説明
12
+
13
+ Pgは[PostgreSQL
14
+ RDBMS](http://www.postgresql.org/)へのRubyのインターフェースです。[PostgreSQL
15
+ 9.3以降](http://www.postgresql.org/support/versioning/)で動作します。
16
+
17
+ 簡単な使用例は次の通りです。
18
+
19
+ ```ruby
20
+ #!/usr/bin/env ruby
21
+
22
+ require 'ysql'
23
+
24
+ # データベースへの現在の接続を表に出力します
25
+ conn = YSQL.connect( dbname: 'sales' )
26
+ conn.exec( "SELECT * FROM pg_stat_activity" ) do |result|
27
+ puts " PID | User | Query"
28
+ result.each do |row|
29
+ puts " %7d | %-16s | %s " %
30
+ row.values_at('pid', 'usename', 'query')
31
+ end
32
+ end
33
+ ```
34
+
35
+ ## ビルド状況
36
+
37
+ [![Github
38
+ Actionsのビルド状況](https://github.com/ged/ruby-pg/actions/workflows/source-gem.yml/badge.svg?branch=master)](https://github.com/ged/ruby-pg/actions/workflows/source-gem.yml)
39
+ [![バイナリgem](https://github.com/ged/ruby-pg/actions/workflows/binary-gems.yml/badge.svg?branch=master)](https://github.com/ged/ruby-pg/actions/workflows/binary-gems.yml)
40
+ [![Appveyorのビルド状況](https://ci.appveyor.com/api/projects/status/gjx5axouf3b1wicp?svg=true)](https://ci.appveyor.com/project/ged/ruby-pg-9j8l3)
41
+
42
+
43
+ ## 要件
44
+
45
+ * Ruby 2.5かそれより新しいバージョン
46
+ * PostgreSQL 9.3.xかそれ以降のバージョン(ヘッダー付属のもの、例えば-devの名前のパッケージ)。
47
+
48
+ それより前のバージョンのRubyやPostgreSQLでも通常は同様に動作しますが、定期的なテストはされていません。
49
+
50
+
51
+ ## バージョン管理
52
+
53
+ [セマンティックバージョニング](http://semver.org/)の原則にしたがってgemをタグ付けしてリリースしています。
54
+
55
+ この方針の結果として、2つの数字を指定する[悲観的バージョン制約](http://guides.rubygems.org/patterns/#pessimistic-version-constraint)を使ってこのgemへの依存関係を指定することができます(またそうすべきです)。
56
+
57
+ 例えば次の通りです。
58
+
59
+ ```ruby
60
+ spec.add_dependency 'yugabytedb-ysql', '~> 1.0'
61
+ ```
62
+
63
+ ## インストール方法
64
+
65
+ RubyGemsを経由してインストールするには以下とします。
66
+
67
+ gem install yugabytedb-ysql
68
+
69
+ Postgresと一緒にインストールされた'pg_config'プログラムへのパスを指定する必要があるかもしれません。
70
+
71
+ gem install yugabytedb-ysql -- --with-pg-config=<path to pg_config>
72
+
73
+ Bundlerを介してインストールした場合は次のようにコンパイルのためのヒントを与えられます。
74
+
75
+ bundle config build.pg --with-pg-config=<path to pg_config>
76
+
77
+ MacOS Xへインストールする詳しい情報については README-OS_X.rdoc を、Windows用のビルドやインストールの説明については
78
+ README-Windows.rdoc を参照してください。
79
+
80
+ 詰まったときやただ何か喋りたいときのために[Google+グループ](http://goo.gl/TFy1U)と[メーリングリスト](http://groups.google.com/group/ruby-pg)もあります。
81
+
82
+ 署名されたgemとしてインストールしたい場合は、リポジトリの[`certs`ディレクトリ](https://github.com/ged/ruby-pg/tree/master/certs)にgemの署名をする公開証明書があります。
83
+
84
+
85
+ ## 型変換
86
+
87
+ Pgでは任意でRubyと素のCコードにある結果の値やクエリ引数の型変換ができます。
88
+ こうすることでデータベースとのデータの往来を加速させられます。
89
+ なぜなら文字列のアロケーションが減り、(比較的遅い)Rubyのコードでの変換部分が省かれるからです。
90
+
91
+ とても基本的な型変換は次のようにできます。
92
+
93
+ ```ruby
94
+ conn.type_map_for_results = YSQL::BasicTypeMapForResults.new conn
95
+ # ……これは結果の値の対応付けに作用します。
96
+ conn.exec("select 1, now(), '{2,3}'::int[]").values
97
+ # => [[1, 2014-09-21 20:51:56 +0200, [2, 3]]]
98
+
99
+ conn.type_map_for_queries = YSQL::BasicTypeMapForQueries.new conn
100
+ # ……そしてこれは引数値の対応付けのためのものです。
101
+ conn.exec_params("SELECT $1::text, $2::text, $3::text", [1, 1.23, [2, 3]]).values
102
+ # => [["1", "1.2300000000000000E+00", "{2,3}"]]
103
+ ```
104
+
105
+ しかしPgの型変換はかなり調整が効きます。2層に分かれているのがその理由です。
106
+
107
+ ### エンコーダーとデコーダー (ext/pg_*coder.c, lib/pg/*coder.rb)
108
+
109
+ こちらはより低層で、DBMSへ転送するためにRubyのオブジェクトを変換するエンコーディングクラスと、取得してきたデータをRubyのオブジェクトに変換し戻すデコーディングクラスが含まれています。
110
+ クラスはそれぞれの形式によって名前空間 PG::TextEncoder, PG::TextDecoder, PG::BinaryEncoder, そして
111
+ PG::BinaryDecoder に分かれています。
112
+
113
+ エンコーダーないしデコーダーオブジェクトにOIDデータ型や形式コード(テキストないしバイナリ)や任意で名前を割り当てることができます。
114
+ 要素のエンコーダーないしデコーダーを割り当てることによって複合型を構築することもできます。
115
+ PG::Coder オブジェクトは PG::TypeMap をセットアップしたり、その代わりに単一の値と文字列表現とを相互に変換したりするのに使えます。
116
+
117
+ ruby-pgでは以下のPostgreSQLカラム型に対応しています(TE = Text Encoder、TD = Text Decoder、BE =
118
+ Binary Encoder、BD = Binary Decoder)。
119
+
120
+ * Integer:
121
+ [TE](rdoc-ref:PG::TextEncoder::Integer)、[TD](rdoc-ref:PG::TextDecoder::Integer)、[BD](rdoc-ref:PG::BinaryDecoder::Integer)
122
+ 💡
123
+ リンクがないでしょうか。[こちら](https://deveiate.org/code/pg/README_ja_md.html#label-E5-9E-8B-E5-A4-89-E6-8F-9B)を代わりに見てください
124
+ 💡
125
+ * BE:
126
+ [Int2](rdoc-ref:PG::BinaryEncoder::Int2)、[Int4](rdoc-ref:PG::BinaryEncoder::Int4)、[Int8](rdoc-ref:PG::BinaryEncoder::Int8)
127
+ * Float:
128
+ [TE](rdoc-ref:PG::TextEncoder::Float)、[TD](rdoc-ref:PG::TextDecoder::Float)、[BD](rdoc-ref:PG::BinaryDecoder::Float)
129
+ * BE: [Float4](rdoc-ref:PG::BinaryEncoder::Float4),
130
+ [Float8](rdoc-ref:PG::BinaryEncoder::Float8)
131
+ * Numeric:
132
+ [TE](rdoc-ref:PG::TextEncoder::Numeric)、[TD](rdoc-ref:PG::TextDecoder::Numeric)
133
+ * Boolean:
134
+ [TE](rdoc-ref:PG::TextEncoder::Boolean)、[TD](rdoc-ref:PG::TextDecoder::Boolean)、[BE](rdoc-ref:PG::BinaryEncoder::Boolean)、[BD](rdoc-ref:PG::BinaryDecoder::Boolean)
135
+ * String:
136
+ [TE](rdoc-ref:PG::TextEncoder::String)、[TD](rdoc-ref:PG::TextDecoder::String)、[BE](rdoc-ref:PG::BinaryEncoder::String)、[BD](rdoc-ref:PG::BinaryDecoder::String)
137
+ * Bytea:
138
+ [TE](rdoc-ref:PG::TextEncoder::Bytea)、[TD](rdoc-ref:PG::TextDecoder::Bytea)、[BE](rdoc-ref:PG::BinaryEncoder::Bytea)、[BD](rdoc-ref:PG::BinaryDecoder::Bytea)
139
+ * Base64:
140
+ [TE](rdoc-ref:PG::TextEncoder::ToBase64)、[TD](rdoc-ref:PG::TextDecoder::FromBase64)、[BE](rdoc-ref:PG::BinaryEncoder::FromBase64)、[BD](rdoc-ref:PG::BinaryDecoder::ToBase64)
141
+ * Timestamp:
142
+ * TE:
143
+ [現地時間](rdoc-ref:PG::TextEncoder::TimestampWithoutTimeZone)、[UTC](rdoc-ref:PG::TextEncoder::TimestampUtc)、[タイムゾーン付き](rdoc-ref:PG::TextEncoder::TimestampWithTimeZone)
144
+ * TD:
145
+ [現地時間](rdoc-ref:PG::TextDecoder::TimestampLocal)、[UTC](rdoc-ref:PG::TextDecoder::TimestampUtc)、[UTCから現地時間へ](rdoc-ref:PG::TextDecoder::TimestampUtcToLocal)
146
+ * BE:
147
+ [現地時間](rdoc-ref:PG::BinaryEncoder::TimestampLocal)、[UTC](rdoc-ref:PG::BinaryEncoder::TimestampUtc)
148
+ * BD:
149
+ [現地時間](rdoc-ref:PG::BinaryDecoder::TimestampLocal)、[UTC](rdoc-ref:PG::BinaryDecoder::TimestampUtc)、[UTCから現地時間へ](rdoc-ref:PG::BinaryDecoder::TimestampUtcToLocal)
150
+ * 日付:[TE](rdoc-ref:PG::TextEncoder::Date)、[TD](rdoc-ref:PG::TextDecoder::Date)、[BE](rdoc-ref:PG::BinaryEncoder::Date)、[BD](rdoc-ref:PG::BinaryDecoder::Date)
151
+ * JSONとJSONB:
152
+ [TE](rdoc-ref:PG::TextEncoder::JSON)、[TD](rdoc-ref:PG::TextDecoder::JSON)
153
+ * Inet:
154
+ [TE](rdoc-ref:PG::TextEncoder::Inet)、[TD](rdoc-ref:PG::TextDecoder::Inet)
155
+ * Array:
156
+ [TE](rdoc-ref:PG::TextEncoder::Array)、[TD](rdoc-ref:PG::TextDecoder::Array)
157
+ * 複合型(「行」や「レコード」などとも言います):[TE](rdoc-ref:PG::TextEncoder::Record)、[TD](rdoc-ref:PG::TextDecoder::Record)
158
+
159
+ カラム型として使われていませんが、以下のテキスト形式とバイナリ形式もエンコードできます。
160
+
161
+ * COPYの入出力データ:[TE](rdoc-ref:PG::TextEncoder::CopyRow)、[TD](rdoc-ref:PG::TextDecoder::CopyRow),
162
+ [BE](rdoc-ref:PG::BinaryEncoder::CopyRow),
163
+ [BD](rdoc-ref:PG::BinaryDecoder::CopyRow)
164
+ * SQL文字列に挿入するリテラル:[TE](rdoc-ref:PG::TextEncoder::QuotedLiteral)
165
+ * SQLの識別子:
166
+ [TE](rdoc-ref:PG::TextEncoder::Identifier)、[TD](rdoc-ref:PG::TextDecoder::Identifier)
167
+
168
+ ### PG::TypeMap とその派生 (ext/pg_type_map*.c, lib/pg/type_map*.rb)
169
+
170
+ TypeMapはエンコーダーまたはデコーダーのどちらによってどの値を変換するかを定義します。
171
+ 様々な型の対応付け戦略があるので、このクラスにはいくつかの派生が実装されています。
172
+ 型変換の特有の需要に合わせてそれらの派生から選んで調整を加えることができます。
173
+ 既定の型の対応付けは PG::TypeMapAllStrings です。
174
+
175
+ 型の対応付けは、結果の集合それぞれに対し、接続毎ないしクエリ毎に割り当てることができます。
176
+ 型の対応付けはCOPYの入出力データストリーミングでも使うことができます。
177
+ PG::Connection#copy_data を参照してください。
178
+
179
+ 以下の基底となる型の対応付けが使えます。
180
+
181
+ * PG::TypeMapAllStrings - 全ての値と文字列について相互にエンコードとデコードを行います(既定)
182
+ * PG::TypeMapByClass - 送信する値のクラスに基づいてエンコーダーを選択します
183
+ * PG::TypeMapByColumn - カラムの順番によってエンコーダーとデコーダーを選択します
184
+ * PG::TypeMapByOid - PostgreSQLのOIDデータ型によってデコーダーを選択します
185
+ * PG::TypeMapInRuby - Rubyで独自の型の対応付けを定義します
186
+
187
+ 以下の型の対応付けは PG::BasicTypeRegistry 由来の型の対応付けが入った状態になっています。
188
+
189
+ * PG::BasicTypeMapForResults - PG::TypeMapByOid
190
+ によくあるPostgreSQLカラム型用にデコーダーが入った状態になっています
191
+ * PG::BasicTypeMapBasedOnResult - PG::TypeMapByOid
192
+ によくあるPostgreSQLカラム型用のエンコーダーが入った状態になっています
193
+ * PG::BasicTypeMapForQueries - PG::TypeMapByClass
194
+ によくあるRubyの値クラス用にエンコーダーが入った状態になっています
195
+
196
+
197
+ ## スレッド対応
198
+
199
+ PGには個々のスレッドが別々の PG::Connection オブジェクトを同時に使えるという点でスレッド安全性があります。
200
+ しかし1つ以上のスレッドから同時にPgのオブジェクトにアクセスすると安全ではありません。
201
+ そのため必ず、毎回新しいスレッドを作るときに新しいデータベースサーバー接続を開くか、スレッド安全性のある方法で接続を管理するActiveRecordのようなラッパーライブラリを使うようにしてください。
202
+
203
+ 以下のようなメッセージが標準エラー出力に表示された場合、恐らく複数のスレッドが1つの接続を使っています。
204
+
205
+ message type 0x31 arrived from server while idle
206
+ message type 0x32 arrived from server while idle
207
+ message type 0x54 arrived from server while idle
208
+ message type 0x43 arrived from server while idle
209
+ message type 0x5a arrived from server while idle
210
+
211
+
212
+ ## Fiber IOスケジューラー対応
213
+
214
+ pg-1.3.0以降で、PgはRuby-3.0で導入された`Fiber.scheduler`に完全に対応しています。
215
+ Windowsでは、`Fiber.scheduler`対応はRuby-3.1以降で使えます。
216
+ `Fiber.scheduler`が走らせているスレッドに登録されている場合、起こりうる全てのブロッキングIO操作はそのスケジューラーを経由します。
217
+ 同期的であったりブロックしたりするメソッド呼び出しについてもpgが内部的に非同期のlibpqインターフェースを使っているのはそれが理由です。
218
+ またlibpqの組み込み関数に代えてRubyのDNS解決を使っています。
219
+
220
+ 内部的にPgは常にlibpqのノンブロッキング接続モードを使います。
221
+ それからブロッキングモードで走っているように振舞いますが、もし`Fiber.scheduler`が登録されていれば全てのブロッキングIOはそのスケジューラーを通じてRubyで制御されます。
222
+ `PG::Connection.setnonblocking(true)`が呼ばれたらノンブロッキング状態が有効になったままになりますが、それ以降のブロッキング状態の制御が無効になるので、呼び出しているプログラムはブロッキング状態を自力で制御しなければなりません。
223
+
224
+ この規則の1つの例外には、`PG::Connection#lo_create`や外部ライブラリを使う認証メソッド(GSSAPI認証など)のような、大きめのオブジェクト用のメソッドがあります。これらは`Fiber.scheduler`と互換性がないため、ブロッキング状態は登録されたIOスケジューラに渡されません。つまり操作は適切に実行されますが、IO待ち状態に別のIOを扱うFiberから使用を切り替えてくることができなくなります。
225
+
226
+
227
+ ## Ractor対応
228
+
229
+ pg-1.5.0以降で、PgはRuby-3.0で導入されたRactorと完全な互換性があります。
230
+ 型エンコーダーないしデコーダー、及び型の対応付けが`Ractor.make_shareable`により凍結されている場合、これらをractor間で共有できます。
231
+ また凍結された PG::Result と PG::Tuple オブジェクトも共有できます。
232
+ 少なくとも全ての凍結されたオブジェクト(ただし PG::Connection
233
+ を除く)はPostgreSQLサーバーとのやり取りをしたり取得されたデータを読むのに使えます。
234
+
235
+ PG::Connection は共有できません。個々の接続を確立するために、それぞれのRactor内で作られなければなりません。
236
+
237
+
238
+ ## 貢献
239
+
240
+ バグを報告したり機能を提案したりGitでソースをチェックアウトしたりするには[プロジェクトページをご確認ください](https://github.com/ged/ruby-pg)。
241
+
242
+ ソースをチェックアウトしたあとは全ての依存関係をインストールします。
243
+
244
+ $ bundle install
245
+
246
+ 拡張ファイル、パッケージファイル、テストデータベースを一掃するには、このコマンドを走らせてください。PostgreSQLのバージョンも切り替わります。
247
+
248
+ $ rake clean
249
+
250
+ 拡張をコンパイルするには次のようにします。
251
+
252
+ $ rake compile
253
+
254
+ `pg_config --bindir`が指すPostgreSQLのバージョンでテストやスペックを走らせるには次のようにします。
255
+
256
+ $ rake test
257
+
258
+ あるいは特定のPostgreSQLのバージョンで、ファイル中の行番号を使って特定のテストを走らせるには次のようにします。
259
+
260
+ $ PATH=/usr/lib/postgresql/14/bin:$PATH rspec -Ilib -fd spec/pg/connection_spec.rb:455
261
+
262
+ APIドキュメントを生成するには次のようにします。
263
+
264
+ $ rake docs
265
+
266
+ 必ず全てのバグと新機能についてテストを使って検証してください。
267
+
268
+ 現在のメンテナはMichael Granger <ged@FaerieMUD.org>とLars Kanis
269
+ <lars@greiz-reinsdorf.de>です。
270
+
271
+
272
+ ## 著作権
273
+
274
+ Copyright (c) 1997-2022 by the authors.
275
+
276
+ * Jeff Davis <ruby-pg@j-davis.com>
277
+ * Guy Decoux (ts) <decoux@moulon.inra.fr>
278
+ * Michael Granger <ged@FaerieMUD.org>
279
+ * Lars Kanis <lars@greiz-reinsdorf.de>
280
+ * Dave Lee
281
+ * Eiji Matsumoto <usagi@ruby.club.or.jp>
282
+ * Yukihiro Matsumoto <matz@ruby-lang.org>
283
+ * Noboru Saitou <noborus@netlab.jp>
284
+
285
+ You may redistribute this software under the same terms as Ruby itself; see
286
+ https://www.ruby-lang.org/en/about/license.txt or the BSDL file in the
287
+ source for details.
288
+ (参考訳:このソフトウェアはRuby自体と同じ条件の元で再配布することができます。詳細については
289
+ https://www.ruby-lang.org/en/about/license.txt やソース中のBSDLファイルを参照してください)
290
+
291
+ Portions of the code are from the PostgreSQL project, and are distributed "
292
+ "under the terms of the PostgreSQL license, included in the file POSTGRES.
293
+ (参考訳:コードの一部はPostgreSQLプロジェクトから来ており、PostgreSQLの使用許諾の条件の元で配布されます。ファイルPOSTGRESに含まれています)
294
+
295
+ Portions copyright LAIKA, Inc.
296
+
297
+
298
+ ## 謝辞
299
+
300
+ 長年にわたって貢献してくださった方々については Contributors.rdoc を参照してください。
301
+
302
+ ruby-listとruby-devメーリングリストの方々に感謝します。またPostgreSQLを開発された方々へも謝意を表します。