-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquickstart.c
More file actions
128 lines (110 loc) · 3.99 KB
/
quickstart.c
File metadata and controls
128 lines (110 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
** Doltlite Quickstart — based on https://sqlite.org/quickstart.html
**
** Demonstrates Dolt version control features through the SQLite C API:
** - Commits, branches, and merges
** - Point-in-time queries (AS OF)
** - Diff and log
**
** Build (from the build/ directory):
** gcc -o quickstart ../examples/quickstart.c \
** -I. -L. -ldoltlite -lpthread -lz
**
** Run:
** ./quickstart
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
/* Print a result set with column headers */
static int callback(void *label, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
printf(" %-15s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
/* Execute SQL, print errors */
static void exec(sqlite3 *db, const char *sql){
char *zErrMsg = 0;
int rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n Statement: %s\n", zErrMsg, sql);
sqlite3_free(zErrMsg);
}
}
int main(int argc, char **argv){
sqlite3 *db;
int rc;
const char *dbpath = argc > 1 ? argv[1] : "quickstart.db";
/* Start fresh — remove DB, WAL, and journal files */
remove(dbpath);
{ char walpath[256]; snprintf(walpath,sizeof(walpath),"%s-wal",dbpath); remove(walpath); }
{ char jrnpath[256]; snprintf(jrnpath,sizeof(jrnpath),"%s-journal",dbpath); remove(jrnpath); }
rc = sqlite3_open(dbpath, &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
/* ---- 1. Create schema and make the first commit ---- */
printf("== Create schema and first commit ==\n\n");
exec(db,
"CREATE TABLE employees("
" id INTEGER PRIMARY KEY,"
" first_name TEXT,"
" last_name TEXT"
");"
"INSERT INTO employees VALUES(0, 'Tim', 'Sehn');"
"INSERT INTO employees VALUES(1, 'Brian', 'Hendriks');"
"INSERT INTO employees VALUES(2, 'Aaron', 'Son');"
);
exec(db, "SELECT dolt_commit('-A', '-m', 'Initial schema and data')");
printf("Log after first commit:\n");
exec(db, "SELECT commit_hash, message FROM dolt_log");
/* ---- 2. Create a branch and make changes ---- */
printf("== Branch and modify ==\n\n");
exec(db, "SELECT dolt_branch('feature')");
exec(db, "SELECT dolt_checkout('feature')");
exec(db,
"UPDATE employees SET first_name='Timothy' WHERE id=0;"
"INSERT INTO employees VALUES(3, 'Daylon', 'Wilkins');"
);
exec(db, "SELECT dolt_commit('-A', '-m', 'Rename Tim, add Daylon')");
/* ---- 3. Point-in-time query: see main without switching ---- */
printf("== Point-in-time query (main vs feature) ==\n\n");
printf("Employees on feature (current branch):\n");
exec(db, "SELECT id, first_name, last_name FROM employees ORDER BY id");
printf("Employees on main (without switching):\n");
exec(db,
"SELECT * FROM dolt_at_employees('main') ORDER BY id"
);
/* ---- 4. Switch back to main and merge ---- */
printf("== Merge feature into main ==\n\n");
exec(db, "SELECT dolt_checkout('main')");
exec(db, "SELECT dolt_merge('feature')");
printf("Employees on main after merge:\n");
exec(db, "SELECT id, first_name, last_name FROM employees ORDER BY id");
/* ---- 5. View the diff (audit log) ---- */
printf("== Audit log: all changes to employees ==\n\n");
exec(db,
"SELECT diff_type, to_id, to_first_name, to_last_name"
" FROM dolt_diff_employees"
);
/* ---- 6. View commit log ---- */
printf("== Full commit log ==\n\n");
exec(db, "SELECT commit_hash, message FROM dolt_log");
/* ---- 7. Tag the release ---- */
printf("== Tag current state ==\n\n");
exec(db, "SELECT dolt_tag('v1.0')");
exec(db, "SELECT tag_name, tag_hash FROM dolt_tags");
/* ---- Cleanup ---- */
sqlite3_close(db);
remove(dbpath);
{ char walpath[256]; snprintf(walpath,sizeof(walpath),"%s-wal",dbpath); remove(walpath); }
{ char jrnpath[256]; snprintf(jrnpath,sizeof(jrnpath),"%s-journal",dbpath); remove(jrnpath); }
printf("Done.\n");
return 0;
}