Cmdlets for EnterpriseDB

Build 22.0.8479

接続の確立

CData Cmdlets ユーザーは、データモジュールをインストールし、接続プロパティを設定してスクリプトを開始できます。このセクションでは、CSV インポートおよびエクスポートcmdlet などのネイティブPowerShell cmdlet でEnterpriseDB Cmdlets を使用する例を示します。

インストールおよび接続

PSGet がある場合は、PowerShell Gallery から次のコマンドを使ってcmdlet をインストールできます。CData サイトからセットアップを取得することもできます。

Install-Module EnterpriseDBCmdlets

プロファイルに以下を追加すると、次のセッションでcmdlet がロードされます。

Import-Module EnterpriseDBCmdlets;

Connect-EnterpriseDB cmdlet を使って、別のcmdlet に渡すことができる接続オブジェクトを作成します。

$conn = Connect-EnterpriseDB -User 'postgres' -Password 'admin' -Server '127.0.0.1' -Port '5444' -Database 'postgres'

Connecting to EnterpriseDB

The following connection properties are required in order to connect to data.

  • User: The user which will be used to authenticate with the EnterpriseDB server.
  • Password: The password which will be used to authenticate with the EnterpriseDB server.
  • Server: The host name or IP of the server hosting the EnterpriseDB database.
  • Port: The port of the server hosting the EnterpriseDB database.

You can also optionally set the following:

  • Database: The default database to connect to when connecting to the EnterpriseDB Server. If this is not set, the user's default database will be used.

データの取得

接続の作成が完了したら、リレーショナルデータベースに対して通常行える操作を 別のcmdlet を使って実行できます。 Select-EnterpriseDB cmdlet はデータを取得するためのネイティブなPowerShell インターフェースを提供します。

$results = Select-EnterpriseDB -Connection $conn -Table ""postgres"."public".Orders" -Columns @("ShipName, ShipCity") -Where "ShipCountry='USA'"
Invoke-EnterpriseDB cmdlet はSQL インターフェースを提供します。このcmdlet を使うと、Query パラメータを介してSQL クエリを実行できます。

cmdlet 出力のパイプ処理

cmdlet は行オブジェクトをパイプラインに一度に一行ずつ返します。以下は、結果をCSV ファイルにエクスポートします。

Select-EnterpriseDB -Connection $conn -Table "postgres"."public".Orders -Where "ShipCountry = 'USA'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my"postgres"."public".OrdersData.csv -NoTypeInformation

Select-EnterpriseDB からの結果をSelect-Object cmdlet にパイプして、Export-CSV cmdlet にパイプする前にいくつかのプロパティを実行していることがわかるでしょう。これをする理由は、CData Cmdlets は接続、テーブル、およびカラムの情報を結果セットの各行オブジェクトに追加しますが、必ずしもその情報がCSV ファイルに必要ではないからです。

ただし、これによってcmdlet の出力を別のcmdlet にパイプすることが容易になります。以下に、結果セットをJSON に変換する例を示します。

 
PS C:\> $conn  = Connect-EnterpriseDB -User 'postgres' -Password 'admin' -Server '127.0.0.1' -Port '5444' -Database 'postgres'
PS C:\> $row = Select-EnterpriseDB -Connection $conn -Table ""postgres"."public".Orders" -Columns (ShipName, ShipCity) -Where "ShipCountry = 'USA'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  ""postgres"."public".Orders",
  "Columns":  [

  ],
  "ShipName":  "MyShipName",
  "ShipCity":  "MyShipCity"
} 

データの削除

以下は、抽出条件に合うあらゆるレコードを削除します。

Select-EnterpriseDB -Connection $conn -Table "postgres"."public".Orders -Where "ShipCountry = 'USA'" | Remove-EnterpriseDB

データの変更

cmdlet はデータクレンジング同様、データの変換を容易にします。次の例は、レコードがすでに存在するかどうか、挿入する前に更新が必要かどうかをチェックしてから、CSV ファイルのデータをEnterpriseDB にロードします。

Import-Csv -Path C:\My"postgres"."public".OrdersUpdates.csv | %{
  $record = Select-EnterpriseDB -Connection $conn -Table "postgres"."public".Orders -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-EnterpriseDB -Connection $conn -Table "postgres"."public".Orders -Columns @("ShipName","ShipCity") -Values @($_.ShipName, $_.ShipCity) -Where "Id  = `'$_.Id`'"
  }else{
    Add-EnterpriseDB -Connection $conn -Table "postgres"."public".Orders -Columns @("ShipName","ShipCity") -Values @($_.ShipName, $_.ShipCity)
  }
}

Copyright (c) 2023 CData Software, Inc. - All rights reserved.
Build 22.0.8479