Guides/JDB ADO.NET Data Provider

From J Wiki
Jump to: navigation, search

How to ...

Overview paragraph. Elaborate.

Subheading 1

More info

Subheading 2

More info

See Also

  • Links ...

amrufon@gmail.com 2010 5 15 20 15 30.840289

Hmmm. Okay. I got Visual Studio .NET C# 2010 Express Edition installed in a WindowsXP VM in my MacBook for a while now but I didn't have a project to do so. So, this afternoon, I've got bored enought playing WOW that I've decided to start working on an ADO.NET data provider for JDB. The idea is that I'll be using the J.NET wrapper class over J and this data provider will communicate with it.

This is what I got so far (this compiles by the way):

Connection.cs - handles connection to JDB

/* *
 * NB. =========================================================
 * NB. amrufon@gmail.com	2010 5 15 19 38 19.441
 * */

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace JDB
{
    public class Connection  : IDbConnection
    {
        //NB. =========================================================
        //NB. Class variables
        string connectionString = "";
        int connectionTimeOut = 0;
        string database = "";
        ConnectionState state = ConnectionState.Closed;
        J jSession = null;

        //NB. =========================================================
        //NB. Class properties
        public string ConnectionString
        {
            get { return this.connectionString; }
            set { this.connectionString = value; }
        }

        //NB. ---------------------------------------------------------
        //NB. There is NO connection timeout, so a read only attribute
        public int ConnectionTimeout { get { return this.connectionTimeOut; } }

        //NB. ---------------------------------------------------------
        //NB. The database name is set through the connection string
        public string Database { get { return this.database; } }

        //NB. ---------------------------------------------------------
        //NB. Connection state is set by the Open and Close methods
        public ConnectionState State { get { return this.state; } }

        //NB. =========================================================
        //NB. Class methods

        public void Open()
        {
            // TODO: Add JDB opening codes here
            this.state = ConnectionState.Open;
        }

        public void Close()
        {
            // TODO: Add JDB closing codes here
            this.state = ConnectionState.Closed;
        }

        public IDbTransaction BeginTransaction()
        {
            throw new System.NotSupportedException();
        }

        public IDbTransaction BeginTransaction(IsolationLevel iso)
        {
            throw new System.NotSupportedException();
        }

        public void ChangeDatabase(string databaseName)
        {
            throw new System.NotSupportedException();
        }

        public IDbCommand CreateCommand()
        {
            IDbCommand idbCommand = (IDbCommand)(new JDB.Command());
            idbCommand.Connection = this;
            return idbCommand;
        }

        // Constructor
        public Connection(string ConnectionString)
        {
            // NOTE: Must add parser to breakdown the connection string
            this.connectionString = ConnectionString;

            // Initialize your J session
            jSession = new J();
        }

        //NB. ---------------------------------------------------------
        //NB. Dispose won't be doing anything
        public void Dispose() { }
    }
}

J.cs - handles communication with the J Session

/* *
 * NB. =========================================================
 * NB. amrufon@gmail.com	2010 5 15 21 1 12.527
 *
 * NOTE: I based this module from Raul Miller's session.cs which can be found here:
 * http://www.jsoftware.com/jwiki/Guides/J%20VB.NET?action=AttachFile&do=view&target=Session.cs.txt
 * */
using System;
using System.Collections.Generic;
using System.Text;
using JEXEServerLib;

namespace JDB
{
    class J
    {
        JEXEServer jObject;

        public J()
        {
            this.initialize();
        }


        public object this[string name]
        {
            get
            {
                object retVal = null;
                this.translateError(this.jObject.GetB(name, out retVal));
                return retVal;
            }
            set { this.translateError(this.jObject.SetB(name, ref value)); }
        }

        void initialize()
        {
            this.jObject = new JEXEServer();
            this.jObject.Quit();
            this.jObject.Do("BINPATH_z_=: 1!:46 ''");
            this.jObject.Do("ARGV_z_=: ,<'j.exe'");
            this.jObject.Do("3 :'0!:0 <BINPATH,''\\profile.ijs'''0");
            this.jObject.Log(1);
            this.jObject.Show(1);
            this.jObject.Do("NB. Welcome to the JDB Server! - amrufon@gmail.com");
        }

        void translateError(int errorNo)
        {
            if (errorNo > 0) {
                object errorMessage = null;
                this.jObject.ErrorTextB(errorNo,out errorMessage);
                throw new Exception(Convert.ToString(errorMessage));
            }
        }
    }
}

Command.cs - epresents an SQL statement that is executed by JDB NOTE: Class incomplete ... will not compile YET (will fix after I finish my breakfast)

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace JDB
{
    public class Command : IDbCommand
    {
        string jdbCommand = "";
        JDB.Connection jdbConnection = null;

        // Properties



        // Overriden methods
        public void Cancel()
        {
            throw new System.NotSupportedException();
        }

        // The user can call this if they like, but were not doing anything
        public void Prepare()
        {
            // Even though were not supporting this, we still check if the
            // connection is open
            if (this.jdbConnection == null ||
                this.jdbConnection.State != ConnectionState.Open)
                throw new InvalidOperationException("An open JDB connection is required");
        }

        // Constructors
        public Command() { }

        public Command(string commandText)
        {
            this.jdbCommand = commandText;
        }

        public IDbDataParameter CreateParameter()
        {
            throw new System.NotSupportedException();
        }

        //Executes an SQL statement against the Connection object
        //and returns the number of rows affected.
        public int ExecutionNonQuery()
        {
            int noOfRecordsAffected = 0;

            if (this.jdbConnection == null ||
                this.jdbConnection.State != ConnectionState.Open)
                throw new InvalidOperationException("An open JDB connection is required");

            // TODO: I have not yet created the READER class, so still a placeholder
            //JDB.DataReader dataReader = new JDB.DataReader(this.jdbCommand);
            //noOfRecordsAffected = dataReader.RecordsAffected;

            return noOfRecordsAffected;
        }

        //public IDataReader ExecuteReader()
        //{
        //    if (this.jdbConnection == null ||
        //        this.jdbConnection.State != ConnectionState.Open)
        //        throw new InvalidOperationException("An open JDB connection is required");
        //}

        public Command(string commandText, JDB.Connection connectionJDB)
        {
            this.jdbConnection = connectionJDB;
            this.jdbCommand = commandText;
        }

        public void Dispose() { }
    }
}