Exemplo n.º 1
0
        public void CreateTableFromSchema()
        {
            EasyTable      tables       = FindTable(1);
            EasyTable      columnsTable = FindTable(2);
            EasyBaseColumn column       = tables.FindColumn("table_name");
            int            index        = column.GetRowCount() - 1;

            string tablename = (string)tables.FindColumn("table_name").GetValue(index);

            EasyIntColumn inx = (EasyIntColumn)
                                columnsTable.FindColumn("table_name").Custom("=", tablename, EasyBaseColumn.IA);

            List <CreateField> columns = new List <CreateField>();

            for (int i = 0; i < inx.GetRowCount(); i++)
            {
                int         j     = (int)inx.GetValue(i);
                CreateField field = new CreateField();
                field.Name       = (string)columnsTable.FindColumn("column_name").GetValue(j);
                field.IsIdentity = (bool)columnsTable.FindColumn("is_identity").GetValue(j);
                string data = (string)columnsTable.FindColumn("data_type").GetValue(j);
                field.FieldType = Type.GetType("System." + data);

                columns.Add(field);
            }

            CreateTableStructure(tablename, columns, 0, null);
        }
Exemplo n.º 2
0
        private object ExecuteExpression(EasyTable table, List <string> list)
        {
            Stack <object> stack = new Stack <object>();

            #region Postfix Notation
            //TODO: apply paranthesis hack
            for (int i = list.Count - 2; i > -1; i--)
            {
                if (list[i] == "|" || list[i] == "&" || list[i] == "=" ||
                    list[i] == "<" || list[i] == ">" ||
                    list[i] == "<=" || list[i] == ">=" ||
                    list[i] == "*" || list[i] == "/" ||
                    list[i] == "+" || list[i] == "-")
                {
                    int k = 0;
                    for (int j = i + 1; j < list.Count; j++)
                    {
                        if (k == 0 && list[j] != "(")
                        {
                            string s = list[j];
                            list[j] = list[i];
                            list[i] = s;
                            break;
                        }
                        else //find matching parantheses
                        {
                            if (list[j] == "(")
                            {
                                k++;
                            }
                            else if (list[j] == ")")
                            {
                                k--;
                            }
                            if (k == 0)
                            {
                                string s = list[i];
                                for (int x = i; x < j; x++)
                                {
                                    list[x] = list[x + 1];
                                }
                                list[j] = s;
                                break;
                            }
                        }
                    }
                }
            }
            #endregion

            foreach (var item in list)
            {
                if (item == "(" || item == ")")
                {
                    continue;
                }

                if (item == "&" || item == "|" ||
                    item == ">" || item == ">=" || item == "<" || item == "<=" ||
                    item == "+" || item == "-" || item == "*" || item == "/" ||
                    item == "=" || item == "!=")
                {
                    string msg = item;
                    object p2  = stack.Pop();
                    object p1  = stack.Pop();
                    //TODO: if p1 isConst swap p1,p2; msg = reverse(msg)

                    EasyBaseColumn column = null;
                    if (p1 is EasyBaseColumn)
                    {
                        column = (EasyBaseColumn)p1;
                    }
                    else
                    {
                        column = table.FindColumn((string)p1, true);
                        //TODO: if p1 and p2, are both id then use a special column to execute
                        //TODO: if(p2 is id) p2 = findCol(p2)
                        //object result = column.Message(msg, p2);
                    }
                    object result = null;

                    //if (msg != "=" || column.Index == null)
                    result = column.Custom(msg, p2, EasyBaseColumn.COLLECT);
                    //else
                    //{
                    //    result = column.Index.Contains(new EasyConst() { Value = p2 });
                    //    EasyBoolColumn b = new EasyBoolColumn();
                    //    for (int i = 0; i < column.GetRowCount(); i++)
                    //    {
                    //        b.Insert(false);
                    //    }
                    //    //if (result != null) b.Values[1] = true;
                    //    if (result != null) b.Values[((EasyValue)result).Index] = true;
                    //    result = b;
                    //}

                    stack.Push(result);

                    continue;
                }
                stack.Push(item);
            }

            //TODO: stack can not contain more than one value
            return(stack.Pop());
        }
Exemplo n.º 3
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);
        }