예제 #1
0
        /// <summary>
        /// SELECT multiple rows with pagination.
        /// This operation will return an empty list if no matching objects are found.
        /// </summary>
        /// <param name="indexStart">Index start.</param>
        /// <param name="maxResults">Maximum number of results to retrieve.</param>
        /// <param name="expr">Filter to apply when SELECTing rows (i.e. WHERE clause).</param>
        /// <param name="resultOrder">Specify on which columns and in which direction results should be ordered.</param>
        /// <returns>List of objects.</returns>
        public List <T> SelectMany <T>(int?indexStart, int?maxResults, DbExpression expr, DbResultOrder[] resultOrder) where T : class, new()
        {
            if (!_Initialized)
            {
                throw new InvalidOperationException("Initialize WatsonORM and database using the .InitializeDatabase() method first.");
            }
            if (expr == null)
            {
                throw new ArgumentNullException(nameof(expr));
            }
            if (resultOrder == null || resultOrder.Length < 1)
            {
                throw new ArgumentNullException(nameof(resultOrder));
            }

            string tableName              = _TypeMetadataMgr.GetTableNameFromType(typeof(T));
            string primaryKeyColumnName   = _TypeMetadataMgr.GetPrimaryKeyColumnName(typeof(T));
            string primaryKeyPropertyName = _TypeMetadataMgr.GetPrimaryKeyPropertyName(typeof(T));

            Expression e = WatsonORMCommon.DbExpressionConverter(expr);

            e.PrependAnd(primaryKeyColumnName, WatsonORMCommon.DbOperatorsConverter(DbOperators.IsNotNull), null);

            DataTable result = _Database.Select(tableName, indexStart, maxResults, null, e, DbResultOrder.ConvertToResultOrder(resultOrder));

            return(DataTableToObjectList <T>(result));
        }
예제 #2
0
        /// <summary>
        /// SELECT multiple rows.
        /// This operation will return an empty list if no matching objects are found.
        /// The ordering used in the underlying query is ascending based on primary key column.
        /// </summary>
        /// <typeparam name="T">Type of object.</typeparam>
        /// <param name="expr">Expression.</param>
        /// <returns>List of objects.</returns>
        public List <T> SelectMany <T>(DbExpression expr) where T : class, new()
        {
            if (!_Initialized)
            {
                throw new InvalidOperationException("Initialize WatsonORM and database using the .InitializeDatabase() method first.");
            }
            if (expr == null)
            {
                throw new ArgumentNullException(nameof(expr));
            }

            string tableName            = _TypeMetadataMgr.GetTableNameFromType(typeof(T));
            string primaryKeyColumnName = _TypeMetadataMgr.GetPrimaryKeyColumnName(typeof(T));

            Expression e = WatsonORMCommon.DbExpressionConverter(expr);

            e.PrependAnd(primaryKeyColumnName, WatsonORMCommon.DbOperatorsConverter(DbOperators.IsNotNull), null);

            ResultOrder[] resultOrder = new ResultOrder[1];
            resultOrder[0] = new ResultOrder(primaryKeyColumnName, OrderDirection.Ascending);

            DataTable result = _Database.Select(tableName, null, null, null, e, resultOrder);

            return(DataTableToObjectList <T>(result));
        }
예제 #3
0
        /// <summary>
        /// DELETE an object by its primary key.
        /// </summary>
        /// <typeparam name="T">Type of object.</typeparam>
        /// <param name="id">Id value.</param>
        public void DeleteByPrimaryKey <T>(object id) where T : class, new()
        {
            if (!_Initialized)
            {
                throw new InvalidOperationException("Initialize WatsonORM and database using the .InitializeDatabase() method first.");
            }
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            string tableName            = _TypeMetadataMgr.GetTableNameFromType(typeof(T));
            string primaryKeyColumnName = _TypeMetadataMgr.GetPrimaryKeyColumnName(typeof(T));

            Expression e = new Expression(primaryKeyColumnName, WatsonORMCommon.DbOperatorsConverter(DbOperators.Equals), id);

            _Database.Delete(tableName, e);
        }
예제 #4
0
        /// <summary>
        /// UPDATE multiple rows.
        /// </summary>
        /// <typeparam name="T">Type of object.</typeparam>
        /// <param name="expr">Expression.</param>
        /// <param name="updateVals">Update values.</param>
        public void UpdateMany <T>(DbExpression expr, Dictionary <string, object> updateVals)
        {
            if (!_Initialized)
            {
                throw new InvalidOperationException("Initialize WatsonORM and database using the .InitializeDatabase() method first.");
            }
            if (expr == null)
            {
                throw new ArgumentNullException(nameof(expr));
            }
            if (updateVals == null || updateVals.Count < 1)
            {
                throw new ArgumentNullException(nameof(updateVals));
            }

            string tableName            = _TypeMetadataMgr.GetTableNameFromType(typeof(T));
            string primaryKeyColumnName = _TypeMetadataMgr.GetPrimaryKeyColumnName(typeof(T));

            Expression e = WatsonORMCommon.DbExpressionConverter(expr);

            e.PrependAnd(primaryKeyColumnName, WatsonORMCommon.DbOperatorsConverter(DbOperators.IsNotNull), null);

            _Database.Update(tableName, updateVals, e);
        }
예제 #5
0
        /// <summary>
        /// UPDATE an object.
        /// </summary>
        /// <typeparam name="T">Type of object.</typeparam>
        /// <param name="obj">Object to UPDATE.</param>
        /// <returns>UPDATEd object.</returns>
        public T Update <T>(T obj) where T : class, new()
        {
            if (!_Initialized)
            {
                throw new InvalidOperationException("Initialize WatsonORM and database using the .InitializeDatabase() method first.");
            }
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            string tableName              = _TypeMetadataMgr.GetTableNameFromObject(obj);
            string primaryKeyColumnName   = _TypeMetadataMgr.GetPrimaryKeyColumnName(typeof(T));
            string primaryKeyPropertyName = _TypeMetadataMgr.GetPrimaryKeyPropertyName(typeof(T));
            object primaryKeyValue        = _TypeMetadataMgr.GetPrimaryKeyValue(obj, primaryKeyPropertyName);

            Dictionary <string, object> updateVals = ObjectToDictionary(obj);
            Expression e = new Expression(primaryKeyColumnName, WatsonORMCommon.DbOperatorsConverter(DbOperators.Equals), primaryKeyValue);

            _Database.Update(tableName, updateVals, e);
            DataTable result = _Database.Select(tableName, null, null, null, e, null);

            return(DataTableToObject <T>(result));
        }