All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.lteconsulting.hexa.client.sql.SQLiteResult Maven / Gradle / Ivy

The newest version!
package fr.lteconsulting.hexa.client.sql;

import java.util.Iterator;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;

public class SQLiteResult implements Iterable
{
	private final JSONObject root;

	public SQLiteResult( JavaScriptObject jso )
	{
		this.root = new JSONObject( jso );
	}
	
	public int size()
	{
		return root.size();
	}

	public Row getRow( int rowIdx )
	{
		JSONArray row = root.get( "" + rowIdx ).isArray();
		return new Row( row );
	}

	public static class Cell
	{
		public String column;
		public String value;
	}

	public static class Row implements Iterable
	{
		JSONArray row;

		Row( JSONArray row )
		{
			this.row = row;
		}

		public Row()
		{
			row = new JSONArray();
		}

		public void addCell( String columnName, String value )
		{
			JSONObject cell = new JSONObject();
			cell.put( "column", new JSONString( columnName ) );
			cell.put( "value", new JSONString( value ) );
			row.set( row.size(), cell );
		}

		public String getColumnValue( String columnName )
		{
			for( int i=0; i iterator()
		{
			return new Iterator()
			{
				int current = 0;

				@Override
				public void remove()
				{
					assert false;
				}

				@Override
				public Cell next()
				{
					JSONObject cellJson = row.get( current ).isObject();
					current++;

					Cell cell = new Cell();
					cell.column = cellJson.get( "column" ).isString().stringValue();
					cell.value = cellJson.get( "value" ).isString().stringValue();

					return cell;
				}

				@Override
				public boolean hasNext()
				{
					return current < row.size();
				}
			};
		}
	}

	@Override
	public Iterator iterator()
	{
		return new Iterator()
		{
			int current = 0;

			@Override
			public void remove()
			{
				assert false;
			}

			@Override
			public Row next()
			{
				JSONArray row = root.get( "" + current ).isArray();
				current++;
				return new Row( row );
			}

			@Override
			public boolean hasNext()
			{
				return current < root.size();
			}
		};
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy