MCPcopy Create free account
hub / github.com/bloomberg/comdb2 / db_csvcopy

Function db_csvcopy

lua/sp.c:3566–3726  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3564}
3565
3566int db_csvcopy(Lua lua)
3567{
3568 luaL_checkudata(lua, 1, dbtypes.db);
3569 lua_remove(lua, 1);
3570
3571 SP sp = getsp(lua);
3572
3573 int rc = 0;
3574 const char *fname_orig = lua_tostring(lua, 1);
3575 const char *tablename = NULL;
3576 const char *cSeparator = ",";
3577 const char *header = NULL;
3578 FILE *fp = NULL;
3579 char *line = NULL;
3580 size_t len = 0;
3581 ssize_t read;
3582 strbuf *columns, *params, *sql;
3583
3584 char path[PATH_MAX + 1];
3585 char *fname = comdb2_realpath(fname_orig, path);
3586
3587 int basedir_len = strlen(thedb->basedir);
3588
3589 if (fname == NULL) {
3590 return luaL_error(lua, strerror(errno));
3591 } else if ((strncmp(thedb->basedir, fname, basedir_len) != 0) || (fname[basedir_len] != '/')) {
3592 return luaL_error(lua, "File not in database directory");
3593 }
3594
3595 if (lua_gettop(lua) >= 2 && !lua_isnil(lua, 2) && !luabb_isnull(lua, 2)) {
3596 tablename = luabb_tostring(lua, 2);
3597 } else {
3598 return luaL_error(lua, "Table name not specified");
3599 }
3600
3601 if (lua_gettop(lua) >= 3 && !lua_isnil(lua, 3) && !luabb_isnull(lua, 3)) {
3602 cSeparator = luabb_tostring(lua, 3);
3603 }
3604
3605 if (lua_gettop(lua) >= 4 && !lua_isnil(lua, 4) && !luabb_isnull(lua, 4)) {
3606 header = luabb_tostring(lua, 4);
3607 }
3608
3609 fp = fopen(fname, "r");
3610
3611 if (fp == NULL) {
3612 return luaL_error(lua, strerror(errno));
3613 }
3614
3615 columns = strbuf_new();
3616 params = strbuf_new();
3617
3618 strbuf_append(columns, "(");
3619 strbuf_append(params, "(");
3620
3621 if (header == NULL) {
3622 read = getline(&line, &len, fp);
3623 header = line;

Callers

nothing calls this directly

Calls 15

luaL_checkudataFunction · 0.85
lua_removeFunction · 0.85
comdb2_realpathFunction · 0.85
luaL_errorFunction · 0.85
lua_gettopFunction · 0.85
luabb_isnullFunction · 0.85
luabb_tostringFunction · 0.85
csv_append_charFunction · 0.85
sqlite3_mprintfFunction · 0.85
sqlite3_freeFunction · 0.85
freeFunction · 0.85
query_tbl_nameFunction · 0.85

Tested by

no test coverage detected