public override void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            sql.Append(" INSERT INTO ");

            sql.Append(Utils.FormatName(TableName, db_target));

            sql.Append(" ");

            if (_Columns != null && _Columns.Count > 0)
            {
                sql.Append("(");
                int colCount = _Columns.Count;
                for (int x = 0; x < colCount; x++)
                {
                    sql.Append(Utils.FormatName(_Columns[x], db_target));
                    if (x + 1 < colCount)
                    {
                        sql.Append(", ");
                    }
                }
                sql.Append(") ");
            }

            Select.GetSql(db_target, ref sql);
        }
Exemplo n.º 2
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            int count = this.Count;

            if (count == 0)
            {
                return;
            }

            sql.Append(" ").Append(_Name).Append(" ");
            for (int x = 0; x < count; x++)
            {
                ICondition cond = _Conditions[x];
                // concat operator
                if (x > 0)
                {
                    if (cond.ConditionType == ConditionType.And)
                    {
                        sql.Append(" AND ");
                    }
                    else if (cond.ConditionType == ConditionType.Or)
                    {
                        sql.Append(" OR ");
                    }
                }
                cond.GetSql(db_target, ref sql);
            }
        }
Exemplo n.º 3
0
        public IHttpActionResult Get([FromUri] string facadeId)
        {
            try
            {
                MongoClient client = new MongoClient();
                var         db     = client.GetDatabase("scheduler");

                var facadeCollection = db.GetCollection <TargetFacade>("targetFacades");
                var collection       = db.GetCollection <DbTarget>("targets");

                var target = facadeCollection.Find(Builders <TargetFacade> .Filter.Where(x => x.Id == facadeId)).FirstOrDefault();

                if (target == null)
                {
                    throw new Exception("Target not found");
                }

                DbTarget toReturn = collection.Find(Builders <DbTarget> .Filter.Where(x => x.Id == target.TargetId && x.UserEmail == User.Identity.Name)).FirstOrDefault();

                if (toReturn == null)
                {
                    throw new Exception("Target not found");
                }

                return(Ok(toReturn));
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }
Exemplo n.º 4
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            int count = _ConditionList.Count;

            if (count == 0)
            {
                return;
            }

            if (Negation)
            {
                sql.Append(" NOT ");
            }

            sql.Append("(");
            for (int x = 0; x < count; x++)
            {
                // concat operator
                if (x > 0)
                {
                    if (_ConditionList[x].ConditionType == ConditionType.And)
                    {
                        sql.Append(" AND ");
                    }
                    else if (_ConditionList[x].ConditionType == ConditionType.Or)
                    {
                        sql.Append(" OR ");
                    }
                }
                _ConditionList[x].GetSql(db_target, ref sql);
            }
            sql.Append(")");
        }
Exemplo n.º 5
0
            public void GetSql(DbTarget db_target, ref StringBuilder sql)
            {
                int Count = Expressions.Count;

                if (Count <= 0)
                {
                    return;
                }

                sql.Append(" ORDER BY ");

                for (int x = 0; x < Count; x++)
                {
                    OrderByExpression item = _Expressions[x];

                    item.GetSql(db_target, ref sql);
                    if (x + 1 < Count)
                    {
                        sql.Append(", ");
                    }
                    else
                    {
                        sql.Append(" ");
                    }
                }
            }
Exemplo n.º 6
0
        public static List <Tuple <DbTarget, DbTarget> > GetCurrentTargets(DayOfWeek d, string userEmail)
        {
            MongoClient client = new MongoClient();
            var         db     = client.GetDatabase("scheduler");
            IMongoCollection <DbTarget>        targets  = db.GetCollection <DbTarget>("targets");
            List <Tuple <DbTarget, DbTarget> > toReturn = new List <Tuple <DbTarget, DbTarget> >();

            var bufTargets  = targets.Find(Builders <DbTarget> .Filter.Where(x => x.UserEmail == userEmail));
            var userTargets = bufTargets.ToList();

            foreach (var target in userTargets)
            {
                DbTarget curr = target;

                while (curr != null)
                {
                    if (curr.StartDate != default(DateTime) && curr.StartDate.Date < DateTime.Now.Date && curr.ActiveDays < curr.Duration && curr.WorkingDays.Contains(d))
                    {
                        toReturn.Add(new Tuple <DbTarget, DbTarget>(target, curr));
                        break;
                    }

                    if (curr.StartDate == default(DateTime))
                    {
                        break;
                    }

                    curr = curr.NextTarget;
                }
            }

            return(toReturn);
        }
Exemplo n.º 7
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            switch (db_target)
            {
            case DbTarget.SqlServer:
                sql.Append("SUBSTRING(");
                break;

            case DbTarget.MySql:
                sql.Append("SUBSTRING(");
                break;

            case DbTarget.SqlLite:
                sql.Append("SUBSTR(");
                break;
            }
            if (!_Expression.IsLiteral)
            {
                sql.Append("(");
            }
            _Expression.GetSql(db_target, ref sql);
            if (!_Expression.IsLiteral)
            {
                sql.Append(")");
            }
            sql.Append(_Position.ToString());
            sql.Append(",");
            sql.Append(_Length.ToString());
            sql.Append(")");
        }
Exemplo n.º 8
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            if (!_Expression1.IsLiteral)
            {
                sql.Append("(");
            }

            _Expression1.GetSql(db_target, ref sql);

            if (!_Expression1.IsLiteral)
            {
                sql.Append(")");
            }

            sql.Append(" AND ");

            if (!_Expression2.IsLiteral)
            {
                sql.Append("(");
            }

            _Expression2.GetSql(db_target, ref sql);

            if (!_Expression2.IsLiteral)
            {
                sql.Append(")");
            }
        }
Exemplo n.º 9
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            sql.Append(" FROM ");
            if (!TableSource.IsLiteral)
            {
                sql.Append("(");
            }
            TableSource.GetSql(db_target, ref sql);
            if (!TableSource.IsLiteral)
            {
                sql.Append(")");
            }
            sql.Append(" ");
            if (Alias != null && Alias.Trim() != "")
            {
                sql.Append(Utils.FormatName(Alias, db_target));
            }

            sql.Append(" ");
            foreach (JoinClause join in Joins)
            {
                join.GetSql(db_target, ref sql);
                sql.Append(" ");
            }
        }
Exemplo n.º 10
0
 public void GetSql(DbTarget db_target, ref StringBuilder sql)
 {
     sql.Append("REPLACE(");
     if (!Text.IsLiteral)
     {
         sql.Append("(");
     }
     Text.GetSql(db_target, ref sql);
     if (!Text.IsLiteral)
     {
         sql.Append(")");
     }
     sql.Append(",");
     if (!OldText.IsLiteral)
     {
         sql.Append("(");
     }
     OldText.GetSql(db_target, ref sql);
     if (!OldText.IsLiteral)
     {
         sql.Append(")");
     }
     sql.Append(",");
     if (!NewText.IsLiteral)
     {
         sql.Append("(");
     }
     NewText.GetSql(db_target, ref sql);
     if (!NewText.IsLiteral)
     {
         sql.Append(")");
     }
     sql.Append(")");
 }
Exemplo n.º 11
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            if (db_target == DbTarget.SqlServer)
            {
                sql.Append("[");
            }
            else if (db_target == DbTarget.MySql)
            {
                sql.Append("`");
            }
            else if (db_target == DbTarget.SqlLite)
            {
                sql.Append("\"");
            }
            sql.Append(_Text);

            if (db_target == DbTarget.SqlServer)
            {
                sql.Append("]");
            }
            else if (db_target == DbTarget.MySql)
            {
                sql.Append("`");
            }
            else if (db_target == DbTarget.SqlLite)
            {
                sql.Append("\"");
            }
        }
Exemplo n.º 12
0
 public override void GetSql(DbTarget db_target, ref StringBuilder sql)
 {
     sql.Append(" UPDATE ");
     sql.Append(Utils.FormatName(TableName, db_target));
     sql.Append(" SET ");
     if (_Columns != null && _Columns.Count > 0)
     {
         int colCount = _Columns.Count;
         for (int x = 0; x < colCount; x++)
         {
             sql.Append(Utils.FormatName(_Columns[x].Name, db_target));
             sql.Append(" = ");
             _Columns[x].Value.GetSql(db_target, ref sql);
             if (x + 1 < colCount)
             {
                 sql.Append(",");
             }
         }
     }
     sql.Append(" ");
     if (_Where != null && _Where.Count > 0)
     {
         _Where.GetSql(db_target, ref sql);
     }
 }
Exemplo n.º 13
0
 public void GetSql(DbTarget db_target, ref StringBuilder sql)
 {
     if (TableAlias != null)
     {
         TableAlias.GetSql(db_target, ref sql);
         sql.Append(".");
     }
     Name.GetSql(db_target, ref sql);
 }
Exemplo n.º 14
0
 public void GetSql(DbTarget db_target, ref StringBuilder sql)
 {
     _expression.GetSql(db_target, ref sql);
     sql.Append(" IS ");
     if (Negation)
     {
         sql.Append("NOT ");
     }
     sql.Append("NULL");
 }
Exemplo n.º 15
0
 public void GetSql(DbTarget db_target, ref StringBuilder sql)
 {
     if (Negation)
     {
         sql.Append("NOT ");
     }
     sql.Append("EXISTS(");
     _expression.GetSql(db_target, ref sql);
     sql.Append(")");
 }
        public override void Handle(Notification n, string status)
        {
            MongoClient client  = new MongoClient();
            var         db      = client.GetDatabase("scheduler");
            var         targets = db.GetCollection <DbTarget>("targets");

            if (status == "Done")
            {
                var bufTargets  = targets.Find(Builders <DbTarget> .Filter.Where(x => x.UserEmail == n.UserEmail));
                var userTargets = bufTargets.ToList();
                List <Tuple <DbTarget, DbTarget> > currTargets = new List <Tuple <DbTarget, DbTarget> >();

                foreach (var target in userTargets)
                {
                    DbTarget curr = target;

                    while (curr != null)
                    {
                        if (curr.StartDate != default(DateTime) && curr.ActiveDays < curr.Duration && curr.WorkingDays.Contains(n.SendingTime.AddDays(-1.0).DayOfWeek))
                        {
                            currTargets.Add(new Tuple <DbTarget, DbTarget>(target, curr));
                            break;
                        }

                        if (curr.StartDate == default(DateTime))
                        {
                            break;
                        }

                        curr = curr.NextTarget;
                    }
                }

                Tuple <DbTarget, DbTarget> tuple = currTargets.Where(x => x.Item2.Name == n.Title).SingleOrDefault();

                if (tuple != default(Tuple <DbTarget, DbTarget>))
                {
                    tuple.Item2.ActiveDays = tuple.Item2.ActiveDays + 1;

                    if (tuple.Item2.ActiveDays == tuple.Item2.Duration)
                    {
                        if (tuple.Item2.NextTarget == null)
                        {
                            HandleFinishedFinalTask(tuple, n.UserEmail, db);
                        }
                        else
                        {
                            HandleFinishedTask(tuple, n.UserEmail, db);
                        }
                    }

                    targets.FindOneAndReplace(x => x.Id == tuple.Item1.Id, tuple.Item1);
                }
            }
        }
Exemplo n.º 17
0
 public void GetSql(DbTarget db_target, ref StringBuilder sql)
 {
     for (int x = 0; x < _list.Count; x++)
     {
         if (x > 0)
         {
             sql.Append(",");
         }
         _list[x].GetSql(db_target, ref sql);
     }
 }
Exemplo n.º 18
0
        public override void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            sql.Append("DELETE FROM ");
            sql.Append(Utils.FormatName(TableName, db_target));
            sql.Append(" ");

            if (_Where != null && _Where.Count > 0)
            {
                _Where.GetSql(db_target, ref sql);
            }
        }
Exemplo n.º 19
0
        public async Task <IHttpActionResult> Post([FromBody] TargetsToSave toSave)
        {
            try
            {
                List <DbTarget> save = new List <DbTarget>();

                for (int i = 0; i < toSave.TargetNames.Count(); ++i)
                {
                    for (int j = 0; j < toSave.Targets.Count(); ++j)
                    {
                        if (toSave.Targets[j].Name == toSave.TargetNames[i])
                        {
                            save.Add(toSave.Targets[j]);
                            break;
                        }
                    }
                }

                save[0].WeekendsRemained = save[0].Difficulty;
                for (int i = 1; i < save.Count(); ++i)
                {
                    save[i - 1].NextTarget   = save[i];
                    save[i].WeekendsRemained = save[i].Difficulty;
                }

                MongoClient client     = new MongoClient();
                var         db         = client.GetDatabase("scheduler");
                var         collection = db.GetCollection <DbTarget>("targets");

                await collection.InsertOneAsync(save[0]);

                DbTarget selected = collection.Find(Builders <DbTarget> .Filter.Where(x => x.UserEmail == User.Identity.Name && x.Name == save[0].Name)).Single();

                TargetFacade f = new TargetFacade()
                {
                    Tags       = toSave.Tags,
                    Title      = toSave.Title,
                    TargetId   = selected.Id,
                    Difficulty = save[0].Difficulty,
                    StartDate  = save[0].StartDate,
                    UserEmail  = User.Identity.Name
                };

                IMongoCollection <TargetFacade> c = db.GetCollection <TargetFacade>("targetFacades");
                await c.InsertOneAsync(f);

                return(Ok("Success"));
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }
Exemplo n.º 20
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            if (db_target == DbTarget.SqlServer)
            {
                sql.Append("CHARINDEX(");
                if (!_expressionToFind.IsLiteral)
                {
                    sql.Append("(");
                }
                _expressionToFind.GetSql(db_target, ref sql);
                if (!_expressionToFind.IsLiteral)
                {
                    sql.Append(")");
                }
                sql.Append(",");
                if (!_expressionToSearch.IsLiteral)
                {
                    sql.Append("(");
                }
                _expressionToSearch.GetSql(db_target, ref sql);
                if (!_expressionToSearch.IsLiteral)
                {
                    sql.Append(")");
                }
            }
            else
            {
                sql.Append("LOCATE(");
                if (!_expressionToSearch.IsLiteral)
                {
                    sql.Append("(");
                }
                _expressionToSearch.GetSql(db_target, ref sql);
                if (!_expressionToSearch.IsLiteral)
                {
                    sql.Append(")");
                }
                sql.Append(",");
                if (!_expressionToFind.IsLiteral)
                {
                    sql.Append("(");
                }
                _expressionToFind.GetSql(db_target, ref sql);
                if (!_expressionToFind.IsLiteral)
                {
                    sql.Append(")");
                }
            }

            sql.Append(",");
            sql.Append(_startLocation.ToString());
            sql.Append(")");
        }
Exemplo n.º 21
0
 public void GetSql(DbTarget db_target, ref StringBuilder sql)
 {
     for (var x = 0; x < _Value.Length; x++)
     {
         if (x > 0)
         {
             sql.Append(",");
         }
         sql.Append("'");
         sql.Append(_Value[x].Replace("'", "''"));
         sql.Append("'");
     }
 }
Exemplo n.º 22
0
 public void GetSql(DbTarget db_target, ref StringBuilder sql)
 {
     if (_Value == null)
     {
         sql.Append("NULL");
     }
     else
     {
         sql.Append("'");
         sql.Append(_Value.Replace("'", "''"));
         sql.Append("'");
     }
 }
Exemplo n.º 23
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            /*switch (_aggregateFunctionType)
             * {
             *  case AggregateFunctionType.Sum:
             *      sql.Append("SUM");
             *      break;
             *
             *  case AggregateFunctionType.Avg:
             *      sql.Append("AVG");
             *      break;
             *
             *  case AggregateFunctionType.Max:
             *      sql.Append("MAX");
             *      break;
             *
             *  case AggregateFunctionType.Min:
             *      sql.Append("MIN");
             *      break;
             *
             *  case AggregateFunctionType.Count:
             *      sql.Append("COUNT");
             *      break;
             *
             * }*/

            sql.Append(_aggregateFunctionType.ToString());
            sql.Append("(");
            if (_expression != null)
            {
                if (!_expression.IsLiteral)
                {
                    sql.Append("(");
                }
                _expression.GetSql(db_target, ref sql);
                if (!_expression.IsLiteral)
                {
                    sql.Append(")");
                }
            }
            else
            {
                if (_aggregateFunctionType == AggregateFunctionType.Count)
                {
                    sql.Append("*");
                }
            }
            sql.Append(")");
        }
Exemplo n.º 24
0
        public static string FormatName(string name, DbTarget db_target)
        {
            string format = "{0}";

            if (db_target == DbTarget.SqlServer)
            {
                format = "[{0}]";
            }
            else if (db_target == DbTarget.MySql)
            {
                format = "`{0}`";
            }


            return(String.Format(format, name));
        }
Exemplo n.º 25
0
 /// <summary>
 /// Migrates the schema for the underlying database.
 /// </summary>
 public void MigrateSchema()
 {
     var availableChangeRepository = new LogDbSchemaChangeRepository();
     var appliedChangeRepository =
         new DbAppliedChangeRepository(
             Factory,
             ConnectionString);
     var migrationTarget =
         new DbTarget(
             Factory,
             ConnectionString);
     var service = new MigrationService();
     service.Migrate(
         availableChangeRepository,
         appliedChangeRepository,
         migrationTarget);
 }
Exemplo n.º 26
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            switch (db_target)
            {
            case DbTarget.SqlServer:
                sql.Append("GETDATE()");
                break;

            case DbTarget.MySql:
                sql.Append("DATE");
                break;

            case DbTarget.SqlLite:
                sql.Append("CURRENT_TIMESTAMP");
                break;
            }
        }
Exemplo n.º 27
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            int count = this.Count;

            if (count == 0)
            {
                return;
            }

            for (int x = 0; x < count; x++)
            {
                this[x].GetSql(db_target, ref sql);
                if (x + 1 < count)
                {
                    sql.Append(",");
                }
            }
        }
Exemplo n.º 28
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            if (!Expression.IsLiteral)
            {
                sql.Append("(");
            }

            Expression.GetSql(db_target, ref sql);
            if (!Expression.IsLiteral)
            {
                sql.Append(")");
            }
            sql.Append(" ");
            if (Alias != null)
            {
                sql.Append(" as ");
                Alias.GetSql(db_target, ref sql);
            }
        }
 public static void Setup(string connectionString)
 {
     var availableChangeRepository =
         new BuildPipelineSchemaRepository();
     SQLiteFactory factory = SQLiteFactory.Instance;
     var appliedChangeRepository =
         new DbAppliedChangeRepository(
             factory,
             connectionString);
     var migrationTarget =
         new DbTarget(
             factory,
             connectionString);
     var service = new MigrationService();
     service.Migrate(
         availableChangeRepository,
         appliedChangeRepository,
         migrationTarget);
 }
Exemplo n.º 30
0
 public void GetSql(DbTarget db_target, ref StringBuilder sql)
 {
     sql.Append("CASE ");
     if (!_InputExpression.IsLiteral)
     {
         sql.Append("(");
     }
     _InputExpression.GetSql(db_target, ref sql);
     if (!_InputExpression.IsLiteral)
     {
         sql.Append(")");
     }
     foreach (WhenExpression we in _WhenExpressions)
     {
         sql.Append(" WHEN ");
         we.BooleanExpression.GetSql(db_target, ref sql);
         sql.Append(" THEN ");
         if (!we.ResultExpression.IsLiteral)
         {
             sql.Append("(");
         }
         we.ResultExpression.GetSql(db_target, ref sql);
         if (!we.ResultExpression.IsLiteral)
         {
             sql.Append(")");
         }
     }
     if (_ElseResultExpression != null)
     {
         sql.Append(" ELSE ");
         if (!_ElseResultExpression.IsLiteral)
         {
             sql.Append("(");
         }
         _ElseResultExpression.GetSql(db_target, ref sql);
         if (!_ElseResultExpression.IsLiteral)
         {
             sql.Append(")");
         }
     }
     sql.Append(" END ");
 }
Exemplo n.º 31
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            if (_Conditions == null)
            {
                return;
            }

            int count = _Conditions.Count;

            if (count == 0)
            {
                return;
            }

            sql.Append(" WHERE ");


            for (int x = 0; x < count; x++)
            {
                ICondition cond = _Conditions[x];

                if (cond.Negation)
                {
                    sql.Append(" NOT ");
                }
                // concat operator
                if (x > 0)
                {
                    if (cond.ConditionType == ConditionType.And)
                    {
                        sql.Append(" AND ");
                    }
                    else if (cond.ConditionType == ConditionType.Or)
                    {
                        sql.Append(" OR ");
                    }
                }

                cond.GetSql(db_target, ref sql);
            }
        }
Exemplo n.º 32
0
        public void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            if (_Value == null || _Value == Convert.DBNull)
            {
                sql.Append("NULL");
            }
            else
            {
                switch (_DataType)
                {
                case DbColumnType.String:
                    sql.Append("'");
                    sql.Append(_Value.ToString().Replace("'", "''"));
                    sql.Append("'");
                    break;

                case DbColumnType.Number:
                    sql.Append(_Value.ToString());
                    break;

                case DbColumnType.Date:
                    sql.Append("'");
                    sql.Append(((DateTime)_Value).ToString("yyyy-MM-dd HH:mm:ss"));
                    sql.Append("'");
                    break;

                case DbColumnType.Boolean:
                    sql.Append(((Boolean)_Value) ? "1" : "0");
                    break;

                case DbColumnType.Binary:
                    sql.Append("'");

                    sql.Append("'");
                    break;
                }
            }
        }