Ejemplo n.º 1
0
 public void CountAsync(string columnName, SqlCondition condition, BasicAction <long> onCounted, bool distinct)
 {
     SqlManager.PoolAsyncOperation(() => {
         var count = _database.Count <TableKey>(columnName, condition, distinct);
         onCounted?.Invoke(count);
     });
 }
Ejemplo n.º 2
0
 public void AverageAsync(string columnName, SqlCondition condition, BasicAction <long> onAverage)
 {
     SqlManager.PoolAsyncOperation(() => {
         var average = _database.Average <TableKey>(columnName, condition);
         onAverage?.Invoke(average);
     });
 }
Ejemplo n.º 3
0
 public void ExistAsync(SqlCondition condition, BasicAction <bool> onExist, bool distinct)
 {
     SqlManager.PoolAsyncOperation(() => {
         var count = _database.Count <TableKey>(condition, distinct);
         onExist?.Invoke(count > 0);
     });
 }
Ejemplo n.º 4
0
 public void MaxAsync(string columnName, SqlCondition condition, BasicAction <TableKey> onMax)
 {
     SqlManager.PoolAsyncOperation(() => {
         var max = _database.Max <TableKey>(columnName, condition);
         onMax?.Invoke(max);
     });
 }
Ejemplo n.º 5
0
 public void MinAsync(string columnName, SqlCondition condition, BasicAction <TableKey> onMin)
 {
     SqlManager.PoolAsyncOperation(() => {
         var min = _database.Min <TableKey>(columnName, condition);
         onMin?.Invoke(min);
     });
 }
Ejemplo n.º 6
0
 public void SelectAllAsync(SqlCondition condition, BasicAction <TableKey[]> onSelected, bool distinct, params string[] columnsToSelect)
 {
     SqlManager.PoolAsyncOperation(() => {
         var tables = _database.SelectAll <TableKey>(condition, distinct, columnsToSelect);
         onSelected?.Invoke(tables);
     });
 }
Ejemplo n.º 7
0
 public void SumAsync(string columnName, SqlCondition condition, BasicAction <decimal> onSum)
 {
     SqlManager.PoolAsyncOperation(() => {
         var sum = _database.Sum <TableKey>(columnName, condition);
         onSum?.Invoke(sum);
     });
 }
Ejemplo n.º 8
0
 public void DeleteAsync(SqlCondition condition, BasicAction onDeleted)
 {
     SqlManager.PoolAsyncOperation(() => {
         var deleted = _database.Delete <TableKey>(condition);
         if (deleted)
         {
             onDeleted?.Invoke();
         }
     });
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Create a new instance of <see cref="SqlCondition"/> and attach to the current instance of <see cref="QueryAccessor{TableKey}"/>
        /// </summary>
        /// <param name="onCondition"></param>
        /// <returns></returns>
        public QueryAccessor <TableKey> UseCondition(BasicAction <SqlCondition> onCondition)
        {
            if (_cd == null)
            {
                _cd = new SqlCondition();
            }

            onCondition(_cd);
            return(this);
        }
Ejemplo n.º 10
0
        internal static void SetQuery(this MySqlCommand command, string query, SqlCondition condition = null)
        {
            command.CommandText = query;
            if (condition != null)
            {
                condition.ApplyToCmd(command);
            }

            command.Prepare();
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Update all elements in the <see cref="Database"/> with current instance's values using the specified <see cref="SqlCondition"/>
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="columnsToUpdate"></param>
        /// <returns></returns>
        public bool Update(SqlCondition condition, params string[] columnsToUpdate)
        {
            var updated = false;

            Database.ExecuteCommand((cmd) => {
                cmd.SetQuery(QueryBuilder.GetUpdateQuery(this, cmd, condition, columnsToUpdate), condition);
                updated = cmd.ExecuteNonQuery() > 0;
            });

            return(updated);
        }
Ejemplo n.º 12
0
        private static string UseBuilder(string baseStr, SqlCondition condition, BasicAction <StringBuilder> onBuilder)
        {
            var sb = new StringBuilder(baseStr);

            onBuilder?.Invoke(sb);

            if (condition != null)
            {
                sb.Append($" { condition.GetQuery() }");
            }

            return(sb.ToString());
        }
Ejemplo n.º 13
0
        internal static string GetSelectQuery(Table table, SqlCondition condition, string[] columnsToSelect, bool distinct)
        {
            return(UseBuilder($"SELECT { (distinct ? "DISTINCT " : string.Empty) }", condition, (sb) => {
                if (columnsToSelect.Length > 0)
                {
                    sb.Append(string.Join(",", columnsToSelect));
                }
                else
                {
                    sb.Append("*");
                }

                sb.Append($" FROM `{ table.Identifier }`");
            }));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Execute a COUNT query with the specified parameters and return the result.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="distinct"></param>
        /// <param name="columnName"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public long Count <T>(string columnName, SqlCondition condition, bool distinct) where T : Table
        {
            long count = 0;

            ExecuteCommand((cmd) => {
                if (SqlManager.CreateTableInstance(out T table))
                {
                    cmd.SetQuery(QueryBuilder.GetCountQuery(table, columnName, condition, distinct), condition);
                    cmd.OnReader((reader) => {
                        count = reader.GetInt64(0);
                    });
                }
            });

            return(count);
        }
Ejemplo n.º 15
0
        public long Average <T>(string columnName, SqlCondition condition) where T : Table
        {
            long avg = -1;

            ExecuteCommand((cmd) => {
                if (SqlManager.CreateTableInstance(out T table))
                {
                    cmd.SetQuery(QueryBuilder.GetAvgQuery(table, columnName, condition), condition);
                    cmd.OnReader((reader) => {
                        avg = reader.GetInt64(0);
                    });
                }
            });

            return(avg);
        }
Ejemplo n.º 16
0
        internal static string GetUpdateQuery(Table table, MySqlCommand command, SqlCondition condition, string[] columns)
        {
            var fields = Table.GetFields(table.GetType());
            var iParam = condition == null ? 0 : condition.ParamIndex + 1;

            return(UseBuilder($"UPDATE `{ table.Identifier }` SET ", condition, (sb) => {
                var cCount = columns.Length;
                if (cCount == 0)
                {
                    cCount--;
                }

                for (var i = 0; i < fields.Length; i++)
                {
                    var field = fields[i];
                    if (field.GetCustomAttribute <PrimaryKey>() != null)
                    {
                        continue;
                    }
                    if (cCount > 0)
                    {
                        if (!columns.Contains(field.Name))
                        {
                            continue;
                        }
                        else
                        {
                            cCount--;
                        }
                    }

                    var pName = $"@{ iParam++ }";

                    command.Parameters.AddWithValue(pName, field.GetValue(table));
                    sb.Append($"{ field.Name }={ pName }");

                    if (cCount == 0)
                    {
                        break;
                    }
                    if (i < fields.Length - 1)
                    {
                        sb.Append(", ");
                    }
                }
            }));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Selects all the elements from the specified <see cref="Table"/> with the specified parameters and returns an instance of the <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="columnsToSelect"></param>
        /// <param name="condition"></param>
        /// <param name="distinct"></param>
        /// <returns></returns>
        public T[] SelectAll <T>(SqlCondition condition, bool distinct, params string[] columnsToSelect) where T : Table
        {
            var result = new T[0];

            ExecuteCommand((cmd) => {
                if (SqlManager.CreateTableInstance(out T table))
                {
                    var records = new List <T>();
                    var fields  = Table.GetFields <T>();

                    cmd.SetQuery(QueryBuilder.GetSelectQuery(table, condition, columnsToSelect, distinct), condition);
                    cmd.OnReader((reader) => {
                        if (SqlManager.CreateTableInstance(out T instance))
                        {
                            for (var i = 0; i < reader.FieldCount; i++)
                            {
                                var field = fields.FirstOrDefault((f) => f.Name == reader.GetName(i));
                                if (field != null)
                                {
                                    object value = null;
                                    if (field.FieldType == typeof(bool))
                                    {
                                        value = reader.GetBoolean(i);
                                    }
                                    else if (!reader.IsDBNull(i))
                                    {
                                        value = reader.GetValue(i);
                                    }

                                    field.SetValue(instance, value);
                                }
                            }

                            records.Add(instance);
                        }
                    });

                    result = records.ToArray();
                }
            });

            return(result);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Delete all the elements from the specified <see cref="Table"/> based on the specified <see cref="SqlCondition"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="condition"></param>
        /// <returns></returns>
        public bool Delete <T>(SqlCondition condition) where T : Table
        {
            if (condition == null)
            {
                throw new ArgumentNullException("condition", "A valid SqlCondition is required.");
            }

            var deleted = false;

            ExecuteCommand((cmd) => {
                if (SqlManager.CreateTableInstance(out T table))
                {
                    cmd.SetQuery(QueryBuilder.GetDeleteQuery(table, condition), condition);
                    deleted = cmd.ExecuteNonQuery() > 0;
                }
            });

            return(deleted);
        }
Ejemplo n.º 19
0
        public decimal Sum <T>(string columnName, SqlCondition condition) where T : Table
        {
            decimal sum = -1;

            ExecuteCommand((cmd) => {
                if (SqlManager.CreateTableInstance(out T table))
                {
                    cmd.SetQuery(QueryBuilder.GetSumQuery(table, columnName, condition), condition);
                    cmd.OnReader((reader) => {
                        if (!reader.IsDBNull(0))
                        {
                            sum = reader.GetDecimal(0);
                        }
                    });
                }
            });

            return(sum);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Selects the first element from the specified <see cref="Table"/> with the specified parameters and returns an instance of the <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="columnsToSelect"></param>
        /// <param name="condition"></param>
        /// <param name="distinct"></param>
        /// <returns></returns>
        public T Select <T>(SqlCondition condition, bool distinct, params string[] columnsToSelect) where T : Table
        {
            T table = null;

            ExecuteCommand((cmd) => {
                if (SqlManager.CreateTableInstance(out table))
                {
                    if (condition == null)
                    {
                        condition = new SqlCondition();
                    }

                    condition.Limit(1);

                    var fields = Table.GetFields <T>();

                    cmd.SetQuery(QueryBuilder.GetSelectQuery(table, condition, columnsToSelect, distinct), condition);
                    cmd.OnReader((reader) => {
                        for (var i = 0; i < reader.FieldCount; i++)
                        {
                            var field = fields.FirstOrDefault((f) => f.Name == reader.GetName(i));
                            if (field != null)
                            {
                                object value = null;
                                if (field.FieldType == typeof(bool))
                                {
                                    value = reader.GetBoolean(i);
                                }
                                else if (!reader.IsDBNull(i))
                                {
                                    value = reader.GetValue(i);
                                }

                                field.SetValue(table, value);
                            }
                        }
                    });
                }
            });

            return(table);
        }
Ejemplo n.º 21
0
        public T Min <T>(string columnName, SqlCondition condition) where T : Table
        {
            T table = null;

            ExecuteCommand((cmd) => {
                if (SqlManager.CreateTableInstance(out table))
                {
                    cmd.SetQuery(QueryBuilder.GetMinQuery(table, columnName, condition), condition);
                    cmd.OnReader((reader) => {
                        var field = Table.GetFields <T>().FirstOrDefault((f) => f.Name == columnName);
                        if (field != null)
                        {
                            field.SetValue(table, reader.GetValue(0));
                        }
                    });
                }
            });

            return(table);
        }
Ejemplo n.º 22
0
 public void CountAsync(string columnName, SqlCondition condition, BasicAction <long> onCounted)
 {
     CountAsync(columnName, condition, onCounted, false);
 }
Ejemplo n.º 23
0
 public void CountAsync(SqlCondition condition, BasicAction <long> onCounted, bool distinct)
 {
     CountAsync(string.Empty, condition, onCounted, distinct);
 }
Ejemplo n.º 24
0
 public long Average(string columnName, SqlCondition condition)
 {
     return(_database.Average <TableKey>(columnName, condition));
 }
Ejemplo n.º 25
0
 /// <summary>
 /// If any <see cref="SqlCondition"/> attached, remove it and return the current instance
 /// </summary>
 /// <returns></returns>
 public QueryAccessor <TableKey> ClearCondition()
 {
     _cd = null;
     return(this);
 }
Ejemplo n.º 26
0
 public TableKey Min(string columnName, SqlCondition condition)
 {
     return(_database.Min <TableKey>(columnName, condition));
 }
Ejemplo n.º 27
0
 public decimal Sum(string columnName, SqlCondition condition)
 {
     return(_database.Sum <TableKey>(columnName, condition));
 }
Ejemplo n.º 28
0
 public void CountAsync(SqlCondition condition, BasicAction <long> onCounted)
 {
     CountAsync(string.Empty, condition, onCounted, false);
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Selects the first element from the specified <see cref="Table"/> with the specified parameters and returns an instance of the <typeparamref name="TableKey"/>.
 /// </summary>
 /// <param name="columnsToSelect"></param>
 /// <param name="condition"></param>
 /// <returns></returns>
 public TableKey Select(SqlCondition condition, params string[] columnsToSelect)
 {
     return(Select(condition, false, columnsToSelect));
 }
Ejemplo n.º 30
0
 public void SelectAllAsync(SqlCondition condition, BasicAction <TableKey[]> onSelected, params string[] columnsToSelect)
 {
     SelectAllAsync(condition, onSelected, false, columnsToSelect);
 }