Added auto-increment ID column

This commit is contained in:
2020-10-06 13:23:31 +01:00
parent 33e70adf88
commit cf7ee1cf73
5 changed files with 34 additions and 8 deletions

View File

@@ -28,3 +28,5 @@ TYPE_STRING <- bitwShiftL(1, 11)
TYPE_BLOB <- bitwShiftL(1, 12)
#' @export
TYPE_TIMESTAMP <- bitwShiftL(1, 13)
#' @export
TYPE_ID <- bitwShiftL(1, 14)

View File

@@ -43,6 +43,9 @@ size_t columnTypeToByteSize(e_column_type type)
case TYPE_TIMESTAMP:
return sizeof(uint32_t);
case TYPE_ID:
return sizeof(uint32_t);
case TYPE_RAW:
return sizeof(char *);
}
@@ -155,6 +158,7 @@ struct column_data_t *initEmptyColumn(e_column_type type, int nullable, const ch
col->isNullable = nullable > 0;
col->isBlob = type == TYPE_BLOB;
col->isTimestamp = type == TYPE_TIMESTAMP;
col->isAutoIncrement = type == TYPE_ID;
return col;
}
@@ -182,6 +186,8 @@ struct column_data_t *columnFromResult(struct stored_conn_t *sconn, MYSQL_RES *r
field->table,
field->table_length
);
col->isAutoIncrement = (field->flags & AUTO_INCREMENT_FLAG) != 0;
col->isTimestamp = (field->flags & TIMESTAMP_FLAG) != 0;
if (col == 0 || num_rows == 0) {
return col;
@@ -344,6 +350,13 @@ int setColumnValue(struct column_data_t *col, uint64_t row, const char *value, s
*(col->data.ptr_uint32 + row) = (uint32_t)strtoul(value, NULL, 10);
break;
}
case TYPE_ID:
{
*(col->data.ptr_uint32 + row) = (uint32_t)strtoul(value, NULL, 10);
break;
}
case TYPE_STRING:
case TYPE_BLOB:
case TYPE_RAW:

View File

@@ -22,7 +22,8 @@ enum e_column_type_t {
TYPE_DOUBLE = 1 << 10,
TYPE_STRING = 1 << 11,
TYPE_BLOB = 1 << 12,
TYPE_TIMESTAMP = 1 << 13
TYPE_TIMESTAMP = 1 << 13,
TYPE_ID = 1 << 14
};
typedef enum e_column_type_t e_column_type;
@@ -38,11 +39,12 @@ struct column_data_t {
size_t n_values;
uint8_t hasPointers :1;
uint8_t isUnsigned :1;
uint8_t isNullable :1;
uint8_t isBlob :1;
uint8_t isTimestamp :1;
uint8_t hasPointers :1;
uint8_t isUnsigned :1;
uint8_t isNullable :1;
uint8_t isBlob :1;
uint8_t isTimestamp :1;
uint8_t isAutoIncrement :1;
union {
void *vptr;

View File

@@ -139,6 +139,7 @@ void db_value_sbva(str_builder *sb, e_column_type type, uint32_t n_args, va_list
nchar = vsprintf(buf, "%"PRIi32, args);
break;
}
case TYPE_ID:
case TYPE_TIMESTAMP:
case TYPE_UINT32:
{
@@ -170,7 +171,7 @@ void db_value_sbva(str_builder *sb, e_column_type type, uint32_t n_args, va_list
default:
{
break;
return;
}
}
strbld_str(sb, buf, (size_t)nchar);

View File

@@ -268,6 +268,10 @@ char *createColString(struct column_data_t *col)
break;
case TYPE_TIMESTAMP:
strbld_str(sb, "TIMESTAMP", 8);
break;
case TYPE_ID:
strbld_str(sb, "INT", 3);
break;
}
if (col->isUnsigned) {
@@ -280,6 +284,10 @@ char *createColString(struct column_data_t *col)
strbld_str(sb, " NOT NULL", 0);
}
if (col->isAutoIncrement) {
strbld_str(sb, " AUTO_INCREMENT", 0);
}
if (strbld_finalize_or_destroy(&sb, &colstr, &colstr_len) != 0) {
return 0;
}