metaSQLiteImpl.c 6.4 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "metaDef.h"
#include "sqlite3.h"

H
more  
Hongze Cheng 已提交
19 20 21 22
struct SMetaDB {
  sqlite3 *pDB;
};

H
Hongze Cheng 已提交
23 24 25 26 27
int metaOpenDB(SMeta *pMeta) {
  char  dir[128];
  int   rc;
  char *err = NULL;

H
more  
Hongze Cheng 已提交
28 29 30 31 32 33
  pMeta->pDB = (SMetaDB *)calloc(1, sizeof(SMetaDB));
  if (pMeta->pDB == NULL) {
    // TODO: handle error
    return -1;
  }

H
Hongze Cheng 已提交
34
  sprintf(dir, "%s/meta.db", pMeta->path);
H
more  
Hongze Cheng 已提交
35
  rc = sqlite3_open(dir, &(pMeta->pDB->pDB));
H
Hongze Cheng 已提交
36 37 38 39 40 41
  if (rc != SQLITE_OK) {
    // TODO: handle error
    printf("failed to open meta.db\n");
  }

  // For all tables
H
more  
Hongze Cheng 已提交
42
  rc = sqlite3_exec(pMeta->pDB->pDB,
H
Hongze Cheng 已提交
43 44 45 46 47 48 49 50 51 52 53
                    "CREATE TABLE IF NOT EXISTS tb ("
                    "  tbname VARCHAR(256) NOT NULL UNIQUE,"
                    "  tb_uid INTEGER NOT NULL UNIQUE "
                    ");",
                    NULL, NULL, &err);
  if (rc != SQLITE_OK) {
    // TODO: handle error
    printf("failed to create meta table tb since %s\n", err);
  }

  // For super tables
H
more  
Hongze Cheng 已提交
54
  rc = sqlite3_exec(pMeta->pDB->pDB,
H
Hongze Cheng 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67
                    "CREATE TABLE IF NOT EXISTS stb ("
                    "    tb_uid INTEGER NOT NULL UNIQUE,"
                    "    tbname VARCHAR(256) NOT NULL UNIQUE,"
                    "    tb_schema BLOB NOT NULL,"
                    "    tag_schema BLOB NOT NULL"
                    ");",
                    NULL, NULL, &err);
  if (rc != SQLITE_OK) {
    // TODO: handle error
    printf("failed to create meta table stb since %s\n", err);
  }

  // For normal tables
H
more  
Hongze Cheng 已提交
68
  rc = sqlite3_exec(pMeta->pDB->pDB,
H
Hongze Cheng 已提交
69 70 71 72 73 74 75 76 77 78 79
                    "CREATE TABLE IF NOT EXISTS ntb ("
                    "    tb_uid INTEGER NOT NULL UNIQUE,"
                    "    tbname VARCHAR(256) NOT NULL,"
                    "    tb_schema BLOB NOT NULL"
                    ");",
                    NULL, NULL, &err);
  if (rc != SQLITE_OK) {
    // TODO: handle error
    printf("failed to create meta table ntb since %s\n", err);
  }

H
more  
Hongze Cheng 已提交
80
  sqlite3_exec(pMeta->pDB->pDB, "BEGIN;", NULL, NULL, &err);
H
Hongze Cheng 已提交
81 82 83 84 85 86 87 88

  tfree(err);

  return 0;
}

void metaCloseDB(SMeta *pMeta) {
  if (pMeta->pDB) {
H
more  
Hongze Cheng 已提交
89 90 91
    sqlite3_exec(pMeta->pDB->pDB, "COMMIT;", NULL, NULL, NULL);
    sqlite3_close(pMeta->pDB->pDB);
    free(pMeta->pDB);
H
Hongze Cheng 已提交
92 93 94 95 96 97
    pMeta->pDB = NULL;
  }

  // TODO
}

H
more  
Hongze Cheng 已提交
98
int metaSaveTableToDB(SMeta *pMeta, const STbCfg *pTbCfg) {
H
more  
Hongze Cheng 已提交
99 100 101 102 103 104 105
  char          sql[256];
  char *        err = NULL;
  int           rc;
  tb_uid_t      uid;
  sqlite3_stmt *stmt;
  char          buf[256];
  void *        pBuf;
H
Hongze Cheng 已提交
106

H
more  
Hongze Cheng 已提交
107
  switch (pTbCfg->type) {
H
Hongze Cheng 已提交
108
    case META_SUPER_TABLE:
H
more  
Hongze Cheng 已提交
109
      uid = pTbCfg->stbCfg.suid;
H
more  
Hongze Cheng 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
      sprintf(sql,
              "INSERT INTO tb VALUES (\'%s\', %" PRIu64
              ");"
              "CREATE TABLE IF NOT EXISTS stb_%" PRIu64
              " ("
              "    tb_uid INTEGER NOT NULL UNIQUE,"
              "    tbname VARCHAR(256),"
              "    tag1 INTEGER);",
              pTbCfg->name, uid, uid);
      rc = sqlite3_exec(pMeta->pDB->pDB, sql, NULL, NULL, &err);
      if (rc != SQLITE_OK) {
        printf("failed to create normal table since %s\n", err);
      }

      sprintf(sql, "INSERT INTO stb VALUES (%" PRIu64 ", %s, ?, ?)", uid, pTbCfg->name);
      sqlite3_prepare_v2(pMeta->pDB->pDB, sql, -1, &stmt, NULL);

      pBuf = buf;
      tdEncodeSchema(&pBuf, pTbCfg->stbCfg.pSchema);
      sqlite3_bind_blob(stmt, 1, buf, POINTER_DISTANCE(pBuf, buf), NULL);
      pBuf = buf;
      tdEncodeSchema(&pBuf, pTbCfg->stbCfg.pTagSchema);
      sqlite3_bind_blob(stmt, 2, buf, POINTER_DISTANCE(pBuf, buf), NULL);

      sqlite3_step(stmt);

      sqlite3_finalize(stmt);

#if 0
      sprintf(sql,
              "INSERT INTO tb VALUES (?, ?);"
              // "INSERT INTO stb VALUES (?, ?, ?, ?);"
              // "CREATE TABLE IF NOT EXISTS stb_%" PRIu64
              // " ("
              // "    tb_uid INTEGER NOT NULL UNIQUE,"
              // "    tbname VARCHAR(256),"
              // "    tag1 INTEGER);"
              ,
              uid);
H
more  
Hongze Cheng 已提交
149 150 151 152 153 154
      rc = sqlite3_prepare_v2(pMeta->pDB->pDB, sql, -1, &stmt, NULL);
      if (rc != SQLITE_OK) {
        return -1;
      }
      sqlite3_bind_text(stmt, 1, pTbCfg->name, -1, SQLITE_TRANSIENT);
      sqlite3_bind_int64(stmt, 2, uid);
H
more  
Hongze Cheng 已提交
155 156 157 158
      sqlite3_step(stmt);
      sqlite3_finalize(stmt);


H
more  
Hongze Cheng 已提交
159 160 161 162 163 164 165 166 167
      // sqlite3_bind_int64(stmt, 3, uid);
      // sqlite3_bind_text(stmt, 4, pTbCfg->name, -1, SQLITE_TRANSIENT);
      // pBuf = buf;
      // tdEncodeSchema(&pBuf, pTbCfg->stbCfg.pSchema);
      // sqlite3_bind_blob(stmt, 5, buf, POINTER_DISTANCE(pBuf, buf), NULL);
      // pBuf = buf;
      // tdEncodeSchema(&pBuf, pTbCfg->stbCfg.pTagSchema);
      // sqlite3_bind_blob(stmt, 6, buf, POINTER_DISTANCE(pBuf, buf), NULL);

H
more  
Hongze Cheng 已提交
168
      rc = sqliteVjj3_step(stmt);
H
Hongze Cheng 已提交
169
      if (rc != SQLITE_OK) {
H
more  
Hongze Cheng 已提交
170
        printf("failed to create normal table since %s\n", sqlite3_errmsg(pMeta->pDB->pDB));
H
Hongze Cheng 已提交
171
      }
H
more  
Hongze Cheng 已提交
172
      sqlite3_finalize(stmt);
H
more  
Hongze Cheng 已提交
173
#endif
H
Hongze Cheng 已提交
174 175
      break;
    case META_NORMAL_TABLE:
H
more  
Hongze Cheng 已提交
176
      // uid = metaGenerateUid(pMeta);
H
more  
Hongze Cheng 已提交
177 178 179 180 181
      // sprintf(sql,
      //         "INSERT INTO tb VALUES (\'%s\', %" PRIu64
      //         ");"
      //         "INSERT INTO ntb VALUES (%" PRIu64 ", \'%s\', );",
      //         pTbCfg->name, uid, uid, pTbCfg->name, );
H
more  
Hongze Cheng 已提交
182 183 184 185 186

      // rc = sqlite3_exec(pMeta->pDB->pDB, sql, NULL, NULL, &err);
      // if (rc != SQLITE_OK) {
      //   printf("failed to create normal table since %s\n", err);
      // }
H
Hongze Cheng 已提交
187 188
      break;
    case META_CHILD_TABLE:
H
more  
Hongze Cheng 已提交
189
#if 0
H
more  
Hongze Cheng 已提交
190
      uid = metaGenerateUid(pMeta);
H
Hongze Cheng 已提交
191 192 193
      // sprintf(sql, "INSERT INTO tb VALUES (\'%s\', %" PRIu64
      //              ");"
      //              "INSERT INTO stb_%" PRIu64 " VALUES (%" PRIu64 ", \'%s\', );");
H
more  
Hongze Cheng 已提交
194
      rc = sqlite3_exec(pMeta->pDB->pDB, sql, NULL, NULL, &err);
H
Hongze Cheng 已提交
195 196 197
      if (rc != SQLITE_OK) {
        printf("failed to create child table since %s\n", err);
      }
H
more  
Hongze Cheng 已提交
198
#endif
H
Hongze Cheng 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212
      break;
    default:
      break;
  }

  tfree(err);

  return 0;
}

int metaRemoveTableFromDb(SMeta *pMeta, tb_uid_t uid) {
  /* TODO */
  return 0;
}