DataWorks
Database Library/Client
Loading...
Searching...
No Matches
database_exec.c
1/* $Id: database_exec.c 98 2024-05-23 23:51:27Z nishi $ */
2/* --- START LICENSE --- */
3/* -------------------------------------------------------------------------- */
4/* Copyright (c) 2024 Crabware. */
5/* Redistribution and use in source and binary forms, with or without modific */
6/* ation, are permitted provided that the following conditions are met: */
7/* 1. Redistributions of source code must retain the above copyright noti */
8/* ce, this list of conditions and the following disclaimer. */
9/* 2. Redistributions in binary form must reproduce the above copyright n */
10/* otice, this list of conditions and the following disclaimer in the documen */
11/* tation and/or other materials provided with the distribution. */
12/* 3. Neither the name of the copyright holder nor the names of its contr */
13/* ibutors may be used to endorse or promote products derived from this softw */
14/* are without specific prior written permission. */
15/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS */
16/* " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, TH */
17/* E IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO */
18/* SE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS */
19/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CON */
20/* SEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITU */
21/* TE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPT */
22/* ION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S */
23/* TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN AN */
24/* Y WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY */
25/* OF SUCH DAMAGE. */
26/* -------------------------------------------------------------------------- */
27/* --- END LICENSE --- */
28
29#include "dw_database.h"
30
31#include "dw_parser.h"
32#include "dw_util.h"
33
34#include <stdbool.h>
35#include <stdlib.h>
36
37struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_db* db, struct __dw_token* token) {
38 struct dataworks_db_result* r = malloc(sizeof(*r));
39 r->value = NULL;
40 r->error = false;
41 if(token->type == __DW_METHOD) {
42 int i;
43 struct dataworks_db_result** results = malloc(sizeof(*results));
44 results[0] = NULL;
45 int argc = 0;
46 if(token->token != NULL) {
47 for(i = 0; token->token[i] != NULL; i++) {
48 argc++;
49 if(token->token[i]->type == __DW_METHOD) {
50 struct dataworks_db_result* r2 = __dataworks_database_execute_code(db, token->token[i]);
51 if(r2->error) {
52 int j;
53 for(j = 0; results[j] != NULL; j++) {
55 }
56 free(results);
58 r = r2;
59 break;
60 }
61 int j;
62 for(j = 0; results[j] != NULL; j++)
63 ;
64 struct dataworks_db_result** newresults = malloc(sizeof(*newresults) * (j + 2));
65 for(j = 0; results[j] != NULL; j++) {
66 newresults[j] = results[j];
67 }
68 free(results);
69 results = newresults;
70 results[j] = r2;
71 results[j + 1] = NULL;
72 } else {
73 struct dataworks_db_result* r2 = malloc(sizeof(*r2));
74 r2->error = false;
75 r2->value = __dw_strdup(token->token[i]->name);
76 int j;
77 for(j = 0; results[j] != NULL; j++)
78 ;
79 struct dataworks_db_result** newresults = malloc(sizeof(*newresults) * (j + 2));
80 for(j = 0; results[j] != NULL; j++) {
81 newresults[j] = results[j];
82 }
83 free(results);
84 results = newresults;
85 results[j] = r2;
86 results[j + 1] = NULL;
87 }
88 }
89 }
90 if(r->error) return r;
91 if(__dw_strcaseequ(token->name, "create_table")) {
92 argc = 0;
93 int first = -1;
94 int j;
95 for(j = 0; results[j] != NULL; j++) {
96 if(results[j]->value != NULL) {
97 argc++;
98 if(first == -1) first = j;
99 }
100 }
101 if(argc < 2) {
102 r->error = true;
104 } else {
105 int j;
106 char* fieldtypes = malloc(argc);
107 fieldtypes[argc - 1] = 0;
108 char** fields = malloc(sizeof(*fields) * (argc));
109 fields[argc - 1] = NULL;
110 argc = 0;
111 for(j = first + 1; results[j] != NULL; j++) {
112 if(results[j]->value != NULL) {
113 char* val = __dw_strdup(results[j]->value);
114 int k;
115 for(k = 0; val[k] != 0; k++) {
116 if(val[k] == ':') {
117 val[k] = 0;
118 if(__dw_strcaseequ(val, "string")) {
119 fieldtypes[argc] = 'S';
120 } else if(__dw_strcaseequ(val, "integer")) {
121 fieldtypes[argc] = 'I';
122 } else if(__dw_strcaseequ(val, "double")) {
123 fieldtypes[argc] = 'D';
124 } else if(__dw_strcaseequ(val, "logical")) {
125 fieldtypes[argc] = 'L';
126 } else if(__dw_strcaseequ(val, "help")) {
127 fieldtypes[argc] = '?';
128 }
129 fields[argc] = __dw_strdup(val + k + 1);
130 argc++;
131 break;
132 }
133 }
134 free(val);
135 }
136 }
137 int er;
138 if((er = dataworks_database_create_table(db, results[first]->value, fields, fieldtypes)) != DW_ERR_SUCCESS) {
139 r->error = true;
140 r->errnum = er;
141 }
142 for(j = 0; fields[j] != NULL; j++) free(fields[j]);
143 free(fields);
144 free(fieldtypes);
145 }
146 } else if(__dw_strcaseequ(token->name, "concat")) {
147 r->value = malloc(1);
148 r->value[0] = 0;
149 int j;
150 for(j = 0; results[j] != NULL; j++) {
151 if(results[j]->value != NULL) {
152 char* tmp = r->value;
153 r->value = __dw_strcat(tmp, results[j]->value);
154 free(tmp);
155 }
156 }
157 } else if(__dw_strcaseequ(token->name, "print")) {
158 int j;
159 for(j = 0; results[j] != NULL; j++) {
160 if(results[j]->value != NULL) {
161 printf("%s\n", results[j]->value);
162 }
163 }
164 } else {
165 r->error = true;
167 }
168 int j;
169 for(j = 0; results[j] != NULL; j++) {
171 }
172 free(results);
173 } else {
174 r->error = true;
176 }
177 return r;
178}
179
181 struct dataworks_db_result* r = malloc(sizeof(*r));
182 r->error = false;
183 r->value = NULL;
184 struct __dw_token* token = __dw_parser_parse(code, true);
185 if(token != NULL) {
186 if(token->error) {
187 r->error = true;
188 r->errnum = token->errnum;
189 } else {
190 dataworks_database_free_result(r);
191 r = __dataworks_database_execute_code(db, token);
192 }
193 __dw_parser_free(token);
194 } else {
195 r->error = true;
197 }
198 return r;
199}
200
201void dataworks_database_free_result(struct dataworks_db_result* result) {
202 if(result->value != NULL) free(result->value);
203 free(result);
204}
DataWorks database.
struct dataworks_db_result * dataworks_database_execute_code(struct dataworks_db *db, const char *code)
Executes the code.
int dataworks_database_create_table(struct dataworks_db *db, const char *name, char **fields, const char *fieldtypes)
Creates a table.
void dataworks_database_free_result(struct dataworks_db_result *result)
Frees the result.
@ DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS
Insufficient arguments.
@ DW_ERR_EXEC_UNKNOWN_METHOD
Unknown method.
@ DW_ERR_SUCCESS
Success.
Definition dw_database.h:67
@ DW_ERR_EXEC_NON_METHOD
Cannot call non-method.
@ DW_ERR_PARSER_NULL
Parser returned NULL.
DataWorks parser.
DataWorks utils.
Database result struct.
char * value
Value.
bool error
True if this is an error.
int errnum
Error number.
Database struct.