예제 #1
0
        public DataTable Select(string tablename, List <string> fields, List <string> groupFields, WhereDelegate where)
        {
            if (groupFields != null && groupFields.Count == 0)
            {
                groupFields = null;
            }

            EasyTable table = FindTable(tablename);

            EasyBoolColumn rows = null;

            if (where != null)
            {
                rows = where ();
                //rows = (EasyBoolColumn)rows.Custom("&", table.RowColumn, EasyBaseColumn.COLLECT);
            }

            DataTable dt = new DataTable();
            //List<EasyBaseColumn> selectedFields = new List<EasyBaseColumn>();
            Dictionary <string, EasyBaseColumn> selectedFields = new Dictionary <string, EasyBaseColumn>();

            foreach (string field in fields)
            {
                if (field != "*")
                {
                    EasyBaseColumn column = null;
                    if (SqlParser.IsId(field))
                    {
                        column = table.FindColumn(field);
                        //selectedFields.Add(column);
                        selectedFields[field] = column;
                        dt.Columns.Add(field, column.GetColumnType());
                    }
                    else
                    {
                        List <string> t = SqlParser.Tokenize(field);
                        column = (EasyBaseColumn)ExecuteExpression(table, t);

                        //selectedFields.Add(column);
                        selectedFields[field] = column;
                        dt.Columns.Add(field, column.GetColumnType());
                    }
                }
                else
                {
                    foreach (var column in table.Columns)
                    {
                        //selectedFields.Add(column);
                        selectedFields[column.Name] = column;
                        dt.Columns.Add(column.Name, column.GetColumnType());
                    }
                }
            }

            //if (rows != null)
            // {
            int k        = 0;
            int rowCount = 0;

            foreach (EasyBaseColumn column in selectedFields.Values)
            {
                rowCount = column.GetRowCount();
                break;
            }
            Hashtable previousValues = new Hashtable();

            for (int i = 0; i < rowCount; i++)
            {
                if (rows != null && !rows.Values[i])
                {
                    continue;
                }
                if (table.RowColumn.Values[i] == 0)
                {
                    continue;
                }

                int j = 0;

                bool group = false;
                if (groupFields != null)
                {
                    group = true;
                    foreach (string groupField in groupFields)
                    {
                        if (k > 0)
                        {
                            object val = selectedFields[groupField].GetValue(i);
                            if (!selectedFields[groupField].EQ(val, previousValues[groupField]))
                            {
                                group = false;
                                previousValues[groupField] = val;
                            }
                        }
                        else
                        {
                            group = false;
                            previousValues[groupField] = selectedFields[groupField].GetValue(i);
                        }
                    }
                }
                if (group)
                {
                    continue;
                }

                dt.Rows.Add();
                foreach (EasyBaseColumn column in selectedFields.Values)
                {
                    dt.Rows[k][j] = column.GetValue(i);
                    j++;
                }
                k++;
            }
            //}
            //else
            //{
            //    int k = 0;
            //    int rowCount = 0;
            //    foreach (EasyBaseColumn column in selectedFields.Values)
            //    {
            //        rowCount = column.GetRowCount();
            //        break;
            //    }
            //    for (int i = 0; i < rowCount; i++)
            //    {
            //        if (table.RowColumn.Values[i] == 0) continue;

            //        dt.Rows.Add();
            //        int j = 0;
            //        foreach (EasyBaseColumn column in selectedFields.Values)
            //        {
            //            dt.Rows[k][j] = column.GetValue(i);
            //            j++;
            //        }
            //        k++;
            //    }
            //}

            return(dt);
        }