Esempio 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);
        }
Esempio n. 2
0
        public static EasyBaseColumn COLLECT(EasyBaseColumn column, object result, int index, object value, bool condition)
        {
            if (result == null && value != null)
            {
                if (value.GetType() == typeof(int))
                {
                    result = new EasyIntColumn();
                }
                else if (value.GetType() == typeof(string))
                {
                    result = new EasyStringColumn();
                }
                else if (value.GetType() == typeof(bool))
                {
                    result = new EasyBoolColumn();
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
            if (result == null)
            {
                return(null);
            }

            EasyBaseColumn b = (EasyBaseColumn)result;

            //if (condition) deðiþtirme!!!
            b.Insert(value);

            return(b);
        }
Esempio n. 3
0
 public void Delete(EasyTable table, EasyBaseColumn condition)
 {
     if (condition is EasyBoolColumn)
     {
         EasyBoolColumn b = (EasyBoolColumn)condition;
         int            l = b.GetRowCount();
         for (int i = 0; i < l; i++)
         {
             if (b.Values[i])
             {
                 table.Delete(i, Transaction);
             }
         }
     }
     else if (condition is EasyIntColumn)
     {
         EasyIntColumn iCol = (EasyIntColumn)condition;
         int           l    = iCol.GetRowCount();
         for (int i = 0; i < l; i++)
         {
             table.Delete(iCol.Values[i], Transaction);
         }
     }
     else
     {
         throw new NotSupportedException();
     }
 }
Esempio n. 4
0
 public void Update(EasyTable table, EasyBaseColumn condition, KeyValuePair <string, object>[] values)
 {
     if (condition is EasyBoolColumn)
     {
         EasyBoolColumn b = (EasyBoolColumn)condition;
         int            l = b.GetRowCount();
         for (int i = 0; i < l; i++)
         {
             if (b.Values[i])
             {
                 table.Update(i, values, Transaction);
             }
         }
     }
     else if (condition is EasyIntColumn)
     {
         EasyIntColumn iCol = (EasyIntColumn)condition;
         int           l    = iCol.GetRowCount();
         for (int i = 0; i < l; i++)
         {
             table.Update(iCol.Values[i], values, Transaction);
         }
     }
     else
     {
         throw new NotSupportedException();
     }
 }
Esempio n. 5
0
 public static object SUM(EasyBaseColumn column, object result, int index, object value, bool condition)
 {
     if (result != null)
     {
         return(column.PLUS(result, value));
     }
     else
     {
         return(value);
     }
 }
Esempio n. 6
0
        public static object COUNT(EasyBaseColumn column, object result, int index, object value, bool condition)
        {
            if (result != null && condition)
            {
                return(((int)result) + 1);
            }
            else if (condition)
            {
                return(1);
            }

            return(null);
        }
Esempio n. 7
0
        /// <summary>
        /// Index Array
        /// </summary>
        public static EasyIntColumn IA(EasyBaseColumn column, object result, int index, object value, bool condition)
        {
            if (result == null)
            {
                result = new EasyIntColumn();
            }
            EasyIntColumn b = (EasyIntColumn)result;

            if (condition)
            {
                b.Insert(index);
            }

            return(b);
        }
Esempio n. 8
0
        /// <summary>
        /// Boolean Index Array
        /// </summary>
        public static EasyBoolColumn BI(EasyBaseColumn column, object result, int index, object value, bool condition)
        {
            if (result == null)
            {
                result = new EasyBoolColumn();
            }
            EasyBoolColumn b = (EasyBoolColumn)result;

            if (condition)
            {
                b.Values.Add(true);
            }
            else
            {
                b.Values.Add(false);
            }

            return(b);
        }
Esempio n. 9
0
        public EasyTable CreateTableStructure(string tablename, List <CreateField> columns, uint useTableId, uint[] useColumnId)
        {
            if (useTableId == 0)
            {
                tableId++;
                useTableId = tableId;
            }

            EasyTable table = new EasyTable()
            {
                Id = useTableId, Server = this
            };

            table.Name = tablename;
            int i = 0;

            foreach (CreateField column in columns)
            {
                uint id = 0;
                if (useColumnId == null)
                {
                    columnId++;
                    id = columnId;
                }
                else
                {
                    id = useColumnId[i];
                }
                i++;

                EasyBaseColumn col = null;
                if (column.FieldType == typeof(string))
                {
                    col = new EasyStringColumn()
                    {
                        Id = id
                    }
                }
                ;

                else if (column.FieldType == typeof(int))
                {
                    col = new EasyIntColumn()
                    {
                        Id = id
                    }
                }
                ;

                else if (column.FieldType == typeof(uint))
                {
                    col = new EasyUIntColumn()
                    {
                        Id = id
                    }
                }
                ;

                else if (column.FieldType == typeof(bool))
                {
                    col = new EasyBoolColumn()
                    {
                        Id = id
                    }
                }
                ;

                else if (column.FieldType == typeof(decimal))
                {
                    col = new EasyDecimalColumn()
                    {
                        Id = id
                    }
                }
                ;
                else
                {
                    throw new Exception("Unknown column type! " + column.FieldType);
                }

                col.Name       = column.Name;
                col.IsIdentity = column.IsIdentity;
                table.AddColumn(col);
            }

            Tables.Add(table);

            return(table);
        }
Esempio n. 10
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());
        }
Esempio n. 11
0
        public object ExecuteStatement(StatementBase statement)
        {
            if (statement is SelectStatement)
            {
                SelectStatement select = (SelectStatement)statement;

                if (select.Where.Count == 0)
                {
                    return(Select(select.TableName, select.Fields, select.GroupFields, null));
                }
                else
                {
                    return(Select(select.TableName, select.Fields, select.GroupFields,
                                  delegate()
                    {
                        return (EasyBoolColumn)ExecuteExpression(FindTable(select.TableName), select.Where);
                    }));
                }
            }
            else if (statement is InsertStatement)
            {
                InsertStatement insert = (InsertStatement)statement;
                return(Insert(
                           FindTable(insert.TableName),
                           insert.Values.ToArray()));
            }
            else if (statement is UpdateStatement)
            {
                UpdateStatement update    = (UpdateStatement)statement;
                EasyTable       table     = FindTable(update.TableName);
                EasyBaseColumn  condition = (EasyBaseColumn)ExecuteExpression(table, update.Where);
                Update(
                    table,
                    condition,
                    update.Values.ToArray());
                return("Success");
            }
            else if (statement is CreateTableStatement)
            {
                CreateTableStatement c = (CreateTableStatement)statement;
                CreateTable(c.TableName, c.Fields);
                return("Success");
            }
            else if (statement is DeleteStatement)
            {
                DeleteStatement d         = (DeleteStatement)statement;
                EasyTable       table     = FindTable(d.TableName);
                EasyBaseColumn  condition = (EasyBaseColumn)ExecuteExpression(table, d.Where);
                Delete(table, condition);
                return("Success");
            }
            else if (statement is SimpleStatement)
            {
                SimpleStatement simple = (SimpleStatement)statement;
                switch (simple.Statement)
                {
                case "begintran":
                    BeginTransaction();
                    break;

                case "commit":
                    Commit();
                    break;

                case "rollback":
                    Rollback();
                    break;

                default:
                    break;
                }
                return("Success");
            }
            throw new NotSupportedException();
        }
Esempio n. 12
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);
        }
Esempio n. 13
0
 public void AddColumn(EasyBaseColumn col)
 {
     Columns.Add(col);
 }