日時型および時間間隔型の処理¶
ここでは、いずれかの日時型の入力パラメータや出力パラメータを使用してカスタム関数を開発する方法について説明します。
次の表に、Denodo のデータ型と Java クラスのマッピングを示します。
Denodo の型 |
パッケージ java.time の Java クラス |
---|---|
localdate |
java.time.LocalDate |
time |
java.time.LocalTime |
timestamp |
java.time.LocalDateTime |
timestamptz |
java.time.ZonedDateTime および java.util.Calendar |
intervalyearmonth |
java.time.Period |
intervaldaysecond |
java.time.Duration |
たとえば、intervalyearmonth 型の入力パラメータを持つカスタム関数を構築するには、その入力パラメータのクラスが java.time.Period でなければなりません。
timestamptz を返すためには、この関数は ZonedDateTime または Calendar を返す必要があります。
以下に、2 つのシグネチャを持つ関数の実装を示します。
@CustomElement(type = CustomElementType.VDPFUNCTION, name = "MY_CUSTOM_FUNCTION")
public class MyCustomFunction {
@CustomExecutor
/*
* Signature #1 has two input parameters: a ZonedDateTime and an Integer.
* The execution engine considers that the types of the input parameters are
* timestamptz and int respectively
*
* It returns a ZonedDateTime. The execution engines converts this into a timestamptz.
*/
public ZonedDateTime execute(@CustomParam(name = "input") ZonedDateTime input,
@CustomParam(name = "increment") Integer increment) {
if (input == null || increment == null) {
return null;
} else {
return input.plusHours(increment);
}
}
@CustomExecutor
/*
* Signature #2 has two input parameters: a Duration and a Long.
* The execution engine considers that the types of the input parameters are
* intervaldaysecond and long respectively
*
* It returns a Duration object. The execution engines converts this into a intervaldaysecond.
*/
public Duration execute(@CustomParam(name = "input") Duration input,
@CustomParam(name = "increment") Long increment) {
if (input == null || increment == null) {
return null;
} else {
return input.plusHours(increment);
}
}
}
クエリのロケールを受け取るには、java.util.Locale 型のパラメータを追加し、このパラメータに @CustomLocale のアノテーションを付けます。実行によって Locale が挿入され、クエリのロケールが取得されます。
以下に例を示します。
@CustomExecutor
public ZonedDateTime execute(@CustomParam(name = "input") ZonedDateTime input,
@CustomParam(name = "increment") Integer increment, @CustomLocale Locale locale) {...
以前のバージョンでは、Calendar クラスは date 型 (非推奨) にマップされています。Denodo 7.0 から、Calendar クラスは timestamptz にマップされるようになりました。Calendar を date にマップしたい場合は、VQL シェルから次のコマンドを実行して、サーバーを再起動してください。
SET 'com.denodo.vdb.compatibility.datetime.custom.mapCalendarToDateType' = 'true';