Skip to main content

Python からのデータベース・メソッドの呼び出し

ここでは、ObjectScript クラス・メソッドを Python アプリケーションから直接呼び出すことを可能にするクラス iris.IRIS のメソッドを説明します。詳細および例は、以下のセクションを参照してください。

Python からのクラス・メソッドの呼び出し

classMethodValue() メソッドと classMethodVoid() メソッドは、ほとんどの目的に使用できますが、特定の返りタイプが必要な場合は、classMethodBoolean()classMethodBytes()classMethodDecimal()classMethodFloat()classMethodIRISList()classMethodInteger()classMethodObject()、および classMethodString()IRIS.classMethodValue() 型キャスト・メソッドも使用できます。

これらのメソッドはすべて、class_name および method_name の文字列引数に加え、0 個以上のメソッド引数を取ります。

以下の例のコードでは、User.NativeTest という名前の ObjectScript テスト・クラスからいくつかのデータ型のクラス・メソッドを呼び出します(このセクションの最後に示す “ObjectScript クラス User.NativeTest” を参照)。

Python からの ObjectScript クラス・メソッドの呼び出し

この例のコードでは、サポートされている各データ型のクラス・メソッドを ObjectScript テスト・クラス User.NativeTest から呼び出します (この例の直後に表示されています)。変数 irispy はクラス iris.IRIS の以前に定義されたインスタンスで、現在サーバに接続されていると想定します (“Python での接続の作成” を参照)。

  className = 'User.NativeTest'

  comment = ".cmBoolean() tests whether arguments 2 and 3 are equal: "
  boolVal = irispy.classMethodBoolean(className,'cmBoolean',2,3)
  print(className + comment + str(boolVal))

  comment = ".cmBytes returns integer arguments 72,105,33 as a byte array (string value 'Hi!'): "
  byteVal = irispy.classMethodBytes(className,'cmBytes',72,105,33) #ASCI 'Hi!'
  print(className + comment + str(byteVal))

  comment = ".cmString() concatenates 'Hello' with argument string 'World': "
  stringVal = irispy.classMethodString(className,'cmString','World')
  print(className + comment + stringVal)

  comment = ".cmLong() returns the sum of arguments 7+8: "
  longVal = irispy.classMethodInteger(className,'cmLong',7,8)
  print(className + comment + str(longVal))

  comment = ".cmDouble() multiplies argument 4.5 by 1.5: "
  doubleVal = irispy.classMethodFloat(className,'cmDouble',4.5)
  print(className + comment + str(doubleVal))

  comment = ".cmList() returns a $LIST containing arguments 'The answer is ' and 42: "
  listVal = irispy.classMethodIRISList(className,"cmList","The answer is ",42);
  print(className + comment+listVal.get(1)+str(listVal.get(2)))

  comment = ".cmVoid assigns argument value 75 to global node ^cmGlobal: "
  try:
    irispy.kill('cmGlobal') # delete ^cmGlobal if it exists
    irispy.classMethodVoid(className,'cmVoid',75)
    nodeVal = irispy.get('cmGlobal');  #get current value of ^cmGlobal
  except:
    nodeVal = 'FAIL'
  print(className + comment + str(nodeVal))

この例では、classMethodValue() (決まった形式のない値を返します)、classMethodDecimal() (主に高精度への対応において classMethodFloat() とは異なります)、および classMethodObject() (“Python からのデータベース・オブジェクトの制御” で説明しています) は省略されています。

ObjectScript クラス User.NativeTest

前の例を実行するには、この ObjectScript クラスがコンパイルされ、サーバで使用可能である必要があります。


Class User.NativeTest Extends %Persistent
  {

  ClassMethod cmBoolean(cm1 As %Integer, cm2 As %Integer) As %Boolean
  {
     Quit (cm1=cm2)
  }

  ClassMethod cmBytes(cm1 As %Integer, cm2 As %Integer, cm3 As %Integer) As %Binary
  {
     Quit $CHAR(cm1,cm2,cm3)
  }

  ClassMethod cmString(cm1 As %String) As %String
  {
     Quit "Hello "_cm1
  }

  ClassMethod cmLong(cm1 As %Integer, cm2 As %Integer) As %Integer
  {
     Quit cm1+cm2
  }

  ClassMethod cmDouble(cm1 As %Double) As %Double
  {
     Quit cm1 * 1.5
  }

  ClassMethod cmVoid(cm1 As %Integer)
  {
     Set ^cmGlobal=cm1
     Quit
  }

  ClassMethod cmList(cm1 As %String, cm2 As %Integer)
  {
     Set list = $LISTBUILD(cm1,cm2)
     Quit list
  }
}

ターミナルからこれらのメソッドを呼び出すことで、それらをテストできます。例を以下に示します。

USER>write ##class(User.NativeTest).cmString("World")
Hello World
Note:
関数とプロシージャ

以前のバージョンの Native SDK では、ObjectScript の関数やプロシージャに直接アクセスするメソッドが提供されていました。これらのメソッドはセキュリティ上の懸念により削除されました。関数やプロシージャは、ObjectScript ラッパー・クラスでメソッドとして利用可能にすることで、引き続き間接的に呼び出すことができます。

引数の参照渡し

InterSystems クラス・ライブラリのほとんどのクラスでは、メソッドが %StatusOpens in a new tab 値のみを返す呼び出し規則を使用します。実際の結果は、参照によって渡される引数で返されます。Native SDK では、メソッドで参照渡しがサポートされます。これには、以下のように引数の値をクラス IRISReference のインスタンスに割り当て、そのインスタンスを引数として渡します。

  ref_object = iris.IRISReference(None); # set initial value to None
  irispy.classMethodObject("%SomeClass","SomeMethod",ref_object);
  returned_value = ref_object.getValue;  # get the method result

以下の例では、標準のクラス・ライブラリ・メソッドが呼び出されます。

参照渡し引数の使用

この例は、%SYS.DatabaseQuery.GetDatabaseFreeSpace()Opens in a new tab を呼び出して、iristemp データベースで利用可能な空き容量の量 (MB 単位) を取得します。

  dir = "C:/InterSystems/IRIS/mgr/iristemp" # directory to be tested
  value = "error"
  status = 0

  freeMB = iris.IRISReference(None) # set initial value to 0
  print("Variable freeMB is type"+str(type(freeMB)) + ", value=" + str(freeMB.getValue()))
  try:
    print("Calling %SYS.DatabaseQuery.GetDatabaseFreeSpace()... ")
    status = irispy.classMethodObject("%SYS.DatabaseQuery","GetDatabaseFreeSpace",dir,freeMB)
    value = freeMB.getValue()
  except:
    print("Call to class method GetDatabaseFreeSpace() returned error:")
  print("(status=" + str(status) + ") Free space in " + dir + " = " + str(value) + "MB\n")

出力 :

Variable freeMB is type<class 'iris.IRISReference'>, value=None
Calling %SYS.DatabaseQuery.GetDatabaseFreeSpace()...
(status=1) Free space in C:/InterSystems/IRIS/mgr/iristemp = 10MB
FeedbackOpens in a new tab