MCPcopy Create free account
hub / github.com/ElementsProject/lightning / finish_td

Function finish_td

plugins/sql.c:1607–1678  ·  view source on GitHub ↗

Creates sql statements, initializes table */

Source from the content-addressed store, hash-verified

1605
1606/* Creates sql statements, initializes table */
1607static void finish_td(struct plugin *plugin, struct table_desc *td)
1608{
1609 struct sql *sql = sql_of(plugin);
1610 char *create_stmt, *insert_stmt;
1611 int err;
1612 char *errmsg;
1613 const char *sep = "";
1614
1615 /* subobject are separate at JSON level, folded at db level! */
1616 if (td->is_subobject)
1617 /* But it might have sub-sub objects! */
1618 goto do_subtables;
1619
1620 create_stmt = tal_fmt(tmpctx, "CREATE TABLE %s (", td->name);
1621 insert_stmt = tal_fmt(td, "INSERT INTO %s VALUES (", td->name);
1622 /* If no created_index, create explicit rowid */
1623 if (!td->has_created_index) {
1624 tal_append_fmt(&create_stmt, "rowid INTEGER PRIMARY KEY, ");
1625 tal_append_fmt(&insert_stmt, "?, ");
1626 td->delete_stmt = NULL;
1627 } else {
1628 td->delete_stmt = tal_fmt(td, "DELETE FROM %s WHERE created_index = ?;", td->name);
1629 }
1630
1631 /* If we're a child array, we reference the parent column */
1632 if (td->parent) {
1633 /* But if parent is a subobject, we reference the outer! */
1634 struct table_desc *parent = td->parent;
1635 while (parent->is_subobject)
1636 parent = parent->parent;
1637 tal_append_fmt(&create_stmt,
1638 "row INTEGER REFERENCES %s(%s) ON DELETE CASCADE,"
1639 " arrindex INTEGER",
1640 parent->name, primary_key_name(parent));
1641 tal_append_fmt(&insert_stmt, "?,?");
1642 sep = ",";
1643 }
1644
1645 for (size_t i = 0; i < tal_count(td->columns); i++) {
1646 const struct column *col = td->columns[i];
1647
1648 if (col->sub) {
1649 add_sub_object(&insert_stmt, &create_stmt,
1650 &sep, col->sub);
1651 continue;
1652 }
1653 tal_append_fmt(&insert_stmt, "%s?", sep);
1654 tal_append_fmt(&create_stmt, "%s%s %s",
1655 sep,
1656 col->dbname,
1657 fieldtypemap[col->ftype].sqltype);
1658 /* created_index serves as primary key if it exists */
1659 if (streq(col->dbname, "created_index"))
1660 tal_append_fmt(&create_stmt, " INTEGER PRIMARY KEY");
1661 sep = ",";
1662 }
1663 tal_append_fmt(&create_stmt, ");");
1664 tal_append_fmt(&insert_stmt, ");");

Callers 1

init_tablemapFunction · 0.85

Calls 5

sql_ofFunction · 0.85
tal_append_fmtFunction · 0.85
primary_key_nameFunction · 0.85
add_sub_objectFunction · 0.85
plugin_errFunction · 0.70

Tested by

no test coverage detected