Class: Jackcess::Database::TableBuilderDSL Private

Inherits:
Object
  • Object
show all
Defined in:
lib/jackcess/database.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

DSL (Domain-Specific Language) for building table schemas.

This class provides a fluent interface for defining table columns and indexes when creating new tables. It’s used internally by #create_table.

Instance Method Summary collapse

Constructor Details

#initialize(builder) ⇒ TableBuilderDSL

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new table builder DSL instance.

Parameters:

  • builder (Java::ComHealthmarketscienceJackcess::TableBuilder)

    The underlying Java table builder



372
373
374
375
# File 'lib/jackcess/database.rb', line 372

def initialize(builder)
  @builder = builder
  @primary_key_column = nil
end

Instance Method Details

#column(name, type, options = {}) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Defines a column in the table.

Examples:

Define columns

db.create_table('Users') do |t|
  t.column 'ID', :long, auto_number: true
  t.column 'Name', :text, length: 255
  t.column 'Email', :text, length: 255
  t.column 'Age', :int
  t.column 'Balance', :currency
  t.column 'IsActive', :boolean
  t.column 'CreatedAt', :date_time
end

Parameters:

  • name (String, Symbol)

    The column name

  • type (Symbol)

    The column data type (:text, :memo, :byte, :int, :long, :float, :double, :currency, :date_time, :boolean, :binary, :guid)

  • options (Hash) (defaults to: {})

    Column options

Options Hash (options):

  • :length (Integer)

    The maximum length for text columns

  • :auto_number (Boolean) — default: false

    Whether this is an auto-incrementing column

Raises:

  • (ArgumentError)

    If name or type is invalid



399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/jackcess/database.rb', line 399

def column(name, type, options = {})
  raise ArgumentError, "Column name cannot be nil" if name.nil?
  raise ArgumentError, "Column name must be a String or Symbol" unless name.is_a?(String) || name.is_a?(Symbol)
  raise ArgumentError, "Column name cannot be empty" if name.to_s.empty?
  raise ArgumentError, "Column type cannot be nil" if type.nil?

  data_type = TypeConverter.ruby_type_to_data_type(type)
  col_builder = ColumnBuilder.new(name, data_type)

  col_builder.set_length(options[:length]) if options[:length]
  col_builder.set_auto_number(options[:auto_number]) if options[:auto_number]

  @builder.add_column(col_builder.to_column)
end

#primary_key(column_name) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Defines the primary key for the table.

Examples:

Set primary key

db.create_table('Users') do |t|
  t.column 'ID', :long, auto_number: true
  t.column 'Name', :text, length: 255
  t.primary_key 'ID'
end

Parameters:

  • column_name (String, Symbol)

    The name of the column to use as primary key

Raises:

  • (ArgumentError)

    If column_name is nil



428
429
430
431
432
433
434
435
436
437
# File 'lib/jackcess/database.rb', line 428

def primary_key(column_name)
  raise ArgumentError, "Primary key column name cannot be nil" if column_name.nil?

  index_builder = com.healthmarketscience.jackcess.IndexBuilder.new(
    com.healthmarketscience.jackcess.IndexBuilder::PRIMARY_KEY_NAME
  )
  index_builder.add_columns(column_name)
  index_builder.set_primary_key
  @builder.add_index(index_builder)
end