| 93 | } |
| 94 | |
| 95 | MetadataResult DumpTable(ShellState &state, const vector<string> &args) { |
| 96 | string zLike; |
| 97 | bool savedShowHeader = state.showHeader; |
| 98 | int savedShellFlags = state.shellFlgs; |
| 99 | state.ShellClearFlag(ShellFlags::SHFLG_Newlines); |
| 100 | state.ShellClearFlag(ShellFlags::SHFLG_Echo); |
| 101 | for (idx_t i = 1; i < args.size(); i++) { |
| 102 | if (args[i][0] == '-') { |
| 103 | const char *z = args[i].c_str() + 1; |
| 104 | if (z[0] == '-') { |
| 105 | z++; |
| 106 | } |
| 107 | if (StringUtil::Equals(z, "newlines")) { |
| 108 | state.ShellSetFlag(ShellFlags::SHFLG_Newlines); |
| 109 | } else { |
| 110 | state.PrintF(PrintOutput::STDERR, "Unknown option \"%s\" on \".dump\"\n", args[i].c_str()); |
| 111 | return MetadataResult::FAIL; |
| 112 | } |
| 113 | } else if (!zLike.empty()) { |
| 114 | zLike = StringUtil::Format("%s OR name LIKE %s ESCAPE '\\'", zLike, SQLString(args[i])); |
| 115 | } else { |
| 116 | zLike = StringUtil::Format("name LIKE %s ESCAPE '\\'", SQLString(args[i])); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | state.PrintF("BEGIN TRANSACTION;\n"); |
| 121 | state.showHeader = 0; |
| 122 | state.nErr = 0; |
| 123 | if (zLike.empty()) { |
| 124 | zLike = "true"; |
| 125 | } |
| 126 | |
| 127 | // Emit CREATE SCHEMA for non-main schemas first |
| 128 | auto zSql = StringUtil::Format("SELECT DISTINCT table_schema FROM information_schema.tables " |
| 129 | "WHERE table_schema != 'main' AND table_schema NOT LIKE 'pg_%%' " |
| 130 | "AND table_schema != 'information_schema' " |
| 131 | "AND table_name IN (SELECT name FROM sqlite_schema WHERE (%s) AND type=='table') " |
| 132 | "ORDER BY table_schema", |
| 133 | zLike); |
| 134 | auto result = state.conn->Query(zSql); |
| 135 | for (auto &row : *result) { |
| 136 | auto schema = row.GetValue<string>(0); |
| 137 | auto create_schema = StringUtil::Format("CREATE SCHEMA IF NOT EXISTS %s;", SQLIdentifier(schema)); |
| 138 | state.PrintF("%s;\n", create_schema.c_str()); |
| 139 | } |
| 140 | |
| 141 | zSql = StringUtil::Format("SELECT name, type, sql FROM sqlite_schema " |
| 142 | "WHERE (%s) AND type=='table'" |
| 143 | " AND sql NOT NULL" |
| 144 | " ORDER BY tbl_name='sqlite_sequence'", |
| 145 | zLike); |
| 146 | state.RunSchemaDumpQuery(zSql); |
| 147 | zSql = StringUtil::Format("SELECT sql FROM sqlite_schema " |
| 148 | "WHERE (%s) AND sql NOT NULL" |
| 149 | " AND type IN ('index','trigger','view')", |
| 150 | zLike); |
| 151 | state.RunTableDumpQuery(zSql); |
| 152 | state.PrintF(state.nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
nothing calls this directly
no test coverage detected