Class: DBI::DBD::SQLAnywhere::Statement

Inherits:
BaseStatement
  • Object
show all
Includes:
Utility
Defined in:
lib/dbd/sqlanywhere/statement.rb

Constant Summary collapse

SQLANY_NATIVE_TYPES =

Conversion table between SQL Anywhere E-SQL types and DBI SQL Types

{
	 0   => DBI::SQL_LONGVARCHAR,
	 384 => DBI::SQL_DATE,
	 388 => DBI::SQL_TIME,
	 392 => DBI::SQL_TIMESTAMP,
	 448 => DBI::SQL_VARCHAR,
	 452 => DBI::SQL_CHAR,
	 456 => DBI::SQL_LONGVARCHAR,
	 460 => DBI::SQL_LONGVARCHAR,
	 480 => DBI::SQL_DOUBLE,
	 482 => DBI::SQL_FLOAT,
	 484 => DBI::SQL_DECIMAL,
	 496 => DBI::SQL_INTEGER,
	 500 => DBI::SQL_SMALLINT,
	 524 => DBI::SQL_BINARY,
	 528 => DBI::SQL_LONGVARBINARY,
	 604 => DBI::SQL_TINYINT,
	 608 => DBI::SQL_BIGINT,
	 612 => DBI::SQL_INTEGER,
	 616 => DBI::SQL_SMALLINT,
	 620 => DBI::SQL_BIGINT,
	 624 => DBI::SQL_BIT,
	 640 => DBI::SQL_LONGVARCHAR
}

Constants included from Utility

Utility::INPUT_ONLY, Utility::INPUT_OUTPUT, Utility::NO_DIRECTION, Utility::OUTPUT_ONLY

Instance Method Summary collapse

Methods included from Utility

#do_bind!

Constructor Details

#initialize(handle, conn, bound = {}) ⇒ Statement

Returns a new instance of Statement.



56
57
58
59
60
61
62
# File 'lib/dbd/sqlanywhere/statement.rb', line 56

def initialize(handle, conn, bound = {} )
	 @handle = handle
	 @conn = conn
	 @arr = []
	 @bound = bound
	 @offset = -1
end

Instance Method Details

#__bound_param(name) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/dbd/sqlanywhere/statement.rb', line 64

def __bound_param(name)
	 if !@bound[name].nil?
	    res, param = SA.instance.api.sqlany_get_bind_param_info(@handle, @bound[name])
	    raise error() if res == 0
	    return param.get_output()
	 end 
end

#bind_param(param, value, attribs) ⇒ Object

Although SQL Anywhere allows multiple result sets, the @fetchable variable disallows there use

def __next_resultset

return SA.instance.api.sqlany_get_next_result(@handle)

end



79
80
81
82
83
84
85
# File 'lib/dbd/sqlanywhere/statement.rb', line 79

def bind_param(param, value, attribs)
	 param -= 1
	 res, param_description = SA.instance.api.sqlany_describe_bind_param(@handle, param)
	 raise error() if res == 0 or param_description.nil?
	 do_bind!(@handle, param_description, value, param, @bound)
	 param_description.finish
end

#cancelObject



190
191
# File 'lib/dbd/sqlanywhere/statement.rb', line 190

def cancel
end

#column_infoObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/dbd/sqlanywhere/statement.rb', line 144

def column_info
	 columns = []
	 if !@handle.nil?
	    max_cols = SA.instance.api.sqlany_num_cols(@handle)
	    raise error() if max_cols == -1
	    max_cols.times do |cols|
  columns << {}
  res, holder, col_name, type, native_type, precision, scale, max_size, nullable = SA.instance.api.sqlany_get_column_info(@handle, cols)
  raise error() if res == 0 or col_name.nil?
  columns[cols]["name"] = col_name
  sql_type = SQLANY_NATIVE_TYPES[native_type]
  columns[cols]["sql_type"] = sql_type
  columns[cols]["type_name"] = DBI::SQL_TYPE_NAMES[sql_type]
         if [ DBI::SQL_CHAR, DBI::SQL_VARCHAR,
              DBI::SQL_BINARY, DBI::SQL_VARBINARY ].include?(sql_type)
             precision = max_size
         end
         if precision != 0 or scale != 0
             columns[cols]["precision"] = precision
             columns[cols]["scale"] = scale
         end
  columns[cols]["nullable"] = (nullable == 0)

  columns[cols]["dbi_type"] = DBI::Type::Boolean if sql_type == DBI::SQL_BIT
	    end
	 end
	 return columns  
end

#executeObject



87
88
89
90
# File 'lib/dbd/sqlanywhere/statement.rb', line 87

def execute()
	 res = SA.instance.api.sqlany_execute(@handle)
	 raise error() if res == 0
end

#fetchObject



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

def fetch()
	 return fetch_scroll(DBI::SQL_FETCH_NEXT, 1)  
end

#fetch_allObject



96
97
98
99
100
101
102
103
104
# File 'lib/dbd/sqlanywhere/statement.rb', line 96

def fetch_all()
	 rows = []
	 loop {
	    new_row = self.fetch_scroll(DBI::SQL_FETCH_NEXT, 1)
	    break if new_row.nil?
	    rows << new_row.clone
	 }
	 return rows
end

#fetch_scroll(direction, offset) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/dbd/sqlanywhere/statement.rb', line 106

def fetch_scroll(direction, offset)
	 res = 0
	 new_offset = @offset

	 case direction
	    when DBI::SQL_FETCH_NEXT
  res = SA.instance.api.sqlany_fetch_next(@handle)
  new_offset += 1
	    when DBI::SQL_FETCH_PRIOR
  res = SA.instance.api.sqlany_fetch_absolute(@handle, @offset)
  new_offset -= 1
	    when DBI::SQL_FETCH_FIRST
  res = SA.instance.api.sqlany_fetch_absolute(@handle, 1)
  new_offset = 0
	    when DBI::SQL_FETCH_LAST
  res = SA.instance.api.sqlany_fetch_absolute(@handle, -1)
  new_offset = self.rows() - 1
	    when DBI::SQL_FETCH_ABSOLUTE
  res = SA.instance.api.sqlany_fetch_absolute(@handle, offset)
  if offset <= 0
		  new_offset = self.rows() + offset 
  else
		  new_offset = offset - 1
  end
	    when DBI::SQL_FETCH_RELATIVE
  res = SA.instance.api.sqlany_fetch_absolute(@handle, @offset + offset + 1)
  new_offset += offset
	 end

	 if (res == 1)
	    retrieve_row_data()
	    @offset = new_offset
	    return @arr
	 else
	    return nil
	 end   
end

#finishObject



183
184
185
186
187
188
# File 'lib/dbd/sqlanywhere/statement.rb', line 183

def finish
	 if !@handle.nil?
	    SA.instance.api.sqlany_free_stmt(@handle);
	    @handle = nil
	 end
end

#rowsObject



173
174
175
176
177
178
179
180
181
# File 'lib/dbd/sqlanywhere/statement.rb', line 173

def rows
	 if !@handle.nil?
	    res = SA.instance.api.sqlany_affected_rows(@handle)
	    raise error() if res == -1
	    return res	 
	 else
	    0
	 end
end