Bir konteyner sağlayıcısı yazma


Bu konu, dosya sistemi sağlayıcısındaki klasörler gibi diğer öğeleri içeren öğeleri destekleyen Windows PowerShell sağlayıcısının yöntemlerinin nasıl uygulanacağını açıklamaktadır. Kapsayıcıları destekleyebilmek için bir sağlayıcının System.Management.Automation.Provider.Containercmdletprovider sınıfından türetmesi gerekir.

Bu konudaki örneklerdeki sağlayıcı, veri deposu olarak Access veritabanını kullanır. Veritabanıyla etkileşimde bulunmak için kullanılan çeşitli yardımcı yöntemler ve sınıflar vardır. Yardımcı yöntemleri içeren numunenin tamamı için bkz. AccessDBProviderSample04.

Windows PowerShell sağlayıcıları hakkında daha fazla bilgi için bkz. Windows PowerShell Sağlayıcısına Genel Bakış.

Konteyner yöntemlerini uygulama

System.Management.Automation.Provider.Containercmdletprovider sınıfı, kapsayıcıları destekleyen ve öğeleri oluşturan, kopyalayan ve kaldıran yöntemleri uygular. Bu yöntemlerin tam listesi için bkz. System.Management.Automation.Provider.ContainerCmdletProvider.

Not

Bu konu, Windows PowerShell Sağlayıcısı Hızlı Başlangıç'taki bilgilere dayanmaktadır. Bu konu, bir sağlayıcı projesinin nasıl kurulacağına veya sürücüleri oluşturup kaldıran System.Management.Automation.Provider.Drivecmdletprovider sınıfından devralınan yöntemlerin nasıl uygulanacağına ilişkin temel bilgileri kapsamaz. Bu konu ayrıca System.Management.Automation.Provider.Itemcmdletprovider sınıfı tarafından kullanıma sunulan yöntemlerin nasıl uygulanacağını da kapsamaz. Öğe cmdlet'lerinin nasıl uygulanacağını gösteren bir örnek için bkz. Öğe sağlayıcı yazma.

Sağlayıcı sınıfını bildirme

Sağlayıcının System.Management.Automation.Provider.Containercmdletprovider sınıfından türeteceğini ve bunu System.Management.Automation.Provider.Cmdletprovide özelliğiyle süsleyeceğini bildirin.

[CmdletProvider("AccessDB", ProviderCapabilities.None)]
   public class AccessDBProvider : ContainerCmdletProvider
   {

   }

GetChildItems'ı Uygulamak

Bir kullanıcı Microsoft.PowerShell.Commands.GetChildItemCommand cmdlet'ini çağırdığında PowerShell motoru System.Management.Automation.Provider.Containercmdletprovider.Getchilditems* yöntemini çağırır. Bu yöntem, belirtilen yoldaki öğenin çocukları olan öğeleri alır.

Access veritabanı örneğinde, System.Management.Automation.Provider.Containercmdletprovider.Getchilditems* yönteminin davranışı, belirtilen öğenin türüne bağlıdır. Öğe sürücü ise, çocuklar tablolardır ve yöntem, veritabanından tablo kümesini döndürür. Belirtilen öğe bir tablo ise alt öğeler o tablonun satırlarıdır. Öğe bir satırsa alt öğesi yoktur ve yöntem yalnızca o satırı döndürür. Tüm alt öğeler System.Management.Automation.Provider.Cmdletprovider.Writeitemobject* yöntemiyle PowerShell motoruna geri gönderilir.

protected override void GetChildItems(string path, bool recurse)
       {
           // If path represented is a drive then the children in the path are
           // tables. Hence all tables in the drive represented will have to be
           // returned
           if (PathIsDrive(path))
           {
               foreach (DatabaseTableInfo table in GetTables())
               {
                   WriteItemObject(table, path, true);

                   // if the specified item exists and recurse has been set then
                   // all child items within it have to be obtained as well
                   if (ItemExists(path) && recurse)
                   {
                       GetChildItems(path + pathSeparator + table.Name, recurse);
                   }
               } // foreach (DatabaseTableInfo...
           } // if (PathIsDrive...
           else
           {
               // Get the table name, row number and type of path from the
               // path specified
               string tableName;
               int rowNumber;

               PathType type = GetNamesFromPath(path, out tableName, out rowNumber);

               if (type == PathType.Table)
               {
                   // Obtain all the rows within the table
                   foreach (DatabaseRowInfo row in GetRows(tableName))
                   {
                       WriteItemObject(row, path + pathSeparator + row.RowNumber,
                               false);
                   } // foreach (DatabaseRowInfo...
               }
               else if (type == PathType.Row)
               {
                   // In this case the user has directly specified a row, hence
                   // just give that particular row
                   DatabaseRowInfo row = GetRow(tableName, rowNumber);
                   WriteItemObject(row, path + pathSeparator + row.RowNumber,
                               false);
               }
               else
               {
                   // In this case, the path specified is not valid
                   ThrowTerminatingInvalidPathException(path);
               }
           } // else
       }

GetChildNames'in Uygulanması

System.Management.Automation.Provider.Containercmdletprovider.Getchildnames* yöntemi, System.Management.Automation.Provider.Containercmdletprovider.Getchilditems* yöntemine benzer; tek fark, öğelerin kendisini değil, yalnızca öğelerin name özelliğini döndürmesidir.

protected override void GetChildNames(string path,
                                     ReturnContainers returnContainers)
       {
           // If the path represented is a drive, then the child items are
           // tables. get the names of all the tables in the drive.
           if (PathIsDrive(path))
           {
               foreach (DatabaseTableInfo table in GetTables())
               {
                   WriteItemObject(table.Name, path, true);
               } // foreach (DatabaseTableInfo...
           } // if (PathIsDrive...
           else
           {
               // Get type, table name and row number from path specified
               string tableName;
               int rowNumber;

               PathType type = GetNamesFromPath(path, out tableName, out rowNumber);

               if (type == PathType.Table)
               {
                   // Get all the rows in the table and then write out the
                   // row numbers.
                   foreach (DatabaseRowInfo row in GetRows(tableName))
                   {
                       WriteItemObject(row.RowNumber, path, false);
                   } // foreach (DatabaseRowInfo...
               }
               else if (type == PathType.Row)
               {
                   // In this case the user has directly specified a row, hence
                   // just give that particular row
                   DatabaseRowInfo row = GetRow(tableName, rowNumber);

                   WriteItemObject(row.RowNumber, path, false);
               }
               else
               {
                   ThrowTerminatingInvalidPathException(path);
               }
           } // else
       }

NewItem'in Uygulanması

System.Management.Automation.Provider.Containercmdletprovider.Newitem* yöntemi, belirtilen yolda belirtilen türde yeni bir öğe oluşturur. Bir kullanıcı Microsoft.PowerShell.Commands.NewItemCommand cmdlet'ini çağırdığında PowerShell motoru bu yöntemi çağırır.

Bu örnekte yöntem, yol ve türün eşleştiğini belirlemek için mantığı uygular. Yani, doğrudan sürücünün (veritabanı) altında yalnızca bir tablo oluşturulabilir ve bir tablonun altında yalnızca bir satır oluşturulabilir. Belirtilen yol ve öğe türü bu şekilde eşleşmezse yöntem bir istisna atar.

protected override void NewItem(string path, string type,
                                   object newItemValue)
       {
           string tableName;
           int rowNumber;

           PathType pt = GetNamesFromPath(path, out tableName, out rowNumber);

           if (pt == PathType.Invalid)
           {
               ThrowTerminatingInvalidPathException(path);
           }

           // Check if type is either "table" or "row", if not throw an
           // exception
           if (!String.Equals(type, "table", StringComparison.OrdinalIgnoreCase)
               && !String.Equals(type, "row", StringComparison.OrdinalIgnoreCase))
           {
               WriteError(new ErrorRecord
                                 (new ArgumentException("Type must be either a table or row"),
                                     "CannotCreateSpecifiedObject",
                                        ErrorCategory.InvalidArgument,
                                             path
                                  )
                         );

               throw new ArgumentException("This provider can only create items of type \"table\" or \"row\"");
           }

           // Path type is the type of path of the container. So if a drive
           // is specified, then a table can be created under it and if a table
           // is specified, then a row can be created under it. For the sake of
           // completeness, if a row is specified, then if the row specified by
           // the path does not exist, a new row is created. However, the row
           // number may not match as the row numbers only get incremented based
           // on the number of rows

           if (PathIsDrive(path))
           {
               if (String.Equals(type, "table", StringComparison.OrdinalIgnoreCase))
               {
                   // Execute command using ODBC connection to create a table
                   try
                   {
                       // create the table using an sql statement
                       string newTableName = newItemValue.ToString();

                       if (!TableNameIsValid(newTableName))
                       {
                           return;
                       }
                       string sql = "create table " + newTableName
                                            + " (ID INT)";

                       // Create the table using the Odbc connection from the
                       // drive.
                       AccessDBPSDriveInfo di = this.PSDriveInfo as AccessDBPSDriveInfo;

                       if (di == null)
                       {
                           return;
                       }
                       OdbcConnection connection = di.Connection;

                       if (ShouldProcess(newTableName, "create"))
                       {
                           OdbcCommand cmd = new OdbcCommand(sql, connection);
                           cmd.ExecuteScalar();
                       }
                   }
                   catch (Exception ex)
                   {
                       WriteError(new ErrorRecord(ex, "CannotCreateSpecifiedTable",
                                 ErrorCategory.InvalidOperation, path)
                                 );
                   }
               } // if (String...
               else if (String.Equals(type, "row", StringComparison.OrdinalIgnoreCase))
               {
                   throw new
                       ArgumentException("A row cannot be created under a database, specify a path that represents a Table");
               }
           }// if (PathIsDrive...
           else
           {
               if (String.Equals(type, "table", StringComparison.OrdinalIgnoreCase))
               {
                   if (rowNumber < 0)
                   {
                       throw new
                           ArgumentException("A table cannot be created within another table, specify a path that represents a database");
                   }
                   else
                   {
                       throw new
                           ArgumentException("A table cannot be created inside a row, specify a path that represents a database");
                   }
               } //if (String.Equals....
               // if path specified is a row, create a new row
               else if (String.Equals(type, "row", StringComparison.OrdinalIgnoreCase))
               {
                   // The user is required to specify the values to be inserted
                   // into the table in a single string separated by commas
                   string value = newItemValue as string;

                   if (String.IsNullOrEmpty(value))
                   {
                       throw new
                           ArgumentException("Value argument must have comma separated values of each column in a row");
                   }
                   string[] rowValues = value.Split(',');

                   OdbcDataAdapter da = GetAdapterForTable(tableName);

                   if (da == null)
                   {
                       return;
                   }

                   DataSet ds = GetDataSetForTable(da, tableName);
                   DataTable table = GetDataTable(ds, tableName);

                   if (rowValues.Length != table.Columns.Count)
                   {
                       string message =
                            String.Format(CultureInfo.CurrentCulture,
                                            "The table has {0} columns and the value specified must have so many comma separated values",
                                                table.Columns.Count);

                       throw new ArgumentException(message);
                   }

                   if (!Force && (rowNumber >=0 && rowNumber < table.Rows.Count))
                   {
                       string message = String.Format(CultureInfo.CurrentCulture,
                                                        "The row {0} already exists. To create a new row specify row number as {1}, or specify path to a table, or use the -Force parameter",
                                                            rowNumber, table.Rows.Count);

                       throw new ArgumentException(message);
                   }

                   if (rowNumber > table.Rows.Count)
                   {
                       string message = String.Format(CultureInfo.CurrentCulture,
                                            "To create a new row specify row number as {0}, or specify path to a table",
                                                table.Rows.Count);

                       throw new ArgumentException(message);
                   }

                   // Create a new row and update the row with the input
                   // provided by the user
                   DataRow row = table.NewRow();
                   for (int i = 0; i < rowValues.Length; i++)
                   {
                       row[i] = rowValues[i];
                   }
                   table.Rows.Add(row);

                   if (ShouldProcess(tableName, "update rows"))
                   {
                       // Update the table from memory back to the data source
                       da.Update(ds, tableName);
                   }

               }// else if (String...
           }// else ...

       }

CopyItem'in Uygulanması

System.Management.Automation.Provider.ContainerCmdletProvider.CopyItem belirtilen öğeyi belirtilen yola kopyalar. Bir kullanıcı Microsoft.PowerShell.Commands.CopyItemCommand cmdlet'ini çağırdığında PowerShell motoru bu yöntemi çağırır. Bu yöntem aynı zamanda özyinelemeli de olabilir; öğenin kendisine ek olarak tüm öğelerin alt öğelerini de kopyalayabilir.

System.Management.Automation.Provider.Containercmdletprovider.Newitem* yöntemine benzer şekilde, bu yöntem, belirtilen öğenin kopyalandığı yol için doğru türde olduğundan emin olmak için mantık gerçekleştirir. Örneğin hedef yol bir tablo ise kopyalanacak öğenin bir satır olması gerekir.

protected override void CopyItem(string path, string copyPath, bool recurse)
       {
           string tableName, copyTableName;
           int rowNumber, copyRowNumber;

           PathType type = GetNamesFromPath(path, out tableName, out rowNumber);
           PathType copyType = GetNamesFromPath(copyPath, out copyTableName, out copyRowNumber);

           if (type == PathType.Invalid)
           {
               ThrowTerminatingInvalidPathException(path);
           }

           if (type == PathType.Invalid)
           {
               ThrowTerminatingInvalidPathException(copyPath);
           }

           // Get the table and the table to copy to
           OdbcDataAdapter da = GetAdapterForTable(tableName);
           if (da == null)
           {
               return;
           }

           DataSet ds = GetDataSetForTable(da, tableName);
           DataTable table = GetDataTable(ds, tableName);

           OdbcDataAdapter cda = GetAdapterForTable(copyTableName);
           if (cda == null)
           {
               return;
           }

           DataSet cds = GetDataSetForTable(cda, copyTableName);
           DataTable copyTable = GetDataTable(cds, copyTableName);

           // if source represents a table
           if (type == PathType.Table)
           {
               // if copyPath does not represent a table
               if (copyType != PathType.Table)
               {
                   ArgumentException e = new ArgumentException("Table can only be copied on to another table location");

                   WriteError(new ErrorRecord(e, "PathNotValid",
                       ErrorCategory.InvalidArgument, copyPath));

                   throw e;
               }

               // if table already exists then force parameter should be set
               // to force a copy
               if (!Force && GetTable(copyTableName) != null)
               {
                   throw new ArgumentException("Specified path already exists");
               }

               for (int i = 0; i < table.Rows.Count; i++)
               {
                   DataRow row = table.Rows[i];
                   DataRow copyRow = copyTable.NewRow();

                   copyRow.ItemArray = row.ItemArray;
                   copyTable.Rows.Add(copyRow);
               }
           } // if (type == ...
           // if source represents a row
           else
           {
               if (copyType == PathType.Row)
               {
                   if (!Force && (copyRowNumber < copyTable.Rows.Count))
                   {
                       throw new ArgumentException("Specified path already exists.");
                   }

                   DataRow row = table.Rows[rowNumber];
                   DataRow copyRow = null;

                   if (copyRowNumber < copyTable.Rows.Count)
                   {
                       // copy to an existing row
                       copyRow = copyTable.Rows[copyRowNumber];
                       copyRow.ItemArray = row.ItemArray;
                       copyRow[0] = GetNextID(copyTable);
                   }
                   else if (copyRowNumber == copyTable.Rows.Count)
                   {
                       // copy to the next row in the table that will
                       // be created
                       copyRow = copyTable.NewRow();
                       copyRow.ItemArray = row.ItemArray;
                       copyRow[0] = GetNextID(copyTable);
                       copyTable.Rows.Add(copyRow);
                   }
                   else
                   {
                       // attempting to copy to a nonexistent row or a row
                       // that cannot be created now - throw an exception
                       string message = String.Format(CultureInfo.CurrentCulture,
                                             "The item cannot be specified to the copied row. Specify row number as {0}, or specify a path to the table.",
                                                    table.Rows.Count);

                       throw new ArgumentException(message);
                   }
               }
               else
               {
                   // destination path specified represents a table,
                   // create a new row and copy the item
                   DataRow copyRow = copyTable.NewRow();
                   copyRow.ItemArray = table.Rows[rowNumber].ItemArray;
                   copyRow[0] = GetNextID(copyTable);
                   copyTable.Rows.Add(copyRow);
               }
           }

           if (ShouldProcess(copyTableName, "CopyItems"))
           {
               cda.Update(cds, copyTableName);
           }

       } //CopyItem

RemoveItem'in Uygulanması

System.Management.Automation.Provider.Containercmdletprovider.Removeitem* yöntemi, belirtilen yoldaki öğeyi kaldırır. Bir kullanıcı Microsoft.PowerShell.Commands.RemoveItemCommand cmdlet'ini çağırdığında PowerShell motoru bu yöntemi çağırır.

protected override void RemoveItem(string path, bool recurse)
       {
           string tableName;
           int rowNumber = 0;

           PathType type = GetNamesFromPath(path, out tableName, out rowNumber);

           if (type == PathType.Table)
           {
               // if recurse flag has been specified, delete all the rows as well
               if (recurse)
               {
                   OdbcDataAdapter da = GetAdapterForTable(tableName);
                   if (da == null)
                   {
                       return;
                   }

                   DataSet ds = GetDataSetForTable(da, tableName);
                   DataTable table = GetDataTable(ds, tableName);

                   for (int i = 0; i < table.Rows.Count; i++)
                   {
                       table.Rows[i].Delete();
                   }

                   if (ShouldProcess(path, "RemoveItem"))
                   {
                       da.Update(ds, tableName);
                       RemoveTable(tableName);
                   }
               }//if (recurse...
               else
               {
                   // Remove the table
                   if (ShouldProcess(path, "RemoveItem"))
                   {
                       RemoveTable(tableName);
                   }
               }
           }
           else if (type == PathType.Row)
           {
               OdbcDataAdapter da = GetAdapterForTable(tableName);
               if (da == null)
               {
                   return;
               }

               DataSet ds = GetDataSetForTable(da, tableName);
               DataTable table = GetDataTable(ds, tableName);

               table.Rows[rowNumber].Delete();

               if (ShouldProcess(path, "RemoveItem"))
               {
                   da.Update(ds, tableName);
               }
           }
           else
           {
               ThrowTerminatingInvalidPathException(path);
           }

       }

Sonraki adımlar

Tipik bir gerçek dünya sağlayıcısı, öğeleri sürücü içinde bir yoldan diğerine taşıma kapasitesine sahiptir. Öğelerin taşınmasını destekleyen bir sağlayıcı örneği için bkz. Gezinme sağlayıcısı yazma.

Ayrıca Bakınız

Gezinme sağlayıcısı yazma

Windows PowerShell Sağlayıcısına Genel Bakış

İlgili makaleler