Beispiel #1
0
        protected bool InsertOrReplaceImpl(DataSource ds, ColumnMode mode, params DataColumn[] columns)
        {
            if (ds.Provider.SupperReplace)
            {
                InsertBucket bucket = GetInsertSql(ds, mode, columns);
                string       sql    = ds.Provider.BuildInsertOrReplaceSql(GetTableName(), bucket.Names, bucket.Values, bucket.Id);
                return(ds.ExecuteNonQuery(sql, bucket.Parameters) > 0);
            }

            DataWhereQueue ps = null;

            KeyValuePair <string, FieldInfo>[] pks = GetPrimaryKeys();
            foreach (KeyValuePair <string, FieldInfo> pair in pks)
            {
                if (ps == null)
                {
                    ps = new DataParameter(pair.Key, pair.Value.GetValue(this));
                }
                else
                {
                    ps &= new DataParameter(pair.Key, pair.Value.GetValue(this));
                }
            }
            if (ExecuteCount(ds, ps) > 0)
            {
                return(UpdateImpl(ds, mode, columns, ps) > 0);
            }

            return(InsertImpl(ds, mode, columns));
        }
Beispiel #2
0
        private DeleteBucket GetDeleteSql(DataSource ds, DataColumn[] keys, DataWhereQueue ps)
        {
            Type       gtype  = typeof(TDbTable <>).MakeGenericType(GetType());
            MethodInfo method = gtype.GetMethod("GetDeleteSql", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic);

            return((DeleteBucket)method.Invoke(null, new object[] { ds, this, keys, ps }));
        }
Beispiel #3
0
 public static DataParameter[] GetParameters(DataWhereQueue queue)
 {
     if (queue != null)
     {
         return(queue.Parameters);
     }
     return(null);
 }
Beispiel #4
0
 public static string GetSqlString(DataWhereQueue queue, DataSource ds, bool prefix, bool select)
 {
     if (queue != null)
     {
         return(queue.GetSqlString(ds, prefix, select));
     }
     return(null);
 }
Beispiel #5
0
 internal DataParameter[] GetParameters()
 {
     if (_where != null)
     {
         return(DataWhereQueue.GetParameters(_where));
     }
     return(null);
 }
Beispiel #6
0
 public static V ExecuteScalar <A, B, V>(DataSource ds, DataColumn column, DataColumn[] group, DataOrder[] order, string aId, string bId, DataJoinType type = DataJoinType.Inner, DataWhereQueue ps = null) where A : DbTable, new() where B : DbTable, new()
 {
     return(ds.ExecuteScalar <V>(ds.Provider.BuildSelectJoinSql(GetTableName <A>(), GetTableName <B>(), type, aId, bId, column.GetSqlString(ds, true, true), DataProvider.GetSqlString(ps, ds, true, false), DataProvider.GetSqlString(order, ds, true, false), DataProvider.GetSqlString(group, ds, true, false)), DataWhereQueue.GetParameters(ps)));
 }
Beispiel #7
0
        public static IList <DataJoin <A, B> > ExecuteReader <A, B>(DataSource ds, DataColumn[] columns, DataColumn[] group, DataOrder[] order, int index, int size, out int count, string aId, string bId, DataJoinType type = DataJoinType.Inner, DataWhereQueue ps = null) where A : DbTable, new() where B : DbTable, new()
        {
            long total;
            IList <DataJoin <A, B> > result = ExecuteReader <A, B>(ds, columns, group, order, index, size, out total, aId, bId, type, ps);

            count = (int)total;
            return(result);
        }
Beispiel #8
0
 public static V ExecuteScalar <T, V>(DataSource ds, string column, DataWhereQueue ps = null) where T : DbTable
 {
     return(ds.ExecuteScalar <V>(ds.Provider.BuildSelectSql(GetTableName <T>(), ds.Provider.EscapeName(column), DataProvider.GetSqlString(ps, ds, false, false)), DataWhereQueue.GetParameters(ps)));
 }
Beispiel #9
0
 public static V ExecuteScalar <T, V>(DataSource ds, DataColumn column, DataColumn[] group, DataOrder[] order, DataWhereQueue ps = null) where T : DbTable
 {
     return(ds.ExecuteScalar <V>(ds.Provider.BuildSelectSql(GetTableName <T>(), column.GetSqlString(ds, false, true), DataProvider.GetSqlString(ps, ds, false, false), DataProvider.GetSqlString(order, ds, false, false), DataProvider.GetSqlString(group, ds, false, false)), DataWhereQueue.GetParameters(ps)));
 }
Beispiel #10
0
 internal DataWhereQueue(DataWhereQueue l, DataWhereQueue r, DataWhereUnionType type)
 {
     AddQueue(l, r, type);
     _list.AddRange(l.Parameters);
     _list.AddRange(r.Parameters);
 }
Beispiel #11
0
        protected static int UpdateImpl <T>(T instance, DataSource ds, ColumnMode mode, DataColumn[] columns, DataWhereQueue ps = null) where T : DbTable
        {
            UpdateBucket bucket = TDbTable <T> .GetUpdateSql(ds, instance, mode, columns, ps);

            return(ds.ExecuteNonQuery(ds.Provider.BuildUpdateSql(GetTableName <T>(), bucket.Sets, bucket.Wheres), bucket.Parameters));
        }
Beispiel #12
0
 public JoinWhereQuery Where(DataWhereQueue where)
 {
     _where = new JoinWhereQuery(this, where);
     return(_where);
 }
Beispiel #13
0
 internal WhereQuery(SelectQuery query, DataWhereQueue where)
 {
     _query = query;
     _where = where;
 }
Beispiel #14
0
        public static IList <T> ExecuteReader <T>(DataSource ds, DataColumn[] columns, DataColumn[] group, DataOrder[] order, long index, int size, out long count, DataWhereQueue ps = null) where T : DbTable, new()
        {
            if (ds.Provider.SupperRowNumber)
            {
                return(ExecuteReaderWithRowNumber <T>(ds, DataProvider.GetSqlString(columns, ds, false, true), DataProvider.GetSqlString(ps, ds, false, false), DataProvider.GetSqlString(order, ds, false, false), DataProvider.GetSqlString(group, ds, false, false), index, size, out count, DataWhereQueue.GetParameters(ps)));
            }
            IList <T> list = ExecuteReaderWithLimitOrTop <T>(ds, DataProvider.GetSqlString(columns, ds, false, true), DataProvider.GetSqlString(ps, ds, false, false), DataProvider.GetSqlString(order, ds, false, false), DataProvider.GetSqlString(group, ds, false, false), index, size, out count, DataWhereQueue.GetParameters(ps));

            if (ds.Provider.SupperTop)
            {
                list = SplitList(list, index, size);
            }
            return(list);
        }
Beispiel #15
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()));
        }
Beispiel #16
0
        public static IList <T> ExecuteReader <T>(DataSource ds, DataColumn[] columns, DataColumn[] group, DataOrder[] order, int index, int size, out int count, DataWhereQueue ps = null) where T : DbTable, new()
        {
            long      total;
            IList <T> result = ExecuteReader <T>(ds, columns, group, order, index, size, out total, ps);

            count = (int)total;
            return(result);
        }
Beispiel #17
0
 public long ExecuteCount(DataSource ds, DataWhereQueue ps = null)
 {
     return(ExecuteCount(ds, DataProvider.GetSqlString(ps, ds, false, false), null, DataWhereQueue.GetParameters(ps)));
 }
Beispiel #18
0
 public static long ExecuteCount <A, B>(DataSource ds, DataColumn[] group, string aId, string bId, DataJoinType type = DataJoinType.Inner, DataWhereQueue ps = null) where A : DbTable where B : DbTable
 {
     return(ExecuteCount <A, B>(ds, DataProvider.GetSqlString(ps, ds, true, false), DataProvider.GetSqlString(group, ds, true, false), aId, bId, type, DataWhereQueue.GetParameters(ps)));
 }
Beispiel #19
0
 public static long ExecuteCount <T>(DataSource ds, DataColumn[] group, DataWhereQueue ps = null) where T : DbTable
 {
     return(ExecuteCount <T>(ds, DataProvider.GetSqlString(ps, ds, false, false), DataProvider.GetSqlString(group, ds, false, false), DataWhereQueue.GetParameters(ps)));
 }
Beispiel #20
0
        protected int UpdateImpl(DataSource ds, ColumnMode mode, DataColumn[] columns, DataWhereQueue ps = null)
        {
            UpdateBucket bucket = GetUpdateSql(ds, mode, columns, ps);

            return(ds.ExecuteNonQuery(ds.Provider.BuildUpdateSql(GetTableName(), bucket.Sets, bucket.Wheres), bucket.Parameters));
        }
Beispiel #21
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()));
        }
Beispiel #22
0
        public static IList <DataJoin <A, B> > ExecuteReader <A, B>(DataSource ds, DataColumn[] columns, DataColumn[] group, DataOrder[] order, long index, int size, out long count, string aId, string bId, DataJoinType type = DataJoinType.Inner, DataWhereQueue ps = null) where A : DbTable, new() where B : DbTable, new()
        {
            if (ds.Provider.SupperRowNumber)
            {
                return(ExecuteJoinReaderWithRowNumber <A, B>(ds, DataProvider.GetSqlString(columns, ds, true, true), DataProvider.GetSqlString(ps, ds, true, false), order, DataProvider.GetSqlString(group, ds, true, false), index, size, out count, aId, bId, type, DataWhereQueue.GetParameters(ps)));
            }
            IList <DataJoin <A, B> > list = ExecuteJoinReaderWithLimitOrTop <A, B>(ds, DataProvider.GetSqlString(columns, ds, true, true), DataProvider.GetSqlString(ps, ds, true, false), DataProvider.GetSqlString(order, ds, true, false), DataProvider.GetSqlString(group, ds, true, false), index, size, out count, aId, bId, type, DataWhereQueue.GetParameters(ps));

            if (ds.Provider.SupperTop)
            {
                list = SplitJoinList(list, index, size);
            }
            return(list);
        }
Beispiel #23
0
 internal JoinWhereQuery(JoinSelectQuery query, DataWhereQueue where)
 {
     _query = query;
     _where = where;
 }
Beispiel #24
0
 public static IList <DataJoin <A, B> > ExecuteReader <A, B>(DataSource ds, int top, DataColumn[] columns, DataColumn[] group, DataOrder[] order, string aId, string bId, DataJoinType type = DataJoinType.Inner, DataWhereQueue ps = null) where A : DbTable, new() where B : DbTable, new()
 {
     return(ds.ExecuteReader <DataJoin <A, B> >(ds.Provider.BuildSelectJoinSql(GetTableName <A>(), GetTableName <B>(), type, aId, bId, DataProvider.GetSqlString(columns, ds, true, true), DataProvider.GetSqlString(ps, ds, true, false), DataProvider.GetSqlString(order, ds, true, false), DataProvider.GetSqlString(group, ds, true, false), top), DataWhereQueue.GetParameters(ps)));
 }
Beispiel #25
0
 private void Add(DataWhereQueue value, DataWhereUnionType type)
 {
     AddQueue(value, type);
     _list.AddRange(value.Parameters);
 }
Beispiel #26
0
        protected static int DeleteImpl <T>(T instance, DataSource ds, DataWhereQueue ps = null) where T : DbTable
        {
            DeleteBucket bucket = TDbTable <T> .GetDeleteSql(ds, instance, null, ps);

            return(ds.ExecuteNonQuery(ds.Provider.BuildDeleteSql(GetTableName <T>(), bucket.Wheres), bucket.Parameters));
        }
Beispiel #27
0
 public static IList <T> ExecuteReader <T>(DataSource ds, DataWhereQueue ps = null) where T : DbTable, new()
 {
     return(ds.ExecuteReader <T>(ds.Provider.BuildSelectSql(GetTableName <T>(), null, DataProvider.GetSqlString(ps, ds, false, false)), DataWhereQueue.GetParameters(ps)));
 }
Beispiel #28
0
        protected int DeleteImpl(DataSource ds, DataWhereQueue ps = null)
        {
            DeleteBucket bucket = GetDeleteSql(ds, null, ps);

            return(ds.ExecuteNonQuery(ds.Provider.BuildDeleteSql(GetTableName(), bucket.Wheres), bucket.Parameters));
        }
Beispiel #29
0
 internal static DeleteBucket GetDeleteSql(DataSource ds, T instance, DataColumn[] keys, DataWhereQueue ps)
 {
     return(DataProvider.GetDeleteSql(ds, instance, keys, ps, TAllNameGetAttFields <T, DataColumnAttribute> .Fields, PrimaryKey));
 }
Beispiel #30
0
 public static IList <T> ExecuteReader <T>(DataSource ds, int top, DataColumn[] columns, DataOrder[] order, DataWhereQueue ps = null) where T : DbTable, new()
 {
     return(ds.ExecuteReader <T>(ds.Provider.BuildSelectSql(GetTableName <T>(), DataProvider.GetSqlString(columns, ds, false, true), DataProvider.GetSqlString(ps, ds, false, false), DataProvider.GetSqlString(order, ds, false, false), null, top), DataWhereQueue.GetParameters(ps)));
 }