Formatting
This commit is contained in:
@@ -118,8 +118,9 @@ static int control(ErlDrvData drv_data, unsigned int command, char *buf,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int return_error(sqlite3_drv_t *drv, int error_code,
|
static inline int return_error(
|
||||||
const char *error, ErlDrvTermData **spec, int *term_count) {
|
sqlite3_drv_t *drv, int error_code, const char *error,
|
||||||
|
ErlDrvTermData **spec, int *term_count) {
|
||||||
*spec = (ErlDrvTermData *) malloc(11 * sizeof(ErlDrvTermData));
|
*spec = (ErlDrvTermData *) malloc(11 * sizeof(ErlDrvTermData));
|
||||||
(*spec)[0] = ERL_DRV_PORT;
|
(*spec)[0] = ERL_DRV_PORT;
|
||||||
(*spec)[1] = driver_mk_port(drv->port);
|
(*spec)[1] = driver_mk_port(drv->port);
|
||||||
@@ -136,7 +137,8 @@ static inline int return_error(sqlite3_drv_t *drv, int error_code,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int output_error(sqlite3_drv_t *drv, int error_code, const char *error) {
|
static inline int output_error(
|
||||||
|
sqlite3_drv_t *drv, int error_code, const char *error) {
|
||||||
ErlDrvTermData *dataset;
|
ErlDrvTermData *dataset;
|
||||||
int term_count;
|
int term_count;
|
||||||
return_error(drv, error_code, error, &dataset, &term_count);
|
return_error(drv, error_code, error, &dataset, &term_count);
|
||||||
@@ -158,8 +160,7 @@ static inline int sql_exec_statement(sqlite3_drv_t *drv, sqlite3_stmt *statement
|
|||||||
// fflush(drv->log);
|
// fflush(drv->log);
|
||||||
|
|
||||||
if (sqlite3_threadsafe()) {
|
if (sqlite3_threadsafe()) {
|
||||||
drv->async_handle =
|
drv->async_handle = driver_async(drv->port, &drv->key, sql_exec_async,
|
||||||
driver_async(drv->port, &drv->key, sql_exec_async,
|
|
||||||
async_command, sql_free_async);
|
async_command, sql_free_async);
|
||||||
} else {
|
} else {
|
||||||
sql_exec_async(async_command);
|
sql_exec_async(async_command);
|
||||||
@@ -186,7 +187,8 @@ static int sql_exec(sqlite3_drv_t *drv, char *command, int command_size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline int decode_and_bind_param(
|
static inline int decode_and_bind_param(
|
||||||
sqlite3_drv_t *drv, char *buffer, int *index, sqlite3_stmt *statement, int param_index, int *type, int *size) {
|
sqlite3_drv_t *drv, char *buffer, int *index,
|
||||||
|
sqlite3_stmt *statement, int param_index, int *type, int *size) {
|
||||||
int result;
|
int result;
|
||||||
ei_get_type(buffer, index, type, size);
|
ei_get_type(buffer, index, type, size);
|
||||||
long long_val;
|
long long_val;
|
||||||
@@ -211,6 +213,7 @@ static inline int decode_and_bind_param(
|
|||||||
result = sqlite3_bind_double(statement, param_index, double_val);
|
result = sqlite3_bind_double(statement, param_index, double_val);
|
||||||
break;
|
break;
|
||||||
case ERL_ATOM_EXT:
|
case ERL_ATOM_EXT:
|
||||||
|
// include space for null separator
|
||||||
char_buf_val = malloc((*size + 1) * sizeof(char));
|
char_buf_val = malloc((*size + 1) * sizeof(char));
|
||||||
ei_decode_atom(buffer, index, char_buf_val);
|
ei_decode_atom(buffer, index, char_buf_val);
|
||||||
if (strncmp(char_buf_val, "null", 5) == 0) {
|
if (strncmp(char_buf_val, "null", 5) == 0) {
|
||||||
@@ -222,7 +225,8 @@ static inline int decode_and_bind_param(
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ERL_STRING_EXT:
|
case ERL_STRING_EXT:
|
||||||
char_buf_val = malloc((*size + 1) * sizeof(char)); // space for null separator
|
// include space for null separator
|
||||||
|
char_buf_val = malloc((*size + 1) * sizeof(char));
|
||||||
ei_decode_string(buffer, index, char_buf_val);
|
ei_decode_string(buffer, index, char_buf_val);
|
||||||
result = sqlite3_bind_text(statement, param_index, char_buf_val, *size, &free);
|
result = sqlite3_bind_text(statement, param_index, char_buf_val, *size, &free);
|
||||||
break;
|
break;
|
||||||
@@ -292,7 +296,6 @@ static int sql_bind_and_exec(sqlite3_drv_t *drv, char *buffer, int buffer_size)
|
|||||||
char param_name[MAXATOMLEN + 1]; // parameter names shouldn't be longer than 256!
|
char param_name[MAXATOMLEN + 1]; // parameter names shouldn't be longer than 256!
|
||||||
while (index < buffer_size) {
|
while (index < buffer_size) {
|
||||||
ei_decode_list_header(buffer, &index, &cur_list_size);
|
ei_decode_list_header(buffer, &index, &cur_list_size);
|
||||||
// note the finish condition; the last element is the tail and we shouldn't decode it!
|
|
||||||
for (i = 0; i < cur_list_size; i++) {
|
for (i = 0; i < cur_list_size; i++) {
|
||||||
ei_get_type(buffer, &index, &type, &size);
|
ei_get_type(buffer, &index, &type, &size);
|
||||||
if (type == ERL_SMALL_TUPLE_EXT) {
|
if (type == ERL_SMALL_TUPLE_EXT) {
|
||||||
@@ -333,9 +336,12 @@ static int sql_bind_and_exec(sqlite3_drv_t *drv, char *buffer, int buffer_size)
|
|||||||
param_index = sqlite3_bind_parameter_index(statement, param_name);
|
param_index = sqlite3_bind_parameter_index(statement, param_name);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return output_error(drv, SQLITE_MISMATCH, "parameter index must be given as integer, atom, or string");
|
return output_error(
|
||||||
|
drv, SQLITE_MISMATCH,
|
||||||
|
"parameter index must be given as integer, atom, or string");
|
||||||
}
|
}
|
||||||
result = decode_and_bind_param(drv, buffer, &index, statement, param_index, &type, &size);
|
result = decode_and_bind_param(
|
||||||
|
drv, buffer, &index, statement, param_index, &type, &size);
|
||||||
if (result != SQLITE_OK) {
|
if (result != SQLITE_OK) {
|
||||||
return result; // error has already been output
|
return result; // error has already been output
|
||||||
}
|
}
|
||||||
@@ -343,11 +349,13 @@ static int sql_bind_and_exec(sqlite3_drv_t *drv, char *buffer, int buffer_size)
|
|||||||
else {
|
else {
|
||||||
IMPLICIT_INDEX:
|
IMPLICIT_INDEX:
|
||||||
if (param_indices_are_explicit) {
|
if (param_indices_are_explicit) {
|
||||||
return output_error(drv, SQLITE_MISUSE,
|
return output_error(
|
||||||
|
drv, SQLITE_MISUSE,
|
||||||
"parameters without indices shouldn't follow indexed or named parameters");
|
"parameters without indices shouldn't follow indexed or named parameters");
|
||||||
}
|
}
|
||||||
|
|
||||||
result = decode_and_bind_param(drv, buffer, &index, statement, param_index, &type, &size);
|
result = decode_and_bind_param(
|
||||||
|
drv, buffer, &index, statement, param_index, &type, &size);
|
||||||
if (result != SQLITE_OK) {
|
if (result != SQLITE_OK) {
|
||||||
return result; // error has already been output
|
return result; // error has already been output
|
||||||
}
|
}
|
||||||
@@ -553,13 +561,13 @@ static void sql_exec_async(void *_async_command) {
|
|||||||
async_command->binaries = binaries;
|
async_command->binaries = binaries;
|
||||||
|
|
||||||
if (next_row == SQLITE_BUSY) {
|
if (next_row == SQLITE_BUSY) {
|
||||||
return_error(drv, SQLITE_BUSY, "SQLite3 database is busy", &async_command->dataset,
|
return_error(drv, SQLITE_BUSY, "SQLite3 database is busy",
|
||||||
&async_command->term_count);
|
&async_command->dataset, &async_command->term_count);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (next_row != SQLITE_DONE) {
|
if (next_row != SQLITE_DONE) {
|
||||||
return_error(drv, next_row, sqlite3_errmsg(drv->db), &async_command->dataset,
|
return_error(drv, next_row, sqlite3_errmsg(drv->db),
|
||||||
&async_command->term_count);
|
&async_command->dataset, &async_command->term_count);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user