Class: DBI::DBD::MSSQL::Database

Inherits:
BaseDatabase show all
Defined in:
lib/dbd/mssql/database.rb

Instance Method Summary collapse

Methods inherited from BaseDatabase

#[], #execute

Constructor Details

#initialize(dbd_db, attr) ⇒ Database

Returns a new instance of Database.



8
9
10
11
# File 'lib/dbd/mssql/database.rb', line 8

def initialize(dbd_db, attr)
  super
  self['AutoCommit'] = true
end

Instance Method Details

#[]=(attr, value) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dbd/mssql/database.rb', line 133

def []=(attr, value)
  if attr == 'AutoCommit' and @attr[attr] != value
    @trans.commit if @trans
    unless value
      @trans = @handle.begin_transaction unless @trans
    else
      @trans = nil
    end
  end
  @attr[attr] = value
end

#columns(table) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dbd/mssql/database.rb', line 54

def columns(table)
  sql = "select object_name(c.object_id) as table_name, c.column_id, c.name, type_name(system_type_id) as sql_type,
      max_length, is_nullable, precision, scale, object_definition(c.default_object_id) as default_value,
      convert(bit,(Select COUNT(*) from sys.indexes as i
        inner join sys.index_columns as ic
          on ic.index_id = i.index_id and ic.object_id = i.object_id
        inner join sys.columns as c2 on ic.column_id = c2.column_id and i.object_id = c2.object_id
      WHERE i.is_primary_key = 1 and ic.column_id = c.column_id and i.object_id=c.object_id)) as is_primary_key,               
      convert(bit,(Select COUNT(*) from sys.indexes as i
        inner join sys.index_columns as ic
          on ic.index_id = i.index_id and ic.object_id = i.object_id
        inner join sys.columns as c2 on ic.column_id = c2.column_id and i.object_id = c2.object_id
      WHERE i.is_primary_key = 0
        and i.is_unique_constraint = 0 and ic.column_id = c.column_id and i.object_id=c.object_id)) as is_index,
      convert(bit,(Select Count(*) from sys.indexes as i inner join sys.index_columns as ic
          on ic.index_id = i.index_id and ic.object_id = i.object_id
        inner join sys.columns as c2 on ic.column_id = c2.column_id and i.object_id = c2.object_id
      WHERE (i.is_unique_constraint = 1) and ic.column_id = c.column_id and i.object_id=c.object_id)) as is_unique
      from sys.columns as c
      WHERE object_name(c.object_id) = @table_name
      order by table_name"
  stmt = prepare(sql)
  stmt.bind_param("table_name", table)
  stmt.execute
  ret = stmt.fetch_all.collect do |row|
    dtn = row[3].upcase
    ColumnInfo.new({
            'name' => row[2].to_s,
            'dbi_type' => MSSQL_TYPEMAP[dtn],
            'mssql_type_name' => dtn,
            'sql_type' =>MSSQL_TO_XOPEN[dtn][0],
            'type_name' => DBI::SQL_TYPE_NAMES[MSSQL_TO_XOPEN[dtn][0]],
            'precision' => row[6].zero? ? row[4] : row[6],
            'default' => row[8],
            'scale' => row[7],
            'nullable' => row[5],
            'primary' => row[9],
            'indexed' => row[10],
            'unique' => row[11]
    })
  end
  stmt.finish
  ret
end

#commitObject



99
100
101
102
103
104
105
106
# File 'lib/dbd/mssql/database.rb', line 99

def commit
  unless @attr['AutoCommit']
    @trans.commit if @trans
    @trans = @handle.begin_transaction
  end
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
end

#current_connectionObject



129
130
131
# File 'lib/dbd/mssql/database.rb', line 129

def current_connection
  @handle
end

#current_transactionObject



46
47
48
# File 'lib/dbd/mssql/database.rb', line 46

def current_transaction
  @trans
end

#disconnectObject



13
14
15
16
17
18
19
20
21
# File 'lib/dbd/mssql/database.rb', line 13

def disconnect
  unless @trans.nil?
    @trans.rollback unless @attr['AutoCommit']
    @trans = nil
  end
  @handle.close
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
end

#do(stmt, bindvars = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/dbd/mssql/database.rb', line 117

def do(stmt, bindvars={})
  st = prepare(stmt)
  bindvars.each { |k, v| st.bind_param(k, v) }
  res = st.execute
  st.finish
  return res
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
ensure
  st.finish if st
end

#has_transaction?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/dbd/mssql/database.rb', line 50

def has_transaction?
  !@trans.nil?
end

#pingObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dbd/mssql/database.rb', line 27

def ping
  cmd = @handle.create_command
  cmd.command_text = "Select 1"
  begin
    cmd.execute_non_query
    return true
  rescue
    return false
  end
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
end

#prepare(statement) ⇒ Object



23
24
25
# File 'lib/dbd/mssql/database.rb', line 23

def prepare(statement)
  Statement.new(statement, self)
end

#rollbackObject



108
109
110
111
112
113
114
115
# File 'lib/dbd/mssql/database.rb', line 108

def rollback
  unless @attr['AutoCommit']
    @trans.rollback if @trans
    @trans = @handle.begin_transaction
  end
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
end

#tablesObject



40
41
42
43
44
# File 'lib/dbd/mssql/database.rb', line 40

def tables
  @handle.get_schema("Tables").rows.collect { |row| row["TABLE_NAME"].to_s }
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
end