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);
        }
Пример #2
0
        /// <summary>
        /// Script create statements for metadata object
        /// </summary>
        private static string ScriptAsCreate(
            IBindingContext bindingContext,
            ConnectionInfo connInfo,
            ObjectMetadata metadata)
        {
            Scripter         scripter = new Scripter(bindingContext.ServerConnection, connInfo);
            StringCollection results  = null;

            if (metadata.MetadataType == MetadataType.Table)
            {
                results = scripter.GetTableScripts(metadata.Name, metadata.Schema);
            }
            else if (metadata.MetadataType == MetadataType.SProc)
            {
                results = scripter.GetStoredProcedureScripts(metadata.Name, metadata.Schema);
            }
            else if (metadata.MetadataType == MetadataType.View)
            {
                results = scripter.GetViewScripts(metadata.Name, metadata.Schema);
            }

            StringBuilder builder = null;

            if (results != null)
            {
                builder = new StringBuilder();
                foreach (var result in results)
                {
                    builder.AppendLine(result);
                    builder.AppendLine();
                }
            }
            return(builder != null?builder.ToString() : null);
        }
        /// <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);
            });
        }
        /// <summary>
        /// Not yet implemented
        /// </summary>
        private static string ScriptAsDelete(
            IBindingContext bindingContext,
            ConnectionInfo connInfo,
            ObjectMetadata metadata)
        {
            Scripter         scripter = new Scripter(bindingContext.ServerConnection, connInfo);
            StringCollection results  = null;
            ScriptingOptions options  = new ScriptingOptions();

            options.ScriptDrops = true;
            switch (metadata.MetadataTypeName)
            {
            case ("Table"):
                results = scripter.GetTableScripts(metadata.Name, metadata.Schema, options);
                break;

            case ("View"):
                results = scripter.GetViewScripts(metadata.Name, metadata.Schema, options);
                break;

            case ("StoredProcedure"):
                results = scripter.GetStoredProcedureScripts(metadata.Name, metadata.Schema, options);
                break;

            case ("Schema"):
                results = scripter.GetSchemaScripts(metadata.Name, metadata.Schema, options);
                break;

            case ("Database"):
                results = scripter.GetDatabaseScripts(metadata.Name, metadata.Schema, options);
                break;

            default:
                results = null;
                break;
            }
            StringBuilder builder = null;

            if (results != null)
            {
                builder = new StringBuilder();
                foreach (var result in results)
                {
                    builder.AppendLine(result);
                    builder.AppendLine();
                }
            }
            return(builder != null?builder.ToString() : null);
        }