How to table fields convert to sql statement

// Copyright (C), 2014, CEU Co., Ltd.
// USR Changed by 2014-10-14T12:27:59 Fandy Xie 谢宇帆
// Version       : 1.0
// Description   : 得到table的select语句
// Return        :
static container YIP_getTableSelectSql(
    tableId     _tableId        = tableNum(Currency),
    dataAreaId  _dataAreaId     = "",
    int         _topOnlyRecords = 0,
    boolean     _asFieldLabel   = true)
{
    DictTable       dicTable;
    DictField       dictField, dfArray;
    set             setLabel    = new set(types::String);
    map             mapField    = new map(types::Integer, types::Container);
    mapEnumerator   me;
    fieldId         fieldId;

    str             sql = "select ";
    int             fieldCnt,m;
    name            fieldName, fieldLabel;
    int             lastCol;
    name            lastColStr;
    container       con, fieldCon;
    identifiername  extend;
    int             a;
    types           type;

str int2ColChar(int _col)
{
    int     k, i;
    str     ret;
;
    i    = _col;
    while(i > 0)
    {
       k    = i mod 26;
       if(k == 0)
       {
           ret  = "Z" + ret;
           i    = i - 1;
       }
       else
       {
           ret  = num2char(k + 64) + ret;
       }
       i    = i / 26;
    }
    return ret;
}
    ;

    dicTable    = new DictTable(_tableId);
    fieldCnt    = dicTable.fieldCnt();

    if(dicTable.isMap())// || dicTable.isSystemTable() || dicTable.isView())
        return con;
    for(m = 1;m <= fieldCnt; m++)
    {
        dictField   = new DictField(dicTable.id(),dicTable.fieldCnt2Id(m));
        fieldId     = dictField.id();
        fieldName   = dictField.name(dbBackend::Sql);
        fieldLabel  = dictField.label();
        type        = dictField.baseType();
        if(dictField.isSystem() && fieldName != "DATAAREAID")
            continue;
        if (!fieldName)
            continue;
        if (type == types::Container)
            continue;

        extend = global::extendedTypeId2name(dictField.typeId());
        if (dictField.arraySize() > 1) // 数组 Dimension
        {
            for(a = 1; a <= dictField.arraySize(); a++)
            {
                dfArray     = new dictField(dicTable.id(), fieldId2Ext(dictField.id(), a));
                fieldId     = dfArray.id();
                fieldName   = dfArray.name(dbBackend::Sql);
                fieldLabel  = dfArray.label();
                if (!fieldLabel)
                    fieldLabel = fieldName;
                mapField.insert(fieldId,[fieldName, fieldLabel]);
            }
        }
        else
        {
            if (!fieldLabel)
                fieldLabel = fieldName;
            mapField.insert(fieldId,[fieldName, fieldLabel]);
        }
        /*
        if (setLabel.in(fieldLabel))
            continue;
        setLabel.add(fieldLabel);
        mapField.insert(fieldId,[fieldName, fieldLabel]);
        */

    }
    me = mapField.getEnumerator();
    while (me.moveNext())
    {
        fieldId     = me.currentKey();
        fieldCon    = me.currentValue();

        fieldName   = conpeek(fieldCon, 1);
        fieldLabel  = conpeek(fieldCon, 2);

        lastCol++;
        if (lastCol == 1 && _topOnlyRecords)
            sql += strfmt(" top %1 ", _topOnlyRecords);
        if (_asFieldLabel)
            sql += strfmt(" %1 as [%2],", fieldName, fieldLabel);
        else
            sql += strfmt(" %1 as [%2],", fieldName, fieldName);
    }
    sql = substr(sql, 1, strlen(sql) - 1);
    sql += " from " + dicTable.name();
    if (_dataAreaId)
        sql += strfmt(" where DataAreaId = '%1'" ,curext());

    lastColStr = int2ColChar(lastCol);
    con = [sql, lastColStr];

    return con;
}



你可能感兴趣的:(How to table fields convert to sql statement)