Skip to content

Commit fed1907

Browse files
committed
Add update function
1 parent 36fb9d4 commit fed1907

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/ulog_sqlite.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,28 @@ uint32_t derive_col_type_or_len(int type, const void *val, int len) {
255255
return col_type_or_len;
256256
}
257257

258+
// Returns one of the four types based on column type or len
259+
// found in header
260+
// See https://www.sqlite.org/fileformat.html#record_format
261+
uint32_t derive_col_type(int hdr_col_type_or_len) {
262+
switch (hdr_col_type_or_len) {
263+
case 7:
264+
return DBLOG_TYPE_REAL;
265+
case 1:
266+
case 2:
267+
case 3:
268+
case 4:
269+
case 5:
270+
case 6:
271+
case 8:
272+
case 9:
273+
return DBLOG_TYPE_INT;
274+
default:
275+
return (hdr_col_type_or_len % 2 ? DBLOG_TYPE_TEXT : DBLOG_TYPE_BLOB);
276+
}
277+
return DBLOG_TYPE_TEXT; // error
278+
}
279+
258280
byte c1, c2, c3;
259281
void saveChecksumBytes(byte * ptr, uint16_t last_pos) {
260282
ptr += last_pos;
@@ -1472,3 +1494,27 @@ int dblog_bin_srch_row_by_val(struct dblog_read_context *rctx, int col_idx,
14721494
rctx->cur_rec_pos = size;
14731495
return DBLOG_RES_OK;
14741496
}
1497+
1498+
// See .h file for API description
1499+
int dblog_upd_col_val(struct dblog_read_context *rctx, int col_idx, const void *val) {
1500+
uint8_t *buf = rctx->buf;
1501+
if (buf[0] != 13)
1502+
return DBLOG_RES_ERR;
1503+
int16_t rec_count = read_uint16(rctx->buf + 3);
1504+
if (rec_count <= rctx->cur_rec_pos)
1505+
return DBLOG_RES_ERR;
1506+
uint32_t u32_at;
1507+
byte *val_at = read_val_at(rctx, rctx->cur_rec_pos, col_idx, &u32_at, 0);
1508+
if (!val_at)
1509+
return DBLOG_RES_NOT_FOUND;
1510+
int len = dblog_derive_data_len(u32_at);
1511+
write_data(val_at, derive_col_type(u32_at), val, len);
1512+
return DBLOG_RES_OK;
1513+
}
1514+
1515+
int dblog_write_cur_page(struct dblog_read_context *rctx, write_fn_def write_fn) {
1516+
struct dblog_write_context wctx;
1517+
wctx.buf = rctx->buf;
1518+
wctx.write_fn = write_fn;
1519+
return write_page(&wctx, rctx->cur_page, get_pagesize(rctx->page_size_exp));
1520+
}

src/ulog_sqlite.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ struct dblog_write_context {
7777
int err_no;
7878
};
7979

80+
typedef int32_t (*write_fn_def)(struct dblog_write_context *ctx, void *buf, uint32_t pos, size_t len);
81+
8082
// Initializes database - writes first page
8183
// and makes it ready for writing data
8284
int dblog_write_init(struct dblog_write_context *wctx);
@@ -202,6 +204,15 @@ int dblog_srch_row_by_id(struct dblog_read_context *rctx, uint32_t rowid);
202204
int dblog_bin_srch_row_by_val(struct dblog_read_context *rctx, int col_idx,
203205
int val_type, void *val, uint16_t len, byte is_rowid);
204206

207+
// Updates value of column at current position
208+
// For text and blob columns, pass the type to dblog_derive_data_len()
209+
// to get the actual length
210+
int dblog_upd_col_val(struct dblog_read_context *rctx, int col_idx, const void *val);
211+
212+
// Writes the current page to disk
213+
// Typically called after updating values using dblog_upd_col_val()
214+
int dblog_write_cur_page(struct dblog_read_context *rctx, write_fn_def write_fn);
215+
205216
#ifdef __cplusplus
206217
}
207218
#endif

0 commit comments

Comments
 (0)