Class: Sybase::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/sybase/connection.rb

Constant Summary collapse

PROPERTIES =
{
  :username    => [ CS_USERNAME,    :string ],
  :password    => [ CS_PASSWORD,    :string ],
  :appname     => [ CS_APPNAME,     :string ],
  :tds_version => [ CS_TDS_VERSION, :int    ],
  :hostname    => [ CS_HOSTNAME,    :string ]
}

Instance Method Summary collapse

Constructor Details

#initialize(context, opts = {}) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sybase/connection.rb', line 12

def initialize(context, opts ={})
  @context = context

  FFI::MemoryPointer.new(:pointer) { |ptr|
    Lib.check Lib.ct_con_alloc(context, ptr), "ct_con_alloc"
    @ptr = FFI::AutoPointer.new(ptr.read_pointer, Lib.method(:ct_con_drop))
  }

  opts.each do |key, value|
    self[key] = value
  end

  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/sybase/connection.rb', line 41

def [](key)
  property, type = property_type_for(key)
  case type
  when :string
    get_string_property property
  when :int
    get_int_property property
  end
end

#[]=(key, value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sybase/connection.rb', line 51

def []=(key, value)
  property, type = property_type_for(key)

  case type
  when :string
    set_string_property property, value
  when :int
    set_int_property property, value
  else
    raise Error, "invalid type: #{type.inspect}"
  end
end

#closeObject



37
38
39
# File 'lib/sybase/connection.rb', line 37

def close
  Lib.check Lib.ct_close(@ptr, CS_UNUSED), "ct_close"
end

#connect(server) ⇒ Object



64
65
66
67
68
69
# File 'lib/sybase/connection.rb', line 64

def connect(server)
  server = server.to_s
  Lib.check Lib.ct_connect(@ptr, server,  server.bytesize), "connect(#{server.inspect}) failed"

  self
end

#debug!Object



33
34
35
# File 'lib/sybase/connection.rb', line 33

def debug!
  Lib.check Lib.ct_debug(@context, to_ptr, CS_SET_FLAG, CS_DBG_ALL, nil, CS_UNUSED)
end

#to_ptrObject



71
72
73
# File 'lib/sybase/connection.rb', line 71

def to_ptr
  @ptr
end