Friday, 27 September 2013

One To Many LINQ Joins Across Nested Collections

One To Many LINQ Joins Across Nested Collections

I have a development scenario where I am joining two collections with
Linq; a single list of column header objects which contain presentation
metadata, and an enumeration of kv dictionaries which result from a web
service call. I can currently iterate (for) through the dictionary
enumeration, and join the single header list to the current kv dictionary
without issue. After joining, I emit a curated array of dictionary values
for each iteration.
What I would like to do is eliminate the for loop, and join the single
header list directly to the entire enumeration. I understand the 1-to-1
collection join pretty well, but the 1-to-N syntax is eluding me.
Details
I have the following working method:
public void GetQueryResults(DataTable outputTable)
{
var odClient = new ODataClient(UrlBase);
var odResponse = odClient.FindEntries(CommandText);
foreach (var row in odResponse)
{
var rowValues = OutputFields
.Join(row, h => h.Key, r => r.Key,
(h, r) => new { Header = h, ResultRow = r })
.Select(r => r.ResultRow.Value);
outputTable.Rows.Add(rowValues.ToArray());
}
}
odResponse contains IEnumerable<IDictionary<string, object>>; OutputFields
contains IList<QueryField>; the .Join produces an enumeration of anons
containing matched field metadata (.Header) and response kv pairs
(.ResultRow); finally, the .Select emits the matched response values for
row consumption. The OutputField collection looks like this:
class QueryField
{
public string Key { get; set; }
public string Label { get; set; }
public int Order { get; set; }
}
Which is declared as:
public IList<QueryField> OutputFields { get; private set; }
By joining the collection of field headers to the response rows, I can
pluck just the columns I need from the response. If the header keys
contain { "size", "shape", "color" } and the response keys contain {
"size", "mass", "color", "longitude", "latitude" }, I will get an array of
values for { "size", "shape", "color" }, where shape is null, and the
mass, longitude, and latitude values are ignored. For the purposes of this
scenario, I am not concerned with ordering. This all works a treat.
Problem
What I'd like to do is refactor this method to return an enumeration of
value array rows, and let the caller manage the consumption of the data:
public IEnumerable<string[]> GetQueryResults()
{
var odClient = new ODataClient(UrlBase);
var odResponse = odClient.FindEntries(CommandText);
var responseRows = //join OutputFields to each row in odResponse by .Key
return responseRows;
}
Followup Question
Would a Linq-implemented solution for this refactor require an immediate
scan of the enumeration, or can it pass back a lazy result? The purpose of
the refactor is to improve encapsulation without causing redundant
collection scans. I can always build imperative loops to reformat the
response data the hard way, but what I'd like from Linq is something like
a closure.
Thanks heaps for spending the time to read this; any suggestions are
appreciated!

SQL Syntax error on commented T-SQL only in SQL Server 2012

SQL Syntax error on commented T-SQL only in SQL Server 2012

I had an error occur while installing on a customer site using SQL Server
2012. I was able to reproduce the syntax error locally on SQLExpress 2012.
The same DDL script runs fine under 2008 R2 but fails with "Incorrect
syntax near '44445'".
Checking the SQL that is being executed, the text '44445' is commented
out. Again, this SQL works on 2008 R2. The last line posted is the syntax
offender. Notice, it is commented out as is most of this example.
[snipped]
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'Updatable' ,
N'USER',N'dbo', N'TABLE',N'PublishLog', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'Updatable', @value=N'True' ,
@level0type=N'USER',@level0name=N'dbo',
@level1type=N'TABLE',@level1name=N'PublishLog'
GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id =
OBJECT_ID(N'[dbo].[MetaData]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
--BEGIN
--CREATE TABLE [dbo].[MetaData](
-- [ID] [int] IDENTITY(1,1) NOT NULL,
-- [DataName] [nvarchar](255) NULL,
-- [DataDescription] [nvarchar](255) NULL,
-- CONSTRAINT [MetaData_PK] PRIMARY KEY NONCLUSTERED
--(
-- [ID] ASC
--) ON [PRIMARY]
--) ON [PRIMARY]
--END
--GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER OFF
--GO
--IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id =
OBJECT_ID(N'[dbo].[T_MetaData_DTrig]') AND OBJECTPROPERTY(id,
N'IsTrigger') = 1)
--EXEC dbo.sp_executesql @statement = N'CREATE TRIGGER
[dbo].[T_MetaData_DTrig] ON [dbo].[MetaData] FOR DELETE AS
--SET NOCOUNT ON
--/* * PREVENT DELETES IF DEPENDENT RECORDS IN ''DocumentsData'' */
--IF (SELECT COUNT(*) FROM deleted, DocumentsData WHERE (deleted.ID =
DocumentsData.MetaTagsID)) > 0
-- BEGIN
-- RAISERROR 44445 ''The record can''''t be deleted or changed.
Since related records exist in table ''''DocumentsData'''', referential
integrity rules would be violated.''
-- ROLLBACK TRANSACTION
-- END
[snipped]

Corrupted image when posting a tweet using spring framework twitter API

Corrupted image when posting a tweet using spring framework twitter API

I am trying to publish a tweet with an embedded image from a Java Tomcat
server using the Spring framework's twitter API. The image is a JPG hosted
online (via Amazon Cloudfront CDN). I try to post using the updateTweet
function in the code snippet below:
import org.springframework.social.twitter.api.TweetData;
import org.springframework.social.twitter.api.TwitterProfile;
import org.springframework.social.twitter.api.Tweet;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
// ...
public Tweet updateTweet(String accessToken, String accessTokenSecret,
String tweetMessage, String imageUrl){
TwitterTemplate twitter = new TwitterTemplate(twitterConsumerKey,
twitterConsumerSecret, accessToken, accessTokenSecret);
twitter.setRequestFactory(twitterHttpRequestFactory);
TweetData tweetData = new TweetData(tweetMessage);
try {
UrlResource imageUrlResource = new UrlResource(imageUrl);
logger.info("Trying to tweet image with url {} content length {}",
imageUrl, imageUrlResource.contentLength());
tweetData = tweetData.withMedia(imageUrlResource);
} catch(MalformedURLException e) {
logger.error("Malformed url for tweet image: {}", imageUrl);
} catch(IOException e) {
logger.error("IOException for tweet image {}\n{}", imageUrl, e);
}
return twitter.timelineOperations().updateStatus(tweetData);
}
The tweet posts to my user's timeline, and does include a JPG image with
the correct dimensions (640x640, like the source image) - however, the
actual image data is corrupted! Here is an example of the corrupted image
that ends up on my twitter timeline:
https://pbs.twimg.com/media/BVC5M5pCcAApIgP.jpg:large
My first thought was that the image data was being truncated somehow.
However, I've confirmed via the logger.info line in the code sample above
that the URLResource pointing at the image reports a content-length
matching the filesize of the original image.
I am unsure why this code is sending corrupted image data to twitter. I
have searched for working examples that post images to twitter using the
TweetData.withMedia function from Spring's framework, but I haven't been
able to find one.

How to color convert a texture before it's time to display with openGL ES

How to color convert a texture before it's time to display with openGL ES

I'm working on a video player (based on openGL ES) for iPhone where the
frames arrive at the renderer at a rate faster than the display rate
(33ms). As the frame arrive I store them and then when it's time to
display a frame, I use an openGL shader to make a color conversion and the
RGB converted image is displayed on the screen. What I would like to do is
to be able to convert the frames as they arrive but I'm not sure how to
proceed. Can some one describe the steps needed to accomplish this? Also
is this necessary, what I mean is it a good practice to do the color
conversion right when the frame is received and only swap the buffers when
the display callback is fired?

Customize color of reference graph in DbVisualizer

Customize color of reference graph in DbVisualizer

Is it somehow possible to create differenet colored groups in the
reference graph of DbVisualizer Free 9.1?
For Example: I have table user and roles, category and pictures and each
picture is related to an user, but user and roles "boxes" in the diagramm
should be shown in green and the other two in red.
I hope you understand me :)
PS: If it is possible in SchemaSpy, it would be also nice to know :)

Thursday, 26 September 2013

how to get only those which have maximum value in a specified column in asp.net c#

how to get only those which have maximum value in a specified column in
asp.net c#

I have a table with "customer_id, date, installment_no, amount" columns. I
want to get the information of last installment of each customer_id till
today. here installment_no is int type and when a new installment is
deposited, the installment_no is increased by 1 in new entry. My table
look like:
CS1001 | 12-06-2013 | 1 | 2500
CS1002 | 19-06-2013 | 1 | 1600
CS1001 | 14-07-2013 | 2 | 2500
I want to get a sqlcommand statement for do so.

Thursday, 19 September 2013

set flag in signal handler

set flag in signal handler

In C++11, what is the safest (and perferrably most efficient) way to
execute unsafe code on a signal being caught, given a type of request-loop
(as part of a web request loop)?
It is acceptable for the 'unsafe code' to be run on the next request being
fired, and no information is lost if the signal is fired multiple times
before the unsafe code is run.
For example, my current code is:
static bool globalFlag = false;
void signalHandler(int sig_num, siginfo_t * info, void * context) {
globalFlag = true;
}
void doUnsafeThings() {
// thigns like std::vector push_back, new char[1024], set global vars,
etc.
}
void doRegularThings() {
// read filesystem, read global variables, etc.
}
void main(void) {
// set up signal handler (for SIGUSR1) ...
struct sigaction sigact;
sigact.sa_sigaction = onSyncSignal;
sigact.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction(SIGUSR1, &sigact, (struct sigaction *)NULL);
// main loop ...
while(acceptMoreRequests()) { // blocks until new request received
if (globalFlag) {
globalFlag = false;
doUnsafeThings();
}
doRegularThings();
}
}
where I know there could be problems in the main loop testing+setting the
globalFlag boolean.

javafx: how to make a custum listview using css

javafx: how to make a custum listview using css

how to make a listview that each item should have a different icon using
css, if it's possible. THANK YOU

PostgreSQL concurent update selects

PostgreSQL concurent update selects

I am attempting to have some sort of update select for a job queue. I need
it to support concurrent processes affecting the same table or database
This server will be used only for the queue so a database per queue is
acceptable. Orginally I was thinking about something like the following:
UPDATE state=1,ts=NOW() FROM queue WHERE ID IN (SELECT ID FROM queue WHERE
state=0 LIMIT X) RETURN *
Which I been reading that this will cause a race condition, I read that
there was a an option for the SELECT subquery to use FOR UPDATE, but then
that will lock the row and concurrent calls will be blocked where I would
not mind if they skip over to the next unlocked row.
So what i am asking for is the best way to have a fifo system in postgres
that requires the least amount of locking the entire database.

Google Sheets - Move a ROW up

Google Sheets - Move a ROW up

I have a spreadsheet with multiple sheets and I am moving rows from sheet
to sheet when a cell has a certain value. Note we are using drop down data
validation in the cell.
I need help moving a row to the top, just below our locked header row of
the same sheet it is in.
This is what I am trying to do.
if (activatedSheetName == testQueue.getName() && activatedCellColumn == 2
&& activatedCellValue == "1st-Urgent" ) {
testQueue.insertRows(3,1);
var rangeToMove =
testQueue.getRange(activatedCellRow,1,1,testQueue.getMaxColumns());
rangeToMove.moveTo(testQueue.getRange("A3"));
testQueue.getRange("A3").setValue(getTimeStamp());
testQueue.deleteRow(activatedCellRow);
}
I know the above code is not right and would appreciate any help.
Thank you!

I want to convert curl to javascript?

I want to convert curl to javascript?

I want to change this code to java script and i don't have knowledge of http
curl -u username:password \
-H 'Content-Type: application/json' \
-H 'User-Agent: MyApp (yourname@example.com)' \
-d '{ "name": "My new project!" }' \
https://xxxxxx.com/999999999/api/v1/projects.json
to
var option = {
method: "get",
muteHttpExceptions: true,
headers: {
"Content-Type": "application/json",
"User-Agent": "MyApp (yourname@example.com)",
"username": "xxxxxx"
}
};
Thanks

Frequency counts in R

Frequency counts in R

This may seem like a very basic R question, but I'd appreciate an answer.
I have a data frame in the form of:
|column1| column2|
a | g
a | h
a | g
b | i
b | g
b | h
c | i

I want to transform it into counts, so the outcome would be like this.
I've tried using table () function, but seem to only be able to get the
count for one column.
|a | b |c
g|2 1 0
h|1 1 0
i|0 1 1

How do I do it in R?

how to group by with a sql that has multiple subqueries

how to group by with a sql that has multiple subqueries

I am trying to add a group by to this statement:
SELECT SC_JOBS.CREATIONDATE,
(SELECT SUM(SC_JOBS.GROSSEXCLVAT) FROM SC_JOBS WHERE
SC_FRAMES.GROUPPRODUCTTYPE = 'ATI' AND SC_FRAMES.JOBID = SC_JOBS.JOBSID
AND SC_JOBS.INVOICEDATE < '1990-01-01') AS Product 1,
(SELECT SUM(SC_JOBS.GROSSEXCLVAT) FROM SC_JOBS WHERE
SC_FRAMES.GROUPPRODUCTTYPE = 'ATI' AND SC_FRAMES.JOBID = SC_JOBS.JOBSID
AND SC_JOBS.INVOICEDATE < '1990-01-01') AS Product 2
FROM SC_JOBS INNER JOIN SC_FRAMES ON SC_FRAMES.JOBID = SC_JOBS.JOBSID
WHERE SC_JOBS.CREATIONDATE BETWEEN :StartDate AND :EndDate ORDER BY
SC_JOBS.CREATIONDATE
Any suggestions please?

Batch .BAT script to rename files

Batch .BAT script to rename files

Im looking for a batch script to (recursively) rename a folder of files..
Example of the rename: 34354563.randomname_newname.png to newname.png
I already dug up the RegEx for matching from the beginning of the string
to the first underscore (its ^(.*?)_ ), but cant convince Windows Batch to
let me copy or rename using RegEx.

Wednesday, 18 September 2013

hide img tag if src is empty but without javascript/jQuery or css3

hide img tag if src is empty but without javascript/jQuery or css3

, am designing templates in Channel Advisor for eBay store and it doesn't
allow javascript/jQuery. Also, the CSS3 doesn't work in various IE
versions specially the img[src*=] implementation is broken.
When I use template tags in img like:
<img src="{{THUMB(ITEMIMAGEURL1)}}">
where {{THUMB(ITEMIMAGEURL1)}} is the image path, if the image is missing
and the template is posted to eBay then the end result would be like this:
<img src="">
and this shows a broken image.
Is there a way to hide <img src=""> with HTML or CSS that works in IE7+

scrape data from a webpage that has no _viewstate

scrape data from a webpage that has no _viewstate

I want to scrap a webpage containing a list of user with addresses, email
etc. webpage contain list of user with pagination i.e. page contains 2
users when I click on page 2 link it will load users list form 2nd page
and update list so on for all pagination links.
I am trying to collect data from http://www.hyundai.co.uk/dealer-locator
with WC1V 7EP zipcode. An example can be more helpful.
Thank

Calling my function I passed through as a parameter

Calling my function I passed through as a parameter

I have a function:
public void Callback()
{
// Does some stuff
}
I want to pass that in to another function, which then passes it to
another function, which then executes the function... I have this so far:
public void StartApp() // entry point
{
StartMyAwesomeAsyncPostRequest( Callback );
}
public void StartMyAwesomeAsyncPostRequest( Delegate callback )
{
// Work out some stuff and start a post request
TheActualAsyncPostRequest( callback );
}
public void TheActualAsyncPostRequest( Delegate callback )
{
// Do some jazz then run the delegated function
callback();
}
I have looked through a few other examples but coming from a PHP and
javascript background, I'm struggling with the explanations, can you
perhaps give me an example or explanation specific to my request? Thanks
in advance!

Sybase/JDBC: how to detect reorgs or exclusive locks?

Sybase/JDBC: how to detect reorgs or exclusive locks?

We use Sybase ASE (15.5) server as our DB and are having strange,
intermittent SPID blocking issues that I am trying to detect and mitigate
programmatically at the application-layer.
Sybase allows you to schedule so-called "reorgs" which from what I can
tell are periodic re-indexes/table compactions, cleanups, etc. Scheduled
DB maintenance, basically.
Every once in a while, we get all the planets coming into alignment with
each other, where:
A query is executed (creating a SPID in Sybase) and hangs for some reason.
This places a (blocking) shared lock on, say, the widgets table; then
The scheduled reorg kicks off, and wants to cleanup the widgets table. The
reorg places an exclusive lock request on widgets, but can't obtain the
lock because widgets is already locked and blocked by the hanging
SPID/query; then
Subsequent queries are executed, each requesting shared locks on widgets;
such that
The whole system is now tied up: the reorg can't start until it obtains an
exclusive lock on widgets, but widgets is tied up in a blocking shared
lock by a hung SPID. And because the reorg has placed an exclusive lock on
widgets, all other queries wanting shared locks on widgets have to wait
until the reorg is complete (because a newly requested exclusive lock
trumps a newly requested shared lock).
I think my ideal strategy here would be to:
Timeout DB queries after say, 2 minutes, which will prevent SPIDs from
hanging and thus preventing the reorgs from running; and then
If a query attempts to hit a table that has an exclusive lock on it,
detect this and hadle it specially (like schedule the query to run again
1hr later, when hopefully the reorg is complete, etc.)
My questions:
How do I timeout a query to release a shared lock after, say, 2mins?
Is there a way to programmatically (most likely through the Sybase JDBC
driver, but perhaps via Sybase command-line, HTTP calls, etc.) determine
if a reorg is running? Or, that an exclusive lock exists on a table? That
way I could detect the exclusive lock and handle it in a special way.
Thanks in advance!

Undoing forced push: how to update remote ref with specific remote revision

Undoing forced push: how to update remote ref with specific remote revision

There is a common situation - forced updating of remote branch with losing
some revisions from it:
$ git push --force origin
Counting objects: 19, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (12/12), 2.27 KiB, done.
Total 12 (delta 8), reused 0 (delta 0)
remote: => Syncing... [OK]
To git@gitserver.com:path/to/project.git
C..B master -> master
In other words, before the pushing remote master was like A---B---C and
forcing update changed it to A---B.
C revision is remote only - it exists on git-server and on the working
station of it's author. It means that there is no such local ref C.
Question 1: is there a way to set remote master to C revision?
I've tried to update it with push --force again like the answer for
similar question offers, but it allows to use only local refs:
$ git push --force origin C:master
error: src refspec C does not match any.
error: failed to push some refs to 'git@gitserver.com:path/to/project.git'
So if I'm getting it right the whole problem is to retrieve this revision
from git server.
Question 2: is there a way to fetch remote revision that doesn't belong to
any branch?
PS: let's think that there is no ssh access to git server, it means that
there is no way to manipulate git from the server side.

validateField doesn't work with models

validateField doesn't work with models

I want to use validateField with models but it says Method is not defined
for this object!
I code like this:
$user_payment=$this->add("Model_Payment");
$user_payment->getField("amount")
->validateNotNull()
->validateField('($this->get())<=0','Please enter a
posetive number!');