function Table(role, table, onProcess) {
    this.role = role;
    this.table = table;
    this.onProcess = onProcess;
    this.functions = new Array;
}

Table.arguments = function(arguments) {
    var result = new Array;
    var n = arguments.length;
    for (var i=0; i<n; i++) {
	result[i] = arguments[i];
    }
    return result;
}

Table.prototype.columns = function(columns) {
    this.functions[this.functions.length] = ['columns', Table.arguments(arguments)];
    return this;
}

Table.prototype.filter = function(filter) {
    this.functions[this.functions.length] = ['filter', Table.arguments(arguments)];
    return this;
}

Table.prototype.sort = function(sort) {
    this.functions[this.functions.length] = ['sort', Table.arguments(arguments)];
    return this;
}

Table.prototype.limit = function(limit) {
    this.functions[this.functions.length] = ['limit', Table.arguments(arguments)];
    return this;
}

Table.prototype.index = function(index) {
    this.functions[this.functions.length] = ['index', Table.arguments(arguments)];
    return this;
}

Table.prototype.group = function(group) {
    this.functions[this.functions.length] = ['group', Table.arguments(arguments)];
    return this;
}

Table.url = function(role, table, f) {
  return '/=/rawdev/data/:/'+role+'/'+table+'/'+f+'/';
}

Table.prototype.process = function(data) {
if (typeof(data) == 'object' &&  data.length==3 && data[0]=='error') RawDev.error(data[2]);
else this.onProcess(data);
}

Table.prototype.post = function(f, args) {
    var url = Table.url(this.role, this.table, f);
    $.post(url, {functions: $.toJSON(this.functions), params: $.toJSON(args)}, $.context(this).callback('process'), 'json');
}

Table.prototype.select = function() {
    this.post('select', Table.arguments(arguments));
}

Table.prototype.get = function() {
    this.post('get', Table.arguments(arguments));
}

Table.prototype.lookup = function() {
    this.post('lookup', Table.arguments(arguments));
}

Table.prototype.call = function() {
    this.post('call', Table.arguments(arguments));
}
