Ejemplo n.º 1
0
        private static void SendNativeProtocol(int socketFd, ICursor cursor, int requestId)
        {
            uint write_len;
            int  DATACONTROL_RESULT_NO_DATA = -1;
            int  COLUMN_TYPE_NULL = 5;
            int  column_count, i, rowcount, size = 0, total_len_of_column_names = 0;

            byte[]       type_array, length_array, string_array, int_tmp, value_array = null;
            string       txt;
            ResultType   result;
            MemoryStream ms;

            if (cursor.Reset() == false)
            {
                Log.Error(LogTag, "Reset is failed  :  " + requestId.ToString());
                return;
            }

            if (cursor.GetRowCount() <= 0)
            {
                Log.Error(LogTag, "The DB does not have another row : " + requestId.ToString());
                int_tmp = BitConverter.GetBytes(DATACONTROL_RESULT_NO_DATA);
                result  = (ResultType)Interop.DataControl.UnsafeCode.WriteResult(socketFd, int_tmp, int_tmp.Length, out write_len);
                return;
            }

            /* 1. column count */
            column_count = cursor.GetColumnCount();
            int_tmp      = BitConverter.GetBytes(column_count);
            result       = (ResultType)Interop.DataControl.UnsafeCode.WriteResult(socketFd, int_tmp, int_tmp.Length, out write_len);
            if (result != ResultType.Success)
            {
                Log.Error(LogTag, "Writing a column_count to a file descriptor is failed.");
                return;
            }

            Log.Info(LogTag, "Writing a column_count " + column_count.ToString());

            /* 2.column type x column_count */
            for (i = 0; i < column_count; i++)
            {
                type_array = BitConverter.GetBytes((int)cursor.GetColumnType(i));
                result     = (ResultType)Interop.DataControl.UnsafeCode.WriteResult(socketFd, type_array, type_array.Length, out write_len);
                if (result != ResultType.Success)
                {
                    Log.Error(LogTag, "Writing a type to a file descriptor is failed.");
                    return;
                }

                Log.Info(LogTag, "Writing a column_type " + cursor.GetColumnType(i).ToString());
            }

            /* 3. column name x column_count */
            for (i = 0; i < column_count; i++)
            {
                Log.Info(LogTag, "Writing a name " + cursor.GetColumnName(i));

                total_len_of_column_names += cursor.GetColumnName(i).Length;
                string_array = Encoding.UTF8.GetBytes(cursor.GetColumnName(i));
                value_array  = new byte[string_array.Length + 1];/*insert null */
                string_array.CopyTo(value_array, 0);
                length_array = BitConverter.GetBytes(value_array.Length);

                result = (ResultType)Interop.DataControl.UnsafeCode.WriteResult(socketFd, length_array, length_array.Length, out write_len);
                if (result != ResultType.Success)
                {
                    Log.Error(LogTag, "Writing a type to a file descriptor is failed.");
                    return;
                }

                result = (ResultType)Interop.DataControl.UnsafeCode.WriteResult(socketFd, value_array, value_array.Length, out write_len);
                if (result != ResultType.Success)
                {
                    Log.Error(LogTag, "Writing a type to a file descriptor is failed.");
                    return;
                }
            }

            /* 4. total length of column names */
            length_array = BitConverter.GetBytes(total_len_of_column_names);
            result       = (ResultType)Interop.DataControl.UnsafeCode.WriteResult(socketFd, length_array, length_array.Length, out write_len);
            if (result != ResultType.Success)
            {
                Log.Error(LogTag, "Writing a total_len_of_column_names to a file descriptor is failed");
                return;
            }

            Log.Info(LogTag, "Writing  total length of column namese " + total_len_of_column_names.ToString());

            /* 5. row count */
            length_array = BitConverter.GetBytes(cursor.GetRowCount());
            Log.Error(LogTag, "=========================== select rowcount " + cursor.GetRowCount().ToString());
            result = (ResultType)Interop.DataControl.UnsafeCode.WriteResult(socketFd, length_array, length_array.Length, out write_len);
            if (result != ResultType.Success)
            {
                Log.Error(LogTag, "Writing a row count to a file descriptor is failed");
                return;
            }

            Log.Error(LogTag, "Writing a row count " + cursor.GetRowCount().ToString());

            rowcount = 0;
            do
            {
                ms = new MemoryStream();

                for (i = 0; i < column_count; i++)
                {
                    type_array = BitConverter.GetBytes((int)cursor.GetColumnType(i));
                    switch (cursor.GetColumnType(i))
                    {
                    case ColumnType.ColumnTypeInt:
                        value_array = BitConverter.GetBytes(cursor.GetInt64Value(i));
                        size        = value_array.Length;
                        break;

                    case ColumnType.ColumnTypeDouble:
                        value_array = BitConverter.GetBytes(cursor.GetDoubleValue(i));
                        size        = value_array.Length;
                        break;

                    case ColumnType.ColumnTypeString:
                        txt = cursor.GetStringValue(i);
                        if (txt == null)
                        {
                            type_array = BitConverter.GetBytes(COLUMN_TYPE_NULL);
                            size       = 0;
                            break;
                        }

                        string_array = Encoding.UTF8.GetBytes(txt);
                        value_array  = new byte[string_array.Length + 1];   /*insert null */
                        string_array.CopyTo(value_array, 0);
                        size = value_array.Length;
                        break;

                    case ColumnType.ColumnTypeBlob:
                        int_tmp = cursor.GetBlobValue(i);
                        if (int_tmp == null)
                        {
                            type_array = BitConverter.GetBytes(COLUMN_TYPE_NULL);
                            size       = 0;
                            break;
                        }

                        value_array = int_tmp;
                        size        = value_array.Length;
                        break;
                    }

                    ms.Write(type_array, 0, type_array.Length);

                    length_array = BitConverter.GetBytes(size);
                    ms.Write(length_array, 0, length_array.Length);
                    if (size > 0)
                    {
                        ms.Write(value_array, 0, value_array.Length);
                    }
                }

                value_array = ms.ToArray();

                result = (ResultType)Interop.DataControl.UnsafeCode.WriteResult(socketFd, value_array, value_array.Length, out write_len);
                if (result != ResultType.Success)
                {
                    Log.Error(LogTag, "Writing a row to a file descriptor is failed");
                    ms.Dispose();
                    return;
                }

                ms.Dispose();
                Log.Info(LogTag, "row_count ~~~~ ", rowcount.ToString());
            }while (cursor.Next());
        }