Skip to content

Commit bbb3e8a

Browse files
committed
C API
1 parent 8779733 commit bbb3e8a

File tree

39 files changed

+1082
-151
lines changed

39 files changed

+1082
-151
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ __pycache__/
5252

5353
.idea/
5454
.vscode/
55+
.cursor/
56+
.cursorrules
5557

5658
# KDevelop IDE
5759
*.kdev4

CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ string(REGEX MATCH "YDB_SDK_VERSION = \"([0-9]+\\.[0-9]+\\.[0-9]+)\"" _ ${YDB_SD
55
set(YDB_SDK_VERSION ${CMAKE_MATCH_1})
66
message(STATUS "YDB С++ SDK version: ${YDB_SDK_VERSION}")
77

8-
project(YDB-CPP-SDK VERSION ${YDB_SDK_VERSION} LANGUAGES C CXX ASM)
8+
project(ydb-cpp-sdk VERSION ${YDB_SDK_VERSION} LANGUAGES C CXX ASM)
99

1010
option(YDB_SDK_INSTALL "Install YDB C++ SDK" Off)
1111
option(YDB_SDK_TESTS "Build YDB C++ SDK tests" Off)
@@ -60,11 +60,12 @@ add_subdirectory(library/cpp)
6060
add_subdirectory(include/ydb-cpp-sdk/client)
6161
add_subdirectory(src)
6262
add_subdirectory(util)
63+
add_subdirectory(c_api)
6364

6465
#_ydb_sdk_validate_public_headers()
6566

6667
if (YDB_SDK_ODBC)
67-
add_subdirectory(odbc)
68+
#add_subdirectory(odbc)
6869
endif()
6970

7071
if (YDB_SDK_EXAMPLES)
@@ -80,7 +81,7 @@ if (YDB_SDK_INSTALL)
8081
install(EXPORT ydb-cpp-sdk-targets
8182
FILE ydb-cpp-sdk-targets.cmake
8283
CONFIGURATIONS RELEASE
83-
NAMESPACE YDB-CPP-SDK::
84+
NAMESPACE ydb-cpp-sdk::
8485
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ydb-cpp-sdk/release
8586
)
8687
configure_package_config_file(

c_api/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
add_library(ydb-c-api STATIC
2+
src/driver.cpp
3+
src/query.cpp
4+
src/result.cpp
5+
src/value.cpp
6+
)
7+
8+
target_include_directories(ydb-c-api PUBLIC include)
9+
10+
target_link_libraries(ydb-c-api
11+
PRIVATE
12+
yutil
13+
ydb-cpp-sdk::Query
14+
ydb-cpp-sdk::Table
15+
ydb-cpp-sdk::Driver
16+
)
17+
18+
add_library(ydb-cpp-sdk::c-api ALIAS ydb-c-api)

c_api/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Синхронный API Асинхронный API
2+
3+
libpq:
4+
```c
5+
PGconn *conn = PQconnectdb("...");
6+
// Блокирует выполнение до завершения
7+
```
8+
9+
libpq:
10+
```c
11+
PGconn *conn = PQconnectStart("...");
12+
do {
13+
pollstatus = PQconnectPoll(conn);
14+
// Ожидание событий
15+
} while (pollstatus != PGRES_POLLING_OK);
16+
```
17+
18+
MySQL:
19+
<br>MYSQL *conn = mysql_init(NULL);
20+
<br>mysql_real_connect(conn, ...);
21+
22+
MySQL:
23+
<br>status = mysql_real_connect_nonblocking(mysql, ...);
24+
<br>while (status == NET_ASYNC_NOT_READY) {
25+
<br> // Обработка других задач<br>
26+
status = mysql_real_connect_nonblocking(...);<br>
27+
}
28+
29+
Выполнение запросов
30+
31+
Синхронный API Асинхронный API
32+
33+
libpq: <br>PGresult *res = PQexec(conn, "SELECT ...");<br>Ожидает завершения выполнения
34+
35+
libpq: <br>PQsendQuery(conn, "SELECT ...");<br>// Можно выполнять другую работу<br>while ((res = PQgetResult(conn)) != NULL) {<br> // Обработка результатов<br>}
36+
37+
MySQL: <br>mysql_query(conn, "SELECT ...");<br>result = mysql_store_result(conn);
38+
39+
MySQL: <br>status = mysql_real_query_nonblocking(mysql, "...");<br>// Проверка status и ожидание<br>status = mysql_store_result_nonblocking(mysql, &result);
40+
41+
Обработка ошибок
42+
43+
Синхронный API Асинхронный API
44+
45+
libpq: <br>if (PQstatus(conn) != CONNECTION_OK) {<br> fprintf(stderr, "%s", PQerrorMessage(conn));<br>}
46+
47+
libpq: <br>Такая же проверка, но в каждом шаге асинхронного процесса:<br>if (pollstatus == PGRES_POLLING_FAILED) {<br> fprintf(stderr, "%s", PQerrorMessage(conn));<br>}
48+
49+
MySQL: <br>if (mysql_query(conn, query)) {<br> fprintf(stderr, "%s", mysql_error(conn));<br>}
50+
51+
MySQL: <br>if (status == NET_ASYNC_ERROR) {<br> fprintf(stderr, "%s", mysql_error(mysql));<br>}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
#include <stddef.h>
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
typedef struct TYdbDriverConfigImpl TYdbDriverConfig;
10+
typedef struct TYdbDriverImpl TYdbDriver;
11+
12+
typedef enum {
13+
YDB_DRIVER_CONFIG_OK,
14+
YDB_DRIVER_CONFIG_INVALID,
15+
} EYdbDriverConfigStatus;
16+
17+
typedef enum {
18+
YDB_DRIVER_OK,
19+
YDB_DRIVER_ERROR,
20+
} EYdbDriverStatus;
21+
22+
// Создание и уничтожение конфигурации
23+
TYdbDriverConfig* YdbCreateDriverConfig(const char* connectionString);
24+
void YdbDestroyDriverConfig(TYdbDriverConfig* config);
25+
26+
// Установка параметров конфигурации
27+
TYdbDriverConfig* YdbSetEndpoint(TYdbDriverConfig* config, const char* endpoint);
28+
TYdbDriverConfig* YdbSetDatabase(TYdbDriverConfig* config, const char* database);
29+
TYdbDriverConfig* YdbSetAuthToken(TYdbDriverConfig* config, const char* token);
30+
TYdbDriverConfig* YdbSetSecureConnection(TYdbDriverConfig* config, const char* cert);
31+
32+
// Получение результата конфигурации
33+
EYdbDriverConfigStatus YdbGetDriverConfigStatus(TYdbDriverConfig* config);
34+
const char* YdbGetDriverConfigErrorMessage(TYdbDriverConfig* config);
35+
36+
// Создание и уничтожение драйвера
37+
TYdbDriver* YdbCreateDriver(const char* connectionString);
38+
TYdbDriver* YdbCreateDriverFromConfig(TYdbDriverConfig* config);
39+
void YdbDestroyDriver(TYdbDriver* driver);
40+
41+
// Получение результата драйвера
42+
EYdbDriverStatus YdbGetDriverStatus(TYdbDriver* driver);
43+
const char* YdbGetDriverErrorMessage(TYdbDriver* driver);
44+
45+
#ifdef __cplusplus
46+
}
47+
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
3+
#include <stddef.h>
4+
5+
#include "driver.h"
6+
#include "result.h"
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
typedef struct TYdbQueryClientImpl TYdbQueryClient;
13+
typedef struct TYdbQueryResultImpl TYdbQueryResult;
14+
15+
typedef enum {
16+
YDB_QUERY_CLIENT_OK,
17+
YDB_QUERY_CLIENT_ERROR,
18+
} EYdbQueryClientError;
19+
20+
typedef enum {
21+
YDB_QUERY_RESULT_OK,
22+
YDB_QUERY_RESULT_ERROR,
23+
} EYdbQueryResultError;
24+
25+
// Создание и уничтожение клиента запросов
26+
TYdbQueryClient* YdbCreateQueryClient(TYdbDriver* driver);
27+
void YdbDestroyQueryClient(TYdbQueryClient* queryClient);
28+
29+
// Выполнение запроса
30+
TYdbQueryResult* YdbExecuteQuery(TYdbQueryClient* queryClient, const char* query);
31+
void YdbDestroyQueryResult(TYdbQueryResult* result);
32+
33+
// Получение результата запроса
34+
int YdbGetQueryResultSetsCount(TYdbQueryResult* result);
35+
TYdbResultSet* YdbGetQueryResultSet(TYdbQueryResult* result, size_t index);
36+
37+
#ifdef __cplusplus
38+
}
39+
#endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include "value.h"
4+
5+
#include <stddef.h>
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
typedef struct TYdbResultSetImpl TYdbResultSet;
12+
13+
typedef enum {
14+
YDB_RESULT_SET_OK,
15+
YDB_RESULT_SET_ERROR,
16+
} EYdbResultSetStatus;
17+
18+
int YdbGetColumnsCount(TYdbResultSet* resultSet);
19+
int YdbGetRowsCount(TYdbResultSet* resultSet);
20+
int YdbIsTruncated(TYdbResultSet* resultSet);
21+
22+
const char* YdbGetColumnName(TYdbResultSet* resultSet, size_t index);
23+
int YdbGetColumnIndex(TYdbResultSet* resultSet, const char* name);
24+
25+
TYdbValue* YdbGetValue(TYdbResultSet* resultSet, size_t rowIndex, const char* name);
26+
TYdbValue* YdbGetValueByIndex(TYdbResultSet* resultSet, size_t rowIndex, size_t columnIndex);
27+
28+
void YdbDestroyResultSet(TYdbResultSet* resultSet);
29+
30+
#ifdef __cplusplus
31+
}
32+
#endif
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
#include <stdbool.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
typedef struct TYdbValueImpl TYdbValue;
11+
typedef struct TYdbParamsImpl TYdbParams;
12+
13+
typedef enum {
14+
YDB_VALUE_OK,
15+
YDB_VALUE_ERROR,
16+
} EYdbValueStatus;
17+
18+
typedef enum {
19+
YDB_TYPE_KIND_UNDEFINED,
20+
YDB_TYPE_KIND_PRIMITIVE,
21+
YDB_TYPE_KIND_OPTIONAL,
22+
YDB_TYPE_KIND_LIST,
23+
YDB_TYPE_KIND_TUPLE,
24+
YDB_TYPE_KIND_STRUCT,
25+
YDB_TYPE_KIND_DICT,
26+
YDB_TYPE_KIND_VARIANT,
27+
} EYdbTypeKind;
28+
29+
typedef enum {
30+
YDB_PRIMITIVE_TYPE_UNDEFINED,
31+
YDB_PRIMITIVE_TYPE_BOOL,
32+
YDB_PRIMITIVE_TYPE_INT8,
33+
YDB_PRIMITIVE_TYPE_UINT8,
34+
YDB_PRIMITIVE_TYPE_INT16,
35+
YDB_PRIMITIVE_TYPE_UINT16,
36+
YDB_PRIMITIVE_TYPE_INT32,
37+
YDB_PRIMITIVE_TYPE_UINT32,
38+
YDB_PRIMITIVE_TYPE_INT64,
39+
YDB_PRIMITIVE_TYPE_UINT64,
40+
YDB_PRIMITIVE_TYPE_FLOAT,
41+
YDB_PRIMITIVE_TYPE_DOUBLE,
42+
YDB_PRIMITIVE_TYPE_STRING,
43+
YDB_PRIMITIVE_TYPE_UTF8,
44+
YDB_PRIMITIVE_TYPE_YSON,
45+
YDB_PRIMITIVE_TYPE_JSON,
46+
YDB_PRIMITIVE_TYPE_JSON_DOCUMENT,
47+
YDB_PRIMITIVE_TYPE_DYNUMBER,
48+
} EYdbPrimitiveType;
49+
50+
EYdbTypeKind YdbGetTypeKind(TYdbValue* value);
51+
EYdbPrimitiveType YdbGetPrimitiveType(TYdbValue* value);
52+
53+
EYdbValueStatus YdbGetBool(TYdbValue* value, bool* result);
54+
EYdbValueStatus YdbGetInt8(TYdbValue* value, int8_t* result);
55+
EYdbValueStatus YdbGetUint8(TYdbValue* value, uint8_t* result);
56+
EYdbValueStatus YdbGetInt16(TYdbValue* value, int16_t* result);
57+
EYdbValueStatus YdbGetUint16(TYdbValue* value, uint16_t* result);
58+
EYdbValueStatus YdbGetInt32(TYdbValue* value, int32_t* result);
59+
EYdbValueStatus YdbGetUint32(TYdbValue* value, uint32_t* result);
60+
EYdbValueStatus YdbGetInt64(TYdbValue* value, int64_t* result);
61+
EYdbValueStatus YdbGetUint64(TYdbValue* value, uint64_t* result);
62+
EYdbValueStatus YdbGetFloat(TYdbValue* value, float* result);
63+
EYdbValueStatus YdbGetDouble(TYdbValue* value, double* result);
64+
EYdbValueStatus YdbGetString(TYdbValue* value, char** result);
65+
EYdbValueStatus YdbGetUtf8(TYdbValue* value, char** result);
66+
EYdbValueStatus YdbGetYson(TYdbValue* value, char** result);
67+
EYdbValueStatus YdbGetJson(TYdbValue* value, char** result);
68+
EYdbValueStatus YdbGetJsonDocument(TYdbValue* value, char** result);
69+
EYdbValueStatus YdbGetDyNumber(TYdbValue* value, char** result);
70+
71+
void YdbDestroyValue(TYdbValue* value);
72+
73+
#ifdef __cplusplus
74+
}
75+
#endif

0 commit comments

Comments
 (0)