| 2504 | } |
| 2505 | |
| 2506 | static int dbtable_copyfrom(Lua lua) |
| 2507 | { |
| 2508 | SP sp = getsp(lua); |
| 2509 | |
| 2510 | dbtable_t *tbl1, *tbl2; |
| 2511 | const char *where_clause = NULL; |
| 2512 | |
| 2513 | int nargs = lua_gettop(lua); |
| 2514 | if (nargs > 3) { |
| 2515 | return luabb_error(lua, sp, "expected 2 arguments found %d %s", nargs, |
| 2516 | __func__); |
| 2517 | } |
| 2518 | |
| 2519 | luaL_checkudata(lua, 1, dbtypes.dbtable); |
| 2520 | tbl1 = (dbtable_t *)lua_topointer(lua, 1); |
| 2521 | |
| 2522 | luaL_checkudata(lua, 2, dbtypes.dbtable); |
| 2523 | tbl2 = (dbtable_t *)lua_topointer(lua, 2); |
| 2524 | |
| 2525 | if (lua_isstring(lua, 3)) where_clause = (const char *)lua_tostring(lua, 3); |
| 2526 | |
| 2527 | char t1n1[MAXTABLELEN], t1sep[2], t1n2[MAXTABLELEN]; |
| 2528 | query_tbl_name(tbl1->table_name, t1n1, t1sep, t1n2); |
| 2529 | |
| 2530 | char t2n1[MAXTABLELEN], t2sep[2], t2n2[MAXTABLELEN]; |
| 2531 | query_tbl_name(tbl2->table_name, t2n1, t2sep, t2n2); |
| 2532 | |
| 2533 | strbuf *sql = strbuf_new(); |
| 2534 | if (where_clause) |
| 2535 | strbuf_appendf( |
| 2536 | sql, "insert into %s%s\"%s\" select * from %s%s\"%s\" where %s", |
| 2537 | t1n1, t1sep, t1n2, t2n1, t2sep, t2n2, where_clause); |
| 2538 | else |
| 2539 | strbuf_appendf(sql, "insert into %s%s\"%s\" select * from %s%s\"%s\"", |
| 2540 | t1n1, t1sep, t1n2, t2n1, t2sep, t2n2); |
| 2541 | |
| 2542 | db_reset(lua); |
| 2543 | sqlite3_stmt *stmt = NULL; |
| 2544 | int rc = lua_prepare_sql(sp, strbuf_buf(sql), &stmt); |
| 2545 | strbuf_free(sql); |
| 2546 | if (rc != 0) { |
| 2547 | return rc; |
| 2548 | } |
| 2549 | |
| 2550 | lua_begin_step(sp->clnt, sp, stmt); |
| 2551 | while ((rc = sqlite3_maybe_step(sp->clnt, stmt)) == SQLITE_ROW) { |
| 2552 | lua_another_step(sp->clnt, stmt, rc); |
| 2553 | } |
| 2554 | lua_end_step(sp->clnt, sp, stmt); |
| 2555 | |
| 2556 | sqlite3_finalize(stmt); |
| 2557 | |
| 2558 | lua_pushinteger(lua, 0); /* Success return code. */ |
| 2559 | return 1; |
| 2560 | } |
| 2561 | |
| 2562 | static int dbtable_name(Lua L) |
| 2563 | { |
nothing calls this directly
no test coverage detected