in sources/cg_test_helpers.c [440:539]
static void cg_dummy_test_populate (charbuf *gen_insert_tables, ast_node *table_ast, int32_t *dummy_value_seed) {
Contract(is_ast_create_table_stmt(table_ast));
sem_struct *sptr = table_ast->sem->sptr;
CSTR table_name = sptr->struct_name;
bool_t add_row;
int32_t row_index = -1;
symtab_entry *table_entry = symtab_find(dummy_test_infos, table_name);
do {
row_index++;
add_row = 0;
CHARBUF_OPEN(names);
CHARBUF_OPEN(values);
CSTR comma = "";
symtab *col_syms = symtab_new();
// extract column values for insert statement from dummy_test info and emit
// the insert statement
if (table_entry) {
symtab *table = (symtab *)table_entry->val;
for (int32_t j = 0; j < table->capacity; j++) {
symtab_entry column_entry = table->payload[j];
if (column_entry.sym) {
CSTR column_name = column_entry.sym;
bytebuf *column_values_entry = (bytebuf *)column_entry.val;
ast_node **column_values = (ast_node **)column_values_entry->ptr;
int32_t size = column_values_entry->used/sizeof(void **);
if (row_index < size) {
CHARBUF_OPEN(str_val);
cg_dummy_test_column_value(&str_val, column_values[row_index]);
bprintf(&values, "%s%s", comma, str_val.ptr);
bprintf(&names, "%s%s", comma, column_name);
comma = ", ";
symtab_add(col_syms, column_name, NULL);
add_row = 1;
CHARBUF_CLOSE(str_val);
}
}
}
}
// we make sure that we add at least DUMMY_TEST_INSERT_ROWS rows per table
if (row_index < DUMMY_TEST_INSERT_ROWS) {
add_row = 1;
}
if (add_row) {
// provide specific values for primary and foreign column to avoid foreign key violation.
for (int32_t i = 0; i < sptr->count; i++) {
sem_t col_type = sptr->semtypes[i];
CSTR column_name = sptr->names[i];
// We find primary and foreign key column that are missing values in the
// insert statement and add those values to avoid sql foreign key violation eror.
if (!symtab_find(col_syms, column_name)) {
if (is_referenceable_by_foreign_key(table_ast, column_name) || is_foreign_key(col_type)) {
CHARBUF_OPEN(str_val);
// we do +1 because index value start at zero and we don't want to insert zero as primary key
int32_t index_value = row_index + 1;
if (is_foreign_key(col_type)) {
cg_parent_column_value(&str_val, table_name, column_name, row_index);
if (str_val.used <= 1) {
// The parent table does not have explicit dummy info on this column.
// In this case the parent table key referenced here was created with default value
// between 1 and DUMMY_TEST_INSERT_ROWS. We just need to select one of these default
// value.
cg_dummy_test_emit_integer_value(&str_val, col_type, cg_validate_value_range(index_value));
}
}
bprintf(&names, "%s%s", comma, column_name);
bprintf(&values, "%s", comma);
comma = ", ";
if (str_val.used > 1) {
bprintf(&values, "%s", str_val.ptr);
} else {
cg_dummy_test_emit_integer_value(&values, col_type, index_value);
}
CHARBUF_CLOSE(str_val);
}
}
}
bprintf(gen_insert_tables,
"INSERT OR IGNORE INTO %s(%s) VALUES(%s) @dummy_seed(%d)%s;\n",
table_name,
names.ptr,
values.ptr,
(*dummy_value_seed)++,
row_index % 2 == 0 ? "" : " @dummy_nullables @dummy_defaults");
}
CHARBUF_CLOSE(values);
CHARBUF_CLOSE(names);
symtab_delete(col_syms);
} while (add_row);
}