yeah... not really sure what you're trying to do with the split algorithm -
it's unnecessarily messy. try just using JSNI - I'm not sure why you're
having problems with the Java regexp. Have you tried 1.6? It seems like
this is a bug in GWT.
private static native String fixQuery(String query, String value) /*--{
return query.replace(/[?]/, value);
} --*/;
for (int i = 0; i < params.length; i++) {
String fixed = fixQuery(statement, params[i]);
assert (!statement.equals(fixed));
statement = fixed;
}
Again, I still think you are doing something wrong with the Gears API if you
have to use this hack (I haven't used gears. Have you tried writing the
Javascript equivalent?
You could also try to use JSNI - maybe the GWT gears API has some problems:
private void nativeGears(Database db, String sql, long userid, String
sessionid, String name, String email)
{
db.execute(sql, [userid, sessionid, name, email]);
}
try {
tryGears(db, statement, theUser.id, theUser.currentSessionId ,
theUser.name , "");
} catch (Exception e) {
e.printStackTrace();
}
If this works, then there's probably something wrong with the Java code (or
maybe a bug with GWT). Try upgrading to 1.6 if that doesn't work. If it
still doesn't work, write the Javascript equivalent.
Also, you've got a logic problem with selectUserByUserId algorithm
First of all, the for loop should be for (; rs.isValidRow(); rs.next() ).
The i counter is redundant. This brings me to the next point - the for loop
is redundant in your algorithm & you will never execute rs.close unless
there is nothing in your result set. Maybe this is what you were trying
for?
public User selectUserByUserId( long userId )
{
final String[] params = new String[] { Long.toString( userId ) };
User user = null;
ResultSet rs = null;
try
{
rs = db.execute( SELECT_USER, params );
if (rs.isValidRow())
{
user = new User();
user.setId( rs.getFieldAsLong( 0 ) );
user.setCurrentSessionId( rs.getFieldAsString( 1 ) );
user.setName( rs.getFieldAsString( 2 ) );
if (!GWT.isScript()) {
rs.next();
assert (!rs.isValidRow());
user = null;
}
}
}
catch ( DatabaseException e )
{
debugDBException( "selectUserByUserId", params, e );
} finally {
if (rs != null)
{
rs.close();
}
}
return user;
}
By the way, there might be a problem with your attempt to manually bind your
values when you insert. Since you always append quotes around the
paramters, they will always be treated as strings. Since SQLite does not
honour the column type, you may see issues appear (at the very best, it'll
manifest as performance problems).
On Mon, May 11, 2009 at 10:37 AM, Evan Ruff <[email protected]> wrote:
>
> Vitali,
>
> GWT does not seem too excited about Matcher and bombs out completely.
> I don't believe it's compatible client side, as it says
> java.util.regex cannot be imported.
>
> I've confirmed that Gears on Android does not support sentences with
> another user.
>
> I misspoke when I posted my code snippet, I was using replaceFirst. As
> I stated before that Regexp ([?]) does not appear to match ? as
> replaceFirst does not ever replace the ?. "\\?" does not seem to work
> in replaceFirst either; however, "\\?" does work with .split.
>
> So, I think the two solutions I've got so far are:
>
> for ( int i = 0; ( statement.indexOf( "?" ) > 0 ); i++ )
> {
> int index = statement.indexOf( '?' );
> String firstPart = statement.substring( 0, index );
> String secondPart = statement.substring( index + 1 );
>
> StringBuilder sb = new StringBuilder();
> sb.append( firstPart ).append( "\"" ).append( params[ i ] ).append
> ( "\"" ).append( secondPart );
> statement = sb.toString();
> }
> return statement;
>
> OR
> StringBuilder sb = new StringBuilder();
> String[] splits = statement.split( "\\?" );
> int j = 0;
>
> sb.append( splits[ 0 ] );
> for( int i = 1; i < splits.length; i++ )
> {
> sb.append( '"' ).append( params[ j ] ).append( '"' ).append( splits
> [ i ] );
> j++;
> }
> if ( j < params.length )
> {
> sb.append( '"' ).append( params[ j ] ).append( '"' );
> j++;
> }
> if ( j != params.length )
> {
> throw new IndexOutOfBoundsException();
> }
> return sb.toString();
>
> With neither method being particularly quick. I suspect the second
> method will give better general performance.
>
> E
>
>
>
> On May 8, 10:46 am, Vitali Lovich <[email protected]> wrote:
> > Have you read the Javadoc? replace replaces string literals. You need
> > myString.replaceAll("[?]", newString); or myString.replace("?",
> newString).
> > In any case, it's not what you want since it replaces *all* instances of
> ?
> > with the same string. Try this:
> >
> > Matcher m = Pattern.compile("[?]").matcher(myString);
> > int i = 0;
> > while (!m.hitEnd())
> > {
> > m.replaceFirst('"' + params[i++] + '"');
> >
> > }
> >
> > You could use String.replaceFirst, but this should be better performing.
> > Otherwise, you have to do:
> >
> > String newString;
> > int i = 0;
> > while (!(newString = myString.replaceFirst("[?]", '"' + params[i++] +
> > '"')).equals(myString))
> > {
> > myString = newString;
> >
> > }
> >
> > but that might be slower than even the split method I gave earlier.
> >
> > And if this code is really slow, there's a faster JSNI way because
> > Javascript has a faster way for literal regexps & I don't believe the
> > compiler makes that optimization yet.
> >
> > In any case, I would really recommend trying to figure out why your SQL
> > stuff doesn't work first, because that is the core of the problem. All
> of
> > this stuff is just hacky workarounds.
> >
> > You really should write the equivalent little javascript snippet & see if
> > that works. If it doesn't, then post to the android list because it's a
> > problem with your code.
> >
> > On Fri, May 8, 2009 at 8:33 AM, Evan Ruff <[email protected]> wrote:
> >
> > > I was going with the ol:
> >
> > > myString.replace( "[?]", newString );
> >
> > > but that would not find the ?, so I went with a "\\?", also
> > > unsuccessfully.
> >
> > > E
> >
> > > On May 8, 12:00 am, Vitali Lovich <[email protected]> wrote:
> > > > What is the exact Java code snippet that you are using?
> >
> > > > On Thu, May 7, 2009 at 10:10 PM, Evan Ruff <[email protected]>
> wrote:
> >
> > > > > Vitali,
> >
> > > > > I was just trying to match '?' in the SQL statement, such as:
> >
> > > > > INSERT into users ( name, city ) VALUES( ?, ? )
> >
> > > > > so I can mash up my own prepare SQL statement.
> >
> > > > > Also, I can run the test on the android phone to see if that might
> be
> > > > > the problem.
> >
> > > > > E
> >
> > > > > On May 7, 4:36 pm, Vitali Lovich <[email protected]> wrote:
> > > > > > What's the code for the regexp that you are trying & what is the
> > > source
> > > > > > string that you are matching against.
> >
> > > > > > On Thu, May 7, 2009 at 3:24 PM, Evan Ruff <[email protected]>
> > > wrote:
> >
> > > > > > > Vitali,
> >
> > > > > > > Thanks for your response!
> >
> > > > > > > That RegExp for the ? was giving me trouble, as it wouldn't
> match.
> >
> > > > > > > I also tried [\\?] without success as well. Any idea on how to
> find
> > > > > > > that guy?
> >
> > > > > > > Thanks,
> >
> > > > > > > E
> >
> > > > > > > On May 7, 2:59 pm, Vitali Lovich <[email protected]> wrote:
> > > > > > > > Javascript & Java have different regular expressions, so if
> you
> > > are
> > > > > > > running
> > > > > > > > your stuff on desktop browsers in Hosted Mode but compiled
> mode
> > > in
> > > > > > > Android,
> > > > > > > > then you might be running into that problem.
> >
> > > > > > > > A performance improvement should be:
> >
> > > > > > > > String statement = new String( sqlStatement );
> > > > > > > > StringBuffer sb = new StringBuffer(statement.length());
> > > > > > > > int index;
> > > > > > > > int previous = 0;
> > > > > > > > int i = 0;
> >
> > > > > > > > while ((index = statement.indexOf('?')) != -1)
> > > > > > > > {
> > > > > > > > String firstPart = statement.substring( previous, index );
> > > > > > > > sb.append
> > > (firstPart).append('"').append(params[i++]).append('"');
> > > > > > > > previous = index + 1;
> >
> > > > > > > > }
> >
> > > > > > > > return sb.toString();
> >
> > > > > > > > or try
> >
> > > > > > > > String statement = new String( sqlStatement );
> > > > > > > > String [] parts = statement.split("[?]");
> > > > > > > > StringBuffer sb = new StringBuffer(statement.length +
> > > parts.length *
> > > > > > > > AVERAGE_PARAM_LENGTH);
> >
> > > > > > > > for (int i = 0; i < parts.length; i++)
> > > > > > > > {
> > > > > > > >
> sb.append(parts[i]).append('"').append(params[i]).append('"')
> >
> > > > > > > > }
> >
> > > > > > > > AVERAGE_PARAM_LENGTH can be anything, but you want the
> resultant
> > > > > > > expression
> > > > > > > > to be as close as possible to the actual final length as
> > > possible,
> > > > > > > without
> > > > > > > > going under (assuming this portion of code is your hotpath -
> > > > > otherwise,
> > > > > > > you
> > > > > > > > won't notice the difference).
> >
> > > > > > > > On Thu, May 7, 2009 at 2:12 PM, Evan Ruff <
> [email protected]>
> > > > > wrote:
> >
> > > > > > > > > Ok so something really fishy is going on with the Gears
> API.
> >
> > > > > > > > > I created my own "prepareSQLStatement" that converts the
> SQL
> > > Call
> > > > > and
> > > > > > > > > parameter list into a string.
> >
> > > > > > > > > private String prepareSQLStatement( String
> sqlStatement,
> > > > > > > String[]
> > > > > > > > > params ) throws DatabaseException
> > > > > > > > > {
> > > > > > > > > try
> > > > > > > > > {
> > > > > > > > > String statement = new String(
> > > sqlStatement
> > > > > );
> >
> > > > > > > > > for ( int i = 0; (
> statement.indexOf(
> > > "?" )
> > > > > > 0
> > > > > > > );
> > > > > > > > > i++ )
> > > > > > > > > {
> > > > > > > > > int index =
> statement.indexOf(
> > > '?'
> > > > > );
> > > > > > > > > String firstPart =
> > > > > statement.substring(
> > > > > > > 0,
> > > > > > > > > index );
> > > > > > > > > String secondPart =
> > > > > statement.substring(
> > > > > > > > > index + 1 );
> >
> > > > > > > > > StringBuffer sb = new
> > > > > StringBuffer();
> > > > > > > > > sb.append( firstPart
> ).append(
> > > "\""
> > > > > > > > > ).append( params[ i ] ).append
> > > > > > > > > ( "\"" ).append( secondPart );
> > > > > > > > > statement = sb.toString();
> > > > > > > > > }
> >
> > > > > > > > > return statement;
> > > > > > > > > }
> > > > > > > > > catch ( IndexOutOfBoundsException indexOut )
> > > > > > > > > {
> > > > > > > > > throw new DatabaseException( "Index
> out
> > > of
> > > > > > > bounds.
> > > > > > > > > SQL Parameter
> > > > > > > > > Error" );
> > > > > > > > > }
> > > > > > > > > }
> >
> > > > > > > > > When I use this method to prepare the statement before
> hitting
> > > > > > > > > db.execute, all of my sql statements work as expected.
> >
> > > > > > > > > Could Android implement it's string manipulation stack
> slightly
> > > > > > > > > differently than all the other platforms? I've got a sneaky
> > > > > suspecion
> > > > > > > > > that the RegExp that's used to parse the '?' in the SQL
> > > commands
> > > > > might
> > > > > > > > > be failing?
> >
> > > > > > > > > Furthermore, can anyone give me an ideas as how to improve
> the
> > > > > > > > > performance of the above method? It creates a noticable
> delay.
> > > Did
> > > > > I
> > > > > > > > > read somewhere that GWT has a slow string manipulation
> engine?
> >
> > > > > > > > > E
> >
> > > > > > > > > On May 6, 2:07 pm, Evan Ruff <[email protected]> wrote:
> > > > > > > > > > Hey Eric,
> >
> > > > > > > > > > I'm pretty much terrified of Javascript.
> >
> > > > > > > > > > I'm a little hesitant to make any determination
> whatsoever as
> > > to
> > > > > the
> > > > > > > > > > cause of the problem. When I run the GWT Gears Sample
> > > > > DatabaseDemo
> > > > > > > > > > project, it works as expected. I can even replace all of
> the
> > > DB
> > > > > > > > > > specifics with the internals of my tables/queries and it
> > > seems to
> > > > > > > > > > work! It's just so freakin' frustrating.
> >
> > > > > > > > > > RememberTheMilk and the mobile GMail seem to make use of
> > > Gears on
> > > > > > > > > > Android without issue, so I really don't know where to go
> > > from
> > > > > here.
> > > > > > > > > > Any other suggestions?
> >
> > > > > > > > > > Here's the replaced DatabaseDemo code, fwiw:
> >
> > > > > > > > > > public class Gears implements EntryPoint
> > > > > > > > > > {
> > > > > > > > > > private static final int NUM_SAVED_ROWS = 3;
> > > > > > > > > > private static final int NUM_DATA_TABLE_COLUMNS =
> 3;
> >
> > > > > > > > > > private final Button addButton = new Button(
> "Add" );
> > > > > > > > > > private final Button clearButton = new Button(
> "Clear
> > > > > > > Database"
> > > > > > > > > );
> > > > > > > > > > private Database db;
> > > > > > > > > > private final TextBox input = new TextBox();
> > > > > > > > > > private final FlexTable dataTable = new
> FlexTable();
> >
> > > > > > > > > > public void onModuleLoad()
> > > > > > > > > > {
> > > > > > > > > > VerticalPanel outerPanel = new
> > > VerticalPanel();
> > > > > > > > > > outerPanel.setSpacing( 10 );
> >
> > > outerPanel.getElement().getStyle().setPropertyPx(
> > > > > > > > > "margin", 15 );
> >
> > > > > > > > > > HorizontalPanel textAndButtonsPanel = new
> > > > > > > > > HorizontalPanel();
> > > > > > > > > > textAndButtonsPanel.add( new Label(
> "Enter a
> > > > > Phrase:
> > > > > > > " )
> > > > > > > > > );
> > > > > > > > > > textAndButtonsPanel.add( input );
> > > > > > > > > > textAndButtonsPanel.add( addButton );
> > > > > > > > > > textAndButtonsPanel.add( clearButton );
> > > > > > > > > > outerPanel.add( textAndButtonsPanel );
> > > > > > > > > > outerPanel.add( new Label( "Last 3
> Entries:"
> > > ) );
> > > > > > > > > > outerPanel.add( dataTable );
> >
> > > > > > > > > > for ( int i = 0; i <= NUM_SAVED_ROWS; ++i
> )
> > > > > > > > > > {
> > > > > > > > > > dataTable.insertRow( i );
> > > > > > > > > > for ( int j = 0; j <
> > > > > NUM_DATA_TABLE_COLUMNS;
> > > > > > > j++
> > > > > > > > > )
> > > > > > > > > > {
> >
> > ...
> >
> > read more ยป
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---