Read column values¶
The row callback receives each row of a result set. Read a field with
row[i].as<T>(), where i is the column's position in the SELECT list and
T matches the column's SQL type.
Read numbers¶
Use the C++ type with the same size as the column:
static void on_row(void *, const tdsl::tds_colmetadata_token &, const tdsl::tdsl_row & row) {
const bool active = row [0].as<bool>(); // bit
const tdsl::int16_t year = row [1].as<tdsl::int16_t>(); // smallint
const tdsl::int32_t id = row [2].as<tdsl::int32_t>(); // int
const tdsl::int64_t total = row [3].as<tdsl::int64_t>(); // bigint
const float temp = row [4].as<float>(); // real
}
The size must match
A type that is wider than the column reads as zero: as<tdsl::int32_t>()
on a smallint column returns 0. If a number reads as zero, check the
column type. The full mapping is in
Data types.
Read strings¶
varchar and char columns read as tdsl::char_view. The view is not
null-terminated, so print it with its size:
const auto name = row [1].as<tdsl::char_view>();
Serial.write(name.data(), name.size());
// or: printf("%.*s", static_cast<int>(name.size()), name.data());
nvarchar and nchar columns are UTF-16. Read them as tdsl::u16le_view,
whose elements are UTF-16 code units:
const auto city = row [2].as<tdsl::u16le_view>();
for (const char16_t c : city) {
Serial.print(static_cast<char>(c)); // fine for ASCII text
}
The views point into the network buffer. Copy the data if you need it after the callback returns.
Tell NULL from zero¶
A NULL field reads as zero or as an empty view. Check is_null() first:
if (row [3].is_null()) {
Serial.println(F("no value"));
} else {
Serial.println(row [3].as<tdsl::int32_t>());
}
Read raw bytes¶
For binary, varbinary, uniqueidentifier and timestamp columns, the
field itself is a view of the bytes:
const auto & blob = row [0];
for (tdsl::uint32_t i = 0; i < blob.size_bytes(); i++) {
Serial.print(blob.data() [i], HEX);
}
Read money¶
const auto price = row [0].as<tdsl::sql_money>();
// price.integer() is the whole part, price.fraction() four decimal digits
Get the column names¶
Column names aren't read by default, to save memory. Turn them on before the query, and read them from the column metadata in the callback:
driver.option_set_read_column_names(true);
static void on_row(void *, const tdsl::tds_colmetadata_token & meta, const tdsl::tdsl_row & row) {
for (tdsl::uint32_t i = 0; i < meta.columns.size(); i++) {
for (const char16_t c : meta.column_names [i]) { // UTF-16
Serial.print(static_cast<char>(c));
}
Serial.print(' ');
}
Serial.println();
}
Pass your own data to the callback¶
The last argument of execute_query (and of execute_rpc) is a pointer that
tdslite hands to every call of the row callback, as its first argument. Use it
instead of a global variable:
struct totals { tdsl::int64_t sum = 0; };
static void add_up(void * user, const tdsl::tds_colmetadata_token &, const tdsl::tdsl_row & row) {
static_cast<totals *>(user)->sum += row [0].as<tdsl::int32_t>();
}
totals t;
driver.execute_query("SELECT stock FROM fruits", add_up, &t);
Related¶
- Data types: how each SQL type is read, and how SQL Server converts the newer types for tdslite.
- Limits: every row must fit into the network buffer.