Skip to content

Commit

Permalink
Merge branch 'main' into pq.c3l
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno authored Dec 30, 2024
2 parents 9f0815d + e6dd243 commit df213d5
Show file tree
Hide file tree
Showing 5 changed files with 357 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This repository contains external libraries for use with C3.
- Vulkan (WIP)
- GLFW https://www.glfw.org/ - WIP
- PQ (PostgreSQL) https://www.postgresql.org/docs/current/libpq.html
- MySQL 8 https://dev.mysql.com/doc/c-api/8.0/en/
- SQLite 3 https://sqlite.com/c3ref/intro.html

## Guide for writing bindings

Expand Down
45 changes: 45 additions & 0 deletions libraries/mysql8.c3l/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"provides": "mysql8",
"targets": {
"macos-aarch64": {
"linked-libraries": [
"mysqlclient"
]
},
"macos-x64": {
"linked-libraries": [
"mysqlclient"
]
},
"linux-x64": {
"linked-libraries": [
"mysqlclient"
]
},
"linux-x86": {
"linked-libraries": [
"mysqlclient"
]
},
"openbsd-x86": {
"linked-libraries": [
"mysqlclient"
]
},
"openbsd-x64": {
"linked-libraries": [
"mysqlclient"
]
},
"freebsd-x64": {
"linked-libraries": [
"mysqlclient"
]
},
"freebsd-x86": {
"linked-libraries": [
"mysqlclient"
]
}
}
}
161 changes: 161 additions & 0 deletions libraries/mysql8.c3l/mysql8.c3i
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// mysql8.c3i
// Binding originally created by Louis @ https://github.com/louis77

module mysql8;

def Mysql = void*;
def MysqlRes = void*;
def MysqlRow = void**;
def MysqlStmt = void*;

const int MYSQL_NO_DATA = 100;

distinct FieldType = int;

const FieldType TYPE_DECIMAL = 0;
const FieldType TYPE_TINY = 1;
const FieldType TYPE_SHORT = 2;
const FieldType TYPE_LONG = 3;
const FieldType TYPE_FLOAT = 4;
const FieldType TYPE_DOUBLE = 5;
const FieldType TYPE_NULL = 6;
const FieldType TYPE_TIMESTAMP = 7;
const FieldType TYPE_LONGLONG = 8;
const FieldType TYPE_INT24 = 9;
const FieldType TYPE_DATE = 10;
const FieldType TYPE_TIME = 11;
const FieldType TYPE_DATETIME = 12;
const FieldType TYPE_YEAR = 13;
const FieldType TYPE_NEWDATE = 14; // Unused
const FieldType TYPE_VARCHAR = 15;
const FieldType TYPE_BIT = 16;
const FieldType TYPE_TIMESTAMP2 = 17;
const FieldType TYPE_DATETIME2 = 18; /**< Internal to MySQL. Not used in protocol */
const FieldType TYPE_TIME2 = 19; /**< Internal to MySQL. Not used in protocol */
const FieldType TYPE_TYPED_ARRAY = 20; /**< Used for replication only */
const FieldType TYPE_INVALID = 243;
const FieldType TYPE_JSON = 245;
const FieldType TYPE_NEWDECIMAL = 246;
const FieldType TYPE_ENUM = 247;
const FieldType TYPE_SET = 248;
const FieldType TYPE_TINY_BLOB = 249;
const FieldType TYPE_MEDIUM_BLOB = 250;
const FieldType TYPE_LONG_BLOB = 251;
const FieldType TYPE_BLOB = 252;
const FieldType TYPE_VAR_STRING = 253;
const FieldType TYPE_STRING = 254;
const FieldType TYPE_GEOMETRY = 255;

enum MysqlOption : CInt {
OPT_CONNECT_TIMEOUT,
OPT_COMPRESS,
OPT_NAMED_PIPE,
INIT_COMMAND,
READ_DEFAULT_FILE,
READ_DEFAULT_GROUP,
SET_CHARSET_DIR,
SET_CHARSET_NAME,
OPT_LOCAL_INFILE,
OPT_PROTOCOL,
SHARED_MEMORY_BASE_NAME,
OPT_READ_TIMEOUT,
OPT_WRITE_TIMEOUT,
OPT_USE_RESULT,
REPORT_DATA_TRUNCATION,
OPT_RECONNECT,
PLUGIN_DIR,
DEFAULT_AUTH,
OPT_BIND,
OPT_SSL_KEY,
OPT_SSL_CERT,
OPT_SSL_CA,
OPT_SSL_CAPATH,
OPT_SSL_CIPHER,
OPT_SSL_CRL,
OPT_SSL_CRLPATH,
OPT_CONNECT_ATTR_RESET,
OPT_CONNECT_ATTR_ADD,
OPT_CONNECT_ATTR_DELETE,
SERVER_PUBLIC_KEY,
ENABLE_CLEARTEXT_PLUGIN,
OPT_CAN_HANDLE_EXPIRED_PASSWORDS,
OPT_MAX_ALLOWED_PACKET,
OPT_NET_BUFFER_LENGTH,
OPT_TLS_VERSION,
OPT_SSL_MODE,
OPT_GET_SERVER_PUBLIC_KEY,
OPT_RETRY_COUNT,
OPT_OPTIONAL_RESULTSET_METADATA,
OPT_SSL_FIPS_MODE,
OPT_TLS_CIPHERSUITES,
OPT_COMPRESSION_ALGORITHMS,
OPT_ZSTD_COMPRESSION_LEVEL,
OPT_LOAD_DATA_LOCAL_DIR,
OPT_USER_PASSWORD,
OPT_SSL_SESSION_DATA,
OPT_TLS_SNI_SERVERNAME
}

enum Autocommit : CInt {
AUTOCOMMIT_OFF,
AUTOCOMMIT_ON
}

extern fn CULong affected_rows(Mysql mysql) @extern("mysql_affected_rows");
extern fn bool autocommit(Mysql mysql, Autocommit mode) @extern("mysql_autocommit");
extern fn void close(Mysql mysql) @extern("mysql_close");
extern fn bool commit(Mysql mysql) @extern("mysql_commit");
extern fn Mysql init(Mysql mysql) @extern("mysql_init");
extern fn CUInt errno(Mysql mysql) @extern("mysql_errno");
extern fn ZString error(Mysql mysql) @extern("mysql_error");
extern fn MysqlRow fetch_row(MysqlRes result) @extern("mysql_fetch_row");
extern fn void free_result(MysqlRes result) @extern("mysql_free_result");
extern fn void library_end() @extern("mysql_server_end");
extern fn CInt library_init(CInt argc, void* argv, void* groups) @extern("mysql_server_init");
extern fn CUInt num_fields(MysqlRes result) @extern("mysql_num_fields");
extern fn CInt options(Mysql *mysql, MysqlOption option, void *arg) @extern("mysql_options");
extern fn CInt ping(Mysql mysql) @extern("mysql_ping");
extern fn Mysql real_connect(Mysql mysql, ZString host, ZString user, ZString passwd, ZString db, CUInt port, ZString unix_socket, CULong client_flag) @extern("mysql_real_connect");
extern fn CInt real_query(Mysql mysql, ZString stmt_str, CULong length) @extern("mysql_real_query");
extern fn bool rollback(Mysql mysql) @extern("mysql_rollback");
extern fn MysqlRes use_result(Mysql mysql) @extern("mysql_use_result");

// Prepared Statement Functions

struct MysqlBind {
CULong *length; /* output length pointer */
bool *is_null; /* Pointer to null indicator */
void* buffer; /* buffer to get/put data */
/* set this if you want to track data truncations happened during fetch */
bool *error;
CUChar *row_ptr; /* for the current data position */
void* store_param_func;
void* fetch_result;
void* skip_result;

CULong buffer_length; /* output buffer length, must be set when fetching str/binary */
CULong offset; /* offset position for char/binary fetch */
CULong length_value; /* Used if length is 0 */
CUInt param_number; /* For null count and error messages */
CUInt pack_length; /* Internal length for packed data */
FieldType buffer_type; /* buffer type */ // ENUM enum_field_types
bool error_value; /* used if error is 0 */
bool is_unsigned; /* set if integer type is unsigned */
bool long_data_used; /* If used with mysql_send_long_data */
bool is_null_value; /* Used if is_null is 0 */
void *extension;
}

extern fn CULong stmt_affected_rows(MysqlStmt stmt) @extern("mysql_stmt_affected_rows");
extern fn bool stmt_bind_param(MysqlStmt stmt, MysqlBind* bind) @extern("mysql_stmt_bind_param");
extern fn bool stmt_close(MysqlStmt stmt) @extern("mysql_stmt_close");
extern fn CInt stmt_execute(MysqlStmt stmt) @extern("mysql_stmt_execute");
extern fn CInt stmt_fetch(MysqlStmt stmt) @extern("mysql_stmt_fetch");
extern fn CInt stmt_fetch_column(MysqlStmt stmt, MysqlBind* bind, CUInt column, CULong offset) @extern("mysql_stmt_fetch_column");
extern fn CUInt stmt_field_count(MysqlStmt stmt) @extern("mysql_stmt_field_count");
extern fn bool stmt_free_result(MysqlStmt stmt) @extern("mysql_stmt_free_result");
extern fn MysqlStmt stmt_init(Mysql mysql) @extern("mysql_stmt_init");
extern fn CInt stmt_next_result(MysqlStmt stmt) @extern("mysql_stmt_next_result");
extern fn CInt stmt_prepare(MysqlStmt stmt, ZString stmt_str, CULong length) @extern("mysql_stmt_prepare");
extern fn CInt stmt_store_result(MysqlStmt stmt) @extern("mysql_stmt_store_result");
extern fn bool stmt_bind_result(MysqlStmt stmt, MysqlBind *bind) @extern("mysql_stmt_bind_result");
45 changes: 45 additions & 0 deletions libraries/sqlite3.c3l/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"provides": "sqlite3",
"targets": {
"macos-aarch64": {
"linked-libraries": [
"sqlite3"
]
},
"macos-x64": {
"linked-libraries": [
"sqlite3"
]
},
"linux-x64": {
"linked-libraries": [
"sqlite3"
]
},
"linux-x86": {
"linked-libraries": [
"sqlite3"
]
},
"openbsd-x86": {
"linked-libraries": [
"sqlite3"
]
},
"openbsd-x64": {
"linked-libraries": [
"sqlite3"
]
},
"freebsd-x64": {
"linked-libraries": [
"sqlite3"
]
},
"freebsd-x86": {
"linked-libraries": [
"sqlite3"
]
}
}
}
104 changes: 104 additions & 0 deletions libraries/sqlite3.c3l/sqlite.c3i
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// sqlite3.c3i
// Binding originally created by Louis @ https://github.com/louis77

module sqlite3;

distinct ResultStatus = int;

const ResultStatus OK = 0; /* Successful result */
const ResultStatus ERROR = 1; /* Generic error */
const ResultStatus INTERNAL = 2; /* Internal logic error in SQLite */
const ResultStatus PERM = 3; /* Access permission denied */
const ResultStatus ABORT = 4; /* Callback routine requested an abort */
const ResultStatus BUSY = 5; /* The database file is locked */
const ResultStatus LOCKED = 6; /* A table in the database is locked */
const ResultStatus NOMEM = 7; /* A malloc() failed */
const ResultStatus READONLY = 8; /* Attempt to write a readonly database */
const ResultStatus INTERRUPT = 9; /* Operation terminated by interrupt()*/
const ResultStatus IOERR = 10; /* Some kind of disk I/O error occurred */
const ResultStatus CORRUPT = 11; /* The database disk image is malformed */
const ResultStatus NOTFOUND = 12; /* Unknown opcode in file_control() */
const ResultStatus FULL = 13; /* Insertion failed because database is full */
const ResultStatus CANTOPEN = 14; /* Unable to open the database file */
const ResultStatus PROTOCOL = 15; /* Database lock protocol error */
const ResultStatus EMPTY = 16; /* Internal use only */
const ResultStatus SCHEMA = 17; /* The database schema changed */
const ResultStatus TOOBIG = 18; /* String or BLOB exceeds size limit */
const ResultStatus CONSTRAINT = 19; /* Abort due to constraint violation */
const ResultStatus MISMATCH = 20; /* Data type mismatch */
const ResultStatus MISUSE = 21; /* Library used incorrectly */
const ResultStatus NOLFS = 22; /* Uses OS features not supported on host */
const ResultStatus AUTH = 23; /* Authorization denied */
const ResultStatus FORMAT = 24; /* Not used */
const ResultStatus RANGE = 25; /* 2nd parameter to bind out of range */
const ResultStatus NOTADB = 26; /* File opened that is not a database file */
const ResultStatus NOTICE = 27; /* Notifications from log() */
const ResultStatus WARNING = 28; /* Warnings from log() */
const ResultStatus ROW = 100; /* step() has another row ready */
const ResultStatus DONE = 101; /* step() has finished executing */

distinct ColumnType = int;

const ColumnType INTEGER = 1;
const ColumnType FLOAT = 2;
const ColumnType BLOB = 4;
const ColumnType NULL = 5;
const ColumnType TEXT = 3;

def SqliteHandle = void*;
def SqliteStmt = void*;
def ExecCallback = fn ResultStatus (void* context, CInt num_columns, ZString* result, ZString* col_names);

extern fn ResultStatus open(
ZString filename,
SqliteHandle* ppDb
) @extern("sqlite3_open");

extern fn ResultStatus close(SqliteHandle handle) @extern("sqlite3_close");

extern fn ResultStatus exec(
SqliteHandle handle,
ZString sql,
ExecCallback callback,
void* arg,
ZString* errmsg
) @extern("sqlite3_exec");

extern fn ResultStatus prepare_v2(
SqliteHandle handle,
ZString zSql,
CInt nByte,
SqliteStmt* ppStmt,
ZString* pzTail
) @extern("sqlite3_prepare_v2");

extern fn ResultStatus step(SqliteStmt stmt) @extern("sqlite3_step");
extern fn ResultStatus finalize(SqliteStmt pStmt) @extern("sqlite3_finalize");
extern fn CLong changes64(SqliteHandle handle) @extern("sqlite3_changes64");
extern fn CInt column_count(SqliteStmt pStmt) @extern("sqlite3_column_count");

extern fn void* column_blob(SqliteStmt stmt, int iCol) @extern("sqlite3_column_blob");
extern fn double column_double(SqliteStmt stmt, int iCol) @extern("sqlite3_column_double");
extern fn CInt column_int(SqliteStmt stmt, int iCol) @extern("sqlite3_column_int");
extern fn CLong column_int64(SqliteStmt stmt, int iCol) @extern("sqlite3_column_int64");
extern fn ZString column_text(SqliteStmt stmt, int iCol) @extern("sqlite3_column_text");
extern fn void* column_text16(SqliteStmt stmt, int iCol) @extern("sqlite3_column_text16");
extern fn void* column_value(SqliteStmt stmt, int iCol) @extern("sqlite3_column_value");
extern fn CInt column_bytes(SqliteStmt stmt, int iCol) @extern("sqlite3_column_bytes");
extern fn CInt column_bytes16(SqliteStmt stmt, int iCol) @extern("sqlite3_column_bytes16");
extern fn ColumnType column_type(SqliteStmt stmt, int iCol) @extern("sqlite3_column_type");

extern fn ResultStatus bind_blob(SqliteStmt stmt, CInt, void*, CInt n, void*) @extern("sqlite3_bind_blob");
extern fn ResultStatus bind_blob64(SqliteStmt stmt, CInt, void*, CULong, void*) @extern("sqlite3_bind_blob64");
extern fn ResultStatus bind_double(SqliteStmt stmt, CInt, double) @extern("sqlite3_bind_double");
extern fn ResultStatus bind_int(SqliteStmt stmt, CInt, CInt) @extern("sqlite3_bind_int");
extern fn ResultStatus bind_int64(SqliteStmt stmt, CInt, CLong) @extern("sqlite3_bind_int64");
extern fn ResultStatus bind_null(SqliteStmt stmt, CInt) @extern("sqlite3_bind_null");
extern fn ResultStatus bind_text(SqliteStmt stmt, CInt, ZString, CInt, void*) @extern("sqlite3_bind_text");
extern fn ResultStatus bind_text16(SqliteStmt stmt, CInt, void*, CInt, void*) @extern("sqlite3_bind_text16");
extern fn ResultStatus bind_value(SqliteStmt stmt, CInt, void*) @extern("sqlite3_bind_value");
extern fn ResultStatus bind_pointer(SqliteStmt stmt, CInt, void*, ZString, void*) @extern("sqlite3_bind_pointer");
extern fn ResultStatus bind_zeroblob(SqliteStmt stmt, CInt, CInt n) @extern("sqlite3_bind_zeroblob");
extern fn ResultStatus bind_zeroblob64(SqliteStmt stmt, CInt, CULong) @extern("sqlite3_bind_zeroblob64");

extern fn ZString errmsg(SqliteHandle handle) @extern("sqlite3_errmsg");

0 comments on commit df213d5

Please sign in to comment.