Разработка информационно-поисковой системы, страница 8

/* XXX : this routine use 1-based ID */

void create_table(const char * caption, int cols, column_t * table)

{

      char cmd[1024];

      wsprintf (cmd, "INSERT INTO tables (name) values (\"%s\")", caption);

      int rc = exec_sql_cmd (cmd, NULL);

      if (rc) return;

      int tid = (int)sqlite3_last_insert_rowid(sql_db);

      wsprintf (cmd, "CREATE TABLE table_%02d "

            "(cid INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, type INTEGER,"

            "width INTEGER, ref_tid INTEGER DEFAULT 0, ref_cid INTEGER DEFAULT 0)", tid);

    rc = exec_sql_cmd (cmd, NULL);

      wsprintf (cmd, "CREATE TABLE table_%02d_data"

            "(id INTEGER PRIMARY KEY AUTOINCREMENT", tid);

      for (int i = 0; i < cols; i++)

            insert_column (tid, &table[i]);

      for (int i = 1; i < cols; i++)

      {

            strcat (cmd, ",\"");

            strcat (cmd, table[i].name);

            strcat (cmd, "\" TEXT");

      }

      strcat (cmd, ")");

      callback_param_t param = {MODE_DEBUG};

      rc = exec_sql_cmd (cmd, /*NULL*/&param);

}

/* XXX : this routine use 1-based ID */

void insert_column(int tid, column_t * column)

{

      char cmd[1024];

      wsprintf (cmd, "INSERT INTO table_%02d (name,type,width,ref_tid,ref_cid)"

            "values (\"%s\",%d,%d,%d,%d)", tid, column->name, column->type,

            column->width, column->ref_tid, column->ref_cid);

      int rc = exec_sql_cmd (cmd, NULL);

}

void load_table(LVWnd_t * wnd)

{

      char cmd[1024], tid[16];

      wsprintf(tid, "table_%02d_data", wnd->tid + 1);

      wsprintf(cmd, "SELECT \"%s\"", table[wnd->tid][0].name);

      for (int i = 1; i < wnd->cols; i++)

            if (table[wnd->tid][i].type == CT_TEXT)

            {

                  strcat(cmd, ",\"");

                  strcat(cmd, table[wnd->tid][i].name);

                  strcat(cmd, "\"");

            }

            else

            if (table[wnd->tid][i].type == CT_COMBO)

            {

                  char part[256];

                  int ref_tid = table[wnd->tid][i].ref_tid;

                  int ref_cid = table[wnd->tid][i].ref_cid;

                  wsprintf(part, ", (SELECT \"%s\" FROM table_%02d_data "

                        "WHERE table_%02d_data.\"%s\"=%s.\"%s\")",

                        table[ref_tid][ref_cid].name, ref_tid + 1,

                        ref_tid + 1, table[ref_tid][0].name,

                        tid, table[wnd->tid][i].name);

            strcat(cmd, part);

            }

            else

            if (table[wnd->tid][i].type == CT_COMBO_EX)

            {

                  char part[256];

                  int ref_tid = table[wnd->tid][i].ref_tid;

                  int ref_cid = table[wnd->tid][i].ref_cid;

                  int isN, start, end;

                  GET_PARAM(isN, start, end, ref_cid);

                  strcat(cmd, ", (SELECT ");

                  if (isN)

                  {    

                        strcat(cmd, "\"№\"||\"");

                        strcat(cmd, table[ref_tid][start++].name);

                        strcat(cmd, "\"|| ");

                  }

                  for (int j = start; j <= end; j++)

                  {

                        strcat(cmd, "\"  \" || \"");

                        strcat(cmd, table[ref_tid][j].name);

                        strcat(cmd, "\"");

                  }

                  wsprintf(part, "FROM table_%02d_data WHERE table_%02d_data.\"%s\"=%s.\"%s\")",

                        ref_tid + 1, ref_tid + 1, table[ref_tid][0].name,

                        tid, table[wnd->tid][i].name);

            strcat(cmd, part);

            }

      strcat(cmd, " FROM ");