Skip to content

Pull request for postgresql-plruby #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,29 @@ PL/ruby
Prerequisite
------------

> * ruby 1.8.7 or later (maybe 1.8.6 too)
> * ruby 1.8.7 or later
> * postgresql >= 7.3

All PostgreSQL headers need to be installed. Command (see `INSTALL` in the
directory postgresql-7.x.y)
All PostgreSQL development headers need to be installed. If you installed
using a package manager of some sort, be sure to install the "dev" package.

If you built from source you can run the following command:

make install-all-headers

Installation
------------

If you're installing the gem:

gem install postgresql-plruby

If you want to allow the ability to require external libraries:

gem install postgresql-plruby -- --with-safe-level=0

If you're doing things the old fashioned way:

ruby extconf.rb
make
make install
Expand Down Expand Up @@ -92,8 +104,7 @@ Test (and examples)
language 'C';

create trusted language 'plruby'
handler plruby_call_handler
lancompiler 'PL/Ruby';
handler plruby_call_handler;


The `trusted` keyword on `create language` tells PostgreSQL,
Expand All @@ -102,6 +113,24 @@ Test (and examples)
absolutely safe, because there is nothing a normal user can do
with PL/Ruby, to get around access restrictions he/she has.

Sample Functions
----------------
CREATE FUNCTION ruby_max(int4, int4) RETURNS int4 AS '
if args[0] > args[1]
return args[0]
else
return args[1]
end
' LANGUAGE 'plruby';

select ruby_max(1,2);

CREATE FUNCTION ruby_max2(x int4, y int4) RETURNS int4 AS '
return x > y ? x : y
' LANGUAGE 'plruby';

select ruby_max2(7,8);

Documentation
-------------

Expand Down
48 changes: 48 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'rake'
require 'rake/testtask'
require 'rake/clean'
require 'rbconfig'
include Config

# TODO: Reorganize code to use Rake compiler.

CLEAN.include(
'**/*.gem', # Gem files
'**/*.rbc', # Rubinius
'**/*.o', # C object file
'**/*.log', # Ruby extension build log
'**/Makefile', # C Makefile
'**/conftest.dSYM', # OS X build directory
"**/*.#{CONFIG['DLEXT']}" # C shared object
)

desc 'Build the postgresql-plruby source code'
task :build => [:clean] do
ruby "extconf.rb"
sh "make"
end

namespace :gem do
desc 'Create the gem'
task :create => [:clean] do
spec = eval(IO.read('postgresql-plruby.gemspec'))
Gem::Builder.new(spec).build
end

desc 'Install the gem'
task :install => [:create] do
file = Dir["*.gem"].first
sh "gem install #{file}"
end
end

# TODO: Reorganize tests to make them work.
Rake::TestTask.new do |t|
task :test => [:build]
t.libs << 'ext'
t.test_files = FileList['test/**/*.rb']
t.warning = true
t.verbose = true
end

task :default => :test
7 changes: 5 additions & 2 deletions extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def create_lang(version = 74, suffix = '', safe = 0)
language '#{language}';

create #{trusted} language 'plruby#{suffix}'
handler plruby#{suffix}_call_handler
lancompiler 'PL/Ruby#{suffix}';
handler plruby#{suffix}_call_handler;

========================================================================
EOT
Expand Down Expand Up @@ -87,6 +86,7 @@ def rule(target, clean = nil)
$CFLAGS << " -I" << include_dir
$CFLAGS << " -I" << `#{pg_config} --includedir-server`.strip


if safe = with_config("safe-level")
safe = Integer(safe)
if safe < 0
Expand Down Expand Up @@ -141,6 +141,7 @@ def rule(target, clean = nil)
end

have_func("rb_block_call")
have_header("ruby/st.h")
have_header("st.h")

if version >= 74
Expand All @@ -156,6 +157,8 @@ def rule(target, clean = nil)
$CFLAGS += " -DPG_PL_TRYCATCH"
end

have_library('pq', 'PQconnectdb')

enable_conversion = false
if enable_conversion = enable_config("conversion", true)
$CFLAGS += " -DPLRUBY_ENABLE_CONVERSION"
Expand Down
55 changes: 55 additions & 0 deletions postgresql-plruby.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'rubygems'
require 'rbconfig'

Gem::Specification.new do |spec|
spec.name = 'globegit-postgresql-plruby'
spec.version = '0.5.4'
spec.authors = ['Akinori MUSHA', 'Guy Decoux']
spec.license = 'Ruby'
spec.email = '[email protected]'
spec.homepage = 'https://github.com/knu/postgresql-plruby'
spec.summary = 'Enable Ruby for use as a procedural language within PostgreSQL'
spec.test_files = Dir['test/test*']
spec.extensions = ['extconf.rb']
spec.files = Dir['**/*'].reject{ |f| f.include?('git') || f.include?('tmp') }

spec.rubyforge_project = 'plruby'

spec.extra_rdoc_files = [
'README.markdown',
'Changes'
] + Dir['ext/*.c']

spec.description = <<-EOF
PL/Ruby is a loadable procedural language for the PostgreSQL database
system that enables the Ruby language to create functions and trigger
procedures.
EOF

plruby_bin = 'plruby.' + Config::CONFIG['DLEXT']
plruby_dir = File.join(spec.name + '-' + spec.version.to_s, 'src')
path_to_binary = File.join(Gem.dir, 'gems', plruby_dir, plruby_bin)

possible_paths = Gem.path.map{ |path|
File.join(path, 'gems', plruby_dir, plruby_bin)
}

spec.post_install_message = <<-EOF

Now run the following commands from within a postgresql shell in order
to create the plruby language on in database server:

create function plruby_call_handler() returns language_handler
as '#{path_to_binary}'
language 'C';

create trusted language 'plruby'
handler plruby_call_handler;

NOTE: Your actual path to #{plruby_bin} may be different. Possible
paths to the plruby binary are:

#{possible_paths.join("\n ")}

EOF
end
1 change: 1 addition & 0 deletions src/conversions/basic/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
!enable_config("plruby-shared")
$LIBRUBYARG = ""
end
have_library('ruby18', 'ruby_init')
create_makefile('plruby/plruby_basic')
1 change: 1 addition & 0 deletions src/conversions/bitstring/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
!enable_config("plruby-shared")
$LIBRUBYARG = ""
end
have_library('ruby18', 'ruby_init')
create_makefile('plruby/plruby_bitstring')
6 changes: 6 additions & 0 deletions src/conversions/bitstring/plruby_bitstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,14 @@ name_(VALUE obj, VALUE a) \
}

BIT_OPERATOR(pl_bit_add, bitcat);
#if PG_PL_VERSION >= 91
/* avoid the names bitand and bitor, since they are C++ keywords */
BIT_OPERATOR(pl_bit_and, bit_and);
BIT_OPERATOR(pl_bit_or, bit_or);
#else
BIT_OPERATOR(pl_bit_and, bitand);
BIT_OPERATOR(pl_bit_or, bitor);
#endif
BIT_OPERATOR(pl_bit_xor, bitxor);

static VALUE
Expand Down
1 change: 1 addition & 0 deletions src/conversions/datetime/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
!enable_config("plruby-shared")
$LIBRUBYARG = ""
end
have_library('ruby18', 'ruby_init')
create_makefile('plruby/plruby_datetime')
1 change: 1 addition & 0 deletions src/conversions/geometry/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
!enable_config("plruby-shared")
$LIBRUBYARG = ""
end
have_library('ruby18', 'ruby_init')
create_makefile('plruby/plruby_geometry')
1 change: 1 addition & 0 deletions src/conversions/network/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
!enable_config("plruby-shared")
$LIBRUBYARG = ""
end
have_library('ruby18', 'ruby_init')
create_makefile('plruby/plruby_network')
Loading