private string GenerateScriptSelect(Server server, UrnCollection urns) { string script = string.Empty; ScriptingObject scriptingObject = this.Parameters.ScriptingObjects[0]; Urn objectUrn = urns[0]; string typeName = objectUrn.GetNameForType(scriptingObject.Type); // select from service broker if (string.Compare(typeName, "ServiceBroker", StringComparison.CurrentCultureIgnoreCase) == 0) { script = Scripter.SelectAllValuesFromTransmissionQueue(objectUrn); } // select from queues else if (string.Compare(typeName, "Queues", StringComparison.CurrentCultureIgnoreCase) == 0 || string.Compare(typeName, "SystemQueues", StringComparison.CurrentCultureIgnoreCase) == 0) { script = Scripter.SelectAllValues(objectUrn); } // select from table or view else { Database db = server.Databases[databaseName]; bool isDw = db.IsSqlDw; script = new Scripter().SelectFromTableOrView(server, objectUrn, isDw); } return(script); }
private string QualifiedObjectName(Urn urn) { if (urn == null) { return(String.Empty); } string name = urn.GetNameForType(urn.Type); string schema = urn.GetAttribute("Schema", urn.Type); return("[" + schema + "]" + ".[" + name + "]"); }
/// <summary> /// Runs the async task that performs the scripting operation. /// </summary> private void RunSelectTask(ConnectionInfo connInfo, ScriptingParams parameters, RequestContext <ScriptingResult> requestContext) { ConnectionServiceInstance.ConnectionQueue.QueueBindingOperation( key: ConnectionServiceInstance.ConnectionQueue.AddConnectionContext(connInfo, "Scripting"), bindingTimeout: ScriptingOperationTimeout, bindOperation: (bindingContext, cancelToken) => { string script = string.Empty; ScriptingObject scriptingObject = parameters.ScriptingObjects[0]; try { Server server = new Server(bindingContext.ServerConnection); server.DefaultTextMode = true; // build object URN SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(parameters.ConnectionString); Urn objectUrn = BuildScriptingObjectUrn(server, connectionStringBuilder, scriptingObject); string typeName = objectUrn.GetNameForType(scriptingObject.Type); // select from service broker if (string.Compare(typeName, "ServiceBroker", StringComparison.CurrentCultureIgnoreCase) == 0) { script = Scripter.SelectAllValuesFromTransmissionQueue(objectUrn); } // select from queues else if (string.Compare(typeName, "Queues", StringComparison.CurrentCultureIgnoreCase) == 0 || string.Compare(typeName, "SystemQueues", StringComparison.CurrentCultureIgnoreCase) == 0) { script = Scripter.SelectAllValues(objectUrn); } // select from table or view else { Database db = server.Databases[connectionStringBuilder.InitialCatalog]; bool isDw = db.IsSqlDw; script = new Scripter().SelectFromTableOrView(server, objectUrn, isDw); } // send script result to client requestContext.SendResult(new ScriptingResult { Script = script }).Wait(); } catch (Exception e) { requestContext.SendError(e).Wait(); } return(null); }); }
private void Download(IEnumerable <IGrouping <int, string> > smoObjectsGroup) { Parallel.ForEach(smoObjectsGroup, g => { Server serv = new Server(new ServerConnection() { ConnectionString = ServerOption.ToString(), }); Scripter scripter = new Scripter { Server = serv }; scripter.Options.IncludeHeaders = true; scripter.Options.SchemaQualify = true; scripter.Options.AllowSystemObjects = false; try { foreach (var urn in g) { var urnn = new Urn(urn); var type = urnn.Type; var name = urnn.GetNameForType(type); if (!urnn.GetAttribute("Schema").ToUpper().Equals("dbo".ToUpper())) { continue; } var smoObject = serv.GetSmoObject(urnn); var scriptLines = scripter.Script(new SqlSmoObject[] { smoObject }); Logger.Log($"Forming script: '{name}'"); if (scriptLines.Count == 0) { Logger.Log($"Неизвестная ошибка формирования скрипта для: '{name}'"); continue; } scriptLines.RemoveAt(0); if (ServerOption.ReplaceFirstCreate) { var replacer = ReplacerFactories.GetReplacer(type); scriptLines = replacer.Replace(scriptLines); } var outputString = OutputSchemaObject(ServerOption.DbName, scriptLines); var extension = "sql"; var fileName = $"{name}.{type}.{extension}"; var fullFilePath = Path.Combine(WriteToFolderPath, ServerOption.ServerName, DateTime.Now.ToString("yyyyMMdd"), fileName); Logger.Log($"Write File: '{fullFilePath}'"); WriteFile(fullFilePath, outputString); } } catch (Exception e) { Logger.Log(e); } }); }