HHeLiBeXの日記 正道編

日々の記憶の記録とメモ‥

TIMESTAMP のリテラル

TIMESTAMP のリテラルを記述する際に、DB2 で苦労した記憶が唐突によみがえってきたので、手元にある DBMS を比べてみた。

これまで Epictetus に頼ってきた Firebird へのアクセスを自分で行おうということで、わざわざ Java プログラムを作って検証。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

public class Main {

    /**
     * @param args
     * @throws ClassNotFoundException 
     * @throws SQLException 
     */
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String driverCls = args[0];
        String url = args[1];
        String userId = args[2];
        String password = args[3];

        Class.forName(driverCls);

        Connection connection = DriverManager.getConnection(url, userId, password);
        try {
            test(connection);
        } finally {
            connection.close();
        }
    }

    private static void test(Connection connection) {
        try {
            {
                PreparedStatement pstmt = connection.prepareStatement("CREATE TABLE HOGE(id VARCHAR(32), val TIMESTAMP)");
                try {
                    pstmt.executeUpdate();
                } finally {
                    pstmt.close();
                }
            }
            try {
                {
                    String[] values = {
                        "TIMESTAMP '2009-12-22 16:06:32'",
                        "TIMESTAMP('2009-12-23 16:06:32')",
                    };
                    for (int i = 0; i < values.length; ++i) {
                        try {
                            PreparedStatement pstmt = connection.prepareStatement("INSERT INTO HOGE(id, val) VALUES(?, " + values[i] + ")");
                            try {
                                pstmt.setString(1, values[i]);
                                pstmt.executeUpdate();
                            } finally {
                                pstmt.close();
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                {
                    PreparedStatement pstmt = connection.prepareStatement("SELECT id, val FROM HOGE");
                    try {
                        ResultSet resultSet = pstmt.executeQuery();
                        for (int i = 0; resultSet.next(); ++i) {
                            String id = resultSet.getString(1);
                            Timestamp timestamp = resultSet.getTimestamp(2);
                            System.out.printf("%2d: %-32s %s\n", i, id, timestamp);
                        }
                    } finally {
                        pstmt.close();
                    }
                }
            } finally {
                PreparedStatement pstmt = connection.prepareStatement("DROP TABLE HOGE");
                try {
                    pstmt.executeUpdate();
                } finally {
                    pstmt.close();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

ちなみに、発行しているクエリは次のもの。

CREATE TABLE HOGE(id VARCHAR(32), val TIMESTAMP) ;
INSERT INTO HOGE(id, val) VALUES('TIMESTAMP ''2009-12-22 16:06:32''', TIMESTAMP '2009-12-22 16:06:32') ;
INSERT INTO HOGE(id, val) VALUES('TIMESTAMP(''2009-12-23 16:06:32'')', TIMESTAMP('2009-12-23 16:06:32')) ;
SELECT * FROM HOGE ;
DROP TABLE HOGE ;

で、実行結果。
IBM DB2 Express-C v9.1.2

 0: TIMESTAMP('2009-12-23 16:06:32') 2009-12-23 16:06:32.0

IBM DB2 Express-C v9.5.2

 0: TIMESTAMP('2009-12-23 16:06:32') 2009-12-23 16:06:32.0

IBM DB2 Express-C v9.7.0

 0: TIMESTAMP '2009-12-22 16:06:32'  2009-12-22 16:06:32.0
 1: TIMESTAMP('2009-12-23 16:06:32') 2009-12-23 16:06:32.0

Oracle Database 10g Express Edition

 0: TIMESTAMP '2009-12-22 16:06:32'  2009-12-22 16:06:32.0

PostgreSQL v8.3

 0: TIMESTAMP '2009-12-22 16:06:32'  2009-12-22 16:06:32.0

MySQL v5.1

 0: TIMESTAMP '2009-12-22 16:06:32'  2009-12-22 16:06:32.0
 1: TIMESTAMP('2009-12-23 16:06:32') 2009-12-23 16:06:32.0

Firebird v2.1.0

 0: TIMESTAMP '2009-12-22 16:06:32'  2009-12-22 16:06:32.0

普通、TIMESTAMP のリテラルに丸括弧はつかないので DB2 で苦労したのだが、気づいてみれば、DB2 v9.7 では丸括弧がつかないほうもサポートされるようになったようだ。
MySQLDB2 v9.7 と同様に2通りの記述ができるのは知らんかった。