[odb-users] Quering with empty string parameter error. Microsoft SQL Server

Andrey Paraskevopulo andrey.psv at gmail.com
Fri Jun 28 10:12:09 EDT 2019


Problem:
Unable to use empty string query parameters for the VARCHAR(MAX) fields.

Example:
db.query(odb::query<Data>::name == "/*or std::string()*/);
result: exception: COUNT field incorrect or syntax error

Condition:
- mssql
- odb 2.4
- field type is VARCHAR(MAX)
- query condition (not persist parameter)

As I understand, the problem that param in statement exists("... WHERE
name=?"), but the value in not binded.

Possible solutions:

1. Init buffer data for the empty string with empty size (for the query
param)
file: query.cxx
    void long_query_param_impl::
    copy ()
    {
...
//replace
        if (chunk != chunk_null)
        {
          if (buf_.capacity () < size_ + n)
            buf_.capacity (size_ + n, size_);

          memcpy (buf_.data () + size_, buf, n);
          size_ += n;
        }
//with
        if (chunk != chunk_null)
        {
          if (n == 0)
          {
            if (buf_.capacity() == 0 && buf != nullptr)
            {
               assert(size_ == 0);
               buf_.capacity(size_ + 1, size_);
            }
          }
          else
          {
            if (buf_.capacity() < size_ + n)
               buf_.capacity(size_ + n, size_);

            memcpy(buf_.data() + size_, buf, n);
            size_ += n;
          }
        }

Remark: solution does not affect Blob variables (they have the null buf for
the empty values). May be the query problem can be reproduced for the Blobs
too.

2. Disable skip empty binds (with null buffer)

file: statement.cxx
    void statement::
    bind_param (bind* b, size_t n)
    {
      SQLRETURN r;

      SQLUSMALLINT i (0);
      for (bind* end (b + n); b != end; ++b)
      {
->//        if (b->buffer == 0) // Skip NULL entries.
->//          continue;

This solves the problem, but for me it hard to predict side effects.
In the same time, it seems that in pgsql null entries are not skipped in
the bind_param function.

Andrey Paraskevopulo


More information about the odb-users mailing list