当前位置: 首页 > 软件库 > 数据库相关 > >

ruby-pg

A PostgreSQL client library for Ruby
授权协议 View license
开发语言 C/C++
所属分类 数据库相关
软件类型 开源软件
地区 不详
投 递 者 田嘉澍
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

pg

home

github.com/ged/ruby-pg

docs

deveiate.org/code/pg

clog

github.com/ged/ruby-pg/blob/master/History.rdoc

Description

Pg is the Ruby interface to the PostgreSQL RDBMS.

It works with PostgreSQL 9.3 and later.

A small example usage:

#!/usr/bin/env ruby

require 'pg'

# Output a table of current connections to the DB
conn = PG.connect( dbname: 'sales' )
conn.exec( "SELECT * FROM pg_stat_activity" ) do |result|
  puts "     PID | User             | Query"
  result.each do |row|
    puts " %7d | %-16s | %s " %
      row.values_at('pid', 'usename', 'query')
  end
end

Build Status

Build Status Appveyor Build Status Github Actions

Requirements

  • Ruby 2.4 or newer

  • PostgreSQL 9.3.x or later (with headers, -dev packages, etc).

It usually works with earlier versions of Ruby/PostgreSQL as well, but those are not regularly tested.

Versioning

We tag and release gems according to the Semantic Versioning principle.

As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.

For example:

spec.add_dependency 'pg', '~> 1.0'

How To Install

Install via RubyGems:

gem install pg

You may need to specify the path to the 'pg_config' program installed with Postgres:

gem install pg -- --with-pg-config=<path to pg_config>

If you're installing via Bundler, you can provide compile hints like so:

bundle config build.pg --with-pg-config=<path to pg_config>

See README-OS_X.rdoc for more information about installing under MacOS X, and README-Windows.rdoc for Windows build/installation instructions.

There's also a Google+ group and a mailing list if you get stuck, or just want to chat about something.

If you want to install as a signed gem, the public certs of the gem signers can be found in the `certs` directory of the repository.

Type Casts

Pg can optionally type cast result values and query parameters in Ruby or native C code. This can speed up data transfers to and from the database, because String allocations are reduced and conversions in (slower) Ruby code can be omitted.

Very basic type casting can be enabled by:

conn.type_map_for_results = PG::BasicTypeMapForResults.new conn
# ... this works for result value mapping:
conn.exec("select 1, now(), '{2,3}'::int[]").values
    # => [[1, 2014-09-21 20:51:56 +0200, [2, 3]]]

conn.type_map_for_queries = PG::BasicTypeMapForQueries.new conn
# ... and this for param value mapping:
conn.exec_params("SELECT $1::text, $2::text, $3::text", [1, 1.23, [2,3]]).values
    # => [["1", "1.2300000000000000E+00", "{2,3}"]]

But Pg's type casting is highly customizable. That's why it's divided into 2 layers:

Encoders / Decoders (ext/pg_*coder.c, lib/pg/*coder.rb)

This is the lower layer, containing encoding classes that convert Ruby objects for transmission to the DBMS and decoding classes to convert received data back to Ruby objects. The classes are namespaced according to their format and direction in PG::TextEncoder, PG::TextDecoder, PG::BinaryEncoder and PG::BinaryDecoder.

It is possible to assign a type OID, format code (text or binary) and optionally a name to an encoder or decoder object. It's also possible to build composite types by assigning an element encoder/decoder. PG::Coder objects can be used to set up a PG::TypeMap or alternatively to convert single values to/from their string representation.

The following PostgreSQL column types are supported by ruby-pg (TE = Text Encoder, TD = Text Decoder, BE = Binary Encoder, BD = Binary Decoder):

  • Integer: TE, TD, BD �� No links? Switch to here ��

    • BE: Int2, Int4, Int8

  • Float: TE, TD, BD

  • Numeric: TE, TD

  • Boolean: TE, TD, BE, BD

  • String: TE, TD, BE, BD

  • Bytea: TE, TD, BE, BD

  • Base64: TE, TD, BE, BD

  • Timestamp:

    • TE: local, UTC, with-TZ

    • TD: local, UTC, UTC-to-local

    • BD: local, UTC, UTC-to-local

  • Date: TE, TD

  • JSON and JSONB: TE, TD

  • Inet: TE, TD

  • Array: TE, TD

  • Composite Type (also called “Row” or “Record”): TE, TD

The following text formats can also be encoded although they are not used as column type:

  • COPY input and output data: TE, TD

  • Literal for insertion into SQL string: TE

  • SQL-Identifier: TE, TD

PG::TypeMap and derivations (ext/pg_type_map*.c, lib/pg/type_map*.rb)

A TypeMap defines which value will be converted by which encoder/decoder. There are different type map strategies, implemented by several derivations of this class. They can be chosen and configured according to the particular needs for type casting. The default type map is PG::TypeMapAllStrings.

A type map can be assigned per connection or per query respectively per result set. Type maps can also be used for COPY in and out data streaming. See PG::Connection#copy_data .

The following base type maps are available:

  • PG::TypeMapAllStrings - encodes and decodes all values to and from strings (default)

  • PG::TypeMapByClass - selects encoder based on the class of the value to be sent

  • PG::TypeMapByColumn - selects encoder and decoder by column order

  • PG::TypeMapByOid - selects decoder by PostgreSQL type OID

  • PG::TypeMapInRuby - define a custom type map in ruby

The following type maps are prefilled with type mappings from the PG::BasicTypeRegistry :

  • PG::BasicTypeMapForResults - a PG::TypeMapByOid prefilled with decoders for common PostgreSQL column types

  • PG::BasicTypeMapBasedOnResult - a PG::TypeMapByOid prefilled with encoders for common PostgreSQL column types

  • PG::BasicTypeMapForQueries - a PG::TypeMapByClass prefilled with encoders for common Ruby value classes

Contributing

To report bugs, suggest features, or check out the source with Git, check out the project page.

After checking out the source, run:

$ rake newb

This task will install any missing dependencies, run the tests/specs, and generate the API documentation.

The current maintainers are Michael Granger <ged@FaerieMUD.org> and Lars Kanis <lars@greiz-reinsdorf.de>.

Copying

Copyright © 1997-2019 by the authors.

  • Jeff Davis <ruby-pg@j-davis.com>

  • Guy Decoux (ts) <decoux@moulon.inra.fr>

  • Michael Granger <ged@FaerieMUD.org>

  • Lars Kanis <lars@greiz-reinsdorf.de>

  • Dave Lee

  • Eiji Matsumoto <usagi@ruby.club.or.jp>

  • Yukihiro Matsumoto <matz@ruby-lang.org>

  • Noboru Saitou <noborus@netlab.jp>

You may redistribute this software under the same terms as Ruby itself; see www.ruby-lang.org/en/about/license.txt or the BSDL file in the source for details.

Portions of the code are from the PostgreSQL project, and are distributed under the terms of the PostgreSQL license, included in the file POSTGRES.

Portions copyright LAIKA, Inc.

Acknowledgments

See Contributors.rdoc for the many additional fine people that have contributed to this library over the years.

We are thankful to the people at the ruby-list and ruby-dev mailing lists. And to the people who developed PostgreSQL.

  • ruby用pgsql做orm的时候,需要安装ruby-pg库,默认编译安装会提示缺少xx头文件   max下,我用的傻瓜式pgsql.app gem install pg -- --with-pg-config='/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config'   debian下 sudo apt-get instal

  • 原文网址:https://ryanbigg.com/2014/10/ubuntu-ruby-ruby-install-chruby-and-you Ubuntu,Ruby,ruby-install,chruby,Rails和You 2014年10月5日 最后更新:2017年7月13日 本初学者指南将使用chruby + ruby​​-install和Rails 5.1.2为您的机器设置Ruby 2

  • $ sudo gem install mysql2 --'--with-mysql-config=/opt/mysql/bin/mysql_config' $ sudo gem install mysql --'--with-mysql-config=/opt/mysql/bin/mysql_config' 测试 $ cat testmysql2.rb require 'mysql2' clien

  • 我曾经使用该软件的第二个版本,没有任何问题.在我的上一次申请中,我决定使用最新的“思考 – 狮身人面像”.我有一个奇怪的错误. > NoMethodError in Adverts#index undefined method `next_result' for > #<:client:0xac86a54> 我的宝石文件 gem 'rails','3.2.11' gem 'pg','0.14.0'

  • gem install pg -v '0.18.2' Building native extensions. This could take a while... ERROR: Error installing pg: ERROR: Failed to build gem native extension. current directory: /Users/bevan/.r

  • sudo apt-get install libpq-dev sudo gem install pg -v '0.21.0' 转载于:https://www.cnblogs.com/Alex0111/p/7551341.html

 相关资料
  • Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言 目录结构 /usr/ruby: Ruby 二进制命令路径 /usr/lib/ruby: Ruby 扩展、依赖等目录

  • Ruby是一种跨平台、面向对象的动态类型编程语言。Ruby 体现了表达的一致性和简单性,它不仅是一门编程语言,更是表达想法的一种简练方式。 Ruby的作者于1993年2月24日开始编辑Ruby,直至1995年12月才正式公开发布于fj(新聞群組)。之所以称为Ruby,是因为Perl的发音与6月的诞生石pearl(珍珠)相同,因此Ruby以7月的诞生石ruby(红宝石)命名。 Ruby明显比其他类似

  • Ruby++ 是一个调用 Ruby API 的 C++ 接口。 It supports the development of an extension-library and Ruby embedded program.

  • cPanel的这个接口允许您创建和安装Ruby on Rails应用程序。 如果您开发了Ruby on Rails应用程序,则可以使用此界面将其部署到服务器。 要创建Ruby on Rails应用程序,请按照下列步骤操作 - Step 1 - 单击cPanel Home的Software Section下的Ruby on Rails。 Step 2 - 在Ruby on Rails接口中,您将找到

  • 主要内容:Gem,gem 包的构建,修改国内源RubyGems 是 Ruby 的一个包管理器,它提供一个分发 Ruby 程序和库的标准格式,还提供一个管理程序包安装的工具。 RubyGems 旨在方便地管理 gem 安装的工具,以及用于分发 gem 的服务器。这类似于 Ubuntu 下的apt-get, Centos 的 yum,Python 的 pip。 RubyGems大约创建于2003年11月,从Ruby 1.9版起成为Ruby标准库的

  • 主要内容:环境配置,使用 Ruby 解析 JSON,input.json 文件,实例本章节我们将为大家介绍如何使用 Ruby 语言来编码和解码 JSON 对象。 环境配置 在使用 Ruby 编码或解码 JSON 数据前,我们需要先安装 Ruby JSON 模块。在安装该模块前你需要先安装 Ruby gem,我们使用 Ruby gem 安装 JSON 模块。 但是,如果你使用的是最新版本的 Ruby,可能已经安装了 gem,解析来我们就可以使用以下命令来安装Ruby JSON 模块

  • 主要内容:实例前面一章节我们介绍了 Ruby DBI 的使用。这章节我们技术 Ruby 连接 Mysql 更高效的驱动 mysql2,目前也推荐使用这种方式连接 MySql。 安装 mysql2 驱动: gem install mysql2 你需要使用 –with-mysql-config 配置 mysql_config 的路径,如: –with-mysql-config=/some/random/path/b

  • 主要内容:语法,yield 语句,实例,实例,块和方法,实例,实例,BEGIN 和 END 块,实例您已经知道 Ruby 如何定义方法以及您如何调用方法。类似地,Ruby 有一个块的概念。 块由大量的代码组成。 您需要给块取个名称。 块中的代码总是包含在大括号 {} 内。 块总是从与其具有相同名称的函数调用。这意味着如果您的块名称为 test,那么您要使用函数 test 来调用这个块。 您可以使用 yield 语句来调用块。 语法 block_name{ statement1 statement