|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Table model: CREATE DATABASE/TABLE, insert rows via Tablet, SELECT, DROP DATABASE. |
| 22 | + * Requires IoTDB table-SQL support. Edit HOST / PORT / credentials below. |
| 23 | + */ |
| 24 | + |
| 25 | +#include <stdio.h> |
| 26 | +#include <stdlib.h> |
| 27 | +#include <string.h> |
| 28 | + |
| 29 | +#include "SessionC.h" |
| 30 | + |
| 31 | +#define HOST "127.0.0.1" |
| 32 | +#define PORT 6667 |
| 33 | +#define USER "root" |
| 34 | +#define PASS "root" |
| 35 | + |
| 36 | +#define DB_NAME "cdemo_db" |
| 37 | +#define TABLE_NAME "cdemo_t0" |
| 38 | + |
| 39 | +static void fail(const char* ctx, CTableSession* s) { |
| 40 | + fprintf(stderr, "[table_example] %s failed: %s\n", ctx, ts_get_last_error()); |
| 41 | + if (s) { |
| 42 | + ts_table_session_close(s); |
| 43 | + ts_table_session_destroy(s); |
| 44 | + } |
| 45 | + exit(1); |
| 46 | +} |
| 47 | + |
| 48 | +int main(void) { |
| 49 | + /* Last arg: default database name; empty string leaves it unset (we USE "cdemo_db" via SQL below). */ |
| 50 | + CTableSession* session = ts_table_session_new(HOST, PORT, USER, PASS, ""); |
| 51 | + if (!session) { |
| 52 | + fprintf(stderr, "[table_example] ts_table_session_new returned NULL: %s\n", ts_get_last_error()); |
| 53 | + return 1; |
| 54 | + } |
| 55 | + if (ts_table_session_open(session) != TS_OK) { |
| 56 | + fail("ts_table_session_open", session); |
| 57 | + } |
| 58 | + |
| 59 | + char sql[512]; |
| 60 | + snprintf(sql, sizeof(sql), "DROP DATABASE IF EXISTS %s", DB_NAME); |
| 61 | + (void)ts_table_session_execute_non_query(session, sql); |
| 62 | + |
| 63 | + snprintf(sql, sizeof(sql), "CREATE DATABASE %s", DB_NAME); |
| 64 | + if (ts_table_session_execute_non_query(session, sql) != TS_OK) { |
| 65 | + fail("CREATE DATABASE", session); |
| 66 | + } |
| 67 | + |
| 68 | + snprintf(sql, sizeof(sql), "USE \"%s\"", DB_NAME); |
| 69 | + if (ts_table_session_execute_non_query(session, sql) != TS_OK) { |
| 70 | + fail("USE DATABASE", session); |
| 71 | + } |
| 72 | + |
| 73 | + const char* ddl = |
| 74 | + "CREATE TABLE " TABLE_NAME " (" |
| 75 | + "tag1 string tag," |
| 76 | + "attr1 string attribute," |
| 77 | + "m1 double field)"; |
| 78 | + if (ts_table_session_execute_non_query(session, ddl) != TS_OK) { |
| 79 | + fail("CREATE TABLE", session); |
| 80 | + } |
| 81 | + |
| 82 | + const char* columnNames[] = {"tag1", "attr1", "m1"}; |
| 83 | + TSDataType_C dataTypes[] = {TS_TYPE_STRING, TS_TYPE_STRING, TS_TYPE_DOUBLE}; |
| 84 | + TSColumnCategory_C colCategories[] = {TS_COL_TAG, TS_COL_ATTRIBUTE, TS_COL_FIELD}; |
| 85 | + |
| 86 | + CTablet* tablet = ts_tablet_new_with_category(TABLE_NAME, 3, columnNames, dataTypes, colCategories, 100); |
| 87 | + if (!tablet) { |
| 88 | + fail("ts_tablet_new_with_category", session); |
| 89 | + } |
| 90 | + |
| 91 | + int i; |
| 92 | + for (i = 0; i < 5; i++) { |
| 93 | + if (ts_tablet_add_timestamp(tablet, i, (int64_t)i) != TS_OK) { |
| 94 | + ts_tablet_destroy(tablet); |
| 95 | + fail("ts_tablet_add_timestamp", session); |
| 96 | + } |
| 97 | + if (ts_tablet_add_value_string(tablet, 0, i, "device_A") != TS_OK) { |
| 98 | + ts_tablet_destroy(tablet); |
| 99 | + fail("ts_tablet_add_value_string tag", session); |
| 100 | + } |
| 101 | + if (ts_tablet_add_value_string(tablet, 1, i, "attr_val") != TS_OK) { |
| 102 | + ts_tablet_destroy(tablet); |
| 103 | + fail("ts_tablet_add_value_string attr", session); |
| 104 | + } |
| 105 | + if (ts_tablet_add_value_double(tablet, 2, i, (double)i * 1.5) != TS_OK) { |
| 106 | + ts_tablet_destroy(tablet); |
| 107 | + fail("ts_tablet_add_value_double", session); |
| 108 | + } |
| 109 | + } |
| 110 | + if (ts_tablet_set_row_count(tablet, 5) != TS_OK) { |
| 111 | + ts_tablet_destroy(tablet); |
| 112 | + fail("ts_tablet_set_row_count", session); |
| 113 | + } |
| 114 | + |
| 115 | + if (ts_table_session_insert(session, tablet) != TS_OK) { |
| 116 | + ts_tablet_destroy(tablet); |
| 117 | + fail("ts_table_session_insert", session); |
| 118 | + } |
| 119 | + ts_tablet_destroy(tablet); |
| 120 | + |
| 121 | + CSessionDataSet* dataSet = NULL; |
| 122 | + if (ts_table_session_execute_query(session, "SELECT * FROM " TABLE_NAME, &dataSet) != TS_OK) { |
| 123 | + fail("ts_table_session_execute_query", session); |
| 124 | + } |
| 125 | + if (!dataSet) { |
| 126 | + fprintf(stderr, "[table_example] dataSet is NULL\n"); |
| 127 | + ts_table_session_close(session); |
| 128 | + ts_table_session_destroy(session); |
| 129 | + return 1; |
| 130 | + } |
| 131 | + ts_dataset_set_fetch_size(dataSet, 1024); |
| 132 | + |
| 133 | + int count = 0; |
| 134 | + while (ts_dataset_has_next(dataSet)) { |
| 135 | + CRowRecord* record = ts_dataset_next(dataSet); |
| 136 | + if (!record) { |
| 137 | + break; |
| 138 | + } |
| 139 | + printf("[table_example] row %d: time=%lld\n", count, (long long)ts_row_record_get_timestamp(record)); |
| 140 | + ts_row_record_destroy(record); |
| 141 | + count++; |
| 142 | + } |
| 143 | + ts_dataset_destroy(dataSet); |
| 144 | + printf("[table_example] SELECT returned %d row(s).\n", count); |
| 145 | + |
| 146 | + snprintf(sql, sizeof(sql), "DROP DATABASE IF EXISTS %s", DB_NAME); |
| 147 | + (void)ts_table_session_execute_non_query(session, sql); |
| 148 | + |
| 149 | + ts_table_session_close(session); |
| 150 | + ts_table_session_destroy(session); |
| 151 | + return 0; |
| 152 | +} |
0 commit comments