Archive for the ‘ClojureCLR’ Category

  1. Hello, Connector/NET(ClojureCLR) World!

    Posted on 4月 15th, 2013 by cx20

    Connector/NET(ClojureCLR)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は ClojureCLR による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(ClojureCLR + Connector/NET + MySQL)

    (System.Reflection.Assembly/LoadWithPartialName "System.Data")
    (System.Reflection.Assembly/LoadWithPartialName "MySql.Data")
     
    (import '(MySql.Data.MySqlClient MySqlConnection))
    (import '(MySql.Data.MySqlClient MySqlCommand))
    (import '(MySql.Data.MySqlClient MySqlDataReader))
     
    (def conStr "server=localhost;user id=root;password=P@ssW0rd")
    (def sqlStr "SELECT 'Hello, Connector/NET World!' AS Message")
    (def con (MySqlConnection. conStr))
    (def cmd (MySqlCommand. sqlStr con))
    (.Open con)
    (def reader (.ExecuteReader cmd))
    (if (.Read reader)
        (do
            (System.Console/WriteLine (.GetName reader 0))
            (System.Console/WriteLine "---------------------")
            (System.Console/WriteLine (.GetValue reader 0))
        )
    )
    (.Close reader)
    (.Close con)

    コンパイル方法

    C:¥> Clojure.Main hello.clj

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  2. Hello, ODP.NET(ClojureCLR) World!

    Posted on 2月 18th, 2013 by cx20

    ODP.NET(ClojureCLR)

    ODP.NET(Oracle Data Provider for .NET)は、.NET ベースの Oracle Database 接続用 API である。ODAC(Oracle Data Access Component)と呼ばれるパッケージに含まれる。
    .NET 環境での Oracle Database 用データプロバイダとしては、マイクロソフト社が提供する「Microsoft Oracle Client」とオラクル社が提供する「ODP.NET」があるが、現在、「Microsoft Oracle Client」はマイクロソフト社自身が非推奨としており、今後は ODP.NET の使用が推奨されている。

    データプロバイダ 説明
    System.Data.OracleClient .NET Framework Data Provider for Oracle
    Oracle.DataAccess.Client Oracle Data Provider for .NET

    ソースコード(ClojureCLR + ODP.NET + Oracle)

    (System.Reflection.Assembly/LoadWithPartialName "System.Data")
    (System.Reflection.Assembly/LoadWithPartialName "Oracle.DataAccess")
     
    (import '(Oracle.DataAccess.Client OracleConnection))
    (import '(Oracle.DataAccess.Client OracleCommand))
    (import '(Oracle.DataAccess.Client OracleDataReader))
     
    (def conStr "Data Source=ORCL;User ID=scott;Password=tiger")
    (def sqlStr "SELECT 'Hello, ODP.NET World!' AS Message FROM DUAL")
    (def con (OracleConnection. conStr))
    (def cmd (OracleCommand. sqlStr con))
    (.Open con)
    (def reader (.ExecuteReader cmd))
    (if (.Read reader)
        (do
            (System.Console/WriteLine (.GetName reader 0))
            (System.Console/WriteLine "---------------------")
            (System.Console/WriteLine (.GetValue reader 0))
        )
    )
    (.Close reader)
    (.Close con)

    コンパイル方法

    C:¥> Clojure.Main hello.clj

    実行結果

    MESSAGE
    ---------------------
    Hello, ODP.NET World!
  3. Hello, ADO.NET(ClojureCLR) World!

    Posted on 2月 17th, 2013 by cx20

    ADO.NET(ClojureCLR)

    ADO.NET(ActiveX Data Objects .NET)は、ADO の後継で .NET ベースの DBMS 接続用 API である。
    .NET データプロバイダを介することで様々な DBMS への接続が可能となっている。
    .NET Framework 標準で使用できる .NET データプロバイダとしては、以下のようなプロバイダがある。

    データプロバイダ名 説明
    System.Data.SqlClient Microsoft SQL Server
    System.Data.OleDb OLE DB
    System.Data.Odbc ODBC
    System.Data.OracleClient Oracle

    ソースコード(ClojureCLR + ADO.NET + OLE DB + Jet データベース)

    (System.Reflection.Assembly/LoadWithPartialName "System.Data")
     
    (import '(System.Data.OleDb OleDbConnection))
    (import '(System.Data.OleDb OleDbCommand))
    (import '(System.Data.OleDb OleDbDataReader))
     
    (def conStr "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hello.mdb")
    (def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message")
    (def con (OleDbConnection. conStr))
    (def cmd (OleDbCommand. sqlStr con))
    (.Open con)
    (def reader (.ExecuteReader cmd))
    (if (.Read reader)
        (do
            (System.Console/WriteLine (.GetName reader 0))
            (System.Console/WriteLine "---------------------")
            (System.Console/WriteLine (.GetValue reader 0))
        )
    )
    (.Close reader)
    (.Close con)

    ソースコード(ClojureCLR + ADO.NET + OLE DB + ACE データベース)

    (System.Reflection.Assembly/LoadWithPartialName "System.Data")
     
    (import '(System.Data.OleDb OleDbConnection))
    (import '(System.Data.OleDb OleDbCommand))
    (import '(System.Data.OleDb OleDbDataReader))
     
    (def conStr "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=hello.accdb")
    (def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message")
    (def con (OleDbConnection. conStr))
    (def cmd (OleDbCommand. sqlStr con))
    (.Open con)
    (def reader (.ExecuteReader cmd))
    (if (.Read reader)
        (do
            (System.Console/WriteLine (.GetName reader 0))
            (System.Console/WriteLine "---------------------")
            (System.Console/WriteLine (.GetValue reader 0))
        )
    )
    (.Close reader)
    (.Close con)

    ソースコード(ClojureCLR + ADO.NET + SQL Server)

    (System.Reflection.Assembly/LoadWithPartialName "System.Data")
     
    (import '(System.Data.SqlClient SqlConnection))
    (import '(System.Data.SqlClient SqlCommand))
    (import '(System.Data.SqlClient SqlDataReader))
     
    (def conStr "SERVER=(local);DATABASE=master;UID=sa;PWD=P@ssW0rd")
    (def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message")
    (def con (SqlConnection. conStr))
    (def cmd (SqlCommand. sqlStr con))
    (.Open con)
    (def reader (.ExecuteReader cmd))
    (if (.Read reader)
        (do
            (System.Console/WriteLine (.GetName reader 0))
            (System.Console/WriteLine "---------------------")
            (System.Console/WriteLine (.GetValue reader 0))
        )
    )
    (.Close reader)
    (.Close con)

    ソースコード(ClojureCLR + ADO.NET + Oracle)

    (System.Reflection.Assembly/LoadWithPartialName "System.Data")
    (System.Reflection.Assembly/LoadWithPartialName "System.Data.OracleClient")
     
    (import '(System.Data.OracleClient OracleConnection))
    (import '(System.Data.OracleClient OracleCommand))
    (import '(System.Data.OracleClient OracleDataReader))
     
    (def conStr "Data Source=ORCL;User ID=scott;Password=tiger")
    (def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL")
    (def con (OracleConnection. conStr))
    (def cmd (OracleCommand. sqlStr con))
    (.Open con)
    (def reader (.ExecuteReader cmd))
    (if (.Read reader)
        (do
            (System.Console/WriteLine (.GetName reader 0))
            (System.Console/WriteLine "---------------------")
            (System.Console/WriteLine (.GetValue reader 0))
        )
    )
    (.Close reader)
    (.Close con)

    ソースコード(ClojureCLR + ADO.NET + ODBC + MySQL)

    (System.Reflection.Assembly/LoadWithPartialName "System.Data")
     
    (import '(System.Data.Odbc OdbcConnection))
    (import '(System.Data.Odbc OdbcCommand))
    (import '(System.Data.Odbc OdbcDataReader))
     
    (def conStr "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd")
    (def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL")
    (def con (OdbcConnection. conStr))
    (def cmd (OdbcCommand. sqlStr con))
    (.Open con)
    (def reader (.ExecuteReader cmd))
    (if (.Read reader)
        (do
            (System.Console/WriteLine (.GetName reader 0))
            (System.Console/WriteLine "---------------------")
            (System.Console/WriteLine (.GetValue reader 0))
        )
    )
    (.Close reader)
    (.Close con)

    実行方法

    C:¥> Clojure.Main hello.clj

    実行結果

    Message
    ---------------------
    Hello, ADO.NET World!
  4. Hello, Win32 API(ClojureCLR) World!

    Posted on 6月 21st, 2012 by cx20

    Win32 API(ClojureCLR)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    ClojureCLR には Win32 API を直接呼び出す機能は実装されていないが、C# を経由することで、Win32 API を呼び出すことが可能となっている。

    ソースコード(C#)

    using System;
    using System.Runtime.InteropServices;
     
    namespace Win32Lib
    {
        public class Win32
        {
             [DllImport("user32.dll", CharSet=CharSet.Auto)]
             public extern static uint MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
        }
    }

    コンパイル方法(C#)

    C:¥> csc /target:library Win32Lib.cs

    ソースコード(ClojureCLR)

    (System.Reflection.Assembly/LoadFrom "Win32Lib.dll")
    (Win32Lib.Win32/MessageBox 0  "Hello, Win32 API(ClojureCLR) World!" "Hello, World!", 0 )

    実行方法

    C:¥> Clojure.Main hello.clj

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(ClojureCLR) World!
    ---------------------------
    OK   
    ---------------------------
  5. Hello, Windows Forms(ClojureCLR) World!

    Posted on 6月 11th, 2012 by cx20

    Windows Forms(ClojureCLR)

    Windows フォーム(Windows Forms)は .NET Framework におけるユーザーインターフェイス基盤である。Windows アプリケーションにおけるウィンドウやダイアログに対応する。
    以下は ClojureCLR における Windows フォーム の例となっている。

    ソースコード

    (System.Reflection.Assembly/LoadWithPartialName "System.Windows.Forms")
     
    (import '(System.Windows.Forms Form))
    (import '(System.Windows.Forms Label))
    (import '(System.Windows.Forms Application))
     
    (def form (Form.))
    (doto form
      (.set_Size (new System.Drawing.Size 640 480))
      (.set_Text "Hello, World!")
    )
    (def label1 (Label.))
    (doto label1
      (.set_Size (new System.Drawing.Size 320 20))
      (.set_Text "Hello, Windows Forms(ClojureCLR) World!")
    )
    (doto (.Controls form)
    (.Add label1))
     
    (. Application Run form)

    実行方法

    C:¥> Clojure.Main Hello.clj

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Windows Forms(ClojureCLR) World!   |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  6. Hello, ClojureCLR World!

    Posted on 1月 23rd, 2012 by cx20

    ClojureCLR

    ClojureCLR は Lisp の方言の一つ Clojure の .NET 実装である。
    Clojure の構文が使える他、.NET のライブラリが利用できる。

    ソースコード

    (println "Hello, ClojureCLR World!")

    ソースコード(.NET ライブラリを使用した場合)

    (System.Console/WriteLine "Hello, Clojure World!")

    実行方法(.NET Framework)

    C:¥> Clojure.Main hello.clj

    実行結果

    Hello, ClojureCLR World!