String ForceInsertRowid(const SqlInsert& insert, Sql& cursor) { return ForceInsertRowid(SqlStatement(insert).Get(cursor.GetDialect()), cursor); }
void DlgSqlExport::Run(Sql& cursor, String command, String tablename) { Title(Nvl(tablename, t_("SQL query")) + t_(" export")); object_name <<= tablename; if(!cursor.Execute(command)) { Exclamation(NFormat(t_("Error executing [* \1%s\1]: \1%s"), command, cursor.GetLastError())); return; } for(int i = 0; i < cursor.GetColumns(); i++) { const SqlColumnInfo& sci = cursor.GetColumnInfo(i); String type; switch(sci.type) { case BOOL_V: case INT_V: type = t_("integer"); break; case DOUBLE_V: type = t_("real number"); break; case STRING_V: case WSTRING_V: type = t_("string"); break; case DATE_V: type = t_("date"); break; case TIME_V: type = t_("date/time"); break; case /*ORA_BLOB_V*/-1: type = t_("BLOB"); break; case /*ORA_CLOB_V*/-2: type = t_("CLOB"); break; default: type = FormatInt(sci.type); break; } columns.Add(sci.name, sci.type, sci.width, 1); } static String cfg; LoadFromString(*this, cfg); SyncUI(); while(TopWindow::Run() == IDOK) try { String out_table = ~object_name; String delim; switch((int)~delimiters) { case DELIM_TAB: delim = "\t"; break; case DELIM_SEMICOLON: delim = ";"; break; } Vector<int> out; String colstr; String title; for(int i = 0; i < columns.GetCount(); i++) if(columns.Get(i, 3)) { out.Add(i); String cname = cursor.GetColumnInfo(i).name; colstr << (i ? ", " : "") << cname; if(i) title << delim; title << cname; } if(out.IsEmpty()) { throw Exc(t_("No columns selected!")); continue; } String rowbegin, rowend; int fmt = ~format; FileSel fsel; String ext; switch(fmt) { case FMT_TEXT: { rowend = ""; ext = ".txt"; fsel.Type(t_("Text files (*.txt)"), "*.txt"); break; } case FMT_SQL: { if(identity_insert) rowbegin << "set identity_insert " << out_table << " on "; rowbegin << "insert into " << out_table << "(" << colstr << ") values ("; rowend = ");"; ext = ".sql"; fsel.Type(t_("SQL scripts (*.sql)"), "*.sql"); break; } } fsel.AllFilesType().DefaultExt(ext.Mid(1)); if(!IsNull(recent_file)) fsel <<= ForceExt(recent_file, ext); if(!fsel.ExecuteSaveAs(t_("Save export as"))) continue; recent_file = ~fsel; FileOut fo; if(!fo.Open(recent_file)) { Exclamation(NFormat(t_("Error creating file [* \1%s\1]."), recent_file)); continue; } if(fmt == FMT_TEXT) fo.PutLine(title); Progress progress(t_("Exporting row %d")); while(cursor.Fetch()) { String script = rowbegin; for(int i = 0; i < out.GetCount(); i++) { Value v = cursor[out[i]]; switch(fmt) { case FMT_TEXT: { if(i) script.Cat(delim); if(IsString(v) && quote) { String s = v; script << '\"'; for(const char *p = s, *e = s.End(); p < e; p++) if(*p == '\"') script.Cat("\"\""); else script.Cat(*p); script << '\"'; } else script << StdFormat(v); break; } case FMT_SQL: { if(i) script.Cat(", "); script << SqlCompile(cursor.GetDialect(), SqlFormat(v)); break; } } } script << rowend; fo.PutLine(script); /* if(autocommit && --left <= 0) { fo.PutLine("commit;"); left = autocommit; } */ if(progress.StepCanceled()) { Exclamation(t_("Export aborted!")); return; } } fo.Close(); if(fo.IsError()) throw Exc(NFormat(t_("Error writing file %s."), recent_file)); break; } catch(Exc e) { ShowExc(e); } cfg = StoreAsString(*this); }