Question

I am a complete newbie in C++ and I'm having difficulty in resolving the following situation:

Suppose we have a data dictionary table as the following:

---------------------------------------
tbl_name | fld_name | fld_typ | is_pk |
---------------------------------------
  X      |  fld1    |   INT   |    1  |
---------------------------------------
  X      |  fld2    | TEXT    |    0  | 
---------------------------------------
  X      |  fld3    | TEXT    |    0  | 
---------------------------------------
  Y      |  fld1    |   INT   |    1  |
---------------------------------------     
  Y      |  fld2    | TEXT    |    0  | 

I would like to create table 'X' in such way:

CREATE TABLE X (fld1 INT, fld2 TEXT, fld3 TEXT, PRIMARY KEY (fld1));

For now I have got a C++ function which does a select * from data_dictionary and adds all fields in a vector. The problem is how can I retrieve fields from the list to make my "CREATE TABLE" query?

I'm struggling to get all those fld_names, fld_typs for table_name = X to make my query. If anyone has got any idea, that would be appreciated!

Thanks heaps!

Here is the source code:

std::vector<std::string> gettableinfo(std::string table) {
    std::vector<std::string> list_table;
    string prep_query =
            string(
                    "SELECT table_name, column_name, data_type, is_pk, length, start_position FROM dd_table WHERE table_name = '")
                    + table + string("' ALLOW FILTERING;");
    boost::shared_ptr<cql::cql_query_t> select(
            new cql::cql_query_t(prep_query, cql::CQL_CONSISTENCY_ONE));
    query_result = session->query(select);
    query_result.wait();
    if (query_result.get().error.is_err()) {
        cout << "ERROR IN THE QUERY" << endl;
    } else {
        cout << "QUERY SUCCESSFUL: " + prep_query << endl;
    }
    if (query_result.get().result) {
        if (query_result.get().result != 0) {
            cql::cql_result_t& result = *query_result.get().result;
            std::size_t const columns(result.column_count());
            while (result.next()) {
                int counter = 0;
                for (std::size_t column(0); column != columns; ++column) {
                    cql::cql_column_type_enum type;
                    if (result.column_type(column, type)) {

                        switch (type) {
                        case cql::CQL_COLUMN_TYPE_INT: {
                            int value;
                            if (result.get_int(column, value)) {
                                list_table.push_back(
                                        static_cast<ostringstream*>(&(ostringstream()
                                                << value))->str());
                            } else {
                                std::cout
                                        << "failed to extract int for column \n";
                            }
                            break;
                        }
                        case cql::CQL_COLUMN_TYPE_VARCHAR: {
                            std::string value;
                            if (result.get_string(column, value)) {
                                list_table.push_back(value);
                            } else {
                                std::cout
                                        << "failed to extract text for column \n";
                            }
                            break;
                        }

                        }
                    }
                }
            }

        } else {
            cout << "0 ROW RETURNED" << endl;
        }
    }

    return list_table;
}
Était-ce utile?

La solution

In my database project, I have Field classes and Record classes.

The Field class provides functionality for extracting the data member from a result set and and also retrieving the field name.

class Field
{
  public:  
    virtual std::string get_field_name(void) const = 0;
    virtual void load_from_result_set(const Result_Set& rs,
                                      unsigned int column) = 0;
};

The table name is placed into the Record. The Record is a container of Field:

class Record
{
  public:
    virtual std::string get_table_name(void) const = 0;
    virtual void load_from_result_set(const Result_Set& rs) = 0;

  private:
     std::vector< boost::smart_ptr<Field> >  m_field_container;
};

The Record also supports visitors and iterators.

The various types of fields are classes derived from the Field base class.

Hope this helps.
{You can search StackOverflow for some of my questions: "C++ Thomas Matthews record field".}

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top