Ejemplo n.º 1
0
        protected Table()
        {
            var link = GetType().GetCustomAttribute <TableLink>(false);

            if (link != null)
            {
                Identifier = link.TableName;

                if (link.DatabaseType != null && SqlManager.TrySearchDatabase(link.DatabaseType, out Database db))
                {
                    Database = db;
                }

                if (Database == null)
                {
                    throw new ArgumentNullException($"The database isn't registered for table '{ link.TableName }'");
                }
            }
        }
Ejemplo n.º 2
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.º 3
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.º 4
0
 /// <summary>
 /// Insert the current instance values in <see cref="Database"/>
 /// </summary>
 /// <returns></returns>
 public void InsertAsync(BasicAction <long> onResult)
 {
     SqlManager.PoolAsyncOperation(() => {
         onResult(Insert());
     });
 }
Ejemplo n.º 5
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="onUpdated"></param>
 /// <param name="columnsToUpdate"></param>
 public void UpdateAsync(SqlCondition condition, BasicAction <bool> onUpdated, params string[] columnsToUpdate)
 {
     SqlManager.PoolAsyncOperation(() => {
         onUpdated(Update(condition, columnsToUpdate));
     });
 }