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; }
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); }
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); }
/// <summary> /// Test CLOB INSERT, using a txt input file /// </summary> private static void Test_Clob_FromFile() { using (CUBRIDConnection conn = new CUBRIDConnection()) { conn.ConnectionString = TestCases.connString; conn.Open(); CreateTestTableLOB(conn); string sql = "insert into t (c) values(?)"; CUBRIDCommand cmd = new CUBRIDCommand(sql, conn); CUBRIDClob Clob = new CUBRIDClob(conn); StreamReader r = new StreamReader("../../../BSD License.txt"); string writestring = r.ReadToEnd(); r.Close(); Clob.SetString(1, writestring); CUBRIDParameter param = new CUBRIDParameter(); param.ParameterName = "?"; param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_CLOB; param.Value = Clob; cmd.Parameters.Add(param); cmd.ExecuteNonQuery(); cmd.Close(); string sql2 = "SELECT c from t"; using (CUBRIDCommand cmd2 = new CUBRIDCommand(sql2, conn)) { DbDataReader reader = cmd2.ExecuteReader(); while (reader.Read()) { CUBRIDClob cImage = (CUBRIDClob)reader[0]; string str2 = cImage.GetString(1, (int)cImage.ClobLength); StreamWriter w = new StreamWriter("testout.txt"); w.Write(str2); w.Close(); StreamReader r2 = new StreamReader("testout.txt"); string readstring = r2.ReadToEnd(); r2.Close(); Debug.Assert(writestring.Length == readstring.Length, "The inserted CLOB length is not valid!"); Debug.Assert(writestring.Equals(readstring), "The CLOB was not inserted correctly!"); } } CleanupTestTableLOB(conn); } }
/// <summary> /// Test CLOB UPDATE /// </summary> private static void Test_Clob_Update() { String str; using (CUBRIDConnection conn = new CUBRIDConnection()) { conn.ConnectionString = TestCases.connString; conn.Open(); CreateTestTableLOB(conn); string sql1 = "insert into t (c) values(?)"; using (CUBRIDCommand cmd1 = new CUBRIDCommand(sql1, conn)) { CUBRIDClob Clob1 = new CUBRIDClob(conn); Clob1.SetString(1, "test string to be inserted"); CUBRIDParameter param1 = new CUBRIDParameter(); param1.ParameterName = "?"; param1.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_CLOB; param1.Value = Clob1; cmd1.Parameters.Add(param1); cmd1.ExecuteNonQuery(); cmd1.Close(); string sql = "UPDATE t SET c = ?"; CUBRIDCommand cmd = new CUBRIDCommand(sql, conn); CUBRIDClob Clob = new CUBRIDClob(conn); str = conn.ConnectionString; //Use the ConnectionString for testing Clob.SetString(1, str); CUBRIDParameter param = new CUBRIDParameter(); param.ParameterName = "?"; param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_CLOB; param.Value = Clob; cmd.Parameters.Add(param); cmd.ExecuteNonQuery(); } string sql2 = "SELECT c from t"; using (CUBRIDCommand cmd2 = new CUBRIDCommand(sql2, conn)) { DbDataReader reader = cmd2.ExecuteReader(); while (reader.Read()) { CUBRIDClob cImage = (CUBRIDClob)reader[0]; string str2 = cImage.GetString(1, (int)cImage.ClobLength); Debug.Assert(str.Length == str2.Length, "The selected CLOB length is not valid!"); Debug.Assert(str.Equals(str2), "The CLOB was not selected correctly!"); } } CleanupTestTableLOB(conn); } }
/// <summary> /// Test CLOB SELECT, using CUBRIDDataAdapter and DataSet /// </summary> private static void Test_Clob_SelectDataAdapter2() { using (CUBRIDConnection conn = new CUBRIDConnection()) { conn.ConnectionString = TestCases.connString; conn.Open(); CreateTestTableLOB(conn); string sql1 = "insert into t (c) values(?)"; CUBRIDCommand cmd1 = new CUBRIDCommand(sql1, conn); CUBRIDClob Clob1 = new CUBRIDClob(conn); String str1 = conn.ConnectionString; //Use ConnectionString content for testing Clob1.SetString(1, str1); CUBRIDParameter param = new CUBRIDParameter(); param.ParameterName = "?"; param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_CLOB; param.Value = Clob1; cmd1.Parameters.Add(param); cmd1.ExecuteNonQuery(); cmd1.Close(); string sql = "SELECT c from t"; DataSet ds = new DataSet(); 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++) { CUBRIDClob cImage = (CUBRIDClob)dt.Rows[j]["c"]; string str = cImage.GetString(1, (int)cImage.ClobLength); Debug.Assert(str.Length == str1.Length, "The selected CLOB length is not valid!"); Debug.Assert(str.Equals(str1), "The CLOB was not selected correctly!"); } CleanupTestTableLOB(conn); } }
/// <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); }
public static void Test_CUBRIDClob_Select() { Configuration cfg = (new Configuration()).Configure().AddAssembly(typeof(TestCUBRIDClobType).Assembly); using (CUBRIDConnection conn = new CUBRIDConnection(cfg.GetProperty(NHibernate.Cfg.Environment.ConnectionString))) { conn.Open(); TestCases.ExecuteSQL("drop table if exists TestCUBRIDClob", conn); TestCases.ExecuteSQL("create table TestCUBRIDClob(c_integer int not null auto_increment," + "c_clob CLOB," + "primary key (c_integer))", conn); const string sql = "insert into TestCUBRIDClob values(1, ?)"; CUBRIDCommand cmd = new CUBRIDCommand(sql, conn); CUBRIDClob Clob = new CUBRIDClob(conn); StreamReader originalFileReader = new StreamReader("../../BSD License.txt"); string clobStringToInsert = originalFileReader.ReadToEnd(); originalFileReader.Close(); Clob.SetString(1, clobStringToInsert); CUBRIDParameter param = new CUBRIDParameter(); param.ParameterName = "?p"; param.Value = Clob; cmd.Parameters.Add(param); cmd.Parameters[0].DbType = DbType.AnsiString; cmd.ExecuteNonQuery(); cmd.Close(); ISessionFactory sessionFactory = cfg.BuildSessionFactory(); using (var session = sessionFactory.OpenSession()) { //Retrieve the inserted information IQuery query = session.CreateQuery("FROM TestCUBRIDClobType"); IList<TestCUBRIDClobType> testQuery = query.List<TestCUBRIDClobType>(); Debug.Assert(testQuery[0].c_integer == 1); CUBRIDClob bImage = testQuery[0].c_clob; string clobInserted = bImage.GetString(1, (int)testQuery[0].c_clob.ClobLength); Debug.Assert(clobStringToInsert.Length == clobInserted.Length); Debug.Assert(clobStringToInsert == clobInserted); } //Clean the database schema TestCases.ExecuteSQL("drop table if exists TestCUBRIDClob", conn); } }