インデックスの型を調べる

テーブルオブジェクトを取得できて、付与されているインデックスの数も分かったら次はこれ。

既に1個前のエントリで触れているように、handler::index_typeを使う。table->fileがhandlerへのポインタ。

    table = open_table(thd, table_list, thd->mem_root, 0, 0);
    printf("table alias is %s\n", table->alias);

    uint nkeys = table->s->keys;
    int i = 0;
    for (; i < nkeys; i++) {
      const char* tkey;
      tkey = table->file->index_type(i);
      printf("number %d key's type is %s\n", i, tkey);
    }

ちゃんと取れました。

table alias is t2
number 0 key's type is BTREE
number 1 key's type is BTREE
number 2 key's type is BTREE



で、実はもう1個方法がある。

KEYオブジェクトに触れれば、、、

typedef struct st_key {
  uint  key_length;                     /* Tot length of key */
  uint  flags;                          /* dupp key and pack flags */
  uint  key_parts;                      /* How many key_parts */
  uint  extra_length;
  uint  usable_key_parts;               /* Should normally be = key_parts */
  enum  ha_key_alg algorithm;
  KEY_PART_INFO *key_part;
  char  *name;                          /* Name of key */
  ...
} KEY;

名前とか、キーを構成するカラム数とか、インデックスの型(enum ha_key_alg)とか取れる。

しかしTABLEオブジェクトにあるKEYオブジェクトへのポインタって、プライマリキーだけっぽいんだよなぁ・・・。全キーへのポインタが取れればこっちのほうがいいのだけれども。



いくつかの既存のソースを見たら、table->key_infoってKEYオブジェクトへの配列へのポインタだったw

普通に行ける。

    printf("table alias is %s\n", table->alias);

    uint nkeys = table->s->keys;
    int i = 0;
    for (; i < nkeys; i++) {
      const char* tkey;
      tkey = table->file->index_type(i);
      printf("number %d key's type is %s\n", i, tkey);

      KEY key;
      key = table->key_info[i];
      printf("name = %s, key_length = %d, key_parts = %d, algorithm = %d\n",
             key.name, key.key_length, key.key_parts, key.algorithm);
    }

handler APIを使う必要無し。

table alias is t2
number 0 key's type is BTREE
name = PRIMARY, key_length = 4, key_parts = 1, algorithm = 0
number 1 key's type is BTREE
name = idx2, key_length = 5, key_parts = 1, algorithm = 0
number 2 key's type is BTREE
name = idx3, key_length = 10, key_parts = 2, algorithm = 0

補足: KEY.algorithmはenum ha_key_alg型。

enum ha_key_alg {
  HA_KEY_ALG_UNDEF=     0,              /* Not specified (old file) */
  HA_KEY_ALG_BTREE=     1,              /* B-tree, default one          */
  HA_KEY_ALG_RTREE=     2,              /* R-tree, for spatial searches */
  HA_KEY_ALG_HASH=      3,              /* HASH keys (HEAP tables) */
  HA_KEY_ALG_FULLTEXT=  4               /* FULLTEXT (MyISAM tables) */
};

0を返してきているということは、old file???