Archive for the ‘Cobra’ Category

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

    Posted on 4月 10th, 2013 by cx20

    Connector/NET(Cobra)

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

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

    use System
    use System.Data
    use MySql.Data.MySqlClient
     
    class Program
        def main is shared
            conStr = "server=localhost;user id=root;password=P@ssW0rd"
            sqlStr = "SELECT 'Hello, Connector/NET World!' AS Message"
     
            con = MySqlConnection(conStr)
            cmd = MySqlCommand(sqlStr, con)
            con.open()
     
            reader = cmd.executeReader()
            while reader.read()
                Console.writeLine( reader.getName(0) )
                Console.writeLine( "---------------------" )
                Console.writeLine( reader.getValue(0) )
     
            reader.close()
            con.close()

    コンパイル方法

    C:¥> cobra -compile Hello.cobra

    実行結果

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

    Posted on 2月 22nd, 2013 by cx20

    ODP.NET(Cobra)

    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

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

    use System
    use System.Data
    use Oracle.DataAccess.Client
     
    class Program
        def main is shared
            conStr = "Data Source=ORCL;User ID=scott;Password=tiger"
            sqlStr = "SELECT 'Hello, ODP.NET World!' AS Message FROM DUAL"
     
            con = OracleConnection(conStr)
            cmd = OracleCommand(sqlStr, con)
            con.open()
     
            reader = cmd.executeReader()
            while reader.read()
                Console.writeLine( reader.getName(0) )
                Console.writeLine( "---------------------" )
                Console.writeLine( reader.getValue(0) )
     
            reader.close()
            con.close()

    コンパイル方法

    C:¥> cobra -compile -reference:Oracle.DataAccess.dll Hello.cobra

    実行結果

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

    Posted on 2月 21st, 2013 by cx20

    ADO.NET(Cobra)

    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

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

    use System
    use System.Data
    use System.Data.OleDb
     
    class Program
        def main is shared
            conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hello.mdb;"
            sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
     
            con = OleDbConnection(conStr)
            cmd = OleDbCommand(sqlStr, con)
            con.open()
     
            reader = cmd.executeReader()
            while reader.read()
                Console.writeLine( reader.getName(0) )
                Console.writeLine( "---------------------" )
                Console.writeLine( reader.getValue(0) )
     
            reader.close()
            con.close()

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

    use System
    use System.Data
    use System.Data.OleDb
     
    class Program
        def main is shared
            conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hello.mdb;"
            sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
     
            con = OleDbConnection(conStr)
            cmd = OleDbCommand(sqlStr, con)
            con.open()
     
            reader = cmd.executeReader()
            while reader.read()
                Console.writeLine( reader.getName(0) )
                Console.writeLine( "---------------------" )
                Console.writeLine( reader.getValue(0) )
     
            reader.close()
            con.close()

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

    use System
    use System.Data
    use System.Data.SqlClient
     
    class Program
        def main is shared
            conStr = "SERVER=(local);DATABASE=master;UID=sa;PWD=P@ssW0rd;"
            sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
     
            con = SqlConnection(conStr)
            cmd = SqlCommand(sqlStr, con)
            con.open()
     
            reader = cmd.executeReader()
            while reader.read()
                Console.writeLine( reader.getName(0) )
                Console.writeLine( "---------------------" )
                Console.writeLine( reader.getValue(0) )
     
            reader.close()
            con.close()

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

    use System
    use System.Data
    use System.Data.OracleClient
     
    class Program
        def main is shared
            conStr = "Data Source=ORCL;User ID=scott;Password=tiger"
            sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL"
     
            con = OracleConnection(conStr)
            cmd = OracleCommand(sqlStr, con)
            con.open()
     
            reader = cmd.executeReader()
            while reader.read()
                Console.writeLine( reader.getName(0) )
                Console.writeLine( "---------------------" )
                Console.writeLine( reader.getValue(0) )
     
            reader.close()
            con.close()

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

    use System
    use System.Data
    use System.Data.Odbc
     
    class Program
        def main is shared
            conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd"
            sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
     
            con = OdbcConnection(conStr)
            cmd = OdbcCommand(sqlStr, con)
            con.open()
     
            reader = cmd.executeReader()
            while reader.read()
                Console.writeLine( reader.getName(0) )
                Console.writeLine( "---------------------" )
                Console.writeLine( reader.getValue(0) )
     
            reader.close()
            con.close()

    コンパイル方法(32bit OLE DB 使用時)

    C:¥> cobra -compile -sharp-args:"/platform:x86" Hello.cobra

    コンパイル方法(上記以外)

    C:¥> cobra -compile Hello.cobra

    実行結果

    Message
    ---------------------
    Hello, ADO.NET World!
  4. Hello, COM(Cobra) World!

    Posted on 2月 6th, 2013 by cx20

    COM(Cobra)

    COM(Component Object Model)はマイクロソフトの提唱するプログラム部品の仕様である。
    COM を用いて開発された部品であれば言語を問わず利用することができる。
    以下は Cobra による COM クライアントの例となっている。

    ソースコード(実行時バインディング)

    use System
    use System.Reflection
    use System.Runtime.InteropServices
     
    class Program
        def main is shared
            objType = Type.getTypeFromProgID("Shell.Application") 
            shell = Activator.createInstance(objType)
            param = @[ 0, "Hello, COM(Cobra) World!", 0, 36 ]
            folder = shell.getType.invokeMember( 
                "BrowseForFolder", BindingFlags.InvokeMethod, nil, shell, param )
            if folder  nil
                Marshal.releaseComObject(folder)
            Marshal.releaseComObject(shell)

    コンパイル方法(実行時バインディング)

    C:¥> cobra -compile Hello.cobra

    実行結果

    +----------------------------------------+
    |Browse For Folder                    [X]|
    +----------------------------------------+
    | Hello, COM(Cobra) Wolrd!               |
    |                                        |
    | +------------------------------------+ |
    | |[Windows]                           | |
    | | +[addins]                          | |
    | | +[AppCompat]                       | |
    | | +[AppPatch]                        | |
    | | +[assembly]                        | |
    | |     :                              | |
    | |     :                              | |
    | |     :                              | |
    | +------------------------------------+ |
    | [Make New Folder]    [  OK  ] [Cancel] |
    +----------------------------------------+
  5. Hello, Win32 API(Cobra) World!

    Posted on 6月 23rd, 2012 by cx20

    Win32 API(Cobra)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    Ver0.8 現在、Cobra には 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

    ソースコード(Cobra)

    use Win32Lib
     
    class Hello
     
        def main is shared
            Win32.messageBox( IntPtr.zero, "Hello, Win32 API(Cobra) World!", "Hello, World!", 0 )

    実行方法

    C:¥> cobra hello.cobra

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Cobra) World!
    ---------------------------
    OK   
    ---------------------------
  6. Hello, Windows Forms(Cobra) World!

    Posted on 6月 13th, 2012 by cx20

    Windows Forms(Cobra)

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

    ソースコード

    use System.Windows.Forms
    use System.Drawing
     
    class HelloForm inherits Form
     
        cue init
            base.init
            .size = Size( 640, 480 )
            .text = "Hello, World"
            label1 = Label()
            label1.size = Size( 320, 20 )
            label1.text = "Hello, Windows Forms(Cobra) World!"
            .controls.add( label1 )
     
    class Program
     
        def main is shared
            has STAThread
            form = HelloForm()
            Application.run( form )

    実行方法

    C:¥> cobra -target:winexe -compile Hello.cobra

    実行結果

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

    Posted on 1月 25th, 2012 by cx20

    Cobra

    Cobra は .NET Framework 向けのオブジェクト指向言語である。名前の由来は Python(ニシキヘビ)に対するものと思われる。構文は Python の特徴を引き継いでいる。
    C# 同様に、UNIX 環境向けの .NET Framework 互換プロジェクト「Mono」により他の OS でも動作させることができる。

    ソースコード

    #!/usr/bin/env cobra
    class Program
        def main is shared
            print 'Hello, Cobra World!'

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

    #!/usr/bin/env cobra
    use System
    class Program
        def main is shared
            Console.writeLine('Hello, Cobra World!')

    実行方法(スクリプトファイルを指定して実行)

    実際には、コンパイルされた後に実行される。

    $ cobra hello.cobra

    実行方法(実行権限を付与して実行)

    $ chmod +x ./hello.cobra
    $ ./hello.cobra

    コンパイル&実行方法(Mono)

    $ cobra -c hello.cobra
    $ mono ./hello.exe

    コンパイル&実行方法(.NET Framework)

    C:¥> cobra -c hello.cobra
    C:¥> hello

    実行結果

    Hello, Cobra World!