2 Replies Latest reply on Mar 25, 2020 2:37 PM by John Pulley

    What c++ data type can be used with inserter::addrow to hold a value or hyperapi::null.

    John Pulley

      I'm pulling data from an existing database where many of the columns can be null.  I can't seem to find a C++ hyperapi data type to use for a field which can be either hyperapi::Timestamp or hyperapi::null.   I'm adding rows with over 100 columns, so I'd like to just prepare the data and call hyperapi::inserter::addRow instead of inserting data column by column.

        • 1. Re: What c++ data type can be used with inserter::addrow to hold a value or hyperapi::null.
          Jonas Kammerer

          Hello!


          This is the method to create a Column in the C++ hyperapi:

           

          /**
            * Creates a column.
            *
            * \param name The name
            * \param type The type
            * \param nullability The nullability
            */
          Column(Name name, SqlType type, Nullability nullability = Nullability::Nullable)

           

          To allow null values in a column, use the Nullability parameter: `Nullability::Nullable` which should already be default. This should work:


          ...

          hyperapi::TableDefinition def1("mytable");
          def1.addColumn({"a", hyperapi::SqlType::timestamp(), hyperapi::Nullability::Nullable}); // explicitly nullable
          def1.addColumn({"b", hyperapi::SqlType::timestamp()}); // implicitly nullable since the default parameter value is "Nullable"
          connection.getCatalog().createTable(def1);

          hyperapi::Inserter inserter(connection, def1);
          hyperapi::Timestamp timestamp1{{2019, 4, 24}, {14, 22, 1, 249}};
          hyperapi::Timestamp timestamp2{{2019, 5, 4}, {17, 13, 5}};
          inserter.addRow(timestamp1, timestamp2);
          inserter.addRow(timestamp2, hyperapi::null);
          inserter.addRow(hyperapi::null, hyperapi::null);
          inserter.execute();

           

           

          Let me know if this works for you.

           

          Cheers,
          Jonas

          • 2. Re: What c++ data type can be used with inserter::addrow to hold a value or hyperapi::null.
            John Pulley

            I found the solution I needed for C++, using the hyperapi::optional template.

             

            For example, if you need an integer column value which might be null or have a value, use a variable of the type "hyperapi::optional<int> ".  I used a small method I called "hyper_t to do the conversion.  Then a call to AddRow looked something like this:  inserter.addRow(hyper_t(value1), hyper_t(value2), hyper_t(another_value), .......);

             

            ...

             

            hyperapi::optional<int> CSTableauSupport::hyper_t(int* val)

            {

                 hyperapi::optional<int> empty;

             

                 // Return int or empty value.

                 if (val == NULL || *val < 0)  // Note: In my data, the numbers are all non-negative.  -1 is sometimes used instead null to indicate an empty field.

                      return empty;

                 else

                 return *val;

            }