Пример #1
0
        public static CmdParameter CreateDeleteSql(ISessionImplementor session, IEntityPersister entityPersister, IEntity obj)
        {
            var classMetadata = entityPersister.ClassMetadata;

            if (string.IsNullOrEmpty(obj.Conditaion))
            {
                var v = classMetadata.ColumnInfos.Find(t => t.IsPrimaryKey).PropertyInfo.FastGetValue(obj);
                return !IsNull(v) ? CreateDeleteSql(session, entityPersister, (int)v) : null;
            }
            List<DbParameter> parameters;
            string rConditaion = GetConditaion(session, entityPersister, obj, out parameters);
            var where = GetWhereCaluse(rConditaion);
            var result = new CmdParameter();
            if (where.Length > 0)
            {
                result.Parameters = parameters.ToArray();
                result.CommandText = string.Format("DELETE FROM {0} WHERE {1}{2}", classMetadata.TableName, where, entityPersister.Driver.MultipleQueriesSeparator);
            }
            return result;
        }
Пример #2
0
        private static List<string> CreateNoBatchInsertSql(IEntityPersister entityPersister, int listCount, int batchSize, int batchTime, IEntity[] list)
        {
            var result = new List<string>();
            var columns = new StringBuilder();
            StringBuilder value;
            StringBuilder values;
            StringBuilder batch;
            var classMetadata = entityPersister.ClassMetadata;
            var columnInfos = classMetadata.ColumnInfos.FindAll(t => t.IsColumn);
            object v;
            for (var i = 0; i < batchTime; i++)
            {
                batch = new StringBuilder();
                for (var j = 0; j < batchSize; j++)
                {
                    values = new StringBuilder();
                    var s = i * batchSize + j;
                    value = new StringBuilder();
                    foreach (var columnInfo in columnInfos)
                    {
                        if (s > listCount) break;
                        v = columnInfo.PropertyInfo.FastGetValue(list[s]);
                        if (columnInfo.IsIncrement) continue;
                        if (s == 0) columns.AppendFormat("{0},", columnInfo.PropertyName);
                        value.AppendFormat("{0},", GetDbString(columnInfo, v));
                    }
                    if (s == 0) columns = columns.Remove(columns.Length - 1, 1);

                    if (value.Length > 0) values.AppendFormat("({0}),", value.Remove(value.Length - 1, 1));
                    batch.AppendFormat("INSERT INTO {0} ({1}) VALUES {2}{3}", classMetadata.TableName, columns, values.Remove(values.Length - 1, 1), entityPersister.Driver.MultipleQueriesSeparator);
                }
                result.Add(batch.ToString());
            }
            return result;
        }
Пример #3
0
        private static string GetConditaion(ISessionImplementor session, IEntityPersister entityPersister, IEntity obj, out List<DbParameter> parameters)
        {
            var rConditaion = obj.Conditaion;
            var classMetadata = entityPersister.ClassMetadata;
            parameters = new List<DbParameter>();
            foreach (ColumnInfo columnInfo in classMetadata.ColumnInfos)
            {
                var value = columnInfo.PropertyInfo.FastGetValue(obj);
                if (IsNull(value)) continue;
                if (IsNullValue(value) && columnInfo.IsNullable) continue;

                var index = Contains(obj.Conditaion, columnInfo.PropertyName);
                if (index == 0)
                {
                    rConditaion = rConditaion.Replace(string.Format(LikeString[0], columnInfo.PropertyName), entityPersister.Driver.FormatNameForSql(columnInfo.Name));
                    entityPersister.SetDbParamter(session, parameters, columnInfo.Name, value);
                    //entityPersister.SetDbParamter(session, parameters, classMetadata.PrimaryKey, value);
                }
                else if (index > 0)
                {
                    rConditaion = rConditaion.Replace(string.Format(LikeString[index], columnInfo.PropertyName), string.Format(LikeValueString[index - 1], Escape(value.ToString(), "")));
                }
            }
            return rConditaion;
        }
Пример #4
0
        public static CmdParameter CreateUpdateSql(ISessionImplementor session, IEntityPersister entityPersister, IEntity obj)
        {
            var classMetadata = entityPersister.ClassMetadata;
            var rConditaion = obj.Conditaion;
            var columns = new StringBuilder();

            List<DbParameter> parameters = new List<DbParameter>();
            foreach (ColumnInfo columnInfo in classMetadata.ColumnInfos)
            {
                var value = columnInfo.PropertyInfo.FastGetValue(obj);
                if (IsNull(value)) continue;
                if (columnInfo.IsPrimaryKey && string.IsNullOrEmpty(obj.Conditaion))
                {
                    rConditaion = string.Format("{0}={1}", columnInfo.Name, entityPersister.Driver.FormatNameForSql(columnInfo.Name));
                    entityPersister.SetDbParamter(session, parameters, columnInfo.Name, value);
                }
                else
                {
                    var index = Contains(obj.Conditaion, columnInfo.Name);
                    if (index == 0)
                    {
                        rConditaion = rConditaion.Replace(string.Format(LikeString[0], columnInfo.Name), entityPersister.Driver.FormatNameForSql(columnInfo.Name));
                        entityPersister.SetDbParamter(session, parameters, classMetadata.PKColumnName, value);
                    }
                    else if (index > 0)
                    {
                        rConditaion = rConditaion.Replace(string.Format(LikeString[index], columnInfo.Name), string.Format(LikeValueString[index - 1], Escape(value.ToString(), "")));
                    }
                    else if (index == -1)
                    {
                        columns.AppendFormat("{0}={1},", columnInfo.Name, entityPersister.Driver.FormatNameForSql(columnInfo.Name));

                        if (columnInfo.IsNullable && IsNullValue(value))
                        {
                            entityPersister.SetDbParamter(session, parameters, columnInfo.Name, DBNull.Value);
                        }
                        else
                        {
                            entityPersister.SetDbParamter(session, parameters, columnInfo.Name, value);
                        }
                    }

                }

            }
            var where = GetWhereCaluse(rConditaion);
            var result = new CmdParameter();
            if (where.Length > 0 && columns.Length > 0)
            {
                result.Parameters = parameters.ToArray();
                result.CommandText = string.Format("UPDATE {0} SET {1} WHERE {2}{3}", classMetadata.TableName, columns.Remove(columns.Length - 1, 1), where, entityPersister.Driver.MultipleQueriesSeparator);
            }

            return result;
        }
Пример #5
0
        public static CmdParameter CreateSelectSql(ISessionImplementor session, IEntityPersister entityPersister, IEntity obj)
        {
            var classMetadata = entityPersister.ClassMetadata;
            var result = new CmdParameter();
            if (string.IsNullOrEmpty(obj.Conditaion))
            {
                var columnInfo = classMetadata.ColumnInfos.Find(t => t.IsPrimaryKey);
                if (columnInfo != null)
                {
                    var v = columnInfo.PropertyInfo.FastGetValue(obj);
                    if (!IsNull(v)) result = CreateSelectSql(session, entityPersister, v);
                }

            }

            List<DbParameter> parameters;
            string rConditaion = GetConditaion(session, entityPersister, obj, out parameters);

            var sql = new StringBuilder("SELECT ");
            sql.Append(string.IsNullOrEmpty(obj.ResultColumns) ? "*" : obj.ResultColumns);
            //sql.Append("*");        //delete
            sql.AppendFormat(" FROM {0} ", classMetadata.TableName);
            var where = GetWhereCaluse(rConditaion);
            if (where.Length > 0)
            {
                sql.AppendFormat(" WHERE {0} ", where);
                result.Parameters = parameters.ToArray();
            }
            sql.Append(obj.OrderBy ?? "");
            sql.Append(entityPersister.Driver.MultipleQueriesSeparator);
            result.CommandText = sql.ToString();
            return result;
        }
Пример #6
0
 public static List<string> CreateInsertSql(IEntityPersister entityPersister, IEntity[] list)
 {
     var result = new List<string>();
     var listCount = list.Length;
     if (listCount > 0)
     {
         var batchSize = entityPersister.Factory.Settings.BatchSize;
         if (batchSize > listCount) batchSize = listCount;
         var batchTime = (listCount + batchSize - 1) / batchSize;
         result = entityPersister.Driver.SupportsBacthInsert ? CreateBatchInsertSql(entityPersister, listCount, batchSize, batchTime, list) : CreateNoBatchInsertSql(entityPersister, listCount, batchSize, batchTime, list);
     }
     return result;
 }
Пример #7
0
        public static CmdParameter CreateInsertSql(ISessionImplementor session, IEntityPersister entityPersister, IEntity obj)
        {
            var result = new CmdParameter();

            var classMetadata = entityPersister.ClassMetadata;
            var driver = entityPersister.Driver;

            var sql = new StringBuilder("INSERT INTO");
            sql.AppendFormat(" {0} (", classMetadata.TableName);
            var values = new StringBuilder();
            var tParameters = new List<DbParameter>();
            var columnInfos = classMetadata.ColumnInfos.FindAll(t => t.IsColumn);

            foreach (var columnInfo in columnInfos)
            {
                var v = columnInfo.PropertyInfo.FastGetValue(obj);
                if (IsNull(v)) continue;
                if (columnInfo.IsNullable && IsNullValue(v)) continue;
                sql.AppendFormat("{0},", columnInfo.Name);
                values.AppendFormat("{0},", driver.FormatNameForSql(columnInfo.Name));
                entityPersister.SetDbParamter(session, tParameters, columnInfo.Name, v);
            }
            if (values.Length > 0)
            {
                sql.Replace(',', ')', sql.Length - 1, 1);
                sql.AppendFormat(" VALUES ({0}){1}", values.Remove(values.Length - 1, 1), driver.MultipleQueriesSeparator);

                result.CommandText = sql.ToString();
                result.Parameters = tParameters.ToArray();
            }

            return result;
        }
Пример #8
0
 public static CmdParameter CreateInsertReturnIdentitySql(ISessionImplementor session, IEntityPersister entityPersister, IEntity obj)
 {
     var result = CreateInsertSql(session, entityPersister, obj);
     result.CommandText = string.Format("{0}{1}", result.CommandText, entityPersister.Driver.GetIdentityString);
     return result;
 }