<% /*
    $Revision$
    $Id$

    File:           model_generic_select.inc

    Description:    Generiek model voor willekeurige select statement
    Parameters:
    Context:

    Notes:
*/

function model_generic_selecttemplate()
{
    this.table = "<unknown>";
    this.primary = null;
    this.records_name = "<unknown>s";
    this.record_name = "<unknown>";
    this.fields = {};
    this.list = { columns: [] };
    this.search = { filters: [] };
    this.soft_delete = null;
    this.autfunction = "WEB_FACFAC";
}

function model_generic_select(sql, autfunction)
{
    if (!sql.match(/^\s*(SELECT|WITH)/i))
        shared.simpel_page("SQL statement must start with SELECT or WITH"); // beschermt tegen INSERT, DELETE en UPDATE

    var model = new model_generic_selecttemplate();
    model.autfunction = autfunction;
    model.table = "sql";
    model.sql = sql;
    model.record_name = model.table.substring(0,1).toUpperCase() + model.table.substring(1);
    model.records_name = model.record_name + "s";

    try
    {
        var hiresTimer = new ActiveXObject("SLNKDWF.About");
        var tm_start = hiresTimer.usTimer;
        var oRs = Oracle.RealConnection.Execute(sql);
        var tm = (hiresTimer.usTimer - tm_start) / 1000;
    }
    catch (e)
    {
        var msg = e.description;
        if (msg.match('^ROW\-00004')) // ROW-00004: Invalid column datatype
            msg += '\nHint: Query should not contain mld_t_uitvoertijd columns';
        api2.error(500, msg);
    }

    // fields maken
    model.nrfields = 0;
    for (i= 0; i < oRs.Fields.Count; i++)
    {
        var fld = oRs.Fields(i).Name.toLowerCase();
        model.nrfields++;
        var field = { dbs: fld,
                      label: fld,
                      typ: "varchar"
                    };

        var kolomtype = oRs.Fields(i).Type;
        field.kk =kolomtype;
        if (kolomtype == adInteger || kolomtype == adDecimal)
        {
           field.typ = 'number';
        }
        else if (kolomtype == adCurrency || kolomtype == adNumeric || kolomtype == adVarNumeric || kolomtype == adDouble)
        {
           field.typ = 'float';
           field.autofloat = true;
        }
        else if (kolomtype == adDBTimeStamp)
        {
           field.typ = 'datetime';
           field.withSeconds = true;
        }
        else if (fld.match(/t_uitvoertijd$/)) // Helaas, hier komen we nooit, er is eerder bij het openen
        {                                     // van de recordset al een ROW-00004: Invalid column datatype
          field.typ = 'varchar';
          field.readonly = true;
          field.sql = "mld.uitvoertijd_to_char({0})".format(fld);
        }

        model.fields[fld] = field;
    }
    oRs.Close();

    model.REST_GET = function _GET(params)
    {
        user.checkAutorisation("WEB_FACFAC");

        var sql = this.sql;
        var json = api2.sql2json (params, sql, this );

        return json;
    };

    if (!model.nrfields)
        abort_with_warning("No columns found for {0}".format(sql)); // voorkomt ook latere sql-injection op tabelnaam

    __Log(model);
    return model;
}

%>
