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

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

Instance Method Summary collapse

Constructor Details

#initialize(statement, db) ⇒ Statement

Returns a new instance of Statement.



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

def initialize(statement, db)
  @connection = db.current_connection;
  @command = @connection.create_command
  @previous_statement = @connection.instance_variable_get :@current_statement
  @connection.instance_variable_set :@current_statement, self
  @statement = DBI::Adonet::PreparedStatement.new(db, statement.to_s).bind
  @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



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

def bind_param(name, value, attribs={})
  unless name.to_s.empty?
    name = "p#{name}" if name.is_a? Numeric
    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



92
93
94
95
# File 'lib/dbd/mssql/statement.rb', line 92

def cancel
  finish
  @command.cancel
end

#column_infoObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dbd/mssql/statement.rb', line 101

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

#do_finish(check_pending_fetches) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dbd/mssql/statement.rb', line 75

def do_finish(check_pending_fetches)
  if @reader and not @reader.is_closed
    if check_pending_fetches
      @pending_fetches = []
      while (res = fetch) != nil
        @pending_fetches << res
      end
    else
      @pending_fetches = nil
    end
  @reader.close
end
  
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
end

#executeObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dbd/mssql/statement.rb', line 35

def execute
  @previous_statement.finish if @previous_statement
  
  @current_index = 0
  @rows = []
  @schema = nil
  finish
  @reader = @command.execute_reader
  schema
  unless SQL.query?(@statement.to_s)
    do_finish true
  end
  @reader.records_affected
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
end

#fetchObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dbd/mssql/statement.rb', line 52

def fetch
  if @reader and @reader.is_closed
    if @pending_fetches
      return @pending_fetches.shift
    else
      return nil
    end
  end
  res = nil
  if  @reader.read
    res = read_row(@reader)
  end
  puts "result: #{res}, \nclass: #{res.class}" if $quoting_test
  
  res
rescue RuntimeError, System::Data::SqlClient::SqlException => err
  raise DBI::DatabaseError.new(err.message)
end

#finishObject



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

def finish
  do_finish false
end

#rowsObject



127
128
129
130
131
# File 'lib/dbd/mssql/statement.rb', line 127

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

#schemaObject



97
98
99
# File 'lib/dbd/mssql/statement.rb', line 97

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