Skip to content

Commit 0c96692

Browse files
committed
Oracle FetchData<T>() - Get BFILE as Base64 string
1 parent fcde5d0 commit 0c96692

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

QueryDB.Core.Tests/Entities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public class Orders
159159
}
160160
public class DataTypes
161161
{
162-
public byte[] BFile_Column { get; set; }
162+
public string BFile_Column { get; set; }
163163
public byte[] Blob_Column { get; set; }
164164
public string Char_Column { get; set; }
165165
public string Clob_Column { get; set; }

QueryDB.Core.Tests/OracleTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public void Test_Oracle_FetchData_Entity_DataTypes_Check()
236236
Assert.IsTrue(data.Count == 1);
237237
var dataType = data.FirstOrDefault();
238238
Console.WriteLine(dataType.BFile_Column);
239-
Assert.IsTrue(dataType.BFile_Column is byte[] && dataType.BFile_Column == null);
239+
Assert.AreEqual(GetBase64Content(Environment.CurrentDirectory + "/SeedData/oracle.sql"), dataType.BFile_Column);
240240
Assert.IsTrue(dataType.Blob_Column is byte[] && dataType.Blob_Column != null);
241241
Assert.AreEqual("A", dataType.Char_Column);
242242
Assert.AreEqual("Sample CLOB data", dataType.Clob_Column);
@@ -257,7 +257,8 @@ public void Test_Oracle_FetchData_Entity_DataTypes_Check()
257257
Assert.AreEqual("Sample VARCHAR data", dataType.Varchar_Column);
258258
Assert.AreEqual("Sample VARCHAR2 data", dataType.Varchar2_Column);
259259
}
260-
[TestMethod]
260+
261+
[TestMethod]
261262
[TestCategory(DB_TESTS), TestCategory(ORACLE_TESTS)]
262263
public void Test_Oracle_FetchData_Entity_DataTypes_Strict_Check()
263264
{

QueryDB/Oracle/Adapter.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ internal List<DataDictionary> FetchData(string selectSql, OracleConnection conne
7979
foreach (var prop in typeof(T).GetProperties())
8080
{
8181
if ((strict || Utils.ColumnExists(reader, prop.Name)) && !reader.IsDBNull(reader.GetOrdinal(prop.Name)))
82-
prop.SetValue(addObjectRow, reader[prop.Name]);
82+
{
83+
if (Utils.IsBFileColumn(reader, prop.Name))
84+
prop.SetValue(addObjectRow, Utils.GetBFileContent(reader, prop.Name));
85+
else
86+
prop.SetValue(addObjectRow, reader[prop.Name]);
87+
}
8388
}
8489
dataList.Add(addObjectRow);
8590
}

QueryDB/Resources/Utils.cs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,40 @@ internal static bool ColumnExists(IDataReader reader, string columnName)
3434
/// <param name="reader">The Oracle data reader containing the column.</param>
3535
/// <param name="columnIndex">The index of the column to check.</param>
3636
/// <returns>Returns <c>true</c> if the column is of type "BFILE"; otherwise, <c>false</c>.</returns>
37-
3837
internal static bool IsBFileColumn(OracleDataReader reader, int columnIndex)
3938
{
4039
const string BFILE_COLUMN = "BFILE";
4140
string columnType = reader.GetDataTypeName(columnIndex);
4241
return columnType.Equals(BFILE_COLUMN, StringComparison.OrdinalIgnoreCase);
4342
}
4443

44+
/// <summary>
45+
/// Determines if a specified column in an Oracle data reader, identified by its column name, is of type "BFILE".
46+
/// </summary>
47+
/// <param name="reader">The Oracle data reader containing the column.</param>
48+
/// <param name="columnName">The name of the column to check.</param>
49+
/// <returns>Returns <c>true</c> if the column is of type "BFILE"; otherwise, <c>false</c>. Returns <c>false</c> if the column does not exist.</returns>
50+
internal static bool IsBFileColumn(OracleDataReader reader, string columnName)
51+
{
52+
const string BFILE_COLUMN = "BFILE";
53+
try
54+
{
55+
int columnIndex = reader.GetOrdinal(columnName);
56+
string columnType = reader.GetDataTypeName(columnIndex);
57+
return columnType.Equals(BFILE_COLUMN, StringComparison.OrdinalIgnoreCase);
58+
}
59+
catch (IndexOutOfRangeException)
60+
{
61+
return false;
62+
}
63+
}
64+
4565
/// <summary>
4666
/// Retrieves the content of a BFILE column from an Oracle data reader as a Base64-encoded string.
4767
/// </summary>
4868
/// <param name="reader">The Oracle data reader containing the BFILE column.</param>
4969
/// <param name="columnIndex">The index of the BFILE column to read.</param>
5070
/// <returns>Returns the BFILE content as a Base64-encoded string, or an empty string if the BFILE is null.</returns>
51-
5271
internal static string GetBFileContent(OracleDataReader reader, int columnIndex)
5372
{
5473
string content = string.Empty;
@@ -70,5 +89,42 @@ internal static string GetBFileContent(OracleDataReader reader, int columnIndex)
7089
}
7190
return content;
7291
}
92+
93+
/// <summary>
94+
/// Retrieves the content of a BFILE column from an Oracle data reader as a Base64-encoded string, using the column name.
95+
/// </summary>
96+
/// <param name="reader">The Oracle data reader containing the BFILE column.</param>
97+
/// <param name="columnName">The name of the BFILE column to read.</param>
98+
/// <returns>Returns the BFILE content as a Base64-encoded string, or an empty string if the BFILE is null or the column does not exist.</returns>
99+
internal static string GetBFileContent(OracleDataReader reader, string columnName)
100+
{
101+
string content = string.Empty;
102+
try
103+
{
104+
int columnIndex = reader.GetOrdinal(columnName);
105+
var bFile = reader.GetOracleBFile(columnIndex);
106+
if (bFile != null)
107+
{
108+
bFile.OpenFile();
109+
byte[] buffer = new byte[bFile.Length];
110+
int bytesReadTotal = 0;
111+
while (bytesReadTotal < buffer.Length)
112+
{
113+
int bytesRead = bFile.Read(buffer, bytesReadTotal, buffer.Length - bytesReadTotal);
114+
if (bytesRead == 0)
115+
break;
116+
bytesReadTotal += bytesRead;
117+
}
118+
content = Convert.ToBase64String(buffer);
119+
bFile.Close();
120+
}
121+
}
122+
catch (IndexOutOfRangeException)
123+
{
124+
return content;
125+
}
126+
return content;
127+
}
128+
73129
}
74130
}

0 commit comments

Comments
 (0)