2004-03-12 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[mono.git] / mcs / class / System.Data / System.Data.OleDb / OleDbCommand.cs
index 15cc1a2bda4c077bf44e90a0e48705aebd9c0b17..8de3e49794cb44f52eef899a778651a9023631c5 100644 (file)
@@ -1,15 +1,19 @@
 //
 // System.Data.OleDb.OleDbCommand
 //
-// Author:
+// Authors:
 //   Rodrigo Moya (rodrigo@ximian.com)
+//   Tim Coleman (tim@timcoleman.com)
 //
 // Copyright (C) Rodrigo Moya, 2002
+// Copyright (C) Tim Coleman, 2002
 //
 
 using System.ComponentModel;
 using System.Data;
 using System.Data.Common;
+using System.Collections;
+using System.Runtime.InteropServices;
 
 namespace System.Data.OleDb
 {
@@ -18,117 +22,124 @@ namespace System.Data.OleDb
        /// </summary>
        public sealed class OleDbCommand : Component, ICloneable, IDbCommand
        {
-               private string m_command_string = null;
-               private OleDbConnection m_connection = null;
-               private OleDbTransaction m_transaction = null;
-               private int m_timeout = 30; // 30 is the default, as per .NET docs
-               private CommandType m_type = CommandType.Text;
-               private OleDbParameterCollection m_parameters;
-
-               /*
-                * Constructors
-                */
-               
+               #region Fields
+
+               string commandText;
+               int timeout;
+               CommandType commandType;
+               OleDbConnection connection;
+               OleDbParameterCollection parameters;
+               OleDbTransaction transaction;
+               bool designTimeVisible;
+               OleDbDataReader dataReader;
+               CommandBehavior behavior;
+               IntPtr gdaCommand;
+
+               #endregion // Fields
+
+               #region Constructors
+
                public OleDbCommand ()
                {
-                       m_parameters = new OleDbParameterCollection ();
+                       commandText = String.Empty;
+                       timeout = 30; // default timeout per .NET
+                       commandType = CommandType.Text;
+                       connection = null;
+                       parameters = new OleDbParameterCollection ();
+                       transaction = null;
+                       designTimeVisible = false;
+                       dataReader = null;
+                       behavior = CommandBehavior.Default;
+                       gdaCommand = IntPtr.Zero;
                }
 
-               public OleDbCommand (string s) : this ()
+               public OleDbCommand (string cmdText) : this ()
                {
-                       m_command_string = s;
+                       CommandText = cmdText;
                }
 
-               public OleDbCommand (string s, OleDbConnection cnc) : this ()
+               public OleDbCommand (string cmdText, OleDbConnection connection)
+                       : this (cmdText)
                {
-                       m_command_string = s;
-                       m_connection = cnc;
+                       Connection = connection;
                }
 
-               public OleDbCommand (string s,
-                                    OleDbConnection cnc,
-                                    OleDbTransaction xtrans) : this ()
+               public OleDbCommand (string cmdText,
+                                    OleDbConnection connection,
+                                    OleDbTransaction transaction) : this (cmdText, connection)
                {
-                       m_command_string = s;
-                       m_connection = cnc;
-                       m_transaction = xtrans;
+                       this.transaction = transaction;
                }
 
-               /*
-                * Properties
-                */
-               
-               string IDbCommand.CommandText
+               #endregion // Constructors
+
+               #region Properties
+       
+               public string CommandText 
                {
                        get {
-                               return m_command_string;
+                               return commandText;
                        }
-                       set {
-                               m_command_string = value;
+                       set { 
+                               commandText = value;
                        }
                }
 
-               int IDbCommand.CommandTimeout
-               {
+               public int CommandTimeout {
                        get {
-                               return m_timeout;
+                               return timeout;
                        }
                        set {
-                               m_timeout = value;
+                               timeout = value;
                        }
                }
 
-               CommandType IDbCommand.CommandType
-               {
+               public CommandType CommandType { 
                        get {
-                               return m_type;
+                               return commandType;
                        }
                        set {
-                               m_type = value;
+                               commandType = value;
                        }
                }
 
-               IDbConnection IDbCommand.Connection
-               {
+               public OleDbConnection Connection { 
                        get {
-                               return m_connection;
+                               return connection;
                        }
                        set {
-                               m_connection = (OleDbConnection) value;
+                               connection = value;
                        }
                }
 
-               public bool DesignTimeVisible
-               {
-                       [MonoTODO]
+               public bool DesignTimeVisible { 
                        get {
-                               throw new NotImplementedException ();
+                               return designTimeVisible;
                        }
-                       [MonoTODO]
                        set {
-                               throw new NotImplementedException ();
+                               designTimeVisible = value;
                        }
                }
 
-               IDataParameterCollection IDbCommand.Parameters
-               {
+               public OleDbParameterCollection Parameters {
                        get {
-                               return m_parameters;
+                               return parameters;
+                       }
+                       set {
+                               parameters = value;
                        }
                }
 
-               IDbTransaction IDbCommand.Transaction
-               {
+               public OleDbTransaction Transaction {
                        get {
-                               return m_transaction;
+                               return transaction;
                        }
                        set {
-                               m_transaction = (OleDbTransaction) value;
+                               transaction = value;
                        }
                }
 
-               UpdateRowSource IDbCommand.UpdatedRowSource
-               {
+               public UpdateRowSource UpdatedRowSource { 
                        [MonoTODO]
                        get {
                                throw new NotImplementedException ();
@@ -139,63 +150,186 @@ namespace System.Data.OleDb
                        }
                }
 
-               /*
-                * Methods
-                */
-               
+               IDbConnection IDbCommand.Connection {
+                       get {
+                               return Connection;
+                       }
+                       set {
+                               Connection = (OleDbConnection) value;
+                       }
+               }
+
+               IDataParameterCollection IDbCommand.Parameters  {
+                       get {
+                               return Parameters;
+                       }
+               }
+
+               IDbTransaction IDbCommand.Transaction  {
+                       get {
+                               return Transaction;
+                       }
+                       set {
+                               Transaction = (OleDbTransaction) value;
+                       }
+               }
+
+               #endregion // Properties
+
+               #region Methods
+
                [MonoTODO]
-               void IDbCommand.Cancel ()
+               public void Cancel () 
                {
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
-               object ICloneable.Clone ()
+               public OleDbParameter CreateParameter ()
                {
-                       throw new NotImplementedException ();
+                       return new OleDbParameter ();
                }
-               
+
                IDbDataParameter IDbCommand.CreateParameter ()
                {
-                       return new OleDbParameter ();
+                       return CreateParameter ();
+               }
+               
+               [MonoTODO]
+               protected override void Dispose (bool disposing)
+               {
+                       throw new NotImplementedException ();
                }
 
+               private void SetupGdaCommand ()
+               {
+                       GdaCommandType type;
+                       
+                       switch (commandType) {
+                       case CommandType.TableDirect :
+                               type = GdaCommandType.Table;
+                               break;
+                       case CommandType.StoredProcedure :
+                               type = GdaCommandType.Procedure;
+                               break;
+                       case CommandType.Text :
+                       default :
+                               type = GdaCommandType.Sql;
+                               break;
+                       }
+                       
+                       if (gdaCommand != IntPtr.Zero) {
+                               libgda.gda_command_set_text (gdaCommand, commandText);
+                               libgda.gda_command_set_command_type (gdaCommand, type);
+                       } else {
+                               gdaCommand = libgda.gda_command_new (commandText, type, 0);
+                       }
+
+                       //libgda.gda_command_set_transaction 
+               }
                
-               int IDbCommand.ExecuteNonQuery ()
+               public int ExecuteNonQuery ()
                {
-                       if (m_command_string == null)
-                               return -1;
+                       if (connection == null)
+                               throw new InvalidOperationException ("connection == null");
+                       if (connection.State == ConnectionState.Closed)
+                               throw new InvalidOperationException ("State == Closed");
+                       // FIXME: a third check is mentioned in .NET docs
 
-                       // FIXME
-                       return 0;
+                       IntPtr gdaConnection = connection.GdaConnection;
+                       IntPtr gdaParameterList = parameters.GdaParameterList;
+
+                       SetupGdaCommand ();
+                       return libgda.gda_connection_execute_non_query (gdaConnection,
+                                                                       (IntPtr) gdaCommand,
+                                                                       gdaParameterList);
+               }
+
+               public OleDbDataReader ExecuteReader ()
+               {
+                       return ExecuteReader (CommandBehavior.Default);
                }
 
-               [MonoTODO]
                IDataReader IDbCommand.ExecuteReader ()
                {
-                       throw new NotImplementedException ();
+                       return ExecuteReader ();
+               }
+
+               public OleDbDataReader ExecuteReader (CommandBehavior behavior)
+               {
+                       ArrayList results = new ArrayList ();
+                       IntPtr rs_list;
+                       GdaList glist_node;
+
+                       if (connection.State != ConnectionState.Open)
+                               throw new InvalidOperationException ("State != Open");
+
+                       this.behavior = behavior;
+
+                       IntPtr gdaConnection = connection.GdaConnection;
+                       IntPtr gdaParameterList = parameters.GdaParameterList;
+
+                       /* execute the command */
+                       SetupGdaCommand ();
+                       rs_list = libgda.gda_connection_execute_command (
+                               gdaConnection,
+                               gdaCommand,
+                               gdaParameterList);
+                       if (rs_list != IntPtr.Zero) {
+                               glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
+
+                               while (glist_node != null) {
+                                       results.Add (glist_node.data);
+                                       if (glist_node.next == IntPtr.Zero)
+                                               break;
+
+                                       glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
+                                                                                      typeof (GdaList));
+                               }
+                               dataReader = new OleDbDataReader (this, results);
+                               dataReader.NextResult ();
+                       }
+
+                       return dataReader;
                }
 
-               [MonoTODO]
                IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
                {
-                       throw new NotImplementedException ();
+                       return ExecuteReader (behavior);
                }
                
+               public object ExecuteScalar ()
+               {
+                       SetupGdaCommand ();
+                       OleDbDataReader reader = ExecuteReader ();
+                       if (reader == null) {
+                               return null;
+                       }
+                       if (!reader.Read ()) {
+                               reader.Close ();
+                               return null;
+                       }
+                       object o = reader.GetValue (0);
+                       reader.Close ();
+                       return o;
+               }
+
                [MonoTODO]
-               object IDbCommand.ExecuteScalar ()
+               object ICloneable.Clone ()
                {
-                       throw new NotImplementedException ();
+                       throw new NotImplementedException ();   
                }
 
-               void IDbCommand.Prepare ()
+               [MonoTODO]
+               public void Prepare ()
                {
-                       // FIXME: prepare string with parameters
+                       throw new NotImplementedException ();   
                }
 
                public void ResetCommandTimeout ()
                {
-                       m_timeout = 30;
+                       timeout = 30;
                }
+
+               #endregion
        }
 }