Skip to content

Commit 1cb83e4

Browse files
committed
Oracle FetchData<T>() - GEt BFILE as byte[]
1 parent 32b4d41 commit 1cb83e4

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
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 string BFile_Column { get; set; }
162+
public byte[] 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/TestBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,10 @@ protected string GetBase64Content(string filePath)
9797
string base64Content = Convert.ToBase64String(fileBytes);
9898
return base64Content;
9999
}
100+
101+
internal static string ConvertByteArrayToBase64(byte[] data)
102+
{
103+
return Convert.ToBase64String(data);
104+
}
100105
}
101106
}

QueryDB/Oracle/Adapter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ internal List<DataDictionary> FetchData(string selectSql, OracleConnection conne
4848
{
4949
string key = upperCaseKeys ? reader.GetName(i).ToUpper() : reader.GetName(i);
5050
if (Utils.IsBFileColumn(reader, i))
51-
addRow.ReferenceData.Add(key, Utils.GetBFileContent(reader, i));
51+
addRow.ReferenceData.Add(key, Utils.GetBFileBase64Content(reader, i));
5252
else if (reader.GetValue(i) is byte[] value)
5353
addRow.ReferenceData.Add(key, Convert.ToBase64String(value));
5454
else
@@ -81,7 +81,7 @@ internal List<DataDictionary> FetchData(string selectSql, OracleConnection conne
8181
if ((strict || Utils.ColumnExists(reader, prop.Name)) && !reader.IsDBNull(reader.GetOrdinal(prop.Name)))
8282
{
8383
if (Utils.IsBFileColumn(reader, prop.Name))
84-
prop.SetValue(addObjectRow, Utils.GetBFileContent(reader, prop.Name));
84+
prop.SetValue(addObjectRow, Utils.GetBFileByteContent(reader, prop.Name));
8585
else
8686
prop.SetValue(addObjectRow, reader[prop.Name]);
8787
}

QueryDB/Resources/Utils.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ internal static bool IsBFileColumn(OracleDataReader reader, string columnName)
6868
/// <param name="reader">The Oracle data reader containing the BFILE column.</param>
6969
/// <param name="columnIndex">The index of the BFILE column to read.</param>
7070
/// <returns>Returns the BFILE content as a Base64-encoded string, or an empty string if the BFILE is null.</returns>
71-
internal static string GetBFileContent(OracleDataReader reader, int columnIndex)
71+
internal static string GetBFileBase64Content(OracleDataReader reader, int columnIndex)
7272
{
7373
string content = string.Empty;
7474
var bFile = reader.GetOracleBFile(columnIndex);
@@ -96,7 +96,7 @@ internal static string GetBFileContent(OracleDataReader reader, int columnIndex)
9696
/// <param name="reader">The Oracle data reader containing the BFILE column.</param>
9797
/// <param name="columnName">The name of the BFILE column to read.</param>
9898
/// <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)
99+
internal static string GetBFileBase64Content(OracleDataReader reader, string columnName)
100100
{
101101
string content = string.Empty;
102102
try
@@ -126,5 +126,40 @@ internal static string GetBFileContent(OracleDataReader reader, string columnNam
126126
return content;
127127
}
128128

129+
/// <summary>
130+
/// Retrieves the content of a BFILE column from an Oracle data reader as a byte array, using the column name.
131+
/// </summary>
132+
/// <param name="reader">The Oracle data reader containing the BFILE column.</param>
133+
/// <param name="columnName">The name of the BFILE column to read.</param>
134+
/// <returns>Returns the BFILE content as a byte array, or <c>null</c> if the BFILE is null or the column does not exist.</returns>
135+
internal static byte[] GetBFileByteContent(OracleDataReader reader, string columnName)
136+
{
137+
byte[] buffer = null;
138+
try
139+
{
140+
int columnIndex = reader.GetOrdinal(columnName);
141+
var bFile = reader.GetOracleBFile(columnIndex);
142+
if (bFile != null && !reader.IsDBNull(columnIndex))
143+
{
144+
bFile.OpenFile();
145+
buffer = new byte[bFile.Length];
146+
int bytesReadTotal = 0;
147+
while (bytesReadTotal < buffer.Length)
148+
{
149+
int bytesRead = bFile.Read(buffer, bytesReadTotal, buffer.Length - bytesReadTotal);
150+
if (bytesRead == 0)
151+
break;
152+
bytesReadTotal += bytesRead;
153+
}
154+
bFile.Close();
155+
}
156+
}
157+
catch (IndexOutOfRangeException)
158+
{
159+
return buffer;
160+
}
161+
return buffer;
162+
}
163+
129164
}
130165
}

0 commit comments

Comments
 (0)