remove previously added and() clause from Where com.datastax.driver.core.querybuilder.Select.Where - cassandra

for (int i=0; i<mycolumns.length; i++)
{
where.and(QueryBuilder.eq(COLNAME, mycolumns[i]));
//how to remove the above and() call
}
In every iteration of the loop, I want to execute the query and then substitute the value in next loop iteration.

I'm not completely clear on what you are trying to accomplish. I am guessing that you are trying to update multiple rows sharing a primary key, updating 1 row at a time?
Unfortunately this isn't possible since when you call where.and you are adding data to the Where object and it is returning you a reference to the same Where object.
In short, Where is not immutable and neither is the Statement it belongs to, so you won't get a new copy every time you call it, rather you get an updated version of the Where object.
What you could do is generate your Statement again (whether it be QueryBuilder.update,delete, or insert) in the loop like:
for (int i=0; i<mycolumns.length; i++) {
Statement stmt = QueryBuilder.update("tableName").where(eq("key", 1)).and(QueryBuilder.eq(COLNAME, mycolumns[i]));
session.execute(stmt);
}

Related

Reading Real Time Table - Selenium

I'm trying to read a dynamic table, which is updated 1-3 times per second. I'm using Selenium, in Python 3.x, but if you have a solution for other languages I can work it out as well.
My question is: what is the best practice for reading frequently updated tables?
What I've tried:
driver.wait.until along with expected_conditions
re-read the table with a call to find_elements if a stale exception is thrown
Neither of them is working, due to the high refreshing rate. I can successfully retrieve the table for a moment, but when I try to access its rows the moment after, I get a stale exception. It's worth to say that when I try the same code in the same table when there are less frequent updates everything works fine.
I'm not posting any code for the moment, as I'd be interested in knowing what more experienced people do in this case.
My naive thinking: Being non-expert (but keen to learn) in web scraping nor in any web-related languages, I'd say that if this was a problem with dynamic data, I'd take a pointer or a reference to the actual table (and then looping dynamically on the rows). Is that possible in this framework?
We usually get stale element exception when the Webelement has been changed at present when compared to its attributes at the time of webelement's creation.
Let's say the intent is to print second data element in a table every seconds, our code looks like this, (Sorry for giving the code in Java)
//This will work if the page is static
WebElement element = driver.findElement(By.xpath("//td[2]"));
for(int i = 0; i< 10;i++)
{
System.out.println(element.getText());
Thread.sleep(1000);
}
To make this work for dynamic loading tables / refreshing tables we need to initiate the webelement before the each iteration something like this,
//This will work for dynamic content
WebElement element = null;
for(int i = 0; i< 10;i++)
{
element = driver.findElement(By.xpath("//td[2]"));
System.out.println(element.getText());
Thread.sleep(1000);
}
In the case, if you need to get the i'th cell value in a table, we can parameter the value inside the xpath such as,
//In this case we need the fifth cell value
int j = 5;
WebElement element = null;
for(int i = 0; i< 10;i++)
{
element = driver.findElement(By.xpath("//td["+j+"]"));
System.out.println(element.getText());
Thread.sleep(1000);
}
In the case if you need to have all five cell values,
WebElement element = null;
for(int i = 1; i<=5;i++)
{
element = driver.findElement(By.xpath("//td["+i+]"));
System.out.println(element.getText());
Thread.sleep(1000);
}
Just construct a loop accordingly.
Hope this helps you. Thanks.

NetSuite Update Customer subscription entries

I'm triing to update customer subscriptions list in netsuite.
var itemCount = recLead.getLineItemCount('subscriptions');
for (var i = 1; i < itemCount; i++ ) { recLead.setCurrentLineItemValue('subscriptions', 'subscribed', 'T');}
But error throws:
Notice (SuiteScript)
You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist.
If you are planning on using the "current" line item function, then you do need to select the line to use. As below:
var itemCount=recLead.getLineItemCount('subscriptions');
for(var i=1;i<=itemCount;i++){
recLead.selectLineItem('item',i);
recLead.setCurrentLineItemValue('subscriptions','subscribed','T');
recLead.commitLineItem('item');
}
Alternatively, if you do not want to do it that way, you can use setLineItemValue, instead.
var itemCount=recLead.getLineItemCount('subscriptions');
for(var i=1;i<=itemCount;i++){
recLead.setLineItemValue('subscriptions','subscribed',i,'T');
}
Both, effectively, work the same.
BTW, since you have to start at row 1, you need to make sure you use i<=itemCount. Otherwise, if there are 10 rows, you will miss the last row. When you move to 2.0, and start your count at 0, you can use i< itemCount.
Before using setCurrentLineItemValue, you need to select the line using selectLineItem then commitLineItem to save the changes.

How to call Page.ClientScript.RegisterStartupScript From foreach for each iteration

How to call Page.ClientScript.RegisterStartupScript from foreach for each iteration. It looks like it call all iterations once at end
int saveImageCount=0;
foreach (DataRow Stdrow in key.ColumnValues)
{
saveImageCount++
Page.ClientScript.RegisterStartupScript(GetType(), "SaveImage" + saveImageCount, "javascript:SaveImage();", true);
}
Unable to call javascript:SaveImage for each iteration
They will all have the same type, so they'll override each other until the last one, and the last one will "win".
Instead build up a string and then, outside of the foreach, register the string.
#Jonathan,I think you are a little bit mislead,its Type and key that makes unique combination not only type.
So #brijesh is 95% right but he has to use Register.ClientScriptBlock as it will be registered based on event not on page load.
int saveImageCount = 0;
foreach (DataRow Stdrow in key.ColumnValues)
{
saveImageCount++;
ClientScript.RegisterClientScriptBlock(this.GetType(), "script"+saveImageCount, "alert('data has been added successfully');", true);
}
GetType and "script"+saveImageCount will make a unique key combination and hence your script will be called without hesitation.

C# error Collection was modified; enumeration operation might not execute

Below is my code which am calling and am getting below exception.
Collection was modified; enumeration operation might not execute.
In this code am checking if dataset contains tb_error table name and then checking the row count.
If rowcount> 1 , insert into db.
after that i want to clear that table and after that i need to clear other view also.
Please help me where to modify my code.
if (MainClass.OutputDataset.Tables.Contains(tb_error.TableName))
{
foreach (DataRow drErr in MainClass.OutputDataset.Tables[tb_error.TableName].Rows)
{
//insert into DB
}
}
if (MainClass.OutputDataset != null && MainClass.OutputDataset.Tables["tb_error"].Rows.Count > 0)
{
MainClass.OutputDataset.Tables["tb_error"].Clear();
}
MainClass.dsinput.Tables.Remove("BSData_VW");
}
This happens because the underlying collection has since had items added or removed, which invalidates the loop.
You can get around this by taking a snapshot, e.g.:
foreach (DataRow drErr in MainClass.OutputDataset.Tables[tb_error.TableName].Rows.ToList())
{
//insert into DB
}
The key is the .ToList() call at the end, which means the foreach loop only operates on Rows as it is at the point-in-time.
When you get an error like that, you pretty much have to abandon using foreach and come up with some other looping mechanism. You can try using a for statement or rolling your own with a variable and a while statement.

How to maintain counters with LinqToObjects?

I have the following c# code:
private XElement BuildXmlBlob(string id, Part part, out int counter)
{
// return some unique xml particular to the parameters passed
// remember to increment the counter also before returning.
}
Which is called by:
var counter = 0;
result.AddRange(from rec in listOfRecordings
from par in rec.Parts
let id = GetId("mods", rec.CKey + par.UniqueId)
select BuildXmlBlob(id, par, counter));
Above code samples are symbolic of what I am trying to achieve.
According to the Eric Lippert, the out keyword and linq does not mix. OK fair enough but can someone help me refactor the above so it does work? A colleague at work mentioned accumulator and aggregate functions but I am novice to Linq and my google searches were bearing any real fruit so I thought I would ask here :).
To Clarify:
I am counting the number of parts I might have which could be any number of them each time the code is called. So every time the BuildXmlBlob() method is called, the resulting xml produced will have a unique element in there denoting the 'partNumber'.
So if the counter is currently on 7, that means we are processing 7th part so far!! That means XML returned from BuildXmlBlob() will have the counter value embedded in there somewhere. That's why I need it somehow to be passed and incremented every time the BuildXmlBlob() is called per run through.
If you want to keep this purely in LINQ and you need to maintain a running count for use within your queries, the cleanest way to do so would be to make use of the Select() overloads that includes the index in the query to get the current index.
In this case, it would be cleaner to do a query which collects the inputs first, then use the overload to do the projection.
var inputs =
from recording in listOfRecordings
from part in recording.Parts
select new
{
Id = GetId("mods", recording.CKey + part.UniqueId),
Part = part,
};
result.AddRange(inputs.Select((x, i) => BuildXmlBlob(x.Id, x.Part, i)));
Then you wouldn't need to use the out/ref parameter.
XElement BuildXmlBlob(string id, Part part, int counter)
{
// implementation
}
Below is what I managed to figure out on my own:.
result.AddRange(listOfRecordings.SelectMany(rec => rec.Parts, (rec, par) => new {rec, par})
.Select(#t => new
{
#t,
Id = GetStructMapItemId("mods", #t.rec.CKey + #t.par.UniqueId)
})
.Select((#t, i) => BuildPartsDmdSec(#t.Id, #t.#t.par, i)));
I used resharper to convert it into a method chain which constructed the basics for what I needed and then i simply tacked on the select statement right at the end.

Resources