G
Guest
Hi,
I have a structre of a database record,
and I want to write a function that makes a
statement for inserting it into a database.
I have written make_statement_string() function
for this, and I have posted my code here.
But I'm not sure if this code is very good or
efficient. What are your opinion? I try to code
using the KISS principle, but its kinda hard
when you use strings, since you have to allocate
memory for them of the right size.
#include <stdio.h>
#include <stdlib.h>
struct fdesc {
char *name;
/* other irrelevant stuff ... */
};
struct record {
size_t count;
struct fdesc *desc;
/* other record data
* ... */
};
/* Returns a comma-separated string of num question marks */
char *question_marks(size_t num)
{
char *buf, *ptr;
size_t i;
if (num == 0)
return NULL;
buf = malloc((num - 1) * 3 + 2);
ptr = buf;
for (i = 0; i < num - 1; i++) {
strcpy(ptr, "?, ");
ptr += 3;
}
*ptr++ = '?';
*ptr = '\0';
return buf;
}
/* Make a comma-separated list of the names in the record r */
char *get_names(struct record r)
{
size_t i, sz;
size_t index;
char *buf, *ptr;
/* calcuate how much memory we need for the string.
* The number 2 is for a comma and a space */
sz = 0;
for (i = 0; i < r.count; i++)
sz += strlen(r.desc.name) + 2;
/* no comma or space after the last name, but
* keep one for the '\0' */
sz--;
buf = malloc(sz);
index = 0;
for (i = 0; i < r.count; i++) {
if (i != 0) {
strcpy(&buf[index], ", ");
index += 2;
}
ptr = r.desc.name;
while (*ptr != '\0')
buf[index++] = *ptr++;
}
buf[index] = '\0';
return buf;
}
char *make_statement_string(struct record r)
{
char *names, *values;
char *statement;
names = get_names(r);
values = question_marks(r.count);
statement = malloc(30 + strlen(names) + strlen(values));
sprintf(statement, "INSERT INTO table (%s) VALUES(%s)", names, values);
free(names);
free(values);
return statement;
}
I have a structre of a database record,
and I want to write a function that makes a
statement for inserting it into a database.
I have written make_statement_string() function
for this, and I have posted my code here.
But I'm not sure if this code is very good or
efficient. What are your opinion? I try to code
using the KISS principle, but its kinda hard
when you use strings, since you have to allocate
memory for them of the right size.
#include <stdio.h>
#include <stdlib.h>
struct fdesc {
char *name;
/* other irrelevant stuff ... */
};
struct record {
size_t count;
struct fdesc *desc;
/* other record data
* ... */
};
/* Returns a comma-separated string of num question marks */
char *question_marks(size_t num)
{
char *buf, *ptr;
size_t i;
if (num == 0)
return NULL;
buf = malloc((num - 1) * 3 + 2);
ptr = buf;
for (i = 0; i < num - 1; i++) {
strcpy(ptr, "?, ");
ptr += 3;
}
*ptr++ = '?';
*ptr = '\0';
return buf;
}
/* Make a comma-separated list of the names in the record r */
char *get_names(struct record r)
{
size_t i, sz;
size_t index;
char *buf, *ptr;
/* calcuate how much memory we need for the string.
* The number 2 is for a comma and a space */
sz = 0;
for (i = 0; i < r.count; i++)
sz += strlen(r.desc.name) + 2;
/* no comma or space after the last name, but
* keep one for the '\0' */
sz--;
buf = malloc(sz);
index = 0;
for (i = 0; i < r.count; i++) {
if (i != 0) {
strcpy(&buf[index], ", ");
index += 2;
}
ptr = r.desc.name;
while (*ptr != '\0')
buf[index++] = *ptr++;
}
buf[index] = '\0';
return buf;
}
char *make_statement_string(struct record r)
{
char *names, *values;
char *statement;
names = get_names(r);
values = question_marks(r.count);
statement = malloc(30 + strlen(names) + strlen(values));
sprintf(statement, "INSERT INTO table (%s) VALUES(%s)", names, values);
free(names);
free(values);
return statement;
}