Sunday, 1 September 2013

Dynamic Sql and PDO's bindParam. How to get this to work?

Dynamic Sql and PDO's bindParam. How to get this to work?

In my script, I generate sql based on what comes in from the url and thru
htaccess
So, Http://www.domain.com/us/2013
If (isset($_GET['country']) && isset($_GET['year'])) {
$tab = 'country = $_GET['country'] and year = $_GET['year']';
} elseif (isset($_GET['country'])) {
$tab = 'country = $_GET['country']';
} else {
echo 'Error';
}
Then I do:
$sql = 'select * from table where :sub;
$fetchData = $conn->prepare($sql);
$fetchData->bindParam(':sub',$tab);
Somehow, this will not work, even though the entire sql statement gets
constructed. Is there anyway I can do something like this using PDO?

Authenticated call to MtGox WebSocket API in Python 3

Authenticated call to MtGox WebSocket API in Python 3

I'm trying to authenticate with the MtGox.com WebSocket API and after a
long while managed to complete the required "call" attribute of the JSON
data. However, I realized that I was using Python 2 to run my codesample
and the application the API is finally going to be implemented in is
written in Python 3. When I tried to make it work in Python 3 I ran into a
couple of problems I was unable to resolve despite several long attempts.
I also tried 2to3, but seems it doesn't have builtin fixers for these
kinds of problems.
The API specification for authenticated API calls can be found here:
https://en.bitcoin.it/wiki/MtGox/API/Streaming#Authenticated_commands
Here is the working Python 2 script I used for generating the JSON call
which I then ran through a WebSocket console extension I found for Chrome.
import hashlib
import time
import hmac
import json
import base64
import binascii
apikey = ""
apisecret = ""
def _nonce():
"""produce a unique nonce that is guaranteed to be ever increasing"""
microtime = int(time.time() * 1E6)
return microtime
def _reqid(nonce):
return hashlib.md5(str(nonce)).hexdigest()
def send_signed_call(api_endpoint, params):
nonce = _nonce()
reqid = _reqid(nonce)
call = json.dumps({
"id" : reqid,
"nonce" : nonce,
"call" : api_endpoint,
"params" : params,
})
sign = hmac.new(base64.b64decode(apisecret), call,
hashlib.sha512).digest()
signedcall = apikey.replace("-", "").decode("hex") + sign + call
return json.dumps({
"op" : "call",
"call" : base64.b64encode(signedcall),
"id" : reqid,
"context" : "mtgox.com"
})
msg = send_signed_call("private/info", {})
print(msg)
Some of the errors I ran into related to the no longer existing
String.decode("hex"), I've had a few others but unfortunately I haven't
kept track of all of them as I tried a great deal of different approaches.
I also looked at codesamples of the same functionality in other languages
but couldn't find any clue relating to the Python 3 problem. A lot seems
to be having to do with changes made to bytes and strings encoding and
decoding in Python 3.
Thanks a lot in advance!

Datasource mysql failover - howto configure?

Datasource mysql failover - howto configure?

I'm using a spring project with org.apache.commons.dbcp.BasicDataSource
datasource.
If i want my Database to support a failover where do i configure it?
I cant find any references with google

Saturday, 31 August 2013

How to change a character of an string into a different character?

How to change a character of an string into a different character?

I want to convert the space in a string into another character.
Ex:
var country = "United States"
I want the space to be "-" so:
var country = "Unites-States"
This is what I tried:
var country ="United States";
var countryArray = country.split(" ");
var newCountry = function(){
for(i=0;i<countryArray.length;i++){
if(countryArray[i]===","){
return countryArray[i]="-";}
}

jQuery autocomplete remote data using getJson with autocomplete.filter

jQuery autocomplete remote data using getJson with autocomplete.filter

Many people are using jQuery Autocomplete with a remote data source like
this:
$("#auto").autocomplete({
source: function( request, response ) {
$.getJSON( "search.php", { // get the json here
term: extractLast( request.term ) // function further, up not important
}, response );
}
});
and many people are filtering their data arrays like this:
$("#auto").autocomplete({
source: function(request, response) {
var results = $.ui.autocomplete.filter(myarray, request.term); //data in
"myarray"
response(results) ;
}
});
I can't find any example where anyone is filtering a remote data source
and I really need both. I'd like to just combine these to blocks of code
if possible.
Thanks.

Add more constraints to my controllers list method

Add more constraints to my controllers list method

At the moment my controllers list method for the domain object Product is:
def list(Integer max) {
params.max = Math.min(max ?: 10, 100)
[productInstanceList: Product.list(params), productInstanceTotal:
Product.count()]
}
I wish to add more constrains to this. More specifically, I want to get
the logged in user. This has a M:1 relationship to a domain entity called
Manager. Manager has a M:M relationship to domain entity Company. Company
has a 1:M relationship to Product. So I want to only return products that
are from the appropriate company.
I start by doing:
def list(Integer max) {
// 1. Get logged in user.
def user = springSecurityService.currentUser;
// 2. Get corresponding manager.
Manager mgr = user.getManager();
// 3. Get corresponding companies
companies = mgr.getCompanies();
// 4. Get products for all companies - not sure smart way to get this.
// 5. Need to add the products to the query
...
}
As can be seen I get stuck at steps 4, 5. Using raw SQL I would just to a
join to represent to all the User's Manager's Companies' Products and then
put this in as a where predicate and hit the product table.
But I want a grails / groovy solution. I also need to keep the params in
the query as this controller can also have params injected which are pass
into the query,
Any tips?

node.js / sqlite - Serializing callbacks as well as the main functions

node.js / sqlite - Serializing callbacks as well as the main functions

Example:
Database table with 2 rows:
rowid name qty date
1 milk 3 8/25
2 milk 40 8/30
An inventory transaction to subtract 5 milk items needs to be executed
using the oldest milk first.
e.g.
Select rowid 1 wait for its callback and in it Delete rowid 1 as its qty
goes to 0 and is therefore spent
New deletion qty = 5 - 3 = 2. Since its not 0, another Select needs to be
made.
Select rowid 2
wait for its callback and in it Update rowid 2 to set qty = 38.
The serialize capability in the sqlite binding will only serialize the two
Selects. There's no way to know the second Select is even needed until the
first Select and its callback are finished. It's only then that its
realized that the first Select offered an insufficient qty.
How does one serialize the first Select and its callback using pure
JavaScript - no JQuery or other libraries.