PgKingdom

Generally, PgKingdom is a set of tools to generate SQL queries for PostgreSQL.

Installation

Add this line to your application's Gemfile:

gem 'pgkingdom'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pgkingdom

Docs

We used yard gem to handle code documentation. Just do:

$ git clone git://github.com/indiesoft/pgkingdom.git
$ cd pgkingdom
$ bundle install
$ yard docs

And then you can view it by opening path_to_pgkingdom_source/docs/index.html.

Usage stories

Ancient Scrolls

Currently this Gem has WIP status. To get all supported datatypes do:

PgKingdom.available_data_types
# => [:uuid, :boolean, :money, ... ]

If you ever need all PostgreSQL data types, sorted and grouped do:

PgKingdom.data_types
# => {
#  "uuid": [ :uuid ],
#  "boolean": [ :boolean ],
#  "numeric": [
#    [ :smallint, :integer, :bigint ],
#    [ :smallserial, :serial, ...],
#    [ ... ],
#  ],
#  ...
# }

Far far ago in a fairy SQL Kingdom powerful mages written magic scrolls. One day Princess found one and started to read...

Spells - containers (de)materialization

Scroll: The first spell you will learn is Table spell. It used to (de)materialize containers for any type of essence of matter. First, define a table:

@frog_cage = PgKingdom::Table.new :abra_cadabra do
  serial :id, :primary_key
  varchar :name, 50, :not_null
  boolean :prince, :default => 'FALSE'
end
# => #<PgKingdom::Table:...>

@frog_cage.schema # => :public

@frog_cage.name # => :abra_cadabra

Princess: But I don't want to create this frog cage at a festive fair!

(continued to read):

@frog_cage = PgKingdom::Table.new [:abra, :cadabra] do
  # ...
end

# Ensure that cage will be created at your secret depot
@frog_cage.schema # => :abra

@frog_cage.name # => :cadabra

Princess: Fine!

Scroll: ...let's prepare materialization spell for frog cage, do:

@frog_cage.create

Scroll: It will make fairy pen to write a new scroll with following spell:

CREATE TABLE IF NOT EXISTS public.abra_cadabra (
  id SERIAL PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  prince BOOLEAN DEFAULT FALSE
);

Scroll: If you're sure, that there is no frog cages near you, you can do:

@frog_cage.create! # => "CREATE TABLE public.abra_cadabra ( ... );\n"

Scroll: In case, when you're not sure that princess from a neighboring fairy kingdom have her own frog cage, you can do:

@frog_cage = PgKingdom::Table.new [:neighborhood, :abra_cadabra] do 
  # ...
end

@frog_cage.table_exists?
# => "SELECT to_regclass('neighborhood.abra_cadabra') IS NOT NULL AS table_exists;"

Scroll: Remember, if any container will make you tired, just do:

@frog_cage.drop # => "DROP TABLE IF EXISTS public.abra_cadabra;\n"

TODO: INSERT, DELETE

After some time of kissing different frogs...

Princess: I don't want to kiss frogs!....

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/indiesoft/pgkingdom.

License

The gem is available as open source under the terms of the MIT License.