How to fetch data from SQL database using TruClient protocol - javascript

During my load test, I would like to fetch values from an SQL database. How can I achieve this on load runner TrueClient protocol using JavaScript?
This would be great help…

Important: This will only work in TruClient (IE) and not in TruClient (Firefox).
Enter a new "Eveluate Javascript" step, and edit the javasctipt like so:
var connection = new ActiveXObject("ADODB.Connection") ;
var connectionstring="Data Source=<server>;Initial Catalog=<catalog>;User ID=<user>;Password=<password>;Provider=SQLOLEDB";
connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("SELECT * FROM table", connection);
rs.MoveFirst
while(!rs.eof)
{
// Here you should get the value from the 1st cell, 1st column
var value = rs.fields(1);
rs.movenext;
}
rs.close;
connection.close;

There are several options.
I'll list them in order of their complexity:
Option 1:
Use a parameter file to hold all of your data. If you need to modify it periodically, consider placing it in a shared location, accessible for all LGs.
Option 2:
Use the Virtual Table Server (VTS) provided with LoadRunner. It is dedicated to sharing test data between virtual-users. Queries are easy with a built in API.
Option 3:
You could write a custom C function, using LoadRunner DB API to query the DB, calling the function from your script using an Eval C step.
Note this can only be done in VuGen.

Related

Count id in Data Extension and Upsert to another Data Extension using SSJS

This question might be a basic one but I am very new to SSJS so thank you for your understanding.
The data extension names JourneyA, JourneyB, JourneyC...infinity are the result of Journey Builder. Then, I got the data extension name AllJourneys from _Journey.
SELECT JourneyName as "JourneyName",
FROM _Journey j
INNER JOIN(select JourneyID, max(CreatedDate) as MaxDate FROM _Journey
GROUP BY JourneyID) sort on sort.JourneyID = j.JourneyID and j.CreatedDate = sort.MaxDate
After that, I would like to count the number of audiences in each journey and put the results in data extension name Summary using upsert. Additionally, data extension name Summary is a non-sendable data extension.
According to my understanding, the data extension name Summary can be done by SSJS (Script Activity in Automation Studio), right? or please suggest me if it can be done by any other way.
I have done some SSJS but cannot figure out what I sould do next.
<script runat="server">
Platform.Load("core","1");
var AllJourneys = DataExtension.Init('AllJourneys');
var AllJourneysData = testDE.Rows.Retrieve();
var Summary = DataExtension.Init('Summary');
for (var i = 0; i < testDEData.length; i++) {
var JourneyName = AllJourneysData[i].JourneyName;
var count = xxx
var result = Summary.Rows.Upsert({"JourneyName":JourneyName}, ['Count(CusID)'], [count]);
}
</script>
If you know the number of Journey DE's, you could also use SQL Query Activities in Automation studio to do this. Create your data extension for the Summary table, and then run a SQL query or series of queries to look up each of the journey DE's and update the table with the updated value.
I would recommend adding one additional field to your summary table for a REFRESHDATE, so that you can see in the data the last time it was updated.
I suggest you to use the WSproxy to manage data extension data.
The main problem that you have to bypass is that SSJS cannot retrieve more than 2,500 rows per "call".
This tutorial explains how to retrieve more than 2,500 rows.
https://ampscript.xyz/how-tos/how-to-retrieve-more-than-2500-records-from-a-data-extension-with-server-side-javascript/
Then I suggest you to use basic javascript function to check the length or simply manage your returned json to get the desired id count.
You can finally upsert that data with t

Inserting data from JS script into mysql database

I have created a script to count down whatever value I submit into a form and then output "the submitted value + the date of the moment I clicked on the submit button" as a result.
But now I want to store the result into my database every time I use the form by using SQL query and then echo all of these results in another page named "log.php" using SELECT SQL query.
var timelog = [];
function myF() {
countdown(s);
log = document.getElementById("log").innerHTML = s + 'at ' + new Date();
timelog.push(log);
}
function logged() {
document.getElementById("timeloggg").innerHTML = timelog;
}
I have tried to assign the result to a variable, but obviously, I cant use this variable outside of the script.
With some googling, I was told to use Ajax, but sadly I couldn't figure out how to insert the data using ajax, because all of the code examples out there are only about calling data from the database.
So any advice on how to insert the result into my database? I'm still a beginner so please explain in detail if you don't mind.
It is possible, of course, to insert data into your database from client side js, BUT DONT! I can't think of a way to do it that would not expose your database credentials, leaving you open to malicious actors.
What you need to do is set up a php script on your server, then send the data (either by POST or GET) you want inserted to that with an xhr request, and let that php script do the insert. HOWEVER, there is quite a bit to securing even that. Google "how to sanitize mysql inputs in php" and read several articles on it.
Depending on what you need to do, you can sanitize the inputs yourself, but the recommended way to do it is with prepared statements, which you will need to read the documentation for your specific implementation, whether it's mysqli or pdo in mySQL or some other library (say if you're using SQL, postGRE, Oracle, etc).
HTH
=================================================
Here is how to do it in js, BUT DONT DO THIS, unless you are never going to expose this code outside of your local computer.
var connection = new ActiveXObject("ADODB.Connection");
var connectionstring = "Provider=host;Data Source=table;User Id=user;Password=pass;";
connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");
var sql = {{your sql statement}};
rs.Open(sql, connection);
connection.close;
==============================================
For php, do something like this, replacing host, user, pass, db with your actual credentials and hostname and database:
$db = new mysqli({host}, {user}, {pass}, {database});
if($db->connect_errno > 0){ die ("Unable to connect to database [{$db->connect_error}]"); }
to set the connection. If this is a publicly accessible php server, then there are rules about how to set up the connection so that you don't accidentally expose your credentials, but I'm going to skip that for now. You would basically save this into a file that's not accessible from the outside (above the document root, for instance) and then include it, but database security is a complex topic.
To get the values you passed in the query string of your ajax call:
$val1 = $_GET['val1'];
$val2 = $_GET['val2'];
Then to do the insert with a parameterized query:
$query = $db->prepare("
INSERT INTO your_table (field1, field2)
VALUES (?, ?)
");
$query->bind_param('ss', $val1, $val2);
$query->execute();
Now, here you're going to have to look at the documentation. 'ss' means that it's going to treat both of those values you're inserting as strings. I don't know the table set up, so you'll have to look up the right code for whatever you are actually inserting, like if they were integers, then 'ii', or 'si' would mean the first value was a string and the second one was an int.
Here are the allowed values:
i - integer
d - double
s - string
b - BLOB
but look at the documentation for prepared statements anyway. I used msqli in this example.
You might want to check Ajax requests.
I would suggest to start here.
What you will do is basically create asynchronous requests from javascript to a php file on your server.
Ajax allows web pages to be updated asynchronously by exchanging small
amounts of data with the server behind the scenes. This means that it
is possible to update parts of a web page, without reloading the whole
page.

Is there a way to migrate BigQuery tables by using wildcards?

I have a set of tables on BigQuery with some kind of data, and I want to process that data via a JavaScript function I defined. The JS function maps the old data to a new schema, that has to be the one implemented by the new tables.
My set of tables has a common prefix and I want to migrate all of them together to the new schema, by creating tables with a different prefix but keeping the same suffix for all of them.
Example: I have 100 tables called raw_data_SUFFIX and I want to migrate them to 100 tables with a new schema called parsed_data_SUFFIX, keeping each suffix.
This is the simple query for migrating the data
SELECT some_attribute, parse(another_attribute) as parsed
FROM `<my-project>.<dataset>.data_*`
Is there a way to do it via the BigQuery UI?
In order to achieve what you aim, you wold have to use a DDL statement CREATE TABLE as follows:
CREATE TABLE 'project_id.dataset.table' AS SELECT * FROM `project_id.dataset.table_source`
However, it would not be possible to reference multiple destinations with wildcards. As stated in the documentation, here, there are some limitations when using wildcards, among them:
Queries that contain DML statements cannot use a wildcard table as the
target of the query. For example, a wildcard table can be used in the
FROM clause of an UPDATE query, but a wildcard table cannot be used as
the target of the UPDATE operation.
Nonetheless, you can use the Python API to make a request to BigQuery. Then, save each view to a new table, each table's name with a new prefix and old suffix. You can do it as below:
from google.cloud import bigquery
client = bigquery.Client()
dataset_id = 'your_dataset_id'
#list all the tables as objects , each obj has table.project,table.dataset_id,table.table_id
tables = client.list_tables(dataset_id)
#initialising arrays (not necessary)
suffix=[]
table_reference=[]
#looping through all the tables in you dataset
for table in tables:
#Filter if the table's name start with the prefix
if "your_table_prefix" in table.table_id:
#retrieves the suffix, which will be used in the new table's name
#extracts the suffix of the table's name
suffix=table.table_id.strip('your_table_prefix')
#reference the source table
table_reference=".".join([table.project,table.dataset_id,table.table_id])
#table destination with new prefix and old suffix
job_config = bigquery.QueryJobConfig()
table_ref = client.dataset(dataset_id).table("_".join(['new_table_prefix',suffix]))
job_config.destination = table_ref
sql='''
CREATE TEMP FUNCTION
function_name ( <input> )
RETURNS <type>
LANGUAGE js AS """
return <type>;
""";
SELECT function_name(<columns>) FROM `{0}`'''.format(table_reference)
query_job = client.query(
sql,
# Location must match that of the dataset(s) referenced in the query
# and of the destination table.
location='US',
job_config=job_config)
query_job.result() # Waits for the query to finish
print('Query results loaded to table {}'.format(table_ref.path))
Notice that in the sql query, first ''' then """ were used in order to define the query and the JS Temp Function, respectively.
I would like to point that you have to make sure you environment has the appropriate packages to use the Python API for BigQuery, here. You can install the BigQuery package using: pip install --upgrade google-cloud-bigquery.

SQL.js in javascript

I want to store data in a SQLite database directly from a javascript script. I found this SQL.js library that is a port for javascript. However, apparently it's only available for coffeescript. Does anyone know how to use it in javascript? Other ideas about how to store data in SQLite DB are welcomed too.
Update
sql.js now has its own github organisation, where both the original author and I are members: https://github.com/sql-js/sql.js/ .
The API itself itself is now written in javascript.
Original answer
I am the author of this port of the latest version of sqlite to javascript: https://github.com/lovasoa/sql.js
It is based on the one you mentioned (https://github.com/kripken/sql.js), but includes many improvements, including a full documentation: http://lovasoa.github.io/sql.js/documentation/
Here is an example of how to use this version of sql.js
<script src='js/sql.js'></script>
<script>
//Create the database
var db = new SQL.Database();
// Run a query without reading the results
db.run("CREATE TABLE test (col1, col2);");
// Insert two rows: (1,111) and (2,222)
db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,222]);
// Prepare a statement
var stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
stmt.getAsObject({$start:1, $end:1}); // {col1:1, col2:111}
// Bind new values
stmt.bind({$start:1, $end:2});
while(stmt.step()) { //
var row = stmt.getAsObject();
// [...] do something with the row of result
}
</script>
I'm using SQL.js from pure JavaScript without any problems. Simply include the following file:
https://cdnjs.com/libraries/sql.js

Local HTML 5 database usable in Mac Dashboard wigdets?

I'm trying to use HTML 5's local database feature on a Mac Dashboard widget.
I'm programming in Dashcode the following javascript:
if (window.openDatabase)
{
database = openDatabase("MyDB", "1.0", "Sample DB", 1000);
if (database)
{
...database code here...
}
}
Unfortunately the database-variable remains always null after the call to openDatabase-method. I'm starting to think that local databases are not supported in Widgets...
Any ideas?
/pom
No you will not be able to do the above. And even if you could then you would not be able to distribute the widget without distributing the database assuming it was a MySQL or SGLite. (not sure what you mean by HTML 5's local Db.
here are a number of ways round this:-
You can add a data source which can be a JSON file, or an XML file or and RSS feed. So to do this with JSON for example you would write a page on a server in PHP or something that accessed a database so that when the URL was called the result was a JSON string. Take the JSON string and parse it and use it in the Widget. This will let you get data but not save it.
Another way would be to use the user preferences. This allows you to save and retrieve data in the individual widget.
So
var preferenceKey = "key"; // replace with the key for a preference
var preferenceValue = "value"; // replace with a preference to save
// Preference code
widget.setPreferenceForKey(preferenceValue, preferenceKey);
You can then retrieve it with
var preferenceForKey = "key"; // replace with the key for a preference
// Preference code
preferenceForKey = widget.preferenceForKey(preferenceForKey);
The external call, you could also use REST will let you read any amount of data in and the preferences will let you save data for later reuse that will survive log out's and shut downs.
The Apple site has a lot of information about Widgets and tutorials as well thjat are worth working through.
Hope this helps.

Categories