Skip to content

Commit

Permalink
Fixing Some Coverity Issues
Browse files Browse the repository at this point in the history
Signed-off-by: Saranga Athukorale <[email protected]>
  • Loading branch information
sathukorale committed Apr 12, 2019
1 parent 0a6fd2d commit 8d8f199
Show file tree
Hide file tree
Showing 2 changed files with 280 additions and 252 deletions.
101 changes: 54 additions & 47 deletions libDatabaseHelper/classes/mysql/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,70 +326,77 @@ public override void FillDataTable(Type type, ref DataTable table, Selector[] se
if (obj == null)
return;

var commandString = DatabaseEntity.GetSelectCommandString(type);
var whereQuery = "";
var command = GenericConnectionManager.GetConnectionManager(GetSupportedDatabase()).GetConnection(type).CreateCommand();

if (selectors != null && selectors.Any())
using (var command = GenericConnectionManager.GetConnectionManager(GetSupportedDatabase()).GetConnection(type).CreateCommand())
{
whereQuery = selectors.Aggregate(whereQuery, (current, selector) => current + ((current == "" ? "" : " AND ") + selector.SetToCommand(ref command)));
}
var commandString = DatabaseEntity.GetSelectCommandString(type);
var whereQuery = "";

command.CommandText = commandString + (whereQuery != "" ? (" WHERE " + whereQuery) : "") + (limit > 0 ? (" LIMIT " + limit) : "");
if (selectors != null && selectors.Any())
{
whereQuery = selectors.Aggregate(whereQuery, (current, selector) =>
{
var dbCommand = command;
return current + ((current == "" ? "" : " AND ") + selector.SetToCommand(ref dbCommand));
});
}

var reader = command.ExecuteReader();
command.CommandText = commandString + (whereQuery != "" ? (" WHERE " + whereQuery) : "") + (limit > 0 ? (" LIMIT " + limit) : "");

var fieldInfos = obj.GetColumns(true).GetOtherColumns().Where(i => ((TableColumn)i.GetCustomAttributes(typeof(TableColumn), true)[0]).IsRetrievableFromDatabase).ToArray();
var reader = command.ExecuteReader();

table.Rows.Clear();
table.Columns.Clear();
var fieldInfos = obj.GetColumns(true).GetOtherColumns().Where(i => ((TableColumn)i.GetCustomAttributes(typeof(TableColumn), true)[0]).IsRetrievableFromDatabase).ToArray();

var translators = new List<ITranslator>();
table.Rows.Clear();
table.Columns.Clear();

foreach (var fieldInfo in fieldInfos)
{
var cInfo = ((TableColumn)fieldInfo.GetCustomAttributes(typeof(TableColumn), true)[0]);
if (fieldInfo.FieldType == typeof(bool))
table.Columns.Add(cInfo.GridDisplayName ?? fieldInfo.Name, typeof(bool));
else
table.Columns.Add(cInfo.GridDisplayName ?? fieldInfo.Name);
var translators = new List<ITranslator>();

var translator = cInfo.TranslatorType == null ? null : TranslatorRegistry.Instance.Get(cInfo.TranslatorType);
translators.Add(translator);
}
foreach (var fieldInfo in fieldInfos)
{
var cInfo = ((TableColumn)fieldInfo.GetCustomAttributes(typeof(TableColumn), true)[0]);
if (fieldInfo.FieldType == typeof(bool))
table.Columns.Add(cInfo.GridDisplayName ?? fieldInfo.Name, typeof(bool));
else
table.Columns.Add(cInfo.GridDisplayName ?? fieldInfo.Name);

var translator = cInfo.TranslatorType == null ? null : TranslatorRegistry.Instance.Get(cInfo.TranslatorType);
translators.Add(translator);
}

if (!reader.Read())
{
try { reader.Close(); } catch { /* IGNORED */ }
command.Dispose();
return;
}
if (!reader.Read())
{
reader.Close();
return;
}

frmLoadingDialog.ShowWindow();
do
{
if (frmLoadingDialog.GetStatus())
break;
var row = table.Rows.Add();
for (var i = 0; i < table.Columns.Count; i++)
frmLoadingDialog.ShowWindow();

do
{
try
if (frmLoadingDialog.GetStatus())
break;

var row = table.Rows.Add();
for (var i = 0; i < table.Columns.Count; i++)
{
if (translators[i] != null)
try
{
var value = reader[i];
row[i] = (translators.Count > i && i >= 0) ? translators[i].ToTranslated(value) : value.ToString();
if (translators[i] != null)
{
var value = reader[i];
row[i] = (translators.Count > i && i >= 0) ? translators[i].ToTranslated(value) : value.ToString();
}
else
row[i] = reader[i];
}
else
row[i] = reader[i];
catch { /* IGNORED */ }
}
catch { }
}
while (reader.Read());

reader.Close();
frmLoadingDialog.HideWindow();
}
while (reader.Read());
try { reader.Close(); } catch { /* IGNORED */ }
frmLoadingDialog.HideWindow();
command.Dispose();
}

public override PrimaryKeyConstraintDetails GetPrimaryKeyDetails(Type type)
Expand Down
Loading

0 comments on commit 8d8f199

Please sign in to comment.