Exemplo n.º 1
0
    /// <summary>
    /// Test BLOB INSERT
    /// </summary>
    private static void Test_Blob_Insert()
    {
      using (CUBRIDConnection conn = new CUBRIDConnection())
      {
        conn.ConnectionString = TestCases.connString;
        conn.Open();

        CreateTestTableLOB(conn);

        string sql = "insert into t (b) values(?)";
        CUBRIDCommand cmd = new CUBRIDCommand(sql, conn);
        CUBRIDBlob Blob = new CUBRIDBlob(conn);

        byte[] bytes = new byte[256];
        bytes[0] = 69;
        bytes[1] = 98;
        bytes[2] = 99;
        bytes[255] = 122;

        Blob.SetBytes(1, bytes);

        CUBRIDParameter param = new CUBRIDParameter();
        param.ParameterName = "?p";
        param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_BLOB;
        param.Value = Blob;
        cmd.Parameters.Add(param);
        cmd.Parameters[0].DbType = DbType.Binary;
        cmd.ExecuteNonQuery();
        cmd.Close();

        string sql2 = "SELECT b from t";
        CUBRIDCommand cmd2 = new CUBRIDCommand(sql2, conn);
        DbDataReader reader = cmd2.ExecuteReader();
        while (reader.Read())
        {
          CUBRIDBlob bImage = (CUBRIDBlob)reader[0];
          byte[] bytes2 = new byte[(int)bImage.BlobLength];
          bytes2 = bImage.GetBytes(1, (int)bImage.BlobLength);

          Debug.Assert(bytes.Length == bytes2.Length, "The inserted BLOB length is not valid!");
          bool ok = true;
          for (int i = 0; i < bytes.Length; i++)
          {
            if (bytes[i] != bytes2[i])
              ok = false;
          }

          Debug.Assert(ok == true, "The BLOB was not inserted correctly!");
        }

        cmd2.Close();

        CleanupTestTableLOB(conn);
      }
    }
Exemplo n.º 2
0
    private static void TestParameterCollection()
    {
        string sql = "drop table if exists TestTable;";
        using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
        {
            cmd.ExecuteNonQuery();
        }

        sql = "CREATE TABLE TestTable (clsid BLOB);";
        using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
        {
            cmd.ExecuteNonQuery();
        }
        byte[] bytes = new byte[36] { 55, 56, 50, 69, 55, 57, 67, 69, 45, 50, 70, 68, 68, 45, 52, 68, 50, 55, 45, 65, 51, 48, 48, 45, 69, 48, 56, 56, 70, 56, 68, 68, 55, 54, 66, 69 };
        sql = "INSERT INTO TestTable VALUES(?);";
        using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
        {
            CUBRIDBlob Blob = new CUBRIDBlob(conn);
            Blob.SetBytes(1, bytes);
            CUBRIDParameter param = new CUBRIDParameter();
            param.ParameterName = "?p";
            param.Value = Blob; cmd.Parameters.Add(param);
            cmd.Parameters[0].DbType = DbType.Binary;

            try
            {
                cmd.Parameters.Insert(0, param);
            }
            catch (Exception e)
            {
                Debug.Assert(e.Message == "Parameter already added to the collection!");
            }
            try
            {
                cmd.Parameters.Insert(0, null);
            }
            catch (Exception e)
            {
                string es = e.ToString();
                Debug.Assert(e.Message == "Only CUBRIDParameter objects are valid!");
            }
            cmd.ExecuteNonQuery();
        }
        using (CUBRIDCommand cmd = new CUBRIDCommand("Select * from TestTable", conn))
        {
            using (CUBRIDDataReader reader = (CUBRIDDataReader)cmd.ExecuteReader())
            {
                reader.Read(); byte[] buffer = new byte[36];
                long len = reader.GetBytes(0, 0, buffer, 0, 36);
                ASCIIEncoding encoding = new ASCIIEncoding();
                string clsid = encoding.GetString(buffer);
                Debug.Assert(clsid == "782E79CE-2FDD-4D27-A300-E088F8DD76BE");
            }
        }
    }
Exemplo n.º 3
0
    public static void Test_CUBRIDBlob_Select()
    {
      Configuration cfg = (new Configuration()).Configure().AddAssembly(typeof(TestCUBRIDBlobType).Assembly);
      using (CUBRIDConnection conn = new CUBRIDConnection(cfg.GetProperty(NHibernate.Cfg.Environment.ConnectionString)))
      {
        conn.Open();
        TestCases.ExecuteSQL("drop table if exists TestCUBRIDBlob", conn);
        TestCases.ExecuteSQL("create table TestCUBRIDBlob(c_integer int not null auto_increment," +
                             "c_blob BLOB," +
                              "primary key (c_integer))", conn);

        const string sql = "insert into TestCUBRIDBlob values(1, ?)";
        CUBRIDCommand cmd = new CUBRIDCommand(sql, conn);
        CUBRIDBlob Blob = new CUBRIDBlob(conn);

        BinaryReader originalFileReader = new BinaryReader(File.Open("../../CUBRID.ico", FileMode.Open));
        byte[] bytesOriginalData = originalFileReader.ReadBytes((int)originalFileReader.BaseStream.Length);
        originalFileReader.Close();
        Blob.SetBytes(1, bytesOriginalData);

        CUBRIDParameter param = new CUBRIDParameter();
        param.ParameterName = "?p";
        param.Value = Blob;
        cmd.Parameters.Add(param);
        cmd.Parameters[0].DbType = DbType.Binary;
        cmd.ExecuteNonQuery();
        cmd.Close();

        ISessionFactory sessionFactory = cfg.BuildSessionFactory();
        using (var session = sessionFactory.OpenSession())
        {
          //Retrieve the inserted information
          IQuery query = session.CreateQuery("FROM TestCUBRIDBlobType");
          IList<TestCUBRIDBlobType> testQuery = query.List<TestCUBRIDBlobType>();
          Debug.Assert(testQuery[0].c_integer == 1);
          CUBRIDBlob bImage = testQuery[0].c_blob;
          byte[] bytesRetrievedData = bImage.GetBytes(1, (int)bImage.BlobLength);

          Debug.Assert(bytesOriginalData.Length == bytesRetrievedData.Length);
          Debug.Assert(bytesOriginalData[0] == bytesRetrievedData[0]);
          Debug.Assert(bytesOriginalData[bytesOriginalData.Length - 1] == bytesRetrievedData[bytesRetrievedData.Length - 1]);
        }

        //Clean the database schema
        TestCases.ExecuteSQL("drop table if exists TestCUBRIDBlob", conn);
      }
    }
Exemplo n.º 4
0
 public static int cci_get_data(ResultTuple rt, int req_handle, int col_no, int type,CUBRIDConnection conn)
 {
     IntPtr value = IntPtr.Zero;
     int indicator = 0, res=0;
     switch ((CUBRIDDataType)type)
     {
         case CUBRIDDataType.CCI_U_TYPE_BLOB:
             res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_BLOB, ref value, ref indicator);
             CUBRIDBlob blob = new CUBRIDBlob(value,conn);
             rt[col_no - 1] = blob;
             break;
         case CUBRIDDataType.CCI_U_TYPE_CLOB:
             res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_CLOB, ref value, ref indicator);
             CUBRIDClob clob = new CUBRIDClob(value,conn);
             rt[col_no - 1] = clob;
             break;
         case CUBRIDDataType.CCI_U_TYPE_INT:             
             res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_STR, ref value, ref indicator);
             if (Marshal.PtrToStringAnsi(value) == null)
             {
                 rt[col_no - 1] = null;
             }
             else
             {
                 rt[col_no - 1] = Convert.ToInt32(Marshal.PtrToStringAnsi(value));
             }
             if (is_collection_type((CUBRIDDataType)type))
             {
                 rt.toArray(col_no - 1);
             }
             break;
         case CUBRIDDataType.CCI_U_TYPE_BIGINT:
             res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_STR, ref value, ref indicator);
             rt[col_no - 1] = Convert.ToInt64(Marshal.PtrToStringAnsi(value));
             if (is_collection_type((CUBRIDDataType)type))
             {
                 rt.toArray(col_no - 1);
             }
             break;
         case CUBRIDDataType.CCI_U_TYPE_OBJECT:
             res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_STR, ref value, ref indicator);
             string oid = Marshal.PtrToStringAnsi(value);
             rt[col_no - 1] = new CUBRIDOid(oid);
             break;
         case CUBRIDDataType.CCI_U_TYPE_BIT:             
             T_CCI_BIT bit = new T_CCI_BIT();
             res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_BIT, ref bit, ref indicator);
             byte[] data = conn.GetEncoding().GetBytes(Marshal.PtrToStringAnsi(bit.buf));
             rt[col_no - 1] = new byte[bit.size];
             Array.Copy(data,(byte[])rt[col_no - 1] ,bit.size);
             break;
         default:
             res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_STR, ref value, ref indicator);
             if (value != IntPtr.Zero)
             {
                 if (conn.GetEncoding().Equals(Encoding.UTF8))
                 {
                     Byte[] v = Encoding.Unicode.GetBytes(Marshal.PtrToStringUni(value));
                     int count = 0;
                     while (count < v.Length && v[count] != 0)
                     {
                         count++;
                     }
                     rt[col_no - 1] = Encoding.Unicode.GetString(Encoding.Convert(Encoding.UTF8, Encoding.Unicode, v, 0, count));
                 }
                 else
                 {
                     rt[col_no - 1] = Marshal.PtrToStringAnsi(value);
                 }
             }
             else {
                 rt[col_no - 1] = String.Empty;
             }
             if (is_collection_type((CUBRIDDataType)type))
             {
                 rt.toArray(col_no - 1);
             }
           break;
     }
     return res;
 }
Exemplo n.º 5
0
        public static int cci_bind_param(CUBRIDConnection conn, int handle, int index, T_CCI_A_TYPE a_type, CUBRIDParameter param, CUBRIDDataType u_type, char flag)
        {
            int    ret = 0;
            IntPtr p   = IntPtr.Zero;

            switch (param.CUBRIDDataType)
            {
            case CUBRIDDataType.CCI_U_TYPE_DATE:
            case CUBRIDDataType.CCI_U_TYPE_TIME:
            case CUBRIDDataType.CCI_U_TYPE_DATETIME:
            case CUBRIDDataType.CCI_U_TYPE_TIMESTAMP:
                string date = param.Value.ToString();
                if (param.Value.GetType() == typeof(DateTime))
                {
                    DateTime d = Convert.ToDateTime(param.Value);
                    date = string.Format("{0:u}", d);
                    date = date.Remove(date.Length - 1);
                }
                p   = Marshal.StringToCoTaskMemAnsi(date);
                ret = bind_param(handle, index, a_type, p, u_type, flag);
                break;

            case CUBRIDDataType.CCI_U_TYPE_SET:
            case CUBRIDDataType.CCI_U_TYPE_MULTISET:
            case CUBRIDDataType.CCI_U_TYPE_SEQUENCE:
                IntPtr   set       = IntPtr.Zero;
                string[] value     = data_format((object[])param.Value, param.InnerCUBRIDDataType);
                int[]    indicator = new int[value.Length];
                for (int i = 0; i < value.Length; i++)
                {
                    if (value[i] != null)
                    {
                        indicator[i] = 0;
                    }
                    else
                    {
                        indicator[i] = 1;
                    }
                }
                //CUBRIDDataType.CCI_U_TYPE_STRING or param.InnerCUBRIDDataType
                ret = cci_set_make(ref set, CUBRIDDataType.CCI_U_TYPE_STRING, value.Length, value, indicator);
                if (ret < 0)
                {
                    return(ret);
                }

                ret = bind_param(handle, index, T_CCI_A_TYPE.CCI_A_TYPE_SET, set, param.CUBRIDDataType, flag);
                cci_set_free(set);
                break;

            case CUBRIDDataType.CCI_U_TYPE_BLOB:
                CUBRIDBlob blob = (CUBRIDBlob)param.Value;
                bind_param(handle, index, T_CCI_A_TYPE.CCI_A_TYPE_BLOB, blob.GetPackedLobHandle(), param.CUBRIDDataType, flag);
                break;

            case CUBRIDDataType.CCI_U_TYPE_CLOB:
                CUBRIDClob clob = (CUBRIDClob)param.Value;
                bind_param(handle, index, T_CCI_A_TYPE.CCI_A_TYPE_CLOB, clob.GetPackedLobHandle(), param.CUBRIDDataType, flag);
                break;

            case CUBRIDDataType.CCI_U_TYPE_BIT:
            case CUBRIDDataType.CCI_U_TYPE_VARBIT:
                T_CCI_BIT bit = new T_CCI_BIT();
                bit.size = ((byte[])param.Value).Length;
                bit.buf  = Marshal.AllocHGlobal(bit.size);
                Marshal.Copy((byte[])param.Value, 0, bit.buf, bit.size);
                p = Marshal.AllocHGlobal(Marshal.SizeOf(bit));
                Marshal.StructureToPtr(bit, p, false);
                ret = bind_param(handle, index, T_CCI_A_TYPE.CCI_A_TYPE_BIT, p, param.CUBRIDDataType, flag);
                Marshal.FreeHGlobal(p);
                break;

            case CUBRIDDataType.CCI_U_TYPE_NULL:
                ret = bind_param(handle, index, a_type, IntPtr.Zero, u_type, flag);
                break;

            default:
                byte[] bind_value;     // = param.Value.ToString();
                if (conn.GetEncoding() != null)
                {
                    bind_value = conn.GetEncoding().GetBytes(param.Value.ToString());
                }
                else
                {
                    bind_value = param.GetParameterEncoding().GetBytes(param.Value.ToString());
                }
                ret = bind_param(handle, index, a_type, bind_value, u_type, flag);
                break;
            }
            return(ret);
        }
Exemplo n.º 6
0
        public static int cci_get_data(ResultTuple rt, int req_handle, int col_no, int type, CUBRIDConnection conn)
        {
            IntPtr value = IntPtr.Zero;
            int    indicator = 0, res = 0;

            switch ((CUBRIDDataType)type)
            {
            case CUBRIDDataType.CCI_U_TYPE_BLOB:
                res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_BLOB, ref value, ref indicator);
                CUBRIDBlob blob = new CUBRIDBlob(value, conn);
                rt[col_no - 1] = blob;
                break;

            case CUBRIDDataType.CCI_U_TYPE_CLOB:
                res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_CLOB, ref value, ref indicator);
                CUBRIDClob clob = new CUBRIDClob(value, conn);
                rt[col_no - 1] = clob;
                break;

            case CUBRIDDataType.CCI_U_TYPE_INT:
                res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_STR, ref value, ref indicator);
                if (Marshal.PtrToStringAnsi(value) == null)
                {
                    rt[col_no - 1] = null;
                }
                else
                {
                    rt[col_no - 1] = Convert.ToInt32(Marshal.PtrToStringAnsi(value));
                }
                if (is_collection_type((CUBRIDDataType)type))
                {
                    rt.toArray(col_no - 1);
                }
                break;

            case CUBRIDDataType.CCI_U_TYPE_BIGINT:
                res            = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_STR, ref value, ref indicator);
                rt[col_no - 1] = Convert.ToInt64(Marshal.PtrToStringAnsi(value));
                if (is_collection_type((CUBRIDDataType)type))
                {
                    rt.toArray(col_no - 1);
                }
                break;

            case CUBRIDDataType.CCI_U_TYPE_OBJECT:
                res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_STR, ref value, ref indicator);
                string oid = Marshal.PtrToStringAnsi(value);
                rt[col_no - 1] = new CUBRIDOid(oid);
                break;

            case CUBRIDDataType.CCI_U_TYPE_BIT:
                T_CCI_BIT bit = new T_CCI_BIT();
                res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_BIT, ref bit, ref indicator);
                byte[] data = new byte[bit.size];
                for (int i = 0; i < bit.size; i++)
                {
                    data[i] = Marshal.ReadByte(bit.buf, i);
                }
                rt[col_no - 1] = new byte[bit.size];
                Array.Copy(data, (byte[])rt[col_no - 1], bit.size);
                break;

            default:
                res = cci_get_data(req_handle, col_no, (int)T_CCI_A_TYPE.CCI_A_TYPE_STR, ref value, ref indicator);
                if (value != IntPtr.Zero)
                {
                    if (conn.GetEncoding().Equals(Encoding.UTF8))
                    {
                        Byte[] v     = Encoding.Unicode.GetBytes(Marshal.PtrToStringUni(value));
                        int    count = 0;
                        while (count < v.Length && v[count] != 0)
                        {
                            count++;
                        }

                        if ((CUBRIDDataType)type == CUBRIDDataType.CCI_U_TYPE_VARBIT)
                        {
                            rt[col_no - 1] = Enumerable.Range(0, count)
                                             .Where(x => x % 2 == 0)
                                             .Select(x => Convert.ToByte(Marshal.PtrToStringAnsi(value).Substring(x, 2), 16))
                                             .ToArray();
                        }
                        else
                        {
                            rt[col_no - 1] = Encoding.Unicode.GetString(Encoding.Convert(Encoding.UTF8, Encoding.Unicode, v, 0, count));
                        }
                    }
                    else
                    {
                        rt[col_no - 1] = Marshal.PtrToStringAnsi(value);
                    }
                }
                else
                {
                    rt[col_no - 1] = null;     // String.Empty;
                }
                if (is_collection_type((CUBRIDDataType)type))
                {
                    rt.toArray(col_no - 1);
                }
                break;
            }
            return(res);
        }
Exemplo n.º 7
0
    /// <summary>
    /// Test BLOB DELETE in a transaction
    /// </summary>
    private static void Test_Blob_DeleteTransaction()
    {
      DbTransaction tran = null;
      byte[] bytes1 = new byte[256];

      using (CUBRIDConnection conn = new CUBRIDConnection())
      {
        conn.ConnectionString = TestCases.connString;
        conn.Open();

        CreateTestTableLOB(conn);

        string sql1 = "insert into t (b) values(?)";
        using (CUBRIDCommand cmd1 = new CUBRIDCommand(sql1, conn))
        {
          CUBRIDBlob Blob = new CUBRIDBlob(conn);

          bytes1[0] = 69;
          bytes1[1] = 98;
          bytes1[2] = 99;
          bytes1[255] = 122;

          Blob.SetBytes(1, bytes1);

          CUBRIDParameter param = new CUBRIDParameter();
          param.ParameterName = "?";
          param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_BLOB;
          param.Value = Blob;
          cmd1.Parameters.Add(param);
          cmd1.Parameters[0].DbType = DbType.Binary;
          cmd1.ExecuteNonQuery();
          cmd1.Close();

          tran = conn.BeginTransaction(IsolationLevel.ReadUncommitted);
          string sql2 = "DELETE from t";
          CUBRIDCommand cmd2 = new CUBRIDCommand(sql2, conn);
          cmd2.ExecuteNonQuery();
        }

        tran.Rollback();
      }

      //We have to close and reopen connection. Otherwise we get an invalid buffer position.
      using (CUBRIDConnection conn = new CUBRIDConnection())
      {
        conn.ConnectionString = TestCases.connString;
        conn.Open();

        string sql = "SELECT b from t";
        using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
        {
          DbDataReader reader = cmd.ExecuteReader();
          while (reader.Read())
          {
            Debug.Assert(reader.HasRows == true);

            CUBRIDBlob bImage = (CUBRIDBlob)reader[0];
            byte[] bytes = new byte[(int)bImage.BlobLength];
            bytes = bImage.GetBytes(1, (int)bImage.BlobLength);

            Debug.Assert(bytes1.Length == bytes.Length);

            bool ok = true;
            for (int i = 0; i < bytes.Length; i++)
            {
              if (bytes1[i] != bytes[i])
                ok = false;
            }

            Debug.Assert(ok == true, "The BLOB DELETE command was not rolled-back correctly!");
          }
        }

        CleanupTestTableLOB(conn);
      }
    }
Exemplo n.º 8
0
    /// <summary>
    /// Test BLOB INSERT in a transaction
    /// </summary>
    private static void Test_Blob_InsertTransaction()
    {
      DbTransaction tran = null;
      using (CUBRIDConnection conn = new CUBRIDConnection())
      {
        conn.ConnectionString = TestCases.connString;
        conn.Open();

        CreateTestTableLOB(conn);

        tran = conn.BeginTransaction(IsolationLevel.ReadUncommitted);
        string sql = "insert into t (b) values(?)";
        using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
        {
          CUBRIDBlob Blob = new CUBRIDBlob(conn);

          byte[] bytes = new byte[256];
          bytes[0] = 69;
          bytes[1] = 98;
          bytes[2] = 99;
          bytes[255] = 122;

          Blob.SetBytes(1, bytes);

          CUBRIDParameter param = new CUBRIDParameter();
          param.ParameterName = "?";
          param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_BLOB;
          param.Value = Blob;
          cmd.Parameters.Add(param);
          cmd.Parameters[0].DbType = DbType.Binary;
          cmd.ExecuteNonQuery();
        }

        tran.Rollback();
      }

      //We have to close and reopen connection. Otherwise we get an invalid buffer position.
      using (CUBRIDConnection conn = new CUBRIDConnection())
      {
        conn.ConnectionString = TestCases.connString;
        conn.Open();
        string sql2 = "SELECT b from t";
        using (CUBRIDCommand cmd2 = new CUBRIDCommand(sql2, conn))
        {
          DbDataReader reader = cmd2.ExecuteReader();
          Debug.Assert(reader.HasRows == false, "Transaction did not rollback!");
        }

        CleanupTestTableLOB(conn);
      }
    }
Exemplo n.º 9
0
    /// <summary>
    /// Test BLOB INSERT, using a jpg image input file
    /// </summary>
    private static void Test_Blob_FromFile()
    {
      BinaryReader b;

      using (CUBRIDConnection conn = new CUBRIDConnection())
      {
        conn.ConnectionString = TestCases.connString;
        conn.Open();

        CreateTestTableLOB(conn);

        string sql = "insert into t (b) values(?)";
        using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
        {
          CUBRIDBlob Blob = new CUBRIDBlob(conn);
          byte[] bytes;
          b = new BinaryReader(File.Open("../../../CUBRID.ico", FileMode.Open));
          int length = (int)b.BaseStream.Length;
          bytes = b.ReadBytes(length);

          Blob.SetBytes(1, bytes);
          CUBRIDParameter param = new CUBRIDParameter();
          param.ParameterName = "?";
          param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_BLOB;
          param.Value = Blob;
          cmd.Parameters.Add(param);
          cmd.Parameters[0].DbType = DbType.Binary;
          cmd.ExecuteNonQuery();
        }

        string sql2 = "SELECT b from t";
        using (CUBRIDCommand cmd2 = new CUBRIDCommand(sql2, conn))
        {
          DbDataReader reader = cmd2.ExecuteReader();
          while (reader.Read())
          {
            CUBRIDBlob bImage = (CUBRIDBlob)reader[0];
            byte[] bytes2 = new byte[(int)bImage.BlobLength];
            bytes2 = bImage.GetBytes(1, (int)bImage.BlobLength);

            FileStream stream = new FileStream("1out.jpg", FileMode.Create);
            BinaryWriter writer = new BinaryWriter(stream);
            writer.Write(bytes2);
            writer.Close();

            BinaryReader b2 = new BinaryReader(File.Open("1out.jpg", FileMode.Open));
            Debug.Assert(b2.BaseStream.Length == b.BaseStream.Length, "The inserted BLOB length is not valid!");
            bool ok = true;
            int file1byte, file2byte;
            b.BaseStream.Position = 0;

            do
            {
              file1byte = b.BaseStream.ReadByte();
              file2byte = b2.BaseStream.ReadByte();
              if (file1byte != file2byte)
                ok = false;
            }
            while (file1byte != -1);

            Debug.Assert(ok == true, "The BLOB was not inserted correctly!");

            b.Close();
            b2.Close();
          }
        }

        CleanupTestTableLOB(conn);
      }
    }
Exemplo n.º 10
0
    /// <summary>
    /// Test BLOB SELECT, using CUBRIDDataAdapter and DataSet
    /// </summary>
    private static void Test_Blob_SelectDataAdapter2()
    {
      using (CUBRIDConnection conn = new CUBRIDConnection())
      {
        conn.ConnectionString = TestCases.connString;
        conn.Open();

        CreateTestTableLOB(conn);

        string sql1 = "insert into t (b) values(?)";
        CUBRIDCommand cmd1 = new CUBRIDCommand(sql1, conn);

        CUBRIDBlob Blob1 = new CUBRIDBlob(conn);

        byte[] bytes1 = new byte[256];
        bytes1[0] = 69;
        bytes1[1] = 98;
        bytes1[2] = 99;
        bytes1[255] = 122;

        Blob1.SetBytes(1, bytes1);

        CUBRIDParameter param = new CUBRIDParameter();
        param.ParameterName = "?";
        param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_BLOB;
        param.Value = Blob1;
        cmd1.Parameters.Add(param);
        cmd1.Parameters[0].DbType = DbType.Binary;
        cmd1.ExecuteNonQuery();
        cmd1.Close();

        string sql = "SELECT b from t";

        DataSet ds = new DataSet("t");
        CUBRIDDataAdapter da = new CUBRIDDataAdapter();
        da.SelectCommand = new CUBRIDCommand(sql, conn);
        da.Fill(ds);

        DataTable dt = ds.Tables[0];
        for (int j = 0; j < dt.Rows.Count; j++)
        {
          CUBRIDBlob bImage = (CUBRIDBlob)dt.Rows[j]["b"];
          byte[] bytes = new byte[(int)bImage.BlobLength];
          bytes = bImage.GetBytes(1, (int)bImage.BlobLength);

          Debug.Assert(bytes1.Length == bytes.Length, "The selected BLOB length is not valid!");
          bool ok = true;
          for (int i = 0; i < bytes.Length; i++)
          {
            if (bytes1[i] != bytes[i])
              ok = false;
          }

          Debug.Assert(ok == true, "The BLOB was not selected correctly!");
        }

        CleanupTestTableLOB(conn);
      }
    }
Exemplo n.º 11
0
    private static void TestGetBytes()
    {
        string sql = "drop table if exists TestTable;";
        using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
        {
            cmd.ExecuteNonQuery();
        }

        sql = "CREATE TABLE TestTable (clsid BLOB);";
        using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
        {
            CUBRIDDataAdapter ad1 = new CUBRIDDataAdapter(cmd);
            CUBRIDDataAdapter ad2 = new CUBRIDDataAdapter("Select * from TestTable",connString);
            cmd.ExecuteNonQuery(); 
        }
        byte[] bytes = new byte[36] { 55, 56, 50, 69, 55, 57, 67, 69, 45, 50, 70, 68, 68, 45, 52, 68, 50, 55, 45, 65, 51, 48, 48, 45, 69, 48, 56, 56, 70, 56, 68, 68, 55, 54, 66, 69 };
        sql = "INSERT INTO TestTable VALUES(?);";
        using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
        { 
            CUBRIDBlob Blob = new CUBRIDBlob(conn);
            Blob.SetBytes(1, bytes); 
            CUBRIDParameter param = new CUBRIDParameter(); 
            param.ParameterName = "?p"; 
            param.Value = Blob; cmd.Parameters.Add(param); 
            cmd.Parameters[0].DbType = DbType.Binary; 
            cmd.ExecuteNonQuery(); 
        }
        using (CUBRIDCommand cmd = new CUBRIDCommand("Select * from TestTable", conn))
        {
            using (CUBRIDDataReader reader = (CUBRIDDataReader)cmd.ExecuteReader())
            { 
                reader.Read(); byte[] buffer = new byte[36]; 
                long len = reader.GetBytes(0, 0, buffer, 0, 36);
                ASCIIEncoding encoding = new ASCIIEncoding(); 
                string clsid = encoding.GetString(buffer); 
                Debug.Assert(clsid == "782E79CE-2FDD-4D27-A300-E088F8DD76BE"); 
            }
        }


        sql = "drop table if exists TestTable;";
        using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
        {
            cmd.ExecuteNonQuery();
        }

        sql = "CREATE TABLE TestTable (clsid CLOB);";
        using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
        {
            cmd.ExecuteNonQuery();
        }
        sql = "INSERT INTO TestTable VALUES('1234567890');";
        using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
        {
            cmd.ExecuteNonQuery();
        }
        using (CUBRIDCommand cmd = new CUBRIDCommand("Select * from TestTable", conn))
        {
            using (CUBRIDDataReader reader = (CUBRIDDataReader)cmd.ExecuteReader())
            {
                reader.Read(); 
                byte[] buffer = new byte[36];
                long len = reader.GetBytes(0, 0, buffer, 0, 8);

                try
                {
                    len = reader.GetBytes(0, 0, buffer, 0, 36);
                }
                catch (Exception e)
                { 
                }
            }
        }
    }
Exemplo n.º 12
0
        /// <summary>
        ///   Reads a stream of bytes from the specified column, starting at location indicated by <paramref name="dataOffset" />, into the buffer, starting at the location indicated by <paramref
        ///    name="bufferOffset" />.
        /// </summary>
        /// <param name="ordinal"> The zero-based column ordinal. </param>
        /// <param name="dataOffset"> The index within the row from which to begin the read operation. </param>
        /// <param name="buffer"> The buffer into which to copy the data. </param>
        /// <param name="bufferOffset"> The index with the buffer to which the data will be copied. </param>
        /// <param name="length"> The maximum number of characters to read. </param>
        /// <returns> The actual number of bytes read. </returns>
        public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length)
        {
            // [APIS-217] Check the index is correct.
            if (ordinal >= FieldCount || ordinal < 0)
            {
                throw new IndexOutOfRangeException();
            }

            if (currentRow > resultCount) //Are we at the end of the data?
            {
                throw new InvalidOperationException(Utils.GetStr(MsgId.BufferIndexMustBeValidIndexInBuffer));
            }

            object val  = GetValue(ordinal);
            string type = GetColumnTypeName(ordinal);

            if (type != "BLOB" && type != "CLOB" && type != "BIT" && type != "VARBIT")
            {
                throw new CUBRIDException(Utils.GetStr(MsgId.GetBytesCanBeCalledOnlyOnBinaryColumns));
            }

            // [APIS-217] Check the offset value is correct.
            if (dataOffset < 0)
            {
                throw new IndexOutOfRangeException(Utils.GetStr(MsgId.DataOffsetMustBeValidPositionInField));
            }

            // [APIS-217] If buffer is a null pointer, return 0.
            if (buffer == null)
            {
                return(0);
            }

            if (bufferOffset >= buffer.Length || bufferOffset < 0)
            {
                throw new IndexOutOfRangeException(Utils.GetStr(MsgId.BufferIndexMustBeValidIndexInBuffer));
            }

            if (buffer.Length < (bufferOffset + length))
            {
                throw new ArgumentException(Utils.GetStr(MsgId.BufferNotLargeEnoughToHoldRequestedData));
            }

            //[APIS-217] Does not determine the val is a NULL pointer.
            if (val == null)
            {
                return(0);
            }

            byte[] bytes;
            //[APIS-217] CUBRIDDataReader.GetBytes, threw an exception.

            if (type == "BIT" || type == "VARBIT")
            {
                bytes = (byte[])GetObject(ordinal);
                Debug.Assert(bytes != null, "bit != null");
                if (dataOffset < bytes.Length && dataOffset + length <= bytes.Length)
                {
                    for (long i = 0; i < length; i++)
                    {
                        buffer[i + bufferOffset] = bytes[i + dataOffset];
                    }
                }
                else
                {
                    throw new IndexOutOfRangeException(Utils.GetStr(MsgId.DataOffsetMustBeValidPositionInField));
                }

                return(length);
            }

            if (type == "BLOB")
            {
                CUBRIDBlob blob = val as CUBRIDBlob;
                // [APIS-217] Check the offset value is correct.
                Debug.Assert(blob != null, "blob != null");
                if (dataOffset < blob.BlobLength && dataOffset + length <= blob.BlobLength)
                {
                    bytes = blob.GetBytes(dataOffset + 1, length);
                }
                else
                {
                    throw new IndexOutOfRangeException(Utils.GetStr(MsgId.DataOffsetMustBeValidPositionInField));
                }
            }
            else // if it is a CLOB type.
            {
                CUBRIDClob clob = val as CUBRIDClob;
                Debug.Assert(clob != null, "clob != null");
                if (dataOffset < clob.ClobLength && dataOffset + length <= clob.ClobLength)
                {
                    bytes = conn.GetEncoding().GetBytes(clob.GetString(dataOffset + 1, length));
                }
                else
                {
                    throw new IndexOutOfRangeException(Utils.GetStr(MsgId.DataOffsetMustBeValidPositionInField));
                }
            }

            dataOffset = 0;

            Buffer.BlockCopy(bytes, (int)dataOffset, buffer, bufferOffset, length);

            return(length);
        }