Example #1
0
        public void CommandType()
        {
            using (HsqlConnection connection = NewConnection())
                using (HsqlCommand testSubject = connection.CreateCommand())
                {
                    CommandType actual = testSubject.CommandType;

                    Assert.AreEqual(System.Data.CommandType.Text, actual);

                    testSubject.CommandType = System.Data.CommandType.StoredProcedure;

                    Assert.AreEqual(System.Data.CommandType.StoredProcedure, actual);

                    testSubject.CommandType = System.Data.CommandType.TableDirect;

                    Assert.AreEqual(System.Data.CommandType.TableDirect, actual);

                    Common.HsqlWarningEventArgs warning = null;

                    testSubject.Warning += delegate(object sender, Common.HsqlWarningEventArgs args) {
                        warning = args;
                    };

                    testSubject.CommandType = (CommandType)100;

                    Assert.IsNotNull(warning);

                    Assert.AreEqual(System.Data.CommandType.Text, actual);
                }
        }
Example #2
0
        public void DeriveParameters()
        {
            using (HsqlConnection connection = NewConnection())
                using (HsqlCommand testSubject = connection.CreateCommand())
                {
                    testSubject.CommandText = "call 1 + cast(? as integer)";
                    testSubject.CommandType = global::System.Data.CommandType.StoredProcedure;
                    testSubject.DeriveParameters();

                    HsqlParameterCollection parameters = testSubject.Parameters;

                    Assert.AreEqual(1, parameters.Count);

                    HsqlParameter parameter = parameters[0];

                    Assert.AreEqual(DbType.Int32, parameter.DbType);
                    Assert.AreEqual(ParameterDirection.Input, parameter.Direction);
                    Assert.AreEqual(false, parameter.IsNullable);
                    Assert.AreEqual(0, parameter.Offset);
                    Assert.AreEqual("@p1", parameter.ParameterName);
                    Assert.AreEqual(10, parameter.Precision);
                    Assert.AreEqual(HsqlProviderType.Integer, parameter.ProviderType);
                    Assert.AreEqual(0, parameter.Scale);
                    Assert.AreEqual(4, parameter.Size);
                    Assert.AreEqual("", parameter.SourceColumn);
                    Assert.AreEqual(false, parameter.SourceColumnNullMapping);
                    Assert.AreEqual(DataRowVersion.Default, parameter.SourceVersion);
                    Assert.AreEqual("NULL", parameter.ToSqlLiteral());
                    Assert.AreEqual(null, parameter.Value);
                }
        }
Example #3
0
 public void Cancel()
 {
     using (HsqlConnection connection = NewConnection())
         using (HsqlCommand testSubject = connection.CreateCommand())
         {
             testSubject.Cancel(); // no-op.
         }
 }
Example #4
0
 public void AddBatch()
 {
     using (HsqlConnection connection = NewConnection())
         using (HsqlCommand testSubject = connection.CreateCommand())
         {
             testSubject.AddBatch();
             //testSubject.C
         }
 }
        /// <summary>
        /// Returns the collection of currently valid initial schema names,
        /// given the specified context.
        /// </summary>
        /// <param name="context">
        /// An <see cref="ITypeDescriptorContext"></see> whose <c>Instance</c>
        /// property supplies the <c>HsqlConnectionStringBuilder</c> use to
        /// connect to a data source to retrieve the currently valid initial
        /// schema names.
        /// </param>
        /// <returns>
        /// A <see cref="TypeConverter.StandardValuesCollection"/> that holds
        /// collection of currently valid initial schema names.
        /// </returns>
        public override TypeConverter.StandardValuesCollection GetStandardValues(
            ITypeDescriptorContext context)
        {
            if (!IsStandardValuesSupported(context))
            {
                return(null);
            }

            List <string> values = new List <string>();

            try
            {
                HsqlConnectionStringBuilder builder
                    = (HsqlConnectionStringBuilder)context.Instance;

                // TODO:  this is sub-optimal, but is currently the best (only?)
                // solution to the problem of how to avoid creating and/or
                // leaving open embedded database instances.
                if (IsEmbeddedProtocol(builder))
                {
                    builder = new HsqlConnectionStringBuilder(
                        builder.ConnectionString);

                    builder.AutoShutdown = true;
                    builder.IfExists     = true;
                }

                using (HsqlConnection connection = new HsqlConnection())
                {
                    connection.ConnectionString = builder.ConnectionString;

                    using (HsqlCommand command = new HsqlCommand(
                               connection,
                               SchemaQuery))
                    {
                        connection.Open();

                        using (HsqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                values.Add(reader.GetString(0));
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
#if DEBUG
                Debug.WriteLine(exception);
#endif
            }

            return(new TypeConverter.StandardValuesCollection(values));
        }
Example #6
0
 public void CreateParameter()
 {
     using (HsqlConnection connection = NewConnection())
         using (HsqlCommand testSubject = connection.CreateCommand())
         {
             HsqlParameter parameter    = testSubject.CreateParameter();
             IDbParmeter   idbparameter = (testSubject as IDbCommand).CreateParameter();
             DbParameter   dbparameter  = (testSubject as DbCommand).CreateParameter();
         }
 }
Example #7
0
        public void CommandTimeout()
        {
            using (HsqlConnection connection = NewConnection())
                using (HsqlCommand testSubject = connection.CreateCommand())
                {
                    int actual = testSubject.CommandTimeout;


                    Assert.AreEqual(30, actual);
                }
        }
Example #8
0
 public virtual void ExecuteNonQuery()
 {
     using (HsqlConnection connection = NewConnection())
         using (HsqlCommand testSubject = connection.CreateCommand())
         {
             testSubject.CommandText = ";";
             int expected = 0;
             int actual   = testSubject.ExecuteNonQuery();
             Assert.AreEqual(expected, actual);
         }
 }
Example #9
0
        public virtual void ExecuteScalar()
        {
            using (HsqlConnection connection = NewConnection())
                using (HsqlCommand testSubject = connection.CreateCommand())
                {
                    testSubject.CommandText = "call database();";
                    object expected = "mem:test";
                    object actual   = testSubject.ExecuteScalar();

                    Assert.AreEqual(expected, actual);
                }
        }
Example #10
0
        public virtual void UpdatedRowSource()
        {
            using (HsqlConnection connection = NewConnection())
                using (HsqlCommand testSubject = connection.CreateCommand())
                {
                    UpdateRowSource expected = UpdateRowSource.Both;
                    UpdateRowSource actual   = testSubject.UpdatedRowSource;

                    Assert.AreEqual(expected, actual);

                    Assert.Fail("TODO");
                }
        }
Example #11
0
        public virtual void GetUpdateCommand()
        {
            HsqlCommandBuilder testSubject = new HsqlCommandBuilder();

            testSubject.DataAdapter = new HsqlDataAdapter(
                "select * from information_schema.system_tables",
                "Protocol=Mem;Path=Test;User Id=SA;");
            bool useColumnsForParameterNames = true;

            HsqlCommand command = testSubject.GetUpdateCommand(useColumnsForParameterNames);

            Assert.Fail("TODO");
        }
Example #12
0
        internal void Dispose()
        {
            m_isDisposed = true;

            HsqlCommand command = m_batchCommand;

            m_commandList  = null;
            m_batchCommand = null;

            if (command != null)
            {
                command.Dispose();
            }
        }
Example #13
0
        public virtual void Prepare()
        {
            using (HsqlConnection connection = NewConnection())
                using (HsqlCommand testSubject = connection.CreateCommand())
                {
                    testSubject.CommandText = "select * from information_schema.system_tables";
                    testSubject.Prepare();

                    bool expected = true;
                    bool actual   = testSubject.IsPrepared;

                    Assert.AreEqual(expected, actual);
                }
        }
Example #14
0
        public virtual void StatementCompleted()
        {
            using (HsqlConnection connection = NewConnection())
                using (HsqlCommand testSubject = connection.CreateCommand())
                {
                    testSubject.StatementCompleted += delegate(object sender, StatementCompletedEventArgs e)
                    {
                        Assert.AreNotSame(testSubject, sender);
                        Assert.AreEqual(0, e.RecordCount);
                    };

                    //
                    Assert.Fail("TODO");
                }
        }
Example #15
0
        public virtual void CreateCommand()
        {
            using (HsqlConnection testSubject = new HsqlConnection())
            {
                HsqlCommand command = testSubject.CreateCommand();

                Assert.AreSame(testSubject, command.Connection);
                Assert.AreEqual(string.Empty, command.CommandText);
                Assert.AreEqual(CommandType.Text, command.CommandType);
                Assert.AreEqual(true, command.DesignTimeVisible);
                Assert.AreEqual(false, command.IsPrepared);
                Assert.AreEqual(UpdateRowSource.Both, command.UpdatedRowSource);
                Assert.AreEqual(null, command.Transaction);
            }
        }
Example #16
0
        public void CommandText()
        {
            using (HsqlConnection connection = NewConnection())
                using (HsqlCommand testSubject = connection.CreateCommand())
                {
                    Assert.AreEqual(string.Empty, testSubject.CommandText);

                    testSubject.CommandText = "select * from information_schema.system_tables";

                    Assert.AreEqual("select * from information_schema.system_tables", testSubject.CommandText);

                    testSubject.CommandText = null;

                    Assert.AreEqual(string.Empty, testSubject.CommandText);
                }
        }
Example #17
0
        public void ExecuteReader()
        {
            using (HsqlConnection connection = NewConnection())
                using (HsqlCommand testSubject = connection.CreateCommand())
                {
                    testSubject.CommandText = "select * from information_schema.system_tables";

                    using (HsqlDataReader reader = testSubject.ExecuteReader())
                    {
                        object[] values = new object[reader.FieldCount];

                        while (reader.Read())
                        {
                            int fieldCount = reader.GetValues(values);

                            for (int i = 0; i < fieldCount; i++)
                            {
                                object value = values[i];
                                Console.Write(value);
                                Console.Write(" : ");
                            }
                            Console.WriteLine();
                        }
                    }
                }

            using (HsqlConnection connection = NewConnection())
                using (HsqlCommand testSubject = connection.CreateCommand())
                {
                    testSubject.CommandText = "select * from information_schema.system_tables";
                    HsqlDataReader reader      = testSubject.ExecuteReader(CommandBehavior.SchemaOnly);
                    DataTable      schemaTable = reader.GetSchemaTable();

                    foreach (DataRow row in schemaTable.Rows)
                    {
                        object[] values = row.ItemArray;

                        foreach (object value in values)
                        {
                            Console.Write(value);
                            Console.Write(" : ");
                        }
                        Console.WriteLine();
                    }
                }
        }
Example #18
0
        public virtual void Transaction()
        {
            using (HsqlConnection connection = NewConnection())
                using (HsqlCommand testSubject = connection.CreateCommand())
                {
                    testSubject.CommandText = "update foo set bar = baz";
                    testSubject.Connection.BeginTransaction(IsolationLevel.ReadUncommitted);
                    HsqlTransaction transaction = testSubject.Transaction;

                    IsolationLevel expected = IsolationLevel.ReadUncommitted;
                    IsolationLevel actual   = transaction.IsolationLevel;

                    Assert.AreEqual(expected, actual);

                    //
                    Assert.Fail("TODO");
                }
        }
Example #19
0
        public void Clone()
        {
            using (HsqlConnection connection = NewConnection())
                using (HsqlTransaction transaction = connection.BeginTransaction())
                    using (HsqlCommand originalCommand = connection.CreateCommand())
                    {
                        originalCommand.CommandType = global::System.Data.CommandType.StoredProcedure;
                        originalCommand.CommandText = "call 1 + cast(? as integer)";
                        originalCommand.DeriveParameters();

                        HsqlCommand clonedCommand = originalCommand.Clone();

                        Assert.AreEqual(originalCommand.CommandText, clonedCommand.CommandText);
                        Assert.AreEqual(originalCommand.CommandTimeout, clonedCommand.CommandTimeout);
                        Assert.AreEqual(originalCommand.CommandType, clonedCommand.CommandType);
                        Assert.AreSame(connection, clonedCommand.Connection);
                        Assert.AreEqual(originalCommand.DesignTimeVisible, clonedCommand.DesignTimeVisible);
                        Assert.AreEqual(originalCommand.Parameters.Count, clonedCommand.Parameters.Count);

                        for (int i = 0; i < originalCommand.Parameters.Count; i++)
                        {
                            HsqlParameter orignalCommandParameter = originalCommand.Parameters[i];
                            HsqlParameter clonedCommandParameter  = clonedCommand.Parameters[i];

                            Assert.AreEqual(orignalCommandParameter.DbType, clonedCommandParameter.DbType);
                            Assert.AreEqual(orignalCommandParameter.Direction, clonedCommandParameter.Direction);
                            Assert.AreEqual(orignalCommandParameter.IsNullable, clonedCommandParameter.IsNullable);
                            Assert.AreEqual(orignalCommandParameter.Offset, clonedCommandParameter.Offset);
                            Assert.AreEqual(orignalCommandParameter.ParameterName, clonedCommandParameter.ParameterName);
                            Assert.AreEqual(orignalCommandParameter.Precision, clonedCommandParameter.Precision);
                            Assert.AreEqual(orignalCommandParameter.ProviderType, clonedCommandParameter.ProviderType);
                            Assert.AreEqual(orignalCommandParameter.Scale, clonedCommandParameter.Scale);
                            Assert.AreEqual(orignalCommandParameter.Size, clonedCommandParameter.Size);
                            Assert.AreEqual(orignalCommandParameter.SourceColumn, clonedCommandParameter.SourceColumn);
                            Assert.AreEqual(orignalCommandParameter.SourceColumnNullMapping, clonedCommandParameter.SourceColumnNullMapping);
                            Assert.AreEqual(orignalCommandParameter.SourceVersion, clonedCommandParameter.SourceVersion);
                            Assert.AreEqual(orignalCommandParameter.ToSqlLiteral(), clonedCommandParameter.ToSqlLiteral());
                            Assert.AreEqual(orignalCommandParameter.Value, clonedCommandParameter.Value);
                        }

                        Assert.AreSame(originalCommand.Transaction, clonedCommand.Transaction);
                        Assert.AreEqual(originalCommand.UpdatedRowSource, clonedCommand.UpdatedRowSource);
                    }
        }
        /// <summary>
        /// Computes the key info dictionary for the column metadata of the
        /// given data reader.
        /// </summary>
        /// <remarks>
        /// Depending upon the column metadata already present in the data
        /// reader, it may be required to perform further access to the
        /// originating data source using the reader's
        /// <c>OriginatingConnection</c>.  This in turn implies that the
        /// <c>OriginatingConnection</c> must be open and must still
        /// represent the originating session on the originating data source;
        /// otherwise, the reported key info may be incorrect or the attempt
        /// access the data source may simply fail.
        /// </remarks>
        /// <param name="reader">
        /// The reader for which to compute the column metadata key info map.
        /// </param>
        /// <returns>
        /// Map {ColumnIdentifier=&gt;KeyInfo}
        /// </returns>
        /// <exception cref="HsqlDataSourceException">
        /// If a data access error occurs.
        /// </exception>
        internal static Dictionary <ColumnIdentifier, KeyInfo> GetKeyInfo(
            HsqlDataReader reader)
        {
            ResultMetaData metaData = reader.m_result.metaData;
            Dictionary <TableIdentifier, object> tableSet
                = new Dictionary <TableIdentifier, object>();
            object placeholder = new object();

            string[] schemaNames = metaData.schemaNames;
            string[] tableNames  = metaData.tableNames;
            string[] columnNames = metaData.colNames;
            int      count       = columnNames.Length;

            for (int i = 0; i < count; i++)
            {
                string tableName  = tableNames[i];
                string columnName = columnNames[i];

                if (string.IsNullOrEmpty(tableName) ||
                    string.IsNullOrEmpty(columnName))
                {   // not a table column
                    continue;
                }

                string          schemaName      = schemaNames[i];
                TableIdentifier tableIdentifier = new TableIdentifier(
                    schemaName, tableName);

                tableSet[tableIdentifier] = placeholder;
            }

            Dictionary <ColumnIdentifier, KeyInfo> columnMap
                = new Dictionary <ColumnIdentifier, KeyInfo>();

            if (tableSet.Count == 0)
            {
                return(columnMap);
            }

            StringBuilder sb = new StringBuilder('(');

            count = 0;

            foreach (TableIdentifier tableIdentifier in tableSet.Keys)
            {
                if (count > 0)
                {
                    sb.Append(" OR ");
                }

                count++;

                sb.Append("(bri.table_schem");

                string schemaName = tableIdentifier.m_schema;

                if (string.IsNullOrEmpty(schemaName))
                {
                    sb.Append(" IS NULL ");
                }
                else
                {
                    sb.Append(" = ").Append(StringConverter.toQuotedString(
                                                schemaName, '\'', /*escape inner quotes*/ true));
                }

                string tableName = tableIdentifier.m_table;

                sb.Append(" AND bri.table_name = ").Append(
                    StringConverter.toQuotedString(tableName, '\'',
                                                   /*escape inner quotes*/ true));

                sb.Append(')');
            }

            sb.Append(')');

            string predicate = sb.ToString();

            using (HsqlCommand command =
                       reader.OriginatingConnection.CreateCommand())
            {
                command.CommandText = string.Format(KeyInfoQuery, predicate);
                command.CommandType = CommandType.Text;

                using (HsqlDataReader keyInfoReader = command.ExecuteReader())
                {
                    while (keyInfoReader.Read())
                    {
                        bool isKey = keyInfoReader.GetBoolean(3);

                        if (!isKey)
                        {
                            continue;
                        }

                        string schema = keyInfoReader.GetString(0);
                        string table  = keyInfoReader.GetString(1);
                        string column = keyInfoReader.GetString(2);

                        ColumnIdentifier key = new ColumnIdentifier(schema,
                                                                    table, column);

                        if (!columnMap.ContainsKey(key))
                        {
                            KeyInfo keyInfo = new KeyInfo();

                            keyInfo.m_isKey    = true;
                            keyInfo.m_isUnique = false;

                            columnMap.Add(key, keyInfo);
                        }
                    }
                }
            }

            return(columnMap);
        }
        internal void Dispose()
        {
            m_isDisposed = true;

            HsqlCommand command = m_batchCommand;

            m_commandList = null;
            m_batchCommand = null;

            if (command != null)
            {
                command.Dispose();
            }
        }
        internal void Append(HsqlCommand command)
        {
            HsqlParameterCollection parameters;

            if (command == null)
            {
                throw new ArgumentNullException(
                    "command");
            }

            string commandText = command.CommandText;

            if (string.IsNullOrEmpty(commandText))
            {
                throw new ArgumentException("Command Text Required",
                    "command.CommandText");
            }

            CommandType commandType = command.CommandType;

            switch (commandType)
            {
                case CommandType.Text:
                case CommandType.StoredProcedure:
                    {
                        parameters = null;
                        HsqlParameterCollection commandParameters = command.Parameters;
                        int parameterCount = commandParameters.Count;

                        if (parameterCount > 0)
                        {
                            parameters = new HsqlParameterCollection();

                            for (int i = 0; i < parameterCount; i++)
                            {
                                HsqlParameter destination = commandParameters[i].Clone();

                                parameters.Add(destination);

                                if (!IdentifierParser.IsMatch(destination
                                    .ParameterName))
                                {
                                    throw new HsqlDataSourceException(
                                        "Bad Parameter Name",
                                        org.hsqldb.Trace.GENERAL_ERROR,"S1000");
                                }
                            }
                        }
                        break;
                    }
                case CommandType.TableDirect:
                    {
                        throw new ArgumentOutOfRangeException(
                            "command.CommandType", commandType,
                            "Enumeration Value Not Supported.");
                    }
                default:
                    {
                        throw new ArgumentOutOfRangeException(
                            "command.CommandType", commandType,
                            "Invalid Enumeration Value");
                    }
            }

            int returnParameterIndex = -1;

            for (int j = 0; j < parameters.Count; j++)
            {
                if (ParameterDirection.ReturnValue == parameters[j].Direction)
                {
                    returnParameterIndex = j;
                    break;
                }
            }

            LocalCommand item = new LocalCommand(
                commandText,
                parameters,
                returnParameterIndex,
                commandType);

            m_commandList.Add(item);
        }
Example #23
0
        internal void Append(HsqlCommand command)
        {
            HsqlParameterCollection parameters;

            if (command == null)
            {
                throw new ArgumentNullException(
                          "command");
            }

            string commandText = command.CommandText;

            if (string.IsNullOrEmpty(commandText))
            {
                throw new ArgumentException("Command Text Required",
                                            "command.CommandText");
            }

            CommandType commandType = command.CommandType;

            switch (commandType)
            {
            case CommandType.Text:
            case CommandType.StoredProcedure:
            {
                parameters = null;
                HsqlParameterCollection commandParameters = command.Parameters;
                int parameterCount = commandParameters.Count;

                if (parameterCount > 0)
                {
                    parameters = new HsqlParameterCollection();

                    for (int i = 0; i < parameterCount; i++)
                    {
                        HsqlParameter destination = commandParameters[i].Clone();

                        parameters.Add(destination);

                        if (!IdentifierParser.IsMatch(destination
                                                      .ParameterName))
                        {
                            throw new HsqlDataSourceException(
                                      "Bad Parameter Name",
                                      org.hsqldb.Trace.GENERAL_ERROR, "S1000");
                        }
                    }
                }
                break;
            }

            case CommandType.TableDirect:
            {
                throw new ArgumentOutOfRangeException(
                          "command.CommandType", commandType,
                          "Enumeration Value Not Supported.");
            }

            default:
            {
                throw new ArgumentOutOfRangeException(
                          "command.CommandType", commandType,
                          "Invalid Enumeration Value");
            }
            }

            int returnParameterIndex = -1;

            for (int j = 0; j < parameters.Count; j++)
            {
                if (ParameterDirection.ReturnValue == parameters[j].Direction)
                {
                    returnParameterIndex = j;
                    break;
                }
            }

            LocalCommand item = new LocalCommand(
                commandText,
                parameters,
                returnParameterIndex,
                commandType);

            m_commandList.Add(item);
        }