开发者

How do I have to use the Field type when writing a mysql storage engine?

I am working on a storage engine for MySQL. But I am already struggling with simply parsing data. For example there is a method with the following signature:

int ha_engine::update_row(const uchar *old_data, uchar *new_data);

So the data is stored in the old_data and new_data arrays. However to get the data out of this array, one should use the Field class to access the data in these rows.

Now the problem is, that I have no idea how to do that. For example this code:

longlong val = table_share->field[0]->val_int();

will not work, or I don't know from which row I will get the first colu开发者_如何学Cmn. So how should one do that??

Thanks in adcance for any help!


You can use the field type information to determine the kind of variable to hold the data and also which function to use to fetch the data.

In my database applications, the type of the field is always known, as the program creates the tables and populates them.

I have a hierarchy of classes derived from a Field class:

class Field
{
  public:
    virtual std::string  get_field_name(void) const = 0;
    virtual std::string  get_value_as_string(void) const = 0;
    virtual void         load_from_mysql_record(const MySQLRecord& r) = 0;
};

class Field_Integer : public Field
{
  public:
    int m_value;
    std::string get_value_as_string(void) const
    {
        std::ostringstream oss;
        oss << m_value;
        return oss.str();
    }
};

class Field_String : public Field
{
  public:
    std::string m_value;
    std::string get_value_as_string(void) const
    {
        return m_value;
    }
};

Each field leaf class would use the get_field_name function to fetch the data from the MySQL record. Alternatively, a method could be created in the Field class to fetch data in a record by field index.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜