Class: DBI::DBD::MSSQL::Statement

Inherits:
BaseStatement show all
Defined in:
lib/dbd/mssql/statement.rb

Instance Attribute Summary

Attributes inherited from BaseStatement

#raise_error

Instance Method Summary collapse

Methods inherited from BaseStatement

#[], #[]=, #bind_params, #fetch_all, #fetch_many, #fetch_scroll

Constructor Details

#initialize(statement, db) ⇒ Statement

Returns a new instance of Statement.



7
8
9
10
11
12
13
14
15
# File 'lib/dbd/mssql/statement.rb', line 7

def initialize(statement, db)
  @connection = db.current_connection;
  @command = @connection.create_command
  @statement = statement.to_s
  @command.command_text = @statement.to_clr_string
  @command.transaction = db.current_transaction if db.has_transaction?
  @current_index = 0
  @db = db
end

Instance Method Details

#bind_param(name, value, attribs = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dbd/mssql/statement.rb', line 17

def bind_param(name, value, attribs={})
  unless name.to_s.empty?
    parameter = @command.create_parameter
    parm_name = name.to_s.to_clr_string
    parameter.ParameterName = parm_name
    val =  value.is_a?(String) ? value.to_clr_string : value #(value || System::DBNull.value)
    parameter.Value = val
    if @command.parameters.contains(parm_name)
      @command.parameters[parm_name] = parameter
    else
      @command.parameters.add parameter
    end                                            
  end
end

#cancelObject



65
66
67
68
# File 'lib/dbd/mssql/statement.rb', line 65

def cancel
  @reader.close if @reader and not @reader.is_closed
  @command.cancel
end

#column_infoObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dbd/mssql/statement.rb', line 75

def column_info
  infos = schema.rows.collect do |row|
    name = row["ColumnName"]
    def_val_col = row.table.columns[name]
    def_val = def_val_col.nil? ? nil : def_val_col.default_value
    dtn = row["DataTypeName"].to_s.upcase
    {

        'name' => name.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' => %w(varchar nvarchar char nchar text ntext).include?(dtn.downcase) ? row["ColumnSize"] : row["NumericPrecision"],
        'default' => def_val,
        'scale' => %w(varchar nvarchar char nchar text ntext).include?(dtn.downcase) ? nil : row["NumericScale"]  ,
        'nullable' => row["AllowDBNull"],
        'primary' => schema.primary_key.select { |pk| pk.column_name == name }.size > 0,
        'unique' => row["IsUnique"]
    }
  end 
  infos
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
end

#executeObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dbd/mssql/statement.rb', line 32

def execute
  @current_index = 0
  @rows = []
  @schema = nil
  @reader.close if @reader and not @reader.is_closed
  @reader = @command.execute_reader
  schema

  unless SQL.query?(@statement.to_s)
    finish
  end
  @reader.records_affected
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
end

#fetchObject



48
49
50
51
52
53
54
55
56
# File 'lib/dbd/mssql/statement.rb', line 48

def fetch
  res = nil
  if  @reader.read
    res = read_row(@reader)
  end
  res
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
end

#finishObject



58
59
60
61
62
63
# File 'lib/dbd/mssql/statement.rb', line 58

def finish
  @reader.close if @reader and not @reader.is_closed
  
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
end

#rowsObject



101
102
103
104
105
# File 'lib/dbd/mssql/statement.rb', line 101

def rows
  return 0 if @reader.nil?
  res = @reader.records_affected
  res == -1 ? 0 : res
end

#schemaObject



70
71
72
73
# File 'lib/dbd/mssql/statement.rb', line 70

def schema

  @schema ||= @reader.get_schema_table || System::Data::DataTable.new
end