Ejemplo n.º 1
0
 public static string GetSqlString(DataWhereQueue queue, DataSource ds, bool prefix, bool select)
 {
     if (queue != null)
     {
         return(queue.GetSqlString(ds, prefix, select));
     }
     return(null);
 }
Ejemplo n.º 2
0
        public static DeleteBucket GetDeleteSql(DataSource ds, object instance, DataColumn[] keys, DataWhereQueue ps, Dictionary <string, KeyValuePair <FieldInfo, DataColumnAttribute> > fields, string[] pks)
        {
            StringBuilder        wheres = new StringBuilder();
            List <DataParameter> list   = new List <DataParameter>();

            KeyValuePair <FieldInfo, DataColumnAttribute> pair;
            int  i      = 0;
            bool setted = false;

            if (keys != null && keys.Length > 0)
            {
                foreach (DataColumn key in keys)
                {
                    pair = fields[key.Column];
                    if (i++ > 0)
                    {
                        wheres.Append(" AND ");
                    }
                    DataParameter dp;
                    //if(key is DataValueColumn)
                    //    dp = (DataValueColumn)key;
                    //else
                    dp = new DataParameter(key.Column, pair.Key.GetValue(instance));
                    wheres.Append(dp.GetSqlString(ds, false, false));
                    list.Add(dp);
                }
                setted = true;
            }
            if (ps != null)
            {
                wheres.Append(ps.GetSqlString(ds, false, false));
                DataParameter[] dp = ps.Parameters;
                if (dp.Length > 0)
                {
                    list.AddRange(dp);
                }
                i      = dp.Length;
                setted = true;
            }
            if (!setted)
            {
                foreach (string key in pks)
                {
                    pair = fields[key];
                    if (i++ > 0)
                    {
                        wheres.Append(" AND ");
                    }
                    DataParameter dp = new DataParameter(key, pair.Key.GetValue(instance));
                    wheres.Append(dp.GetSqlString(ds, false, false));
                    list.Add(dp);
                }
            }

            return(new DeleteBucket(wheres.ToString(), list.ToArray()));
        }
Ejemplo n.º 3
0
        public static UpdateBucket GetUpdateSql(DataSource ds, object instance, ColumnMode mode, DataColumn[] keys, DataWhereQueue ps, Dictionary <string, KeyValuePair <FieldInfo, DataColumnAttribute> > fields, string[] pks)
        {
            StringBuilder        sets   = new StringBuilder();
            StringBuilder        wheres = new StringBuilder();
            List <DataParameter> list   = new List <DataParameter>();

            KeyValuePair <FieldInfo, DataColumnAttribute> pair;

            if (mode == ColumnMode.Include)
            {
                if (keys == null)
                {
                    throw new ArgumentNullException("keys or columns");
                }
                if (keys.Length == 0)
                {
                    throw new ArgumentException();
                }

                int i = 0;
                if (keys != null && keys.Length > 0)
                {
                    foreach (DataColumn key in keys)
                    {
                        pair = fields[key.Column];
                        if (pair.Value == null || (!pair.Value.IsPrimaryKey && !pair.Value.IsIdentity))
                        {
                            if (i++ > 0)
                            {
                                sets.Append(',');
                            }
                            DataParameter dp;
                            //if (key is DataValueColumn)
                            //{
                            //    dp = (DataValueColumn)key;
                            //    sets.Append(dp.GetSqlString(this));
                            //}
                            //else
                            if (key is DataActionColumn)
                            {
                                dp = (DataActionColumn)key;
                                sets.Append(key.GetSqlString(ds, false, false));
                            }
                            else
                            {
                                dp = new DataParameter(key.Column, pair.Key.GetValue(instance));
                                sets.Append(dp.GetSqlString(ds, false, false));
                            }
                            list.Add(dp);
                        }
                    }
                }
            }
            else
            {
                Dictionary <string, bool> dict = null;
                if (keys != null)
                {
                    dict = new Dictionary <string, bool>(keys.Length);
                    foreach (DataColumn key in keys)
                    {
                        dict.Add(key.Column, true);
                    }
                }

                int  i = 0;
                bool value;
                foreach (string key in fields.Keys)
                {
                    pair = fields[key];
                    if (pair.Value == null || !pair.Value.IsPrimaryKey)
                    {
                        if (dict == null || dict.Count == 0 || !dict.TryGetValue(key, out value))
                        {
                            if (i++ > 0)
                            {
                                sets.Append(',');
                            }
                            DataParameter dp = new DataParameter(key, pair.Key.GetValue(instance));
                            sets.Append(dp.GetSqlString(ds, false, false));
                            list.Add(dp);
                        }
                    }
                }
            }

            int j = 0;

            if (ps != null)
            {
                wheres.Append(ps.GetSqlString(ds, false, false));
                DataParameter[] dp = ps.Parameters;
                if (dp.Length > 0)
                {
                    list.AddRange(dp);
                }
                j = dp.Length;
            }
            else
            {
                foreach (string key in pks)
                {
                    pair = fields[key];
                    if (j++ > 0)
                    {
                        wheres.Append(" AND ");
                    }
                    DataParameter dp = new DataParameter(key, pair.Key.GetValue(instance));
                    wheres.Append(dp.GetSqlString(ds, false, false));
                    list.Add(dp);
                }
            }
            return(new UpdateBucket(sets.ToString(), wheres.ToString(), list.ToArray()));
        }