Table of Contents

Interface IMetaDatabase

Namespace
gudusoft.gsqlparser
Assembly
gudusoft.gsqlparser.dll

In order to link column to table correctly without connecting to database, we need to provide a class which implements IMetaDatabase to TGSqlParser. this class tells TGSqlParser the relation ship between column and table.

Take this SQL for example:

SELECT Quantity,b.Time,c.Description
FROM
(SELECT ID2,Time FROM bTab) b
INNER JOIN aTab a on a.ID=b.ID
INNER JOIN cTab c on a.ID=c.ID

General SQL Parser can build relationship between column: ID2 and table: bTable correctly without metadata information from database because there is only one table in from clause. But it can't judge column: Quantity belong to table: aTab or cTab, since no table alias was prefixed to column: Quantity. If no metadata provided, General SQL Parser will link column: Quantity to the first valid table (here it is aTab)

If we create a class metaDB implements IMetaDatabase,then TGSqlParser.setMetaDatabase(new metaDB()), General SQL Parser can take this advantage to create a correct relationship between column and tables. Here is a sample of metaDB, you should create your own metaDB class with meta information from database.

public class metaDB implements IMetaDatabase {

    String columns[][] = {
        {"dbo","aTab","Quantity1"},
        {"dbo","bTab","Quantity2"},
        {"dbo","cTab","Quantity"}
    };

    public boolean checkColumn(String schema, String table, String column){
       boolean bSchema,bTable,bColumn,bRet = false;
        for (int i=0; i<columns.length;i++){
            if (schema == null){
                bSchema = true;
            }else{
                bSchema = columns[i][0].equalsIgnoreCase(schema);
            }

            if (!bSchema) continue;

            bTable = columns[i][1].equalsIgnoreCase(table);
            if (!bTable) continue;

            bColumn = columns[i][2].equalsIgnoreCase(column);
            if (!bColumn) continue;

            bRet =true;
            break;

        }

        return bRet;
    }

}

After TGSqlParser.setMetaDatabase(new metaDB()), General SQL parser can generate this result for you:

atab.id
btab.id
btab.time
ctab.description
ctab.id
ctab.quantity
public interface IMetaDatabase

Methods

checkColumn(string, string, string, string, string)

Implement this function to link column to table

bool checkColumn(string server, string database, string schema, string table, string column)

Parameters

server string
database string
schema string
table string
column string

@return

Returns

bool