예제 #1
0
        public virtual IList ToList(IModel model, string fieldName, Queryset queryset)
        {
            string query = GenerateSelectQuery(model.ModelDbAlias + "." + fieldName, model, queryset);
            IList<DbValue[]> dbValuesList = ExecuteQuery(query, queryset != null ? queryset.GetParamsList() : null);

            ArrayList list = new ArrayList(dbValuesList.Count);
            foreach (DbValue[] dbValues in dbValuesList)
                list.Add(dbValues[0].Value);

            return list;
        }
예제 #2
0
        protected string GenerateSelectQuery(string fieldsList, IModel model, Queryset queryset)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT ");
            sb.Append(fieldsList);
            sb.Append(" FROM ");
            sb.Append(model.ModelDbName + " as " + model.ModelDbAlias);
            if (queryset != null && !queryset.IsEmpty)
            {
                sb.Append(queryset.GetJoins(this));
                sb.Append(queryset.GetWhereClause(this));
                sb.Append(queryset.GetOrderByClause());
                sb.Append(ToSqlLimit(queryset.LimitOffset, queryset.LimitCount));
            }

            return sb.ToString();
        }
예제 #3
0
        public virtual long SelectCount(IModel model, Queryset queryset = null)
        {
            if (model != null)
            {
                string sCountSql = string.Format("SELECT COUNT(1) FROM {0} as {1}{2}{3}",
                                    model.ModelDbName,
                                    model.ModelDbAlias,
                                    queryset != null ? queryset.GetJoins(this): "",
                                    queryset != null ? queryset.GetWhereClause(this) : "");

                object res = ExecuteScalar(sCountSql, queryset != null ? queryset.GetParamsList() : null);

                long count;
                if (res != null && Int64.TryParse(res.ToString(), out count))
                    return count;
            }

            return -1;
        }
예제 #4
0
        public virtual DataTable ToDataTable(IModel model, string[] fieldNames, Queryset queryset)
        {
            string query = "";
            for (int i = 0; i < fieldNames.Length; i++)
            {
                if (i > 0)
                    query += ",";
                query += model.ModelDbAlias + "." + fieldNames[i];
            }

            query = GenerateSelectQuery(query, model, queryset);
            IList<DbValue[]> dbValuesList = ExecuteQuery(query, queryset != null ? queryset.GetParamsList() : null);

            DataTable dt = new DataTable();
            foreach (string fieldName in fieldNames)
                dt.Columns.Add(new DataColumn(fieldName, typeof(object)));

            foreach (DbValue[] dbValues in dbValuesList)
            {
                DataRow row = dt.NewRow();
                foreach (DbValue dbValue in dbValues)
                    row[dbValue.Column] = dbValue.Value;
                dt.Rows.Add(row);
            }
            return dt;
        }
예제 #5
0
        public virtual bool Delete(IModel model, Queryset queryset)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("DELETE FROM ");
            sb.Append(model.ModelDbName + " as " + model.ModelDbAlias);
            if (queryset != null && !queryset.IsEmpty)
            {
                sb.Append(queryset.GetJoins(this));
                sb.Append(queryset.GetWhereClause(this));
            }

            ExecuteNonQuery(sb.ToString(), queryset != null ? queryset.GetParamsList() : null);
            return true;
        }
예제 #6
0
 public virtual IList<DbValue[]> Select(IModel model, Queryset queryset = null)
 {
     string query = GenerateSelectQuery(ToFieldNamesList(model), model, queryset);
     return ExecuteQuery(query, queryset != null ? queryset.GetParamsList() : null);
 }
예제 #7
0
        public IList ToList(string fieldName, Queryset queryset = null)
        {
            if (queryset != null)
                queryset.Compile();

            return DbEngine.ToList(ModelInfo, fieldName, queryset);
        }
예제 #8
0
        public long Count(Queryset queryset = null)
        {
            if (queryset != null)
                queryset.Compile();

            return DbEngine.SelectCount(ModelInfo, queryset);
        }
예제 #9
0
        public ModelsPage Page(int pagenum, int pagesize, Queryset queryset = null)
        {
            if (pagesize > 0)
            {

                long modelsCount = Count(queryset);

                if (queryset != null)
                    queryset = queryset.Clone();
                else
                    queryset = new Queryset(this);

                queryset.Limit((pagenum - 1) * pagesize, pagesize);

                if (modelsCount > 0)
                {
                    int pagesCount = (int)Math.Ceiling((long)modelsCount * 1.0 / pagesize);
                    return new ModelsPage((long)modelsCount > 0 ? Load(queryset) : null,
                                          pagesCount,
                                          pagenum,
                                          pagesize);
                }
            }

            return ModelsPage.Empty;
        }
예제 #10
0
        public DataTable ToDataTable(string[] fieldNames, Queryset queryset = null)
        {
            if (queryset != null)
                queryset.Compile();

            return DbEngine.ToDataTable(ModelInfo, fieldNames, queryset);
        }
예제 #11
0
 public Model Get(Queryset queryset)
 {
     IList<Model> list = All(queryset);
     if (list != null && list.Count > 0)
         return list[0];
     return null;
 }
예제 #12
0
        public Model Get(object pk = null)
        {
            if (pk != null)
            {
                QExprGroup qeg = new AndExprGroup().Add(
                    new QExpr(ModelInfo.ModelPKName, Constants.QueryCompareOps.EXACT, pk)
                );

                IEnumerable<Model> models = new Queryset(this).Filter(qeg).All();
                if (models != null && models.Count() == 1)
                    return models.Single();

                return null;
            }
            else
            {
                return OrmManager.CreateNewModel(ModelInfo.ModelApp, ModelInfo.ModelType);
            }
        }
예제 #13
0
        public bool Delete(Queryset queryset)
        {
            if (queryset != null)
                queryset.Compile();

            return DbEngine.Delete(ModelInfo, queryset);
        }
예제 #14
0
 protected Queryset(Queryset source)
 {
     Queryset.CopyTo(source, this);
 }
예제 #15
0
        protected IList<Model> Load(Queryset queryset = null)
        {
            if (queryset != null)
                queryset.Compile();

            IList<DbValue[]> rows = DbEngine.Select(ModelInfo, queryset);

            if (rows != null && rows.Count > 0)
            {
                List<Model> models = new List<Model>();
                foreach (DbValue[] row in rows)
                {
                    Model model = OrmManager.CreateNewModel(ModelInfo.ModelApp, ModelInfo.ModelType);
                    if (model != null)
                    {
                        foreach (DbValue dbValue in row)
                        {
                            Field field = model.GetFieldByDbName(dbValue.Column);
                            if (field != null)
                                model[field.Name] = field.FromDbValue(dbValue.Value);
                        }
                        model.Status = ModelStatus.Loaded;
                        models.Add(model);
                    }
                }

                return models;
            }

            return null;
        }
예제 #16
0
 protected internal static void CopyTo(Queryset source, Queryset target)
 {
     target._manager = source._manager;
     target._filterConditions = source._filterConditions;
     target._excludeConditions = source._excludeConditions;
     target._compiled = false;
     target._joins = source._joins;
     target.LimitOffset = source.LimitOffset;
     target.LimitCount = source.LimitCount;
     target.OrderByFields = source.OrderByFields;
 }
예제 #17
0
 public IList<Model> All(Queryset queryset = null)
 {
     return Load(queryset);
 }