Esempio n. 1
0
        public virtual int Insert(IDictionary <string, object> o)
        {
            ISqlMapper mapper = new InsertMapper(Factory.CreateConverter(_helper.DBType));
            var        model  = mapper.ObjectToSql(Common.GetTableName(_key, _config.Owner, o.GetType(), _config, o), o, null, _config);
            int        result = 0;

            result = _helper.ExecNoneQueryWithSQL(model.SQL, model.Parameters.ToArray());
            return(result);
        }
Esempio n. 2
0
        public SqlModel ObjectToSql(string tableName, IDictionary <string, object> o, IList <WhereClause> where, TableConfig config = null)
        {
            InsertMapper     insertMapper = new InsertMapper(Converter);
            UpdateByIDMapper updateMapper = new UpdateByIDMapper(Converter);
            StringBuilder    judgement    = new StringBuilder();
            SqlModel         model        = new SqlModel();


            judgement.AppendFormat(" EXISTS(SELECT 1 FROM {0} WHERE ", tableName);
            foreach (var key in o.Keys)
            {
                var column = config?.ColumnMapping?.Find(t => t.SourceColumn.ToLower().Equals(key.ToLower()));
                if (column != null && column.Ingore)
                {
                    continue;
                }
                if (column == null || !column.Primarykey)
                {
                    continue;
                }
                if (column != null && column.GenerateID && o[key] == null)
                {
                    IIDGenerator generator = IDGeneratorFactory.Create(GeneratorType.SnowFlak);
                    o.SetValue(key, generator.Generate());
                }
                string columnName = Common.GetColumnName(key, column);
                if (string.IsNullOrEmpty(columnName))
                {
                    continue;
                }
                judgement.AppendFormat("{0}={1} AND ", Converter.FormatColumn(columnName), Converter.FormatParameter(columnName));
            }

            var insertModel = insertMapper.ObjectToSql(tableName, o, where, config);
            var updateModel = updateMapper.ObjectToSql(tableName, o, where, config);

            judgement.Remove(judgement.Length - 4, 4);
            judgement.Append(")");
            string sql = Converter.BuildIfElseStatement(judgement.ToString(), updateModel.SQL, insertModel.SQL);

            model.SQL        = sql;
            model.Parameters = updateModel.Parameters;
            return(model);
        }
Esempio n. 3
0
        private int Execute(string tableName, IDictionary <string, object> o, SqlModel model)
        {
            int       result = 0;
            DataTable table  = null;

            try
            {
                table = _helper.GetTableWithSQL(model.SQL, model.Parameters.ToArray());
            }
            catch (Exception ex)
            {
                _logger.Write(model.SQL);
                throw ex;
            }
            ISqlMapper mapper = null;

            if (table.Rows.Count > 0)
            {
                mapper = new UpdateByIDMapper(Factory.CreateConverter(_helper.DBType));
            }
            else
            {
                mapper = new InsertMapper(Factory.CreateConverter(_helper.DBType));
            }

            var saveModel = mapper.ObjectToSql(tableName, o, null, _config);

            try
            {
                result = _helper.ExecNoneQueryWithSQL(saveModel.SQL, saveModel.Parameters.ToArray());
            }
            catch (Exception ex)
            {
                _logger.Write(saveModel.SQL);
                throw ex;
            }
            return(result);
        }