Exemplo n.º 1
0
        /// <summary>
        /// Constructs the actual query
        /// </summary>
        /// <returns>Delete query</returns>
        public string Build()
        {
            StringBuilder query = new StringBuilder();

            if (_between)
            {
                var pk = _databaseFields.Single(f => f.Item2 == _primaryKeyReflectionField);

                query.Append("DELETE FROM ");
                query.Append(SQLUtil.GetTableName <T>());
                query.Append(" WHERE ");

                query.Append(pk.Item1);
                query.Append(" BETWEEN ");
                query.Append(_valuesBetweenDouble.Item1);
                query.Append(" AND ");
                query.Append(_valuesBetweenDouble.Item2);
                query.Append(";");
            }
            else
            {
                query.Append("DELETE FROM ");
                query.Append(SQLUtil.GetTableName <T>());
                query.Append(" WHERE ");
                query.Append(new SQLWhere <T>(_rows, true).Build());
                query.Append(";");
            }

            query.Append(Environment.NewLine);
            return(query.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructs the actual query
        /// </summary>
        /// <returns>Insert query header</returns>
        public string Build()
        {
            StringBuilder query = new StringBuilder();

            query.Append("INSERT ");
            query.Append(_ignore ? "IGNORE " : string.Empty);
            query.Append("INTO ");
            query.Append(SQLUtil.GetTableName <T>());

            query.Append(" (");
            foreach (var field in _databaseFields)
            {
                query.Append(field.Item1);
                query.Append(SQLUtil.CommaSeparator);
            }
            query.Remove(query.Length - SQLUtil.CommaSeparator.Length, SQLUtil.CommaSeparator.Length); // remove last ", "
            query.Append(")");
            query.Append(" VALUES" + Environment.NewLine);

            return(query.ToString());
        }
Exemplo n.º 3
0
        public string Build()
        {
            string tableName = SQLUtil.GetTableName <T>();
            var    fields    = SQLUtil.GetFields <T>();

            StringBuilder fieldNames = new StringBuilder();

            foreach (var field in fields)
            {
                fieldNames.Append(field.Item1);
                fieldNames.Append(SQLUtil.CommaSeparator);
            }
            fieldNames.Remove(fieldNames.Length - 2, 2); // remove last ", "

            if (_whereClause.HasConditions)
            {
                return($"SELECT {fieldNames} FROM {_database ?? Settings.TDBDatabase}.{tableName} WHERE {_whereClause.Build()}");
            }

            return($"SELECT {fieldNames} FROM {_database ?? Settings.TDBDatabase}.{tableName}");
        }
Exemplo n.º 4
0
        /// <summary>
        /// Constructs the actual query
        /// </summary>
        /// <returns>A single update query</returns>
        public virtual string Build()
        {
            StringBuilder query = new StringBuilder();

            if (CommentOut)
            {
                query.Append("-- ");
            }

            // Return empty if there are no values or where clause or no table name set
            if (!WhereClause.HasConditions)
            {
                return(string.Empty);
            }

            query.Append("UPDATE ");
            query.Append(SQLUtil.GetTableName <T>());
            query.Append(" SET ");

            bool hasValues = false;

            foreach (var field in _databaseFields)
            {
                object value = field.Item2.GetValue(_value.Data);

                Array arr = value as Array;
                if (arr != null)
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        object v = arr.GetValue(i);
                        if (v == null)
                        {
                            continue;
                        }

                        query.Append(SQLUtil.AddBackQuotes(field.Item3.First().Name + (field.Item3.First().StartAtZero ? i : i + 1)));
                        query.Append("=");
                        query.Append(SQLUtil.ToSQLValue(v, noQuotes: field.Item3.Any(a => a.NoQuotes)));
                        query.Append(SQLUtil.CommaSeparator);

                        hasValues = true;
                    }
                    continue;
                }

                if (value == null)
                {
                    continue;
                }

                if (field.Item2.Name != "VerifiedBuild" || !Settings.SkipOnlyVerifiedBuildUpdateRows)
                {
                    hasValues = true;
                }

                query.Append(field.Item1);
                query.Append("=");
                query.Append(SQLUtil.ToSQLValue(value, noQuotes: field.Item3.Any(a => a.NoQuotes)));
                query.Append(SQLUtil.CommaSeparator);
            }
            if (!hasValues)
            {
                return(string.Empty);
            }

            query.Remove(query.Length - SQLUtil.CommaSeparator.Length, SQLUtil.CommaSeparator.Length); // remove last ", "

            query.Append(" WHERE ");
            query.Append(WhereClause.Build());
            query.Append(";");

            if (!string.IsNullOrWhiteSpace(_value.Comment))
            {
                query.Append(" -- " + _value.Comment);
            }

            return(query.ToString());
        }