diff --git a/src/kit/taosdump/taosdump.c b/src/kit/taosdump/taosdump.c index b652bda6d99ce4fe0fde44eb6a4fa318ddb34dba..b760c642e5f57baf872148d21dcf29ca85061927 100644 --- a/src/kit/taosdump/taosdump.c +++ b/src/kit/taosdump/taosdump.c @@ -13,6 +13,8 @@ * along with this program. If not, see . */ +#include +#include #include #include #include @@ -26,6 +28,12 @@ #include "tsdb.h" #include "tutil.h" +#define AVRO_SUPPORT 0 + +#if AVRO_SUPPORT == 1 +#include +#endif + #define TSDB_SUPPORT_NANOSECOND 1 #define MAX_FILE_NAME_LEN 256 // max file name length on linux is 255 @@ -38,8 +46,8 @@ static int converStringToReadable(char *str, int size, char *buf, int bufsize); static int convertNCharToReadable(char *str, int size, char *buf, int bufsize); -static void taosDumpCharset(FILE *fp); -static void taosLoadFileCharset(FILE *fp, char *fcharset); +static void dumpCharset(FILE *fp); +static void loadFileCharset(FILE *fp, char *fcharset); typedef struct { short bytes; @@ -128,19 +136,23 @@ enum _describe_table_index { TSDB_MAX_DESCRIBE_METRIC }; -#define COL_NOTE_LEN 128 +#define COL_NOTE_LEN 4 +#define COL_TYPEBUF_LEN 16 +#define COL_VALUEBUF_LEN 32 typedef struct { - char field[TSDB_COL_NAME_LEN + 1]; - char type[16]; + char field[TSDB_COL_NAME_LEN]; + char type[COL_TYPEBUF_LEN]; int length; char note[COL_NOTE_LEN]; -} SColDes; + char value[COL_VALUEBUF_LEN]; + char *var_value; +} ColDes; typedef struct { char name[TSDB_TABLE_NAME_LEN]; - SColDes cols[]; -} STableDef; + ColDes cols[]; +} TableDef; extern char version[]; @@ -159,7 +171,8 @@ typedef struct { } TableRecord; typedef struct { - bool isStable; + bool isStb; + bool belongStb; int64_t dumpNtbCount; TableRecord **dumpNtbInfos; TableRecord tableRecord; @@ -329,24 +342,21 @@ static resultStatistics g_resultStatistics = {0}; static FILE *g_fpOfResult = NULL; static int g_numOfCores = 1; -static int taosDumpOut(); -static int taosDumpIn(); -static void taosDumpCreateDbClause(SDbInfo *dbInfo, bool isDumpProperty, +static int dumpOut(); +static int dumpIn(); +static void dumpCreateDbClause(SDbInfo *dbInfo, bool isDumpProperty, FILE *fp); -//static int taosDumpDb(SDbInfo *dbInfo, FILE *fp, TAOS *taos); -static int dumpStable(char *table, FILE *fp, SDbInfo *dbInfo); -static int dumpCreateTableClause(STableDef *tableDes, int numOfCols, +static int dumpCreateTableClause(TableDef *tableDes, int numOfCols, FILE *fp, char* dbName); -static void taosDumpCreateMTableClause(STableDef *tableDes, char *stable, - int numOfCols, FILE *fp, char* dbName); -static int64_t taosDumpTable(char *tbName, char *stable, - FILE *fp, char* dbName, int precision); +static int getTableDes( + char* dbName, char *table, + TableDef *stableDes, bool isSuperTable); static int64_t dumpTableData(FILE *fp, char *tbName, char* dbName, int precision, char *jsonAvroSchema); static int checkParam(); -static void taosFreeDbInfos(); +static void freeDbInfos(); struct arguments g_args = { // connection option @@ -613,6 +623,17 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { return 0; } +static void freeTbDes(TableDef *tableDes) +{ + for (int i = 0; i < TSDB_MAX_COLUMNS; i ++) { + if (tableDes->cols[i].var_value) { + free(tableDes->cols[i].var_value); + } + } + + free(tableDes); +} + static int queryDbImpl(TAOS *taos, char *command) { TAOS_RES *res = NULL; int32_t code = -1; @@ -771,7 +792,7 @@ static int getPrecisionByString(char *precision) return -1; } -static void taosFreeDbInfos() { +static void freeDbInfos() { if (g_dbInfos == NULL) return; for (int i = 0; i < g_args.dumpDbCount; i++) tfree(g_dbInfos[i]); @@ -822,15 +843,20 @@ static int getTableRecordInfo( while ((row = taos_fetch_row(result)) != NULL) { isSet = true; - pTableRecordInfo->isStable = false; + pTableRecordInfo->isStb = false; tstrncpy(pTableRecordInfo->tableRecord.name, (char *)row[TSDB_SHOW_TABLES_NAME_INDEX], min(TSDB_TABLE_NAME_LEN, fields[TSDB_SHOW_TABLES_NAME_INDEX].bytes + 1)); - tstrncpy(pTableRecordInfo->tableRecord.stable, - (char *)row[TSDB_SHOW_TABLES_METRIC_INDEX], - min(TSDB_TABLE_NAME_LEN, - fields[TSDB_SHOW_TABLES_METRIC_INDEX].bytes + 1)); + if (strlen((char *)row[TSDB_SHOW_TABLES_METRIC_INDEX]) > 0) { + pTableRecordInfo->belongStb = true; + tstrncpy(pTableRecordInfo->tableRecord.stable, + (char *)row[TSDB_SHOW_TABLES_METRIC_INDEX], + min(TSDB_TABLE_NAME_LEN, + fields[TSDB_SHOW_TABLES_METRIC_INDEX].bytes + 1)); + } else { + pTableRecordInfo->belongStb = false; + } break; } @@ -855,7 +881,7 @@ static int getTableRecordInfo( while ((row = taos_fetch_row(result)) != NULL) { isSet = true; - pTableRecordInfo->isStable = true; + pTableRecordInfo->isStb = true; tstrncpy(pTableRecordInfo->tableRecord.stable, table, TSDB_TABLE_NAME_LEN); break; @@ -892,7 +918,6 @@ static int inDatabasesSeq( dbname = strsep(&running, ","); } - } return -1; @@ -958,7 +983,213 @@ static int getDumpDbCount() return count; } -static int64_t dumpNormalTableWithoutStb(SDbInfo *dbInfo, char *ntbName) +static void dumpCreateMTableClause( + char* dbName, + char *stable, + TableDef *tableDes, + int numOfCols, + FILE *fp + ) { + int counter = 0; + int count_temp = 0; + + char* tmpBuf = (char *)malloc(COMMAND_SIZE); + if (tmpBuf == NULL) { + errorPrint("%s() LN%d, failed to allocate %d memory\n", + __func__, __LINE__, COMMAND_SIZE); + return; + } + + char *pstr = NULL; + pstr = tmpBuf; + + pstr += sprintf(tmpBuf, + "CREATE TABLE IF NOT EXISTS %s.%s USING %s.%s TAGS (", + dbName, tableDes->name, dbName, stable); + + for (; counter < numOfCols; counter++) { + if (tableDes->cols[counter].note[0] != '\0') break; + } + + assert(counter < numOfCols); + count_temp = counter; + + for (; counter < numOfCols; counter++) { + if (counter != count_temp) { + if (strcasecmp(tableDes->cols[counter].type, "binary") == 0 || + strcasecmp(tableDes->cols[counter].type, "nchar") == 0) { + //pstr += sprintf(pstr, ", \'%s\'", tableDes->cols[counter].note); + if (tableDes->cols[counter].var_value) { + pstr += sprintf(pstr, ", %s", + tableDes->cols[counter].var_value); + } else { + pstr += sprintf(pstr, ", %s", tableDes->cols[counter].value); + } + } else { + pstr += sprintf(pstr, ", %s", tableDes->cols[counter].value); + } + } else { + if (strcasecmp(tableDes->cols[counter].type, "binary") == 0 || + strcasecmp(tableDes->cols[counter].type, "nchar") == 0) { + //pstr += sprintf(pstr, "\'%s\'", tableDes->cols[counter].note); + if (tableDes->cols[counter].var_value) { + pstr += sprintf(pstr, "%s", tableDes->cols[counter].var_value); + } else { + pstr += sprintf(pstr, "%s", tableDes->cols[counter].value); + } + } else { + pstr += sprintf(pstr, "%s", tableDes->cols[counter].value); + } + /* pstr += sprintf(pstr, "%s", tableDes->cols[counter].note); */ + } + + /* if (strcasecmp(tableDes->cols[counter].type, "binary") == 0 || strcasecmp(tableDes->cols[counter].type, "nchar") + * == 0) { */ + /* pstr += sprintf(pstr, "(%d)", tableDes->cols[counter].length); */ + /* } */ + } + + pstr += sprintf(pstr, ");"); + + fprintf(fp, "%s\n", tmpBuf); + free(tmpBuf); +} + +static int convertTbDesToAvroSchema( + char *dbName, char *tbName, TableDef *tableDes, int colCount, + char **avroSchema) +{ + errorPrint("%s() LN%d TODO: covert table schema to avro schema\n", + __func__, __LINE__); + // { + // "namesapce": "database name", + // "type": "record", + // "name": "table name", + // "fields": [ + // { + // "name": "col0 name", + // "type": "long" + // }, + // { + // "name": "col1 name", + // "type": ["int", "null"] + // }, + // { + // "name": "col2 name", + // "type": ["float", "null"] + // }, + // ... + // { + // "name": "coln name", + // "type": ["string", "null"] + // } + // ] + // } + *avroSchema = (char *)calloc(1, + 17 + TSDB_DB_NAME_LEN /* dbname section */ + + 17 /* type: record */ + + 11 + TSDB_TABLE_NAME_LEN /* tbname section */ + + 10 /* fields section */ + + (TSDB_COL_NAME_LEN + 11 + 16) * colCount + 4); /* fields section */ + if (*avroSchema == NULL) { + errorPrint("%s() LN%d, memory allocation failed!\n", __func__, __LINE__); + return -1; + } + + char *pstr = *avroSchema; + pstr += sprintf(pstr, + "{\"namespace\": \"%s\", \"type\": \"record\", \"name\": \"%s\", \"fields\": [", + dbName, tbName); + for (int i = 0; i < colCount; i ++) { + if (0 == i) { + pstr += sprintf(pstr, + "{\"name\": \"%s\", \"type\": \"%s\"", + tableDes->cols[i].field, "long"); + } else { + if (strcasecmp(tableDes->cols[i].type, "binary") == 0 || + strcasecmp(tableDes->cols[i].type, "nchar") == 0) { + pstr += sprintf(pstr, + "{\"name\": \"%s\", \"type\": [\"%s\", \"null\"]", + tableDes->cols[i].field, "string"); + } else { + pstr += sprintf(pstr, + "{\"name\": \"%s\", \"type\": [\"%s\", \"null\"]", + tableDes->cols[i].field, tableDes->cols[i].type); + } + } + if ((i != (colCount -1)) + && (strcmp(tableDes->cols[i + 1].note, "TAG") != 0)) { + pstr += sprintf(pstr, "},"); + } else { + pstr += sprintf(pstr, "}"); + break; + } + } + + pstr += sprintf(pstr, "]}"); + + debugPrint("%s() LN%d, avroSchema: %s\n", __func__, __LINE__, *avroSchema); + + return 0; +} + +static int64_t dumpNormalTable( + char *dbName, + char *stable, + char *tbName, + int precision, + FILE *fp + ) { + int colCount = 0; + + TableDef *tableDes = (TableDef *)calloc(1, sizeof(TableDef) + + sizeof(ColDes) * TSDB_MAX_COLUMNS); + + if (stable != NULL && stable[0] != '\0') { // dump table schema which is created by using super table + colCount = getTableDes(dbName, tbName, tableDes, false); + + if (colCount < 0) { + free(tableDes); + return -1; + } + + // create child-table using super-table + dumpCreateMTableClause(dbName, stable, tableDes, colCount, fp); + + } else { // dump table definition + colCount = getTableDes(dbName, tbName, tableDes, false); + + if (colCount < 0) { + free(tableDes); + return -1; + } + + // create normal-table or super-table + dumpCreateTableClause(tableDes, colCount, fp, dbName); + } + + char *jsonAvroSchema = NULL; + if (g_args.avro) { + if (0 != convertTbDesToAvroSchema( + dbName, tbName, tableDes, colCount, &jsonAvroSchema)) { + freeTbDes(tableDes); + return -1; + } + } + + free(tableDes); + + int64_t ret = 0; + if (!g_args.schemaonly) { + ret = dumpTableData(fp, tbName, dbName, precision, + jsonAvroSchema); + } + + return ret; +} + +static int64_t dumpNormalTableBelongStb( + SDbInfo *dbInfo, char *stbName, char *ntbName) { int64_t count = 0; @@ -980,21 +1211,47 @@ static int64_t dumpNormalTableWithoutStb(SDbInfo *dbInfo, char *ntbName) return -1; } - count = taosDumpTable(ntbName, NULL, - fp, dbInfo->name, getPrecisionByString(dbInfo->precision)); + count = dumpNormalTable( + dbInfo->name, + stbName, + ntbName, + getPrecisionByString(dbInfo->precision), + fp); fclose(fp); return count; } -static int64_t dumpNormalTable(FILE *fp, TAOS *taos, char *dbName, char *tbName, - char *stbName, - int precision) +static int64_t dumpNormalTableWithoutStb(SDbInfo *dbInfo, char *ntbName) { int64_t count = 0; - count = taosDumpTable(tbName, stbName, - fp, dbName, precision); + char tmpBuf[4096] = {0}; + FILE *fp = NULL; + + if (g_args.outpath[0] != 0) { + sprintf(tmpBuf, "%s/%s.%s.sql", + g_args.outpath, dbInfo->name, ntbName); + } else { + sprintf(tmpBuf, "%s.%s.sql", + dbInfo->name, ntbName); + } + + fp = fopen(tmpBuf, "w"); + if (fp == NULL) { + errorPrint("%s() LN%d, failed to open file %s\n", + __func__, __LINE__, tmpBuf); + return -1; + } + + count = dumpNormalTable( + dbInfo->name, + NULL, + ntbName, + getPrecisionByString(dbInfo->precision), + fp); + + fclose(fp); return count; } @@ -1028,12 +1285,12 @@ static void *dumpNtbOfDb(void *arg) { debugPrint("[%d] No.\t%"PRId64" table name: %s\n", pThreadInfo->threadIndex, i, ((TableInfo *)(g_tablesList + pThreadInfo->tableFrom+i))->name); - dumpNormalTable(fp, - pThreadInfo->taos, + dumpNormalTable( pThreadInfo->dbName, - ((TableInfo *)(g_tablesList + pThreadInfo->tableFrom+i))->name, ((TableInfo *)(g_tablesList + pThreadInfo->tableFrom+i))->stable, - pThreadInfo->precision); + ((TableInfo *)(g_tablesList + pThreadInfo->tableFrom+i))->name, + pThreadInfo->precision, + fp); } fclose(fp); @@ -1087,12 +1344,12 @@ static void *dumpNormalTablesOfStb(void *arg) { debugPrint("[%d] sub table %"PRId64": name: %s\n", pThreadInfo->threadIndex, i++, (char *)row[TSDB_SHOW_TABLES_NAME_INDEX]); - dumpNormalTable(fp, - pThreadInfo->taos, + dumpNormalTable( pThreadInfo->dbName, - (char *)row[TSDB_SHOW_TABLES_NAME_INDEX], pThreadInfo->stbName, - pThreadInfo->precision); + (char *)row[TSDB_SHOW_TABLES_NAME_INDEX], + pThreadInfo->precision, + fp); } fclose(fp); @@ -1278,6 +1535,34 @@ static int64_t dumpNtbOfStbByThreads( return records; } +static int dumpStableClasuse(SDbInfo *dbInfo, char *stbName, FILE *fp) +{ + uint64_t sizeOfTableDes = + (uint64_t)(sizeof(TableDef) + sizeof(ColDes) * TSDB_MAX_COLUMNS); + + TableDef *tableDes = (TableDef *)calloc(1, sizeOfTableDes); + if (NULL == tableDes) { + errorPrint("%s() LN%d, failed to allocate %"PRIu64" memory\n", + __func__, __LINE__, sizeOfTableDes); + exit(-1); + } + + int colCount = getTableDes(dbInfo->name, + stbName, tableDes, true); + + if (colCount < 0) { + free(tableDes); + errorPrint("%s() LN%d, failed to get stable[%s] schema\n", + __func__, __LINE__, stbName); + exit(-1); + } + + dumpCreateTableClause(tableDes, colCount, fp, dbInfo->name); + free(tableDes); + + return 0; +} + static int64_t dumpCreateSTableClauseOfDb( SDbInfo *dbInfo, FILE *fp) { @@ -1307,7 +1592,7 @@ static int64_t dumpCreateSTableClauseOfDb( int64_t superTblCnt = 0; while ((row = taos_fetch_row(res)) != NULL) { - if (0 == dumpStable(row[TSDB_SHOW_TABLES_NAME_INDEX], fp, dbInfo)) { + if (0 == dumpStableClasuse(dbInfo, row[TSDB_SHOW_TABLES_NAME_INDEX], fp)) { superTblCnt ++; } } @@ -1389,7 +1674,7 @@ static int64_t dumpNTablesOfDb(SDbInfo *dbInfo) static int64_t dumpWholeDatabase(SDbInfo *dbInfo, FILE *fp) { - taosDumpCreateDbClause(dbInfo, g_args.with_property, fp); + dumpCreateDbClause(dbInfo, g_args.with_property, fp); fprintf(g_fpOfResult, "\n#### database: %s\n", dbInfo->name); @@ -1400,14 +1685,13 @@ static int64_t dumpWholeDatabase(SDbInfo *dbInfo, FILE *fp) return dumpNTablesOfDb(dbInfo); } -static int taosDumpOut() { +static int dumpOut() { TAOS *taos = NULL; TAOS_RES *result = NULL; TAOS_ROW row; FILE *fp = NULL; int32_t count = 0; - TableRecordInfo tableRecordInfo; char tmpBuf[4096] = {0}; if (g_args.outpath[0] != 0) { @@ -1453,7 +1737,7 @@ static int taosDumpOut() { /* --------------------------------- Main Code -------------------------------- */ /* if (g_args.databases || g_args.all_databases) { // dump part of databases or all databases */ /* */ - taosDumpCharset(fp); + dumpCharset(fp); sprintf(command, "show databases"); result = taos_query(taos, command); @@ -1575,11 +1859,13 @@ static int taosDumpOut() { g_totalDumpOutRows += records; } } else { - taosDumpCreateDbClause(g_dbInfos[0], g_args.with_property, fp); + dumpCreateDbClause(g_dbInfos[0], g_args.with_property, fp); } int superTblCnt = 0 ; for (int i = 1; g_args.arg_list[i]; i++) { + TableRecordInfo tableRecordInfo; + if (getTableRecordInfo(g_dbInfos[0]->name, g_args.arg_list[i], &tableRecordInfo) < 0) { @@ -1589,14 +1875,24 @@ static int taosDumpOut() { } int64_t records = 0; - if (tableRecordInfo.isStable) { // dump all table of this stable - int ret = dumpStable( + if (tableRecordInfo.isStb) { // dump all table of this stable + int ret = dumpStableClasuse( + g_dbInfos[0], tableRecordInfo.tableRecord.stable, - fp, g_dbInfos[0]); + fp); if (ret >= 0) { superTblCnt++; records = dumpNtbOfStbByThreads(g_dbInfos[0], g_args.arg_list[i]); } + } else if (tableRecordInfo.belongStb){ + dumpStableClasuse( + g_dbInfos[0], + tableRecordInfo.tableRecord.stable, + fp); + records = dumpNormalTableBelongStb( + g_dbInfos[0], + tableRecordInfo.tableRecord.stable, + g_args.arg_list[i]); } else { records = dumpNormalTableWithoutStb(g_dbInfos[0], g_args.arg_list[i]); } @@ -1611,7 +1907,7 @@ static int taosDumpOut() { /* Close the handle and return */ fclose(fp); taos_free_result(result); - taosFreeDbInfos(); + freeDbInfos(); fprintf(stderr, "dump out rows: %" PRId64 "\n", g_totalDumpOutRows); return 0; @@ -1619,14 +1915,14 @@ _exit_failure: fclose(fp); taos_close(taos); taos_free_result(result); - taosFreeDbInfos(); + freeDbInfos(); errorPrint("dump out rows: %" PRId64 "\n", g_totalDumpOutRows); return -1; } static int getTableDes( char* dbName, char *table, - STableDef *stableDes, bool isSuperTable) { + TableDef *tableDes, bool isSuperTable) { TAOS_ROW row = NULL; TAOS_RES* res = NULL; int colCount = 0; @@ -1655,22 +1951,21 @@ static int getTableDes( TAOS_FIELD *fields = taos_fetch_fields(res); - tstrncpy(stableDes->name, table, TSDB_TABLE_NAME_LEN); + tstrncpy(tableDes->name, table, TSDB_TABLE_NAME_LEN); while ((row = taos_fetch_row(res)) != NULL) { - tstrncpy(stableDes->cols[colCount].field, + tstrncpy(tableDes->cols[colCount].field, (char *)row[TSDB_DESCRIBE_METRIC_FIELD_INDEX], min(TSDB_COL_NAME_LEN + 1, fields[TSDB_DESCRIBE_METRIC_FIELD_INDEX].bytes + 1)); - tstrncpy(stableDes->cols[colCount].type, + tstrncpy(tableDes->cols[colCount].type, (char *)row[TSDB_DESCRIBE_METRIC_TYPE_INDEX], min(16, fields[TSDB_DESCRIBE_METRIC_TYPE_INDEX].bytes + 1)); - stableDes->cols[colCount].length = + tableDes->cols[colCount].length = *((int *)row[TSDB_DESCRIBE_METRIC_LENGTH_INDEX]); - tstrncpy(stableDes->cols[colCount].note, + tstrncpy(tableDes->cols[colCount].note, (char *)row[TSDB_DESCRIBE_METRIC_NOTE_INDEX], min(COL_NOTE_LEN, fields[TSDB_DESCRIBE_METRIC_NOTE_INDEX].bytes + 1)); - colCount++; } @@ -1683,10 +1978,10 @@ static int getTableDes( // if child-table have tag, using select tagName from table to get tagValue for (int i = 0 ; i < colCount; i++) { - if (strcmp(stableDes->cols[i].note, "TAG") != 0) continue; + if (strcmp(tableDes->cols[i].note, "TAG") != 0) continue; sprintf(sqlstr, "select %s from %s.%s", - stableDes->cols[i].field, dbName, table); + tableDes->cols[i].field, dbName, table); res = taos_query(taos, sqlstr); code = taos_errno(res); @@ -1710,7 +2005,7 @@ static int getTableDes( } if (row[TSDB_SHOW_TABLES_NAME_INDEX] == NULL) { - sprintf(stableDes->cols[i].note, "%s", "NULL"); + sprintf(tableDes->cols[i].note, "%s", "NUL"); taos_free_result(res); res = NULL; taos_close(taos); @@ -1722,58 +2017,82 @@ static int getTableDes( //int32_t* length = taos_fetch_lengths(tmpResult); switch (fields[0].type) { case TSDB_DATA_TYPE_BOOL: - sprintf(stableDes->cols[i].note, "%d", + sprintf(tableDes->cols[i].value, "%d", ((((int32_t)(*((char *)row[TSDB_SHOW_TABLES_NAME_INDEX]))) == 1) ? 1 : 0)); break; case TSDB_DATA_TYPE_TINYINT: - sprintf(stableDes->cols[i].note, "%d", + sprintf(tableDes->cols[i].value, "%d", *((int8_t *)row[TSDB_SHOW_TABLES_NAME_INDEX])); break; case TSDB_DATA_TYPE_SMALLINT: - sprintf(stableDes->cols[i].note, "%d", *((int16_t *)row[TSDB_SHOW_TABLES_NAME_INDEX])); + sprintf(tableDes->cols[i].value, "%d", + *((int16_t *)row[TSDB_SHOW_TABLES_NAME_INDEX])); break; case TSDB_DATA_TYPE_INT: - sprintf(stableDes->cols[i].note, "%d", *((int32_t *)row[TSDB_SHOW_TABLES_NAME_INDEX])); + sprintf(tableDes->cols[i].value, "%d", + *((int32_t *)row[TSDB_SHOW_TABLES_NAME_INDEX])); break; case TSDB_DATA_TYPE_BIGINT: - sprintf(stableDes->cols[i].note, "%" PRId64 "", *((int64_t *)row[TSDB_SHOW_TABLES_NAME_INDEX])); + sprintf(tableDes->cols[i].value, "%" PRId64 "", + *((int64_t *)row[TSDB_SHOW_TABLES_NAME_INDEX])); break; case TSDB_DATA_TYPE_FLOAT: - sprintf(stableDes->cols[i].note, "%f", GET_FLOAT_VAL(row[TSDB_SHOW_TABLES_NAME_INDEX])); + sprintf(tableDes->cols[i].value, "%f", + GET_FLOAT_VAL(row[TSDB_SHOW_TABLES_NAME_INDEX])); break; case TSDB_DATA_TYPE_DOUBLE: - sprintf(stableDes->cols[i].note, "%f", GET_DOUBLE_VAL(row[TSDB_SHOW_TABLES_NAME_INDEX])); + sprintf(tableDes->cols[i].value, "%f", + GET_DOUBLE_VAL(row[TSDB_SHOW_TABLES_NAME_INDEX])); break; case TSDB_DATA_TYPE_BINARY: - { - memset(stableDes->cols[i].note, 0, sizeof(stableDes->cols[i].note)); - stableDes->cols[i].note[0] = '\''; - char tbuf[COL_NOTE_LEN]; - converStringToReadable((char *)row[TSDB_SHOW_TABLES_NAME_INDEX], length[0], tbuf, COL_NOTE_LEN); - char* pstr = stpcpy(&(stableDes->cols[i].note[1]), tbuf); - *(pstr++) = '\''; - break; + memset(tableDes->cols[i].value, 0, + sizeof(tableDes->cols[i].value)); + int len = strlen((char *)row[0]); + // FIXME for long value + if (len < (COL_VALUEBUF_LEN - 2)) { + tableDes->cols[i].value[0] = '\''; + converStringToReadable( + (char *)row[0], + length[0], + tableDes->cols[i].value + 1, + len); + tableDes->cols[i].value[len+1] = '\''; + } else { + tableDes->cols[i].var_value = calloc(1, len + 2); + if (tableDes->cols[i].var_value == NULL) { + errorPrint("%s() LN%d, memory alalocation failed!\n", + __func__, __LINE__); + taos_free_result(res); + return -1; + } + tableDes->cols[i].var_value[0] = '\''; + converStringToReadable((char *)row[0], + length[0], + (char *)(tableDes->cols[i].var_value + 1), len); + tableDes->cols[i].var_value[len+1] = '\''; } + break; + case TSDB_DATA_TYPE_NCHAR: { - memset(stableDes->cols[i].note, 0, sizeof(stableDes->cols[i].note)); + memset(tableDes->cols[i].value, 0, sizeof(tableDes->cols[i].note)); char tbuf[COL_NOTE_LEN-2]; // need reserve 2 bytes for ' ' convertNCharToReadable((char *)row[TSDB_SHOW_TABLES_NAME_INDEX], length[0], tbuf, COL_NOTE_LEN); - sprintf(stableDes->cols[i].note, "\'%s\'", tbuf); + sprintf(tableDes->cols[i].value, "\'%s\'", tbuf); break; } case TSDB_DATA_TYPE_TIMESTAMP: - sprintf(stableDes->cols[i].note, "%" PRId64 "", *(int64_t *)row[TSDB_SHOW_TABLES_NAME_INDEX]); + sprintf(tableDes->cols[i].value, "%" PRId64 "", *(int64_t *)row[TSDB_SHOW_TABLES_NAME_INDEX]); #if 0 if (!g_args.mysqlFlag) { - sprintf(tableDes->cols[i].note, "%" PRId64 "", *(int64_t *)row[TSDB_SHOW_TABLES_NAME_INDEX]); + sprintf(tableDes->cols[i].value, "%" PRId64 "", *(int64_t *)row[TSDB_SHOW_TABLES_NAME_INDEX]); } else { char buf[64] = "\0"; int64_t ts = *((int64_t *)row[TSDB_SHOW_TABLES_NAME_INDEX]); time_t tt = (time_t)(ts / 1000); struct tm *ptm = localtime(&tt); strftime(buf, 64, "%y-%m-%d %H:%M:%S", ptm); - sprintf(tableDes->cols[i].note, "\'%s.%03d\'", buf, (int)(ts % 1000)); + sprintf(tableDes->cols[i].value, "\'%s.%03d\'", buf, (int)(ts % 1000)); } #endif break; @@ -1788,74 +2107,7 @@ static int getTableDes( return colCount; } -static int convertSchemaToAvroSchema(STableDef *stableDes, char **avroSchema) -{ - errorPrint("%s() LN%d TODO: covert table schema to avro schema\n", - __func__, __LINE__); - return 0; -} - -static int64_t taosDumpTable( - char *tbName, char *stable, - FILE *fp, char* dbName, int precision) { - int colCount = 0; - - STableDef *tableDes = (STableDef *)calloc(1, sizeof(STableDef) - + sizeof(SColDes) * TSDB_MAX_COLUMNS); - - if (stable != NULL && stable[0] != '\0') { // dump table schema which is created by using super table - /* - colCount = getTableDes(stable, tableDes, taos); - - if (count < 0) { - free(tableDes); - return -1; - } - - dumpCreateTableClause(tableDes, count, fp); - - memset(tableDes, 0, sizeof(STableDef) + sizeof(SColDes) * TSDB_MAX_COLUMNS); - */ - - colCount = getTableDes(dbName, tbName, tableDes, false); - - if (colCount < 0) { - free(tableDes); - return -1; - } - - // create child-table using super-table - taosDumpCreateMTableClause(tableDes, stable, colCount, fp, dbName); - - } else { // dump table definition - colCount = getTableDes(dbName, tbName, tableDes, false); - - if (colCount < 0) { - free(tableDes); - return -1; - } - - // create normal-table or super-table - dumpCreateTableClause(tableDes, colCount, fp, dbName); - } - - char *jsonAvroSchema = NULL; - if (g_args.avro) { - convertSchemaToAvroSchema(tableDes, &jsonAvroSchema); - } - - free(tableDes); - - int64_t ret = 0; - if (!g_args.schemaonly) { - ret = dumpTableData(fp, tbName, dbName, precision, - jsonAvroSchema); - } - - return ret; -} - -static void taosDumpCreateDbClause( +static void dumpCreateDbClause( SDbInfo *dbInfo, bool isDumpProperty, FILE *fp) { char sqlstr[TSDB_MAX_SQL_LEN] = {0}; @@ -1877,35 +2129,7 @@ static void taosDumpCreateDbClause( fprintf(fp, "%s\n\n", sqlstr); } -static int dumpStable(char *stbName, FILE *fp, SDbInfo *dbInfo) -{ - uint64_t sizeOfTableDes = - (uint64_t)(sizeof(STableDef) + sizeof(SColDes) * TSDB_MAX_COLUMNS); - - STableDef *stableDes = (STableDef *)calloc(1, sizeOfTableDes); - if (NULL == stableDes) { - errorPrint("%s() LN%d, failed to allocate %"PRIu64" memory\n", - __func__, __LINE__, sizeOfTableDes); - exit(-1); - } - - int colCount = getTableDes(dbInfo->name, - stbName, stableDes, true); - - if (colCount < 0) { - free(stableDes); - errorPrint("%s() LN%d, failed to get stable[%s] schema\n", - __func__, __LINE__, stbName); - exit(-1); - } - - dumpCreateTableClause(stableDes, colCount, fp, dbInfo->name); - free(stableDes); - - return 0; -} - -static int dumpCreateTableClause(STableDef *tableDes, int numOfCols, +static int dumpCreateTableClause(TableDef *tableDes, int numOfCols, FILE *fp, char* dbName) { int counter = 0; int count_temp = 0; @@ -1956,64 +2180,6 @@ static int dumpCreateTableClause(STableDef *tableDes, int numOfCols, return fprintf(fp, "%s\n\n", sqlstr); } -static void taosDumpCreateMTableClause(STableDef *tableDes, char *stable, - int numOfCols, FILE *fp, char* dbName) { - int counter = 0; - int count_temp = 0; - - char* tmpBuf = (char *)malloc(COMMAND_SIZE); - if (tmpBuf == NULL) { - errorPrint("%s() LN%d, failed to allocate %d memory\n", - __func__, __LINE__, COMMAND_SIZE); - return; - } - - char *pstr = NULL; - pstr = tmpBuf; - - pstr += sprintf(tmpBuf, - "CREATE TABLE IF NOT EXISTS %s.%s USING %s.%s TAGS (", - dbName, tableDes->name, dbName, stable); - - for (; counter < numOfCols; counter++) { - if (tableDes->cols[counter].note[0] != '\0') break; - } - - assert(counter < numOfCols); - count_temp = counter; - - for (; counter < numOfCols; counter++) { - if (counter != count_temp) { - if (strcasecmp(tableDes->cols[counter].type, "binary") == 0 || - strcasecmp(tableDes->cols[counter].type, "nchar") == 0) { - //pstr += sprintf(pstr, ", \'%s\'", tableDes->cols[counter].note); - pstr += sprintf(pstr, ", %s", tableDes->cols[counter].note); - } else { - pstr += sprintf(pstr, ", %s", tableDes->cols[counter].note); - } - } else { - if (strcasecmp(tableDes->cols[counter].type, "binary") == 0 || - strcasecmp(tableDes->cols[counter].type, "nchar") == 0) { - //pstr += sprintf(pstr, "\'%s\'", tableDes->cols[counter].note); - pstr += sprintf(pstr, "%s", tableDes->cols[counter].note); - } else { - pstr += sprintf(pstr, "%s", tableDes->cols[counter].note); - } - /* pstr += sprintf(pstr, "%s", tableDes->cols[counter].note); */ - } - - /* if (strcasecmp(tableDes->cols[counter].type, "binary") == 0 || strcasecmp(tableDes->cols[counter].type, "nchar") - * == 0) { */ - /* pstr += sprintf(pstr, "(%d)", tableDes->cols[counter].length); */ - /* } */ - } - - pstr += sprintf(pstr, ");"); - - fprintf(fp, "%s\n", tmpBuf); - free(tmpBuf); -} - static int writeSchemaToAvro(char *jsonAvroSchema) { errorPrint("%s() LN%d, TODO: implement write schema to avro", @@ -2110,10 +2276,7 @@ static int64_t writeResultToSql(TAOS_RES *res, FILE *fp, char *dbName, char *tbN case TSDB_DATA_TYPE_BINARY: { char tbuf[COMMAND_SIZE] = {0}; - //*(pstr++) = '\''; converStringToReadable((char *)row[col], length[col], tbuf, COMMAND_SIZE); - //pstr = stpcpy(pstr, tbuf); - //*(pstr++) = '\''; curr_sqlstr_len += sprintf(pstr + curr_sqlstr_len, "\'%s\'", tbuf); break; } @@ -2369,7 +2532,6 @@ static int converStringToReadable(char *str, int size, char *buf, int bufsize) { static int convertNCharToReadable(char *str, int size, char *buf, int bufsize) { char *pstr = str; char *pbuf = buf; - // TODO wchar_t wc; while (size > 0) { if (*pstr == '\0') break; @@ -2393,7 +2555,7 @@ static int convertNCharToReadable(char *str, int size, char *buf, int bufsize) { return 0; } -static void taosDumpCharset(FILE *fp) { +static void dumpCharset(FILE *fp) { char charsetline[256]; (void)fseek(fp, 0, SEEK_SET); @@ -2401,7 +2563,7 @@ static void taosDumpCharset(FILE *fp) { (void)fwrite(charsetline, strlen(charsetline), 1, fp); } -static void taosLoadFileCharset(FILE *fp, char *fcharset) { +static void loadFileCharset(FILE *fp, char *fcharset) { char * line = NULL; size_t line_size = 0; @@ -2533,7 +2695,7 @@ static void taosMallocDumpFiles() } } -static void taosFreeDumpFiles() +static void freeDumpFiles() { for (int i = 0; i < g_tsSqlFileNum; i++) { tfree(g_tsDumpInSqlFiles[i]); @@ -2601,7 +2763,7 @@ static FILE* taosOpenDumpInFile(char *fptr) { return f; } -static int taosDumpInOneFile(TAOS* taos, FILE* fp, char* fcharset, +static int dumpInOneFile(TAOS* taos, FILE* fp, char* fcharset, char* encode, char* fileName) { int read_len = 0; char * cmd = NULL; @@ -2658,7 +2820,7 @@ static int taosDumpInOneFile(TAOS* taos, FILE* fp, char* fcharset, return 0; } -static void* taosDumpInWorkThreadFp(void *arg) +static void* dumpInWorkThreadFp(void *arg) { threadInfo *pThread = (threadInfo*)arg; setThreadName("dumpInWorkThrd"); @@ -2672,14 +2834,14 @@ static void* taosDumpInWorkThreadFp(void *arg) } fprintf(stderr, ", Success Open input file: %s\n", SQLFileName); - taosDumpInOneFile(pThread->taos, fp, g_tsCharset, g_args.encode, SQLFileName); + dumpInOneFile(pThread->taos, fp, g_tsCharset, g_args.encode, SQLFileName); } } return NULL; } -static void taosStartDumpInWorkThreads() +static void startDumpInWorkThreads() { pthread_attr_t thattr; threadInfo *pThread; @@ -2711,7 +2873,7 @@ static void taosStartDumpInWorkThreads() pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); if (pthread_create(&(pThread->threadID), &thattr, - taosDumpInWorkThreadFp, (void*)pThread) != 0) { + dumpInWorkThreadFp, (void*)pThread) != 0) { errorPrint("%s() LN%d, thread:%d failed to start\n", __func__, __LINE__, pThread->threadIndex); exit(0); @@ -2728,7 +2890,7 @@ static void taosStartDumpInWorkThreads() free(threadObj); } -static int taosDumpIn() { +static int dumpIn() { assert(g_args.isDumpIn); TAOS *taos = NULL; @@ -2757,19 +2919,19 @@ static int taosDumpIn() { } fprintf(stderr, "Success Open input file: %s\n", g_tsDbSqlFile); - taosLoadFileCharset(fp, g_tsCharset); + loadFileCharset(fp, g_tsCharset); - taosDumpInOneFile(taos, fp, g_tsCharset, g_args.encode, + dumpInOneFile(taos, fp, g_tsCharset, g_args.encode, g_tsDbSqlFile); } taos_close(taos); if (0 != tsSqlFileNumOfTbls) { - taosStartDumpInWorkThreads(); + startDumpInWorkThreads(); } - taosFreeDumpFiles(); + freeDumpFiles(); return 0; } @@ -2890,7 +3052,7 @@ int main(int argc, char *argv[]) { fprintf(g_fpOfResult, "# DumpIn start time: %d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); - if (taosDumpIn() < 0) { + if (dumpIn() < 0) { ret = -1; } } else { @@ -2898,7 +3060,7 @@ int main(int argc, char *argv[]) { fprintf(g_fpOfResult, "# DumpOut start time: %d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); - if (taosDumpOut() < 0) { + if (dumpOut() < 0) { ret = -1; } else { fprintf(g_fpOfResult, "\n============================== TOTAL STATISTICS ============================== \n");