Class: DataObjects::SqlServer::Command

Inherits:
Command
  • Object
show all
Defined in:
lib/do_sqlserver.rb

Constant Summary collapse

IDENTITY_ROWCOUNT_QUERY =

Theoretically, SCOPE_IDENTIY should be preferred, but there are cases where it returns a stale ID, and I don’t know why. IDENTITY_ROWCOUNT_QUERY = ‘SELECT SCOPE_IDENTITY(), @@ROWCOUNT’

'SELECT @@IDENTITY, @@ROWCOUNT'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



117
118
119
# File 'lib/do_sqlserver.rb', line 117

def types
  @types
end

Instance Method Details

#execute_non_query(*args) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/do_sqlserver.rb', line 123

def execute_non_query *args
  DataObjects::SqlServer.check_params @text, args
  begin
    handle = @connection.raw.execute(@text, *args)
  rescue DBI::DatabaseError => e
    handle = @connection.raw.handle
    handle.finish if handle && handle.respond_to?(:finish) && !handle.finished?
    DataObjects::SqlServer.raise_db_error(e, @text, args)
  end
  handle.finish if handle && handle.respond_to?(:finish) && !handle.finished?

  # Get the inserted ID and the count of affected rows:
  inserted_id, row_count = nil, nil
  if (handle = @connection.raw.execute(IDENTITY_ROWCOUNT_QUERY))
    row1 = Array(Array(handle)[0])
    inserted_id, row_count = row1[0].to_i, row1[1].to_i
    handle.finish
  end
  Result.new(self, row_count, inserted_id)
end

#execute_reader(*args) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/do_sqlserver.rb', line 144

def execute_reader *args
  DataObjects::SqlServer.check_params @text, args
  massage_limit_and_offset args
  begin
    handle = @connection.raw.execute(@text, *args)
  rescue DBI::DatabaseError => e
    handle = @connection.raw.handle
    DataObjects::SqlServer.raise_db_error(e, @text, args)
    handle.finish if handle && handle.respond_to?(:finish) && !handle.finished?
  rescue
    handle = @connection.raw.handle
    handle.finish if handle && handle.respond_to?(:finish) && !handle.finished?
    raise
  end
  Reader.new(self, handle)
end

#set_types(*t) ⇒ Object



119
120
121
# File 'lib/do_sqlserver.rb', line 119

def set_types *t
  @types = t.flatten
end