Split things into pieces, continue implementation of mysql backend
This commit is contained in:
@@ -1,18 +0,0 @@
|
|||||||
#ifndef _SMART_RECORDS_MYSQL_BACKEND_H
|
|
||||||
#define _SMART_RECORDS_MYSQL_BACKEND_H
|
|
||||||
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include "smart_records_internals.h"
|
|
||||||
//#include <mysql.h>
|
|
||||||
|
|
||||||
typedef struct mysql_backend_ctx_t {
|
|
||||||
//MYSQL *mysql;
|
|
||||||
char *format;
|
|
||||||
} mysql_backend_ctx_t;
|
|
||||||
|
|
||||||
int mysql_backend_write_record(smart_records_ctx_t *ctx, char **error, va_list ap);
|
|
||||||
int mysql_backend_set_format(smart_records_ctx_t *ctx, char **error, char *format);
|
|
||||||
int mysql_backend_close_records_base(smart_records_ctx_t *ctx);
|
|
||||||
void *mysql_backend_init(smart_records_ctx_t *ctx, char **error);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
61
src/mysql_backend/mysql_backend.c
Normal file
61
src/mysql_backend/mysql_backend.c
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2020 Gérald Colangelo <gcolangelo@ospow.com>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "../smart_records_internals.h"
|
||||||
|
#include "mysql_backend_internals.h"
|
||||||
|
|
||||||
|
int mysql_backend_open_records_base(void *ctx, char **error, const char *uri)
|
||||||
|
{
|
||||||
|
mysql_backend_ctx_t *c;
|
||||||
|
if (!ctx || !uri) {
|
||||||
|
set_error_and_fails("invalid arguments while opening records base");
|
||||||
|
}
|
||||||
|
|
||||||
|
c = (mysql_backend_ctx_t *)ctx;
|
||||||
|
|
||||||
|
c->uri = mysql_parse_uri(error, uri);
|
||||||
|
if (!c->uri)
|
||||||
|
goto failed;
|
||||||
|
if(!mysql_real_connect(c->mysql_obj, c->uri->host,
|
||||||
|
c->uri->user,
|
||||||
|
c->uri->passwd,
|
||||||
|
c->uri->db,
|
||||||
|
c->uri->port,
|
||||||
|
NULL,0)) {
|
||||||
|
set_error_and_fails(mysql_error(c->mysql_obj));
|
||||||
|
}
|
||||||
|
failed:
|
||||||
|
if(c->uri)
|
||||||
|
mysql_free_uri(c->uri);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *mysql_backend_init(char **error)
|
||||||
|
{
|
||||||
|
mysql_backend_ctx_t *ctx;
|
||||||
|
ctx = malloc(sizeof(mysql_backend_ctx_t));
|
||||||
|
ctx->mysql_obj = mysql_init(NULL);
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
13
src/mysql_backend/mysql_backend.h
Normal file
13
src/mysql_backend/mysql_backend.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef _SMART_RECORDS_MYSQL_BACKEND_H
|
||||||
|
#define _SMART_RECORDS_MYSQL_BACKEND_H
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
|
||||||
|
void *mysql_backend_init(char **error);
|
||||||
|
int mysql_backend_open_records_base(void *ctx, char **error, const char *uri);
|
||||||
|
int mysql_backend_write_record(void **ctx, char **error, va_list ap);
|
||||||
|
int mysql_backend_set_format(void **ctx, char **error, char *format);
|
||||||
|
int mysql_backend_close_records_base(void **ctx);
|
||||||
|
|
||||||
|
#endif
|
||||||
24
src/mysql_backend/mysql_backend_internals.h
Normal file
24
src/mysql_backend/mysql_backend_internals.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#ifndef MYSQL_BACKEND_INTERNALS_H
|
||||||
|
#define MYSQL_BACKEND_INTERNALS_H
|
||||||
|
|
||||||
|
#include <mysql.h>
|
||||||
|
#define URI_SCHEME "mysql://user[:password]@host[:port]/db_name.table"
|
||||||
|
|
||||||
|
typedef struct mysql_uri {
|
||||||
|
char *host;
|
||||||
|
char *user;
|
||||||
|
char *passwd;
|
||||||
|
char *db;
|
||||||
|
char *table;
|
||||||
|
unsigned int port;
|
||||||
|
} mysql_uri_t;
|
||||||
|
|
||||||
|
typedef struct mysql_backend_ctx_t {
|
||||||
|
MYSQL *mysql_obj;
|
||||||
|
mysql_uri_t *uri;
|
||||||
|
} mysql_backend_ctx_t;
|
||||||
|
|
||||||
|
|
||||||
|
void mysql_free_uri(mysql_uri_t * uri);
|
||||||
|
mysql_uri_t *mysql_parse_uri(char **error, const char *uri);
|
||||||
|
#endif
|
||||||
@@ -23,19 +23,9 @@ SOFTWARE.
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "smart_records_internals.h"
|
#include "../smart_records_internals.h"
|
||||||
//#include <mysql.h>
|
#include <mysql.h>
|
||||||
|
#include "mysql_backend_internals.h"
|
||||||
#define URI_SCHEME "mysql://user[:password]@host[:port]/db_name.table"
|
|
||||||
|
|
||||||
typedef struct mysql_uri {
|
|
||||||
char *host;
|
|
||||||
char *user;
|
|
||||||
char *passwd;
|
|
||||||
char *db;
|
|
||||||
char *table;
|
|
||||||
unsigned int port;
|
|
||||||
} mysql_uri_t;
|
|
||||||
|
|
||||||
void mysql_free_uri(mysql_uri_t * uri)
|
void mysql_free_uri(mysql_uri_t * uri)
|
||||||
{
|
{
|
||||||
@@ -54,9 +44,7 @@ void mysql_free_uri(mysql_uri_t * uri)
|
|||||||
free(uri);
|
free(uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mysql_uri_t *mysql_parse_uri(char **error, const char *uri)
|
||||||
|
|
||||||
mysql_uri_t *mysql_parse_uri( /* MYSQL *mysql, */ const char *uri, char **error)
|
|
||||||
{
|
{
|
||||||
int conv;
|
int conv;
|
||||||
mysql_uri_t *r=NULL;
|
mysql_uri_t *r=NULL;
|
||||||
@@ -134,23 +122,18 @@ failed:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifdef _TEST
|
#ifdef _TEST
|
||||||
char *tests[7][2] = {
|
char *tests[7][2] = {
|
||||||
{ "Every info provided",
|
{ "Every info provided",
|
||||||
"mysql://leon:bruxelles@lafrite:1024/STOCK_PATATE.avril" },
|
"mysql://leon:bruxelles@lafrite:1024/STOCK_PATATE.avril" },
|
||||||
/*
|
|
||||||
{ "Every info provided except password",
|
{ "Every info provided except password",
|
||||||
"mysql://leon@lafrite:1024/STOCK_PATATE.avril" },
|
"mysql://leon@lafrite:1024/STOCK_PATATE.avril" },
|
||||||
*/
|
|
||||||
{ "Every info provided but empty password",
|
{ "Every info provided but empty password",
|
||||||
"mysql://leon:@lafrite:1024/STOCK_PATATE.avril" },
|
"mysql://leon:@lafrite:1024/STOCK_PATATE.avril" },
|
||||||
{ "Every info provided except port number",
|
{ "Every info provided except port number",
|
||||||
"mysql://leon:coin@lafrite/STOCK_PATATE.avril" },
|
"mysql://leon:coin@lafrite/STOCK_PATATE.avril" },
|
||||||
/*
|
|
||||||
{ "Bad URI #1", "mysql://lafrite/PATATE.avril" },
|
{ "Bad URI #1", "mysql://lafrite/PATATE.avril" },
|
||||||
{ "Bad URI #1", "mysql://lafrite/" },
|
{ "Bad URI #1", "mysql://lafrite/" },
|
||||||
*/
|
|
||||||
{ NULL, NULL },
|
{ NULL, NULL },
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ smart_records_ctx_t *smart_records_init(){
|
|||||||
return malloc(sizeof(smart_records_ctx_t));
|
return malloc(sizeof(smart_records_ctx_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
int smart_records_open_base(smart_records_ctx_t *ctx, char *uri, char **error)
|
int smart_records_open_base(smart_records_ctx_t *ctx, char **error, const char *uri)
|
||||||
{
|
{
|
||||||
int len_uri;
|
int len_uri;
|
||||||
|
|
||||||
@@ -38,11 +38,11 @@ int smart_records_open_base(smart_records_ctx_t *ctx, char *uri, char **error)
|
|||||||
len_uri = strlen(uri);
|
len_uri = strlen(uri);
|
||||||
if(!strncmp("mysql://", uri, len_uri < 8 ? len_uri : 8)) {
|
if(!strncmp("mysql://", uri, len_uri < 8 ? len_uri : 8)) {
|
||||||
#ifdef _HAS_MYSQL
|
#ifdef _HAS_MYSQL
|
||||||
ctx->CTX = malloc(sizeof(mysql_backend_ctx_t));
|
ctx->backend_ctx = NULL;
|
||||||
ctx->write_record = mysql_backend_write_record;
|
ctx->write_record = mysql_backend_write_record;
|
||||||
ctx->set_format = mysql_backend_set_format;
|
ctx->set_format = mysql_backend_set_format;
|
||||||
ctx->close_records_base = mysql_backend_close_records_base;
|
ctx->close_records_base = mysql_backend_close_records_base;
|
||||||
if ( mysql_backend_init(ctx->CTX, error) < 0)
|
if ((ctx->backend_ctx = mysql_backend_init(error) == NULL)
|
||||||
goto failed;
|
goto failed;
|
||||||
#else
|
#else
|
||||||
set_error_and_fails("built without support for mysql:// URIs");
|
set_error_and_fails("built without support for mysql:// URIs");
|
||||||
@@ -52,44 +52,26 @@ int smart_records_open_base(smart_records_ctx_t *ctx, char *uri, char **error)
|
|||||||
|
|
||||||
else if(!strncmp("sqlite://", uri, len_uri < 9 ? len_uri : 9)) {
|
else if(!strncmp("sqlite://", uri, len_uri < 9 ? len_uri : 9)) {
|
||||||
#ifdef _HAS_SQLITE
|
#ifdef _HAS_SQLITE
|
||||||
ctx->CTX = malloc(sizeof(sqlite_backend_ctx_t));
|
|
||||||
ctx->write_record = sqlite_backend_write_record(char *uri);
|
|
||||||
ctx->close_records_base = sqlite_backend_close_records_base(char *uri);
|
|
||||||
if ( sqlite_backend_init(ctx->CTX, error) < 0)
|
|
||||||
goto failed;
|
|
||||||
#else
|
#else
|
||||||
if(error) *error = strdup("built without support for sqlite:// URIs");
|
set_error_and_fails("built without support for sqlite:// URIs");
|
||||||
goto failed;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
else if(!strncmp("udp://", uri, len_uri < 8 ? len_uri : 8)) {
|
else if(!strncmp("udp://", uri, len_uri < 8 ? len_uri : 8)) {
|
||||||
#ifdef _HAS_UDP
|
#ifdef _HAS_UDP
|
||||||
ctx->CTX = malloc(sizeof(udp_backend_ctx_t));
|
|
||||||
ctx->write_record = udp_backend_write_record(char *uri);
|
|
||||||
ctx->close_records_base = udp_backend_close_records_base(char *uri);
|
|
||||||
if ( udp_backend_init(ctx->CTX, error) < 0)
|
|
||||||
goto failed;
|
|
||||||
#else
|
#else
|
||||||
if(error) *error = strdup("built without support for udp:// URIs");
|
set_error_and_fails("built without support for udp:// URIs");
|
||||||
goto failed;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
else if(!strncmp("csv://", uri, len_uri < 8 ? len_uri : 8)) {
|
else if(!strncmp("csv://", uri, len_uri < 8 ? len_uri : 8)) {
|
||||||
#ifdef _HAS_CSV
|
#ifdef _HAS_CSV
|
||||||
ctx->CTX = malloc(sizeof(csv_backend_ctx_t));
|
|
||||||
ctx->write_record = csv_backend_write_record(char *uri);
|
|
||||||
ctx->close_records_base = csv_backend_close_records_base(char *uri);
|
|
||||||
if ( csv_backend_init(ctx->CTX, error) < 0)
|
|
||||||
goto failed;
|
|
||||||
#else
|
#else
|
||||||
if(error) *error = strdup("built without suppor fort csv:// URIs");
|
set_error_and_fails("built without suppor fort csv:// URIs");
|
||||||
goto failed;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if(error) *error = strdup("Unknown scheme or bad URI.");
|
set_error_and_fails("Unknown scheme or bad URI.");
|
||||||
goto failed;
|
goto failed;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -97,6 +79,11 @@ failed:
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int smart_records_set_format(smart_records_ctx_t *ctx, char **error, const char *fmt)
|
||||||
|
{
|
||||||
|
return ctx->set_format(ctx, error, fmt);
|
||||||
|
}
|
||||||
|
|
||||||
int smart_records_close_base(smart_records_ctx_t *ctx)
|
int smart_records_close_base(smart_records_ctx_t *ctx)
|
||||||
{
|
{
|
||||||
return ctx->close_records_base(ctx);
|
return ctx->close_records_base(ctx);
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
#include "smart_records_internals.h"
|
#include "smart_records_internals.h"
|
||||||
|
|
||||||
smart_records_ctx_t *smart_records_init();
|
smart_records_ctx_t *smart_records_init();
|
||||||
int smart_records_open_base(smart_records_ctx_t *ctx, char *uri, char **error);
|
int smart_records_open_base(smart_records_ctx_t *ctx, char **error, const char *uri);
|
||||||
int smart_records_set_format(smart_records_ctx_t *ctx, char *fmt, char **error);
|
int smart_records_set_format(smart_records_ctx_t *ctx, char **error, const char *fmt);
|
||||||
int smart_records_write(smart_records_ctx_t *ctx, char **error, va_list ap);
|
int smart_records_write(smart_records_ctx_t *ctx, char **error, va_list ap);
|
||||||
int smart_records_close_base(smart_records_ctx_t *ctx);
|
int smart_records_close_base(smart_records_ctx_t *ctx);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
typedef struct smart_records_ctx_t {
|
typedef struct smart_records_ctx_t {
|
||||||
void *CTX;
|
void *CTX;
|
||||||
int (*set_format) (struct smart_records_ctx_t *ctx, char **error, char *fmp);
|
int (*set_format) (struct smart_records_ctx_t *ctx, char **error, const char *fmt);
|
||||||
int (*write_record) (struct smart_records_ctx_t *ctx, char **error, va_list ap);
|
int (*write_record) (struct smart_records_ctx_t *ctx, char **error, va_list ap);
|
||||||
int (*close_records_base) (struct smart_records_ctx_t *ctx);
|
int (*close_records_base) (struct smart_records_ctx_t *ctx);
|
||||||
} smart_records_ctx_t ;
|
} smart_records_ctx_t ;
|
||||||
|
|||||||
Reference in New Issue
Block a user