Beispiel #1
0
        private String GetUpdateSql(Tuple <List <String>, List <String> > tuple)
        {
            String        pk     = EntityTableMapper.GetPkColumn(typeof(T));
            StringBuilder colstr = new StringBuilder();
            List <String> cols   = tuple.Item1;
            List <String> props  = tuple.Item2;

            for (Int32 i = 0; i < cols.Count; i++)
            {
                //去掉主键
                if (cols[i] == pk)
                {
                    continue;
                }
                if (colstr.Length != 0)
                {
                    colstr.Append(",");
                }
                // synctimes = synctimes+1 的形式便直接拼接
                //注意,此处在解析header时候,便是1:1的关系,如果是set中便是为空
                if (String.IsNullOrEmpty(props[i]))
                {
                    colstr.Append($" {cols[i]} ");
                }
                else
                {
                    colstr.Append($"{cols[i]} = @{props[i]}");
                }
            }
            return(colstr.ToString());
        }
Beispiel #2
0
        /// <summary>
        /// 此处去掉所有数据库自增数据
        /// </summary>
        /// <param name="tuple"></param>
        /// <returns></returns>
        private String GetInsertSql(Tuple <List <String>, List <String> > tuple)
        {
            HashSet <String> defaults = EntityTableMapper.DefaultColumn(typeof(T));
            StringBuilder    colstr   = new StringBuilder();
            StringBuilder    valstr   = new StringBuilder();
            List <String>    cols     = tuple.Item1;
            List <String>    props    = tuple.Item2;

            for (Int32 i = 0; i < cols.Count; i++)
            {
                //去掉自增键
                if (defaults.Contains(cols[i]))
                {
                    continue;
                }
                if (colstr.Length != 0)
                {
                    colstr.Append(",");
                    valstr.Append(",");
                }
                colstr.Append(cols[i]);
                valstr.Append($"@{props[i]}");
            }
            return($"({colstr.ToString()}) values ({valstr.ToString()})");
        }
Beispiel #3
0
        protected Query()
        {
            Type t = typeof(T);

            ValuePairs = EntityTableMapper.GetColumns(t);
            Reduce     = new QueryParameterReduce();
            Reduce.AddTable(t);
        }
Beispiel #4
0
        public async Task <T> GetAsync(Object pkValue)
        {
            Type   t    = typeof(T);
            String name = EntityTableMapper.GetPkColumn(t);
            Expression <Func <T, Boolean> > expression = PropertyValueExpression <T> .BuildExpression(name, pkValue);

            return(await m_executeBatch.QueryAsync(m_query.Select().Where(expression).End()));
        }
Beispiel #5
0
        private String ConditionConvert(ColumnRelevanceMapper column, bool isNull = false)
        {
            StringBuilder sb = new StringBuilder();

            //如果 null == item.id 的写法, 会解析成
            //column.ColumnName == null && column.SqlOperatorEnum == SqlOperatorEnum.None
            // null,那么就直接返回"", 接下来的  == item.id 会传递false,然后解析 item.id is null
            if (column.ColumnName == null && column.SqlOperatorEnum == SqlOperatorEnum.None)
            {
                return("");
            }
            //如果是 item.id == null 中写法 那么 == null 则返回 is null
            if (isNull || column.ColumnName == null)
            {
                column.SqlOperatorEnum = SqlEnumConvert(column.SqlOperatorEnum);
            }
            if (column.SqlOperatorEnum == SqlOperatorEnum.EqualNotNull ||
                column.SqlOperatorEnum == SqlOperatorEnum.EqualNull)
            {
                String str = "";
                if (!String.IsNullOrEmpty(column.ColumnName))
                {
                    Type   t          = column.TableName;
                    String tableName  = EntityTableMapper.GetTableName(t);
                    String columnName = EntityTableMapper.GetColoumName(t, column.ColumnName);
                    str = $"{tableName}.{columnName}";
                }
                return($"{str} {column.SqlOperatorEnum.GetOperator()}");
            }
            if (column.SqlOperatorEnum != SqlOperatorEnum.None)
            {
                sb.Append(column.SqlOperatorEnum.GetOperator());
            }
            //如果是 直接传递字符串 如 id in (select id from xxx)中,则 括号中则直接拼接,而不用@数字
            //如果是 item.id == 1 中的  == 1,那么只 保存 = @ColumnName
            if (column.TableName == null)
            {
                if (column.ColumnName.IndexOf("(") != -1)
                {
                    sb.Append($" {column.ColumnName} ");
                }
                else
                {
                    sb.Append($"@{column.ColumnName}");
                }
            }
            //如果是 item.id == 1 中的  item.id ,那么就保存 tablename.id
            else
            {
                Type   t          = column.TableName;
                String tableName  = EntityTableMapper.GetTableName(t);
                String columnName = EntityTableMapper.GetColoumName(t, column.ColumnName);
                sb.Append($" {tableName}.{columnName} ");
            }
            return(sb.ToString());
        }
Beispiel #6
0
        public override IBatch End()
        {
            IBatch batch = new MysqlBatch();
            Tuple <List <String>, List <String> > tuple = ConvertHeader(Reduce.GetHeader());
            Type                         t            = typeof(T);
            StringBuilder                sb           = new StringBuilder();
            TableRelevanceMapper         table        = Reduce.GetTable();
            List <ColumnRelevanceMapper> wherecolumns = Reduce.GetWhere();

            sb.Append(table.TableOperatorEnum.GetOperator());
            sb.Append($" {EntityTableMapper.GetTableName(t)} ");
            switch (table.TableOperatorEnum)
            {
            case TableOperatorEnum.Insert:
                sb.Append(GetInsertSql(tuple));
                break;

            case TableOperatorEnum.Update:
                sb.Append(" set ");
                sb.Append(GetUpdateSql(tuple));
                if (wherecolumns.Count == 0)
                {
                    SetNoneWhere(t);
                }
                break;

            case TableOperatorEnum.Delete:
                if (wherecolumns.Count == 0)
                {
                    SetNoneWhere(t);
                }
                break;

            default:
                throw new Exception("请先选择操作种类");
            }
            String where = ConvertToWhere(wherecolumns);
            if (!String.IsNullOrEmpty(where))
            {
                sb.Append($" where {where}");
            }
            batch.SqlBuilder        = sb.ToString();
            batch.DynamicParameters = Reduce.Parameters;
            return(batch);
        }
Beispiel #7
0
        private String ConditionConvert(ColumnRelevanceMapper column, bool isNull = false)
        {
            StringBuilder sb = new StringBuilder();

            //如果 null == item.id 中写法 null,那么返回"", == item.id 返回item.id is null
            //如果是 item.id == null 中写法 那么 == null 则返回 is null
            if (column.ColumnName == null && column.SqlOperatorEnum == SqlOperatorEnum.None)
            {
                return("");
            }
            if (isNull || column.ColumnName == null)
            {
                column.SqlOperatorEnum = SqlEnumConvert(column.SqlOperatorEnum);
            }
            if (column.SqlOperatorEnum == SqlOperatorEnum.EqualNotNull ||
                column.SqlOperatorEnum == SqlOperatorEnum.EqualNull)
            {
                String str = "";
                if (!String.IsNullOrEmpty(column.ColumnName))
                {
                    Type   t          = column.TableName;
                    String tableName  = EntityTableMapper.GetTableName(t);
                    String columnName = EntityTableMapper.GetColoumName(t, column.ColumnName);
                    str = $"{tableName}.{columnName}";
                }
                return($"{str} {column.SqlOperatorEnum.GetOperator()}");
            }
            if (column.SqlOperatorEnum != SqlOperatorEnum.None)
            {
                sb.Append(column.SqlOperatorEnum.GetOperator());
            }
            //如果是 item.id = 1 中的  = 1,那么只 保存 = @ColumnName
            if (column.TableName == null)
            {
                sb.Append($"@{column.ColumnName}");
            }
            else
            {
                Type   t          = column.TableName;
                String tableName  = EntityTableMapper.GetTableName(t);
                String columnName = EntityTableMapper.GetColoumName(t, column.ColumnName);
                sb.Append($" {tableName}.{columnName} ");
            }
            return(sb.ToString());
        }
Beispiel #8
0
 private void SetQueryOtherTable <T1>(Expression <Func <T1, Object> > query = null)
 {
     lock (_lock)
     {
         Type tablename  = typeof(T1);
         var  valuePairs = EntityTableMapper.GetColumns(tablename);
         if (query == null)
         {
             foreach (var item in valuePairs)
             {
                 Reduce.AddQuery(tablename, item.Key);
             }
         }
         else
         {
             Reduce.AddQuery(query);
         }
     }
 }
Beispiel #9
0
        private void SetNoneWhere(Type t)
        {
            String pk = EntityTableMapper.GetPkColumn(t);

            if (String.IsNullOrEmpty(pk))
            {
                throw new ArgumentException("请使用带主键的或者添加删选条件");
            }
            String pkValue = ValuePairs[pk].ColumnName;
            ColumnRelevanceMapper column = new ColumnRelevanceMapper();

            column.ColumnName = pk;
            column.TableName  = t;
            ColumnRelevanceMapper value = new ColumnRelevanceMapper();

            value.ColumnName      = pk;
            value.SqlOperatorEnum = SqlOperatorEnum.Equal;
            Reduce.AddWhere(column);
            Reduce.AddWhere(value);
        }
Beispiel #10
0
        private String ConvertToQuery(List <ColumnRelevanceMapper> query)
        {
            StringBuilder sb = new StringBuilder(256);

            foreach (var column in query)
            {
                if (sb.Length != 0)
                {
                    sb.Append(",");
                }
                if (column.TableName == null)
                {
                    sb.Append($" {column.ColumnName} ");
                    continue;
                }
                String tablename  = EntityTableMapper.GetTableName(column.TableName);
                String columnname = EntityTableMapper.GetColoumName(column.TableName, column.ColumnName);
                sb.Append($"{tablename}.{columnname}");
            }
            return(sb.ToString());
        }
Beispiel #11
0
        private String ConvertToTable(List <TableRelevanceMapper> tables)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var table in tables)
            {
                String tableName = EntityTableMapper.GetTableName(table.TableName);
                if (table.TableOperatorEnum == TableOperatorEnum.None)
                {
                    sb.Insert(0, tableName);
                }
                else
                {
                    String tableOperator = table.TableOperatorEnum.GetOperator();
                    sb.Append($" {tableOperator} {tableName} on ");
                    foreach (var on in table.ColumnOperator)
                    {
                        sb.Append($" {ConditionConvert(on)} ");
                    }
                }
            }
            return(sb.ToString());
        }
Beispiel #12
0
 static Query()
 {
     EntityTableMapper.Add(typeof(T));
 }
Beispiel #13
0
 static Renewal()
 {
     EntityTableMapper.Add(typeof(T));
 }