Sha256: f238a892cd15a74080f89168269c9a2c967a2a971dc22a1477a6385c3f5a6220

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

module SequelRails
  module Storage
    class Jdbc < Abstract

      def _is_mysql?
        config['adapter'].match(/^jdbc:mysql/)
      end

      def _is_postgres?
        config['adapter'].match(/^jdbc:postgresql/)
      end

      def _is_sqlite?
        config['adapter'].match(/^jdbc:sqlite/)
      end

      def _root_url
        config['url'].scan(/^jdbc:mysql:\/\/\w*:?\d*/).first
      end

      def db_name
        config['database']
      end

      def _params
        config['url'].scan(/\?.*$/).first
      end

      def _create
        if _is_sqlite?
          return if in_memory?
          ::Sequel.connect config['url']
        elsif _is_mysql?
          ::Sequel.connect("#{_root_url}#{_params}") do |db|
            db.execute("CREATE DATABASE IF NOT EXISTS `#{db_name}` DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}")
          end
        elsif _is_postgres?
          system("createdb #{db_name}")
        end
      end

      def _drop
        if _is_sqlite?
          return if in_memory?
          ::Sequel.connect config['url']
        elsif _is_mysql?
          ::Sequel.connect("#{_root_url}#{_params}") do |db|
            db.execute("DROP DATABASE IF EXISTS `#{db_name}`")
          end
        elsif _is_postgres?
          system("dropdb #{db_name}")
        end
      end

      private

      def collation
        @collation ||= config['collation'] || ENV['COLLATION'] || 'utf8_unicode_ci'
      end

      def in_memory?
        return false unless _is_sqlite?
        database == ":memory:"
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sequel-rails-0.5.0 lib/sequel_rails/storage/jdbc.rb
sequel-rails-0.4.4 lib/sequel_rails/storage/jdbc.rb
sequel-rails-0.4.3 lib/sequel_rails/storage/jdbc.rb
sequel-rails-0.4.2 lib/sequel_rails/storage/jdbc.rb
sequel-rails-0.4.1 lib/sequel_rails/storage/jdbc.rb