line[i] = 0;
memcpy(line, linebuf, i);
- struct dataworks_db_result* r = dataworks_database_execute_code(db, line);
+ struct dataworks_db_result* r = dataworks_database_execute_code(db, line, true);
if(r->error) {
- printf("%s\n", dataworks_database_strerror(r->errnum));
+ printf("%s.\n", dataworks_database_strerror(r->errnum));
}
free(line);
const char sig[3] = {0x7f, 'D', 'W'};
+const char* dw_errors[] = {"Success", "Used already", "File open fail", "Invalid signature", "Invalid version", "Parser returned NULL", "Cannot call non-method", "Unknown method", "Insufficient arguments", "Too many arguments"};
+
#ifdef M_I86
#define BUFSIZE 128
#endif
struct dataworks_db* db = malloc(sizeof(*db));
db->error = false;
db->fp = NULL;
+ db->name = NULL;
FILE* fp = fopen(fname, "rb+");
if(fp == NULL) {
db->error = true;
int dataworks_database_get_error_number(struct dataworks_db* db) { return db->errnum; }
-const char* dw_errors[] = {"Success", "Used already", "File open fail", "Invalid signature", "Invalid version", "Parser returned NULL", "Cannot call non-method", "Unknown method", "Insufficient arguments"};
-
const char* dataworks_database_strerror(int n) { return dw_errors[n]; }
void dataworks_database_update_mtime(struct dataworks_db* db) {
__dw_big_endian(t, int64_t, fwrite(__converted_ptr, 1, 8, db->fp));
__dw_unlockfile(db->fp);
}
+
+void dataworks_database_free(struct dataworks_db* db) {
+ if(db->fp != NULL) {
+ fclose(db->fp);
+ }
+ if(db->name != NULL) {
+ free(db->name);
+ }
+ free(db);
+}
#include <stdbool.h>
#include <stdlib.h>
+#include <string.h>
-struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_db* db, struct __dw_token* token) {
+struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_db* db, struct __dw_token* token, bool dolog) {
struct dataworks_db_result* r = malloc(sizeof(*r));
r->value = NULL;
r->error = false;
for(i = 0; token->token[i] != NULL; i++) {
argc++;
if(token->token[i]->type == __DW_METHOD) {
- struct dataworks_db_result* r2 = __dataworks_database_execute_code(db, token->token[i]);
+ struct dataworks_db_result* r2 = __dataworks_database_execute_code(db, token->token[i], dolog);
if(r2->error) {
int j;
for(j = 0; results[j] != NULL; j++) {
printf("%s\n", results[j]->value);
}
}
+ } else if(__dw_strcaseequ(token->name, "use")) {
+ int j;
+ bool set = false;
+ for(j = 0; results[j] != NULL; j++) {
+ if(results[j]->value != NULL) {
+ if(set) {
+ r->error = true;
+ r->errnum = DW_ERR_EXEC_TOO_MANY_ARGUMENTS;
+ break;
+ }
+ if(db->name != NULL) free(db->name);
+ bool has = false;
+ char** names = dataworks_database_get_table_list(db);
+ if(names != NULL) {
+ int k;
+ for(k = 0; names[k] != NULL; k++) {
+ if(strcmp(names[k], results[j]->value) == 0) {
+ has = true;
+ }
+ free(names[k]);
+ }
+ free(names);
+ if(has) {
+ db->name = __dw_strdup(results[j]->value);
+ set = true;
+ if(dolog) {
+ printf("Using table `%s'.\n", db->name);
+ }
+ }
+ } else {
+ r->error = true;
+ r->errnum = DW_ERR_PARSER_NULL;
+ }
+ }
+ }
} else {
r->error = true;
r->errnum = DW_ERR_EXEC_UNKNOWN_METHOD;
return r;
}
-struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code) {
+struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code, bool dolog) {
struct dataworks_db_result* r = malloc(sizeof(*r));
r->error = false;
r->value = NULL;
- struct __dw_token* token = __dw_parser_parse(code, true);
+ struct __dw_token* token = __dw_parser_parse(code, dolog);
if(token != NULL) {
if(token->error) {
r->error = true;
r->errnum = token->errnum;
} else {
dataworks_database_free_result(r);
- r = __dataworks_database_execute_code(db, token);
+ r = __dataworks_database_execute_code(db, token, dolog);
}
__dw_parser_free(token);
} else {
*
*/
DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS,
+
+ /**
+ * @~english
+ * @brief Too many arguments
+ *
+ */
+ DW_ERR_EXEC_TOO_MANY_ARGUMENTS,
};
/**
*
*/
int errnum;
+
+ /**
+ * @~english
+ * @brief Database being used.
+ *
+ */
+ char* name;
};
/**
* @brief Executes the code.
* @param db Database
* @param code Code
+ * @param dolog Output the log if true
* @return Result
*
*/
-struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code);
+struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code, bool dolog);
/**
* @~english