Skip to content

Commit 3d4b4e7

Browse files
committed
Added initial support for database integration #138
1 parent 4159b87 commit 3d4b4e7

15 files changed

+607
-2
lines changed

taskt/Core/Automation/Commands/BeginListLoopCommand.cs

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public override void RunCommand(object sender, Core.Script.ScriptAction parentCo
7171
{
7272
listToLoop = (List<OpenQA.Selenium.IWebElement>)complexVariable.VariableValue;
7373
}
74+
else if (complexVariable.VariableValue is DataTable)
75+
{
76+
listToLoop = ((DataTable)complexVariable.VariableValue).Rows;
77+
}
7478
else
7579
{
7680
throw new Exception("Complex Variable List Type<T> Not Supported");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using System;
2+
using System.Xml.Serialization;
3+
using System.Data;
4+
using System.Collections.Generic;
5+
using taskt.UI.Forms;
6+
using System.Windows.Forms;
7+
using taskt.UI.CustomControls;
8+
using System.Data.OleDb;
9+
using System.Drawing;
10+
11+
namespace taskt.Core.Automation.Commands
12+
{
13+
14+
15+
16+
[Serializable]
17+
[Attributes.ClassAttributes.Group("Database Commands")]
18+
[Attributes.ClassAttributes.Description("This command allows you to define a connection to an OLEDB data source")]
19+
[Attributes.ClassAttributes.UsesDescription("Use this command to create a new connection to a database.")]
20+
[Attributes.ClassAttributes.ImplementationDescription("This command implements 'OLEDB' to achieve automation.")]
21+
public class DatabaseDefineConnectionCommand : ScriptCommand
22+
{
23+
[XmlAttribute]
24+
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
25+
[Attributes.PropertyAttributes.InputSpecification("Enter the unique instance name that was specified in the **Create Excel** command")]
26+
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **seleniumInstance**")]
27+
[Attributes.PropertyAttributes.Remarks("Failure to enter the correct instance name or failure to first call **Create Excel** command will cause an error")]
28+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
29+
public string v_InstanceName { get; set; }
30+
31+
[XmlAttribute]
32+
[Attributes.PropertyAttributes.PropertyDescription("Define Connection String")]
33+
[Attributes.PropertyAttributes.InputSpecification("")]
34+
[Attributes.PropertyAttributes.SampleUsage("")]
35+
[Attributes.PropertyAttributes.Remarks("")]
36+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
37+
public string v_ConnectionString { get; set; }
38+
39+
[XmlAttribute]
40+
[Attributes.PropertyAttributes.PropertyDescription("Test Connection Before Proceeding")]
41+
[Attributes.PropertyAttributes.PropertyUISelectionOption("Yes")]
42+
[Attributes.PropertyAttributes.PropertyUISelectionOption("No")]
43+
[Attributes.PropertyAttributes.InputSpecification("Select an option which best fits to the specification you would like to make.")]
44+
[Attributes.PropertyAttributes.SampleUsage("Select one of the provided options.")]
45+
[Attributes.PropertyAttributes.Remarks("")]
46+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
47+
public string v_TestConnection { get; set; }
48+
49+
[XmlIgnore]
50+
[NonSerialized]
51+
private TextBox ConnectionString;
52+
public DatabaseDefineConnectionCommand()
53+
{
54+
this.CommandName = "DatabaseDefineConnectionCommand";
55+
this.SelectionName = "Define Database Connection";
56+
this.CommandEnabled = true;
57+
this.CustomRendering = true;
58+
this.v_InstanceName = "sqlDefault";
59+
this.v_TestConnection = "Yes";
60+
}
61+
62+
public override void RunCommand(object sender)
63+
{
64+
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
65+
var connection = v_ConnectionString.ConvertToUserVariable(sender);
66+
var instance = v_InstanceName.ConvertToUserVariable(sender);
67+
var testPreference = v_TestConnection.ConvertToUserVariable(sender);
68+
69+
var oleDBConnection = new OleDbConnection(connection);
70+
71+
//attempt to open and close connection
72+
if (testPreference == "Yes")
73+
{
74+
oleDBConnection.Open();
75+
oleDBConnection.Close();
76+
}
77+
78+
engine.AddAppInstance(instance, oleDBConnection);
79+
80+
}
81+
public override List<Control> Render(frmCommandEditor editor)
82+
{
83+
base.Render(editor);
84+
85+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_InstanceName", this, editor));
86+
87+
CommandItemControl helperControl = new CommandItemControl();
88+
helperControl.Padding = new Padding(10, 0, 0, 0);
89+
helperControl.ForeColor = Color.AliceBlue;
90+
helperControl.Font = new Font("Segoe UI Semilight", 10);
91+
helperControl.Name = "connection_helper";
92+
helperControl.CommandImage = UI.Images.GetUIImage("VariableCommand");
93+
helperControl.CommandDisplay = "Build Connection String";
94+
helperControl.Click += (sender, e) => Button_Click(sender, e);
95+
96+
97+
ConnectionString = (TextBox)CommandControls.CreateDefaultInputFor("v_ConnectionString", this);
98+
99+
var connectionLabel = CommandControls.CreateDefaultLabelFor("v_ConnectionString", this);
100+
var connectionHelpers = CommandControls.CreateUIHelpersFor("v_ConnectionString", this, new[] { ConnectionString }, editor);
101+
102+
RenderedControls.Add(connectionLabel);
103+
RenderedControls.Add(helperControl);
104+
RenderedControls.AddRange(connectionHelpers);
105+
RenderedControls.Add(ConnectionString);
106+
107+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_TestConnection", this, editor));
108+
109+
return RenderedControls;
110+
111+
}
112+
113+
private void Button_Click(object sender, EventArgs e)
114+
{
115+
ShowConnectionBuilder();
116+
}
117+
118+
public void ShowConnectionBuilder()
119+
{
120+
121+
var MSDASCObj = new MSDASC.DataLinks();
122+
var connection = new ADODB.Connection();
123+
MSDASCObj.PromptEdit(connection);
124+
125+
if (!string.IsNullOrEmpty(connection.ConnectionString))
126+
{
127+
ConnectionString.Text = connection.ConnectionString;
128+
}
129+
}
130+
131+
public override string GetDisplayValue()
132+
{
133+
return $"{base.GetDisplayValue()} - [Instance Name: '{v_InstanceName}']";
134+
}
135+
}
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using System;
2+
using System.Xml.Serialization;
3+
using System.Data;
4+
using System.Collections.Generic;
5+
using taskt.UI.Forms;
6+
using System.Windows.Forms;
7+
using taskt.UI.CustomControls;
8+
using System.Data.OleDb;
9+
10+
namespace taskt.Core.Automation.Commands
11+
{
12+
[Serializable]
13+
[Attributes.ClassAttributes.Group("Database Commands")]
14+
[Attributes.ClassAttributes.Description("This command allows you to perform a database query and apply the result to a dataset")]
15+
[Attributes.ClassAttributes.UsesDescription("Use this command to select data from a database.")]
16+
[Attributes.ClassAttributes.ImplementationDescription("This command implements 'OLEDB' to achieve automation.")]
17+
public class DatabaseExecuteQueryCommand : ScriptCommand
18+
{
19+
[XmlAttribute]
20+
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
21+
[Attributes.PropertyAttributes.InputSpecification("Enter the unique instance name that was specified in the **Create Excel** command")]
22+
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **seleniumInstance**")]
23+
[Attributes.PropertyAttributes.Remarks("Failure to enter the correct instance name or failure to first call **Create Excel** command will cause an error")]
24+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
25+
public string v_InstanceName { get; set; }
26+
27+
[XmlAttribute]
28+
[Attributes.PropertyAttributes.PropertyDescription("Define Query Execution Type")]
29+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
30+
[Attributes.PropertyAttributes.PropertyUISelectionOption("Return Dataset")]
31+
[Attributes.PropertyAttributes.PropertyUISelectionOption("Execute NonQuery")]
32+
[Attributes.PropertyAttributes.InputSpecification("")]
33+
[Attributes.PropertyAttributes.SampleUsage("")]
34+
[Attributes.PropertyAttributes.Remarks("")]
35+
public string v_QueryType { get; set; }
36+
37+
[XmlAttribute]
38+
[Attributes.PropertyAttributes.PropertyDescription("Define Query to Execute")]
39+
[Attributes.PropertyAttributes.InputSpecification("")]
40+
[Attributes.PropertyAttributes.SampleUsage("")]
41+
[Attributes.PropertyAttributes.Remarks("")]
42+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
43+
public string v_Query { get; set; }
44+
45+
[XmlAttribute]
46+
[Attributes.PropertyAttributes.PropertyDescription("Apply Result To Variable")]
47+
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
48+
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
49+
[Attributes.PropertyAttributes.Remarks("If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended.")]
50+
public string v_DatasetName { get; set; }
51+
52+
public DatabaseExecuteQueryCommand()
53+
{
54+
this.CommandName = "DatabaseExecuteQueryCommand";
55+
this.SelectionName = "Execute Database Query";
56+
this.CommandEnabled = true;
57+
this.CustomRendering = true;
58+
this.v_InstanceName = "sqlDefault";
59+
60+
}
61+
62+
public override void RunCommand(object sender)
63+
{
64+
//create engine, instance, query
65+
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
66+
var vInstance = v_InstanceName.ConvertToUserVariable(engine);
67+
var query = v_Query.ConvertToUserVariable(sender);
68+
69+
//define connection
70+
var databaseConnection = (OleDbConnection)engine.GetAppInstance(vInstance);
71+
var queryExecutionType = v_QueryType.ConvertToUserVariable(sender);
72+
73+
//define commad
74+
var oleCommand = new System.Data.OleDb.OleDbCommand(query, databaseConnection);
75+
76+
if (queryExecutionType == "Return Dataset")
77+
{
78+
79+
DataTable dataTable = new DataTable();
80+
OleDbDataAdapter adapter = new OleDbDataAdapter(oleCommand);
81+
databaseConnection.Open();
82+
adapter.Fill(dataTable);
83+
databaseConnection.Close();
84+
85+
86+
dataTable.TableName = v_DatasetName;
87+
engine.DataTables.Add(dataTable);
88+
89+
engine.AddVariable(v_DatasetName, dataTable);
90+
91+
}
92+
else if (queryExecutionType == "Execute NonQuery")
93+
{
94+
databaseConnection.Open();
95+
var result = oleCommand.ExecuteNonQuery();
96+
databaseConnection.Close();
97+
98+
engine.AddVariable(v_DatasetName, result.ToString());
99+
}
100+
else
101+
{
102+
throw new NotImplementedException($"Query Execution Type '{queryExecutionType}' not implemented.");
103+
}
104+
105+
}
106+
public override List<Control> Render(frmCommandEditor editor)
107+
{
108+
base.Render(editor);
109+
110+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_InstanceName", this, editor));
111+
112+
113+
var queryControls = CommandControls.CreateDefaultInputGroupFor("v_Query", this, editor);
114+
var queryBox = (TextBox)queryControls[2];
115+
queryBox.Multiline = true;
116+
queryBox.Height = 150;
117+
118+
RenderedControls.AddRange(queryControls);
119+
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_QueryType", this, editor));
120+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_DatasetName", this, editor));
121+
return RenderedControls;
122+
123+
}
124+
125+
public override string GetDisplayValue()
126+
{
127+
return $"{base.GetDisplayValue()} - [{v_QueryType}, Apply Result to Variable '{v_DatasetName}', Instance Name: '{v_InstanceName}']";
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)