Example #1
0
        private void ExecuteCommand(Query query, OracleCommand cmd)
        {
            /*
             * 1. read a record
             * 2. if first record of result (get schema, signal StartResult)
             * 3. fetch values
             * 4. signal Row
             * 5. if no more (signal EndResult)
             * 6. if next result goto 1
             *
             */
            try
            {
                int ticks = Environment.TickCount;

                using (OracleDataReader reader = cmd.ExecuteReader())
                {
                    do
                    {
                        var table = new OraTable();
                        int count = 0;

                        while (!isCancelled && reader.Read())
                        {
                            if (count == 0)
                            {
                                table.Schema = new OraSchema(reader);
                                // EVENT StartResult(query.SQL,schema)
                            }

                            var row = new OraRow();
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                switch (table.Schema.GetDbType(i))
                                {
                                case OracleDbType.Clob:
                                case OracleDbType.NClob:
                                    if (reader.FieldCount == 1)
                                    {
                                        // SPECIAL CASE!
                                        // if we're only asking for exactly one CLOB then
                                        // let's read it in its entirety...

                                        var clob   = reader.GetOracleClob(i);
                                        var buffer = new byte[clob.Length];
                                        if (clob.Read(buffer, 0, (int)clob.Length) > 0)
                                        {
                                            row.Add(
                                                Encoding.Default.GetString(buffer).Replace("\x00", string.Empty));
                                        }
                                        else
                                        {
                                            row.Add(null);
                                        }
                                    }
                                    else
                                    {
                                        row.Add(null);
                                    }
                                    break;

                                case OracleDbType.BFile:
                                case OracleDbType.Blob:
                                case OracleDbType.Raw:
                                    // we treat null differently than DbNull
                                    // when we interpret the results later on...
                                    row.Add(null);
                                    break;

                                default:
                                    try
                                    {
                                        row.Add(reader.GetValue(i));
                                    }
                                    catch (InvalidCastException)
                                    {
                                        // handle when NUMBER column has greater precision than
                                        // can be represented by a .NET decimal
                                        row.Add(reader.GetProviderSpecificValue(i));
                                    }
                                    break;
                                }
                            }

                            table.Add(row);

                            // EVENT Row(row)

                            count++;
                        }

                        query.Data.Add(table);
                        query.AffectedRecords = table.Count;

                        // EVENT EndResult(count)
                    } while (!isCancelled && reader.NextResult());

                    reader.Close();
                }

                query.Ticks = Environment.TickCount - ticks;

                if (isOutputEnabled)
                {
                    GetOutputLines(query);
                }
            }
            catch (OracleException exc)
            {
                query.AddMessage(exc);

                if (exc.Message == String.Empty)
                {
                    Logger.WriteLine(exc.StackTrace);
                }
                else
                {
                    Logger.WriteLine(exc.Message);
                }
            }
            catch (Exception exc)
            {
                query.AddMessage(exc);

                if (exc.Message == String.Empty)
                {
                    Logger.WriteLine(exc.StackTrace);
                }
                else
                {
                    Logger.WriteLine(exc.Message);
                }
            }
        }
Example #2
0
        private void ExecuteCommand(Query query, OracleCommand cmd)
        {
            /*
             * 1. read a record
             * 2. if first record of result (get schema, signal StartResult)
             * 3. fetch values
             * 4. signal Row
             * 5. if no more (signal EndResult)
             * 6. if next result goto 1
             *
             */
            try
            {
                int ticks = Environment.TickCount;

                using (OracleDataReader reader = cmd.ExecuteReader())
                {
                    do
                    {
                        var table = new OraTable();
                        int count = 0;

                        while (!isCancelled && reader.Read())
                        {
                            if (count == 0)
                            {
                                table.Schema = new OraSchema(reader);
                                // EVENT StartResult(query.SQL,schema)
                            }

                            var row = new OraRow();
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                switch (table.Schema.GetDbType(i))
                                {
                                case OracleDbType.BFile:
                                case OracleDbType.Blob:
                                case OracleDbType.Clob:
                                case OracleDbType.NClob:
                                case OracleDbType.Raw:
                                    // we treat null differently than DbNull
                                    // when we interpret the results later on...
                                    row.Add(null);
                                    break;

                                default:
                                    row.Add(reader.GetValue(i));
                                    break;
                                }
                            }

                            table.Add(row);

                            // EVENT Row(row)

                            count++;
                        }

                        query.Data.Add(table);
                        query.AffectedRecords = table.Count;

                        // EVENT EndResult(count)
                    } while (!isCancelled && reader.NextResult());

                    reader.Close();
                }

                query.Ticks = Environment.TickCount - ticks;

                if (isOutputEnabled)
                {
                    GetOutputLines(query);
                }
            }
            catch (OracleException exc)
            {
                query.AddMessage(exc);

                if (exc.Message == String.Empty)
                {
                    Logger.WriteLine(exc.StackTrace);
                }
                else
                {
                    Logger.WriteLine(exc.Message);
                }
            }
            catch (Exception exc)
            {
                query.AddMessage(exc);

                if (exc.Message == String.Empty)
                {
                    Logger.WriteLine(exc.StackTrace);
                }
                else
                {
                    Logger.WriteLine(exc.Message);
                }
            }
        }