How to Run Query Through Classes on d365fo x++
Run Query Through Class
1. Add Base Enum
2. Add Extended Data Types
3. Add These Two Classes
- SqlBrowser
- SQLHelper
class SqlBrowser
{
public static void Main(Args _args)
{
if(isSystemAdministrator())
{
Dialog Dialog = new Dialog("SQL");
DialogField querySelection = Dialog.addField(enumStr(QuerySelectionType),"SQL query Type");
DialogField queryInput = Dialog.addField(extendedTypeStr(QueryEdt),"SQL query");
//Note extendedTypeStr used is string EDT with memo string size i.e no restriction on how bug the query is
Dialog.run();
if(Dialog.closedOk() && queryInput.value() && querySelection.value() == QuerySelectionType::Select)
{
str query = queryInput.value();
container con = SQLHelper::getExpectedResultFromQuery(query);
container con1 = conpeek(con,1);
info(con2Str(con1));
File::SendStringAsFileToUser(con2Str(con1),'sqlresult.txt');//exporting result of select statement to text file
}
else if(Dialog.closedOk() && queryInput.value() && querySelection.value() != QuerySelectionType::Select)
{
str query = queryInput.value();
SQLHelper::Manipulating_data(query);
info("Row(s) Affected");
}
}
}
}
-----------------------------------------------------------------------------------------------------
class SQLHelper
{
/// <summary>
/// Returns the rows obtained after executing a SQL statement on the DB, after formatting them w.r.t the data type. Converts the rows from ResultSet into Container.
/// </summary>
/// <param name="queryStmt">
/// The SQL statement executed on the backend DB.
/// </param>
/// <returns>
/// Container of rows returned after executing the result.
/// </returns>
/// <remarks>
/// </remarks>
public static Container getExpectedResultFromQuery(str queryStmt)
{
int i;
int colType;
container expectedSubResult, expectedResult;
int counter;
ResultSet resultSet;
;
try
{
// Executes the Query statement on the back end database.
resultSet = SQLHelper::resultSetExecuteQuery(queryStmt);
while(resultSet.next())
{
expectedSubResult = connull();
for ( i = 1; i <= resultSet.getMetaData().getColumnCount(); i++)
{
colType = resultSet.getMetaData().getColumnType(i);
switch (colType)
{
case 0: // String
case 8: // Memo
expectedSubResult += resultSet.getString(i);
break;
case 1: // Interger
expectedSubResult += resultSet.getInt(i);
break;
case 2: // Real
expectedSubResult += resultSet.getReal(i);
break;
case 3: // Date
expectedSubResult += resultSet.getDate(i);
break;
case 4: // Enum
expectedSubResult += resultSet.getBoolean(i);
break;
case 6: // UtcDateTime
expectedSubResult += dateTime2str(resultSet.getDateTime(i));
break;
case 45: // Guid
expectedSubResult += guid2str(resultSet.getGuid(i));
break;
case 49: // Int64
expectedSubResult += resultSet.getInt64(i);
break;
default:
break;
}
// End of Switch
}
// End of for
expectedResult += [expectedSubResult];
}
//info(con2Str(expectedSubResult));//can be commented- just used to info expectedSubResul
// End of while
}
catch
{
Error('error');
}
return expectedResult;
}
/// <summary>
/// Returns the set of rows after executing the SQL statement on the backend database using the ResultSet class.
/// </summary>
/// <param name="strQuery">
/// The SQL statement executed on the backend DB.
/// </param>
/// <returns>
/// ResultSet containg the rows obtained after executing the Query
/// </returns>
/// <remarks>
/// </remarks>
private server static ResultSet resultSetExecuteQuery(str strQuery)
{
Connection connection = new Connection();
Statement statement = connection.createStatement();
SqlStatementExecutePermission permission;
ResultSet resultSet;
// Get the formatted Query statement.
strQuery = strfmt(strQuery,SRSStatementQuery::getDbSchemaPrefix());
permission = new SqlStatementExecutePermission(strQuery);
permission.assert();
resultSet = statement.executeQuery(strQuery);
return resultSet;
}
public static void Manipulating_data(str queryStmt)
{
str sql;
Connection conn;
SqlStatementExecutePermission permission;
;
sql = queryStmt;
permission = new SqlStatementExecutePermission(sql);
conn = new Connection();
permission = new SqlStatementExecutePermission(sql);
permission.assert();
conn.createStatement().executeUpdate(sql);
// the permissions needs to be reverted back to original condition.
CodeAccessPermission::revertAssert();
}
}
----------------------------------------------------------------------------------------------------------------
How to Access:
To access SqlBrowser class
Enter the following URL
www.YourERPUrl.com/?mi=sysclassrunner&cls=SqlBrowser
Enter the following URL
www.YourERPUrl.com/?mi=sysclassrunner&cls=SqlBrowser
Output:
Comments
Post a Comment