private Command _CreateUpdateByIdCommand(object data, Type type)
        {
            Anito.Data.Schema.TypeTable schemaTable = Provider.GetSchema(type);

            Command sqlCommand = new Command();

            StringBuilder commandTextBuilder = new StringBuilder();
            StringBuilder paramBuilder       = new StringBuilder();
            StringBuilder whereBuilder       = new StringBuilder();

            foreach (TypeColumn column in (from col in schemaTable where !col.ViewOnly select col))
            {
                if (sqlCommand.Parameters.ContainsKey(string.Format("{0}{1}", PARAM_IDENTIFIER, column.Name)))
                {
                    continue;
                }

                if (!column.IsIdentity)
                {
                    if (paramBuilder.Length > 0)
                    {
                        paramBuilder.Append(COMMA + SPACE);
                    }
                    paramBuilder.Append(column.Name + SPACE + EQUALS + SPACE);
                    paramBuilder.Append(PARAM_IDENTIFIER + column.Name);
                    if (!sqlCommand.Parameters.ContainsKey(string.Format("{0}{1}", PARAM_IDENTIFIER, column.Name)))
                    {
                        sqlCommand.AddParam(string.Format("{0}{1}", PARAM_IDENTIFIER, column.Name));
                    }
                }
                else if (column.IsIdentity)
                {
                    if (whereBuilder.Length > 0)
                    {
                        whereBuilder.Append(SPACE + AND + SPACE);
                    }
                    whereBuilder.Append(column.Name);
                    whereBuilder.Append(SPACE);
                    whereBuilder.Append(EQUALS);
                    whereBuilder.Append(SPACE);
                    whereBuilder.Append(PARAM_IDENTIFIER_ORIGINAL + column.Name);
                    if (!sqlCommand.Parameters.ContainsKey(string.Format("{0}{1}", PARAM_IDENTIFIER_ORIGINAL, column.Name)))
                    {
                        sqlCommand.AddParam(string.Format("{0}{1}", PARAM_IDENTIFIER_ORIGINAL, column.Name));
                    }
                }
            }
            commandTextBuilder.Append(string.Format("{0} {1} {2}",
                                                    UPDATE, schemaTable.UpdateSource, SET));
            commandTextBuilder.Append(SPACE);
            commandTextBuilder.Append(paramBuilder.ToString());
            commandTextBuilder.Append(SPACE);
            commandTextBuilder.Append(WHERE);
            commandTextBuilder.Append(SPACE);
            commandTextBuilder.Append(whereBuilder.ToString());
            commandTextBuilder.Append(SEMI_COLON);

            sqlCommand.SqlCommand.CommandText = commandTextBuilder.ToString();
            return(sqlCommand);
        }
        private string SelectColumnsStatement(Type type)
        {
            if (!ColumnSelectCache.ContainsKey(type))
            {
                Anito.Data.Schema.TypeTable schemaTable = Provider.GetSchema(type);
                StringBuilder columnSb = new StringBuilder();
                foreach (TypeColumn column in schemaTable)
                {
                    if (columnSb.Length > 0)
                    {
                        columnSb.Append(", ");
                    }

                    if (column.IsIdentity)
                    {
                        columnSb.Append(string.Format("{0}.{1} AS {2}", schemaTable.ViewSource, column.Name, column.Name));
                    }
                    else
                    {
                        if (!column.IsNullable)
                        {
                            switch (column.Type.Name.ToUpper())
                            {
                            case "INT16":
                            case "INT32":
                            case "INT64":
                            case "SINGLE":
                            case "DATETIME":
                            case "DECIMAL":
                            case "DOUBLE":
                            case "BOOLEAN":
                                columnSb.Append(string.Format("ISNULL({0}.{1},{2}) AS {3}", schemaTable.ViewSource, column.Name, 0, column.Name));
                                break;

                            case "STRING":
                            case "BYTE[]":
                            case "BYTE":
                                columnSb.Append(string.Format("{0}.{1} AS {2}", schemaTable.ViewSource, column.Name, column.Name));
                                break;

                            case "GUID":
                                columnSb.Append(string.Format("ISNULL({0}.{1},'{2}') AS {3}", schemaTable.ViewSource, column.Name, default(Guid), column.Name));
                                break;
                            }
                        }
                        else
                        {
                            columnSb.Append(string.Format("{0}.{1} AS {2}", schemaTable.ViewSource, column.Name, column.Name));
                        }
                    }
                }
                ColumnSelectCache.Add(type, columnSb.ToString());
            }
            return(ColumnSelectCache[type]);
        }
        public override ICommand CreateCountCommand <T>()
        {
            Type   typeT    = typeof(T);
            string uniqueId = "1801F51F-A4EC-4A78-8AE8-BCB30402E4B5";

            if (!CommandCache.ContainsKey(string.Format(CACHE_KEY, uniqueId, typeT.FullName)))
            {
                Anito.Data.Schema.TypeTable schemaTable = Provider.GetSchema(typeT);
                Command sqlCommand = (Command)CreateCountCommand(schemaTable.ViewSource);
                CommandCache.Add(string.Format(CACHE_KEY, uniqueId, typeT.FullName), sqlCommand);
            }
            return(CommandCache[string.Format(CACHE_KEY, uniqueId, typeT.FullName)]);
        }
        public override ICommand CreateDeleteByKeyCommand(object data, Type type)
        {
            string uniqueId = "92C92A43-D467-4931-8C12-F26AA936E7D7";

            if (!CommandCache.ContainsKey(string.Format(CACHE_KEY, uniqueId, type.FullName)))
            {
                Anito.Data.Schema.TypeTable schemaTable = Provider.GetSchema(type);

                Command sqlCommand = _CreateDeletebyKeyCommand(data, schemaTable);
                CommandCache.Add(string.Format(CACHE_KEY, uniqueId, type.FullName), sqlCommand);
            }
            return(CommandCache[string.Format(CACHE_KEY, uniqueId, type.FullName)]);
        }
        public override ICommand CreateDeleteByIdCommand(object data, Type type)
        {
            string uniqueId = "F58B3FAE-6E43-4B39-9850-8E2D4EC63419";

            if (!CommandCache.ContainsKey(string.Format(CACHE_KEY, uniqueId, type.FullName)))
            {
                Anito.Data.Schema.TypeTable schemaTable = Provider.GetSchema(type);

                Command sqlCommand = _CreateDeletebyIdCommand(data, schemaTable);
                CommandCache.Add(string.Format(CACHE_KEY, uniqueId, type.FullName), sqlCommand);
            }
            return(CommandCache[string.Format(CACHE_KEY, uniqueId, type.FullName)]);
        }
        public override ICommand CreateGetListCommand <T>(Expression expression)
        {
            Type typeT = typeof(T);

            Anito.Data.Schema.TypeTable schemaTable = Provider.GetSchema(typeT);

            Translator translator = new Translator(Provider);
            string     whereText  = translator.Translate(expression);

            Command sqlCommand = new Command();

            sqlCommand.SqlCommand.CommandText = string.Format(
                "{0} {1} {2} {3} {4} {5}",
                SELECT, SelectColumnsStatement(typeT), FROM, schemaTable.ViewSource, WHERE, whereText
                );
            return(sqlCommand);
        }
        public override ICommand CreateGetListCommand <T>(IFilterCriteria criteria)
        {
            Type   typeT    = typeof(T);
            string uniqueId = "232FDED0-6C97-40B4-930F-FC190124D181";

            if (!CommandCache.ContainsKey(string.Format(CACHE_KEY, uniqueId, typeT.FullName)))
            {
                Anito.Data.Schema.TypeTable schemaTable = Provider.GetSchema(typeT);

                Command sqlCommand = new Command();

                sqlCommand.SqlCommand.CommandText = string.Format(
                    "{0} {1} {2} {3}",
                    SELECT, SelectColumnsStatement(typeT), FROM, schemaTable.ViewSource
                    );
                CommandCache.Add(string.Format(CACHE_KEY, uniqueId, typeT.FullName), sqlCommand);
            }
            return(CommandCache[string.Format(CACHE_KEY, uniqueId, typeT.FullName)]);
        }
        public override ICommand CreateGetObjectByKeyCommand <T>()
        {
            Type   typeT    = typeof(T);
            string uniqueId = "4F9C6FBE-2971-46A8-A367-B28AB5F65895";

            if (!CommandCache.ContainsKey(string.Format(CACHE_KEY, uniqueId, typeT.FullName)))
            {
                Anito.Data.Schema.TypeTable schemaTable = Provider.GetSchema(typeT);

                Command sqlCommand = new Command();

                var keyList = from column in schemaTable where column.IsPrimaryKey select column;

                if (keyList.Count() < 1)
                {
                    throw new TypeNoKeyException(typeof(T));
                }

                StringBuilder paramBuilder = new StringBuilder();
                foreach (Anito.Data.Schema.TypeColumn column in keyList)
                {
                    if (paramBuilder.Length > 0)
                    {
                        paramBuilder.Append(string.Format("{0}{1}{2}", SPACE, AND, SPACE));
                    }
                    paramBuilder.Append(string.Format("{0} = {1}{2}", column.Name, PARAM_IDENTIFIER, column.Name));
                    sqlCommand.AddParam(string.Format("{0}{1}", PARAM_IDENTIFIER, column.Name));
                }

                sqlCommand.SqlCommand.CommandText =
                    string.Format("{0} {1} {2} {3} {4} {5}",
                                  SELECT, SelectColumnsStatement(typeT), FROM, schemaTable.ViewSource, WHERE, paramBuilder.ToString());
                CommandCache.Add(string.Format(CACHE_KEY, uniqueId, typeT.FullName), sqlCommand);
            }
            return(CommandCache[string.Format(CACHE_KEY, uniqueId, typeT.FullName)]);
        }
Beispiel #9
0
        protected override Expression ProcessMemberAccess(MemberExpression expression)
        {
            if (expression.Expression != null && expression.Expression.NodeType == ExpressionType.Parameter)
            {
                Anito.Data.Schema.TypeTable schemaTable = Provider.GetSchema(expression.Member.DeclaringType);

                CommandTextBuilder.Append(schemaTable.GetDbColumn(expression.Member.Name));

                return(expression);
            }
            else if (expression.Expression != null && expression.Expression.NodeType == ExpressionType.MemberAccess)
            {
                PropertyInfo propertyInfo = expression.Member as PropertyInfo;

                MemberExpression memberExpression = expression.Expression as MemberExpression;

                FieldInfo          fieldInfo   = memberExpression.Member as FieldInfo;
                ConstantExpression cExpression = memberExpression.Expression as ConstantExpression;
                object             objectValue = fieldInfo.GetValue(cExpression.Value);

                object value = propertyInfo.GetValue(objectValue, null);

                switch (Type.GetTypeCode(value.GetType()))
                {
                case TypeCode.String | TypeCode.DateTime:
                    CommandTextBuilder.Append(string.Format("'{0}'", value));
                    break;

                default:
                    CommandTextBuilder.Append(value);
                    break;
                }
                return(expression);
            }
            else if (expression.Expression != null && expression.Expression.NodeType == ExpressionType.Constant)
            {
                FieldInfo          fieldInfo   = expression.Member as FieldInfo;
                ConstantExpression cExpression = expression.Expression as ConstantExpression;
                object             value       = fieldInfo.GetValue(cExpression.Value);

                switch (Type.GetTypeCode(value.GetType()))
                {
                case TypeCode.String | TypeCode.DateTime:
                    CommandTextBuilder.Append(string.Format("'{0}'", value));
                    break;

                default:
                    CommandTextBuilder.Append(value);
                    break;
                }
                return(expression);
            }
            else if (expression.Expression != null && expression.Expression.NodeType == ExpressionType.Call)
            {
                PropertyInfo propertyInfo = expression.Member as PropertyInfo;

                MethodCallExpression callExpression = expression.Expression as MethodCallExpression;

                MethodInfo methodInfo   = callExpression.Method;
                object[]   methodParams = new object[callExpression.Arguments.Count];
                for (int i = 0; i < callExpression.Arguments.Count; i++)
                {
                    ConstantExpression consExpression = callExpression.Arguments[i] as ConstantExpression;
                    methodParams[i] = consExpression.Value;
                }

                MemberExpression memberExpression = callExpression.Object as MemberExpression;
                FieldInfo        fieldInfo        = memberExpression.Member as FieldInfo;

                ConstantExpression cExpression = memberExpression.Expression as ConstantExpression;

                object objectContainer = fieldInfo.GetValue(cExpression.Value);

                object objectValue = methodInfo.Invoke(objectContainer, methodParams);

                object value = propertyInfo.GetValue(objectValue, null);

                switch (Type.GetTypeCode(value.GetType()))
                {
                case TypeCode.String | TypeCode.DateTime:
                    CommandTextBuilder.Append(string.Format("'{0}'", value));
                    break;

                default:
                    CommandTextBuilder.Append(value);
                    break;
                }
                return(expression);
            }
            throw new NotSupportedException(string.Format("The member '{0}' is not supported", expression.Member.Name));
        }
        public override ICommand CreateInsertCommand(object data)
        {
            Type   typeT    = data.GetType();
            string uniqueId = "26ABBE10-3D2A-40be-ABAC-F92EF9040608";

            if (!CommandCache.ContainsKey(string.Format(CACHE_KEY, uniqueId, typeT.FullName)))
            {
                Anito.Data.Schema.TypeTable schemaTable = Provider.GetSchema(typeT);

                Command sqlCommand = new Command();

                StringBuilder commandTextBuilder = new StringBuilder();
                StringBuilder paramBuilder       = new StringBuilder();
                StringBuilder columnBuilder      = new StringBuilder();

                TypeColumn identityColumn = null;
                foreach (TypeColumn column in (from col in schemaTable where !col.ViewOnly select col))
                {
                    if (column.IsIdentity)
                    {
                        identityColumn = column;
                        continue;
                    }

                    if (sqlCommand.Parameters.ContainsKey(string.Format("{0}{1}", PARAM_IDENTIFIER, column.Name)))
                    {
                        continue;
                    }

                    if (columnBuilder.Length > 0 && paramBuilder.Length > 0)
                    {
                        columnBuilder.Append(COMMA);
                        paramBuilder.Append(COMMA);
                    }
                    columnBuilder.Append(column.Name);
                    paramBuilder.Append(PARAM_IDENTIFIER + column.Name);
                    if (!sqlCommand.Parameters.ContainsKey(string.Format("{0}{1}", PARAM_IDENTIFIER, column.Name)))
                    {
                        sqlCommand.AddParam(string.Format("{0}{1}", PARAM_IDENTIFIER, column.Name));
                    }
                }
                commandTextBuilder.Append(string.Format("{0} {1} {2}",
                                                        INSERT, INTO, schemaTable.UpdateSource));
                commandTextBuilder.Append(OPEN_PARENTHESES);
                commandTextBuilder.Append(columnBuilder.ToString());
                commandTextBuilder.Append(CLOSE_PARENTHESES);
                commandTextBuilder.Append(SPACE);
                commandTextBuilder.Append(VALUES + OPEN_PARENTHESES);
                commandTextBuilder.Append(paramBuilder.ToString());
                commandTextBuilder.Append(CLOSE_PARENTHESES + SEMI_COLON);

                if (identityColumn != null)
                {
                    commandTextBuilder.Append(string.Format("SELECT {0} FROM {1} WHERE {2} = (SELECT IDENT_CURRENT('{3}'));"
                                                            , schemaTable.ColumnList
                                                            , schemaTable.ViewSource
                                                            , identityColumn.Name
                                                            , schemaTable.ViewSource));
                }

                sqlCommand.SqlCommand.CommandText = commandTextBuilder.ToString();

                CommandCache.Add(string.Format(CACHE_KEY, uniqueId, typeT.FullName), sqlCommand);
            }
            return(CommandCache[string.Format(CACHE_KEY, uniqueId, typeT.FullName)]);
        }
        public override ICommand CreateGetListByPageCommand <T>(Expression expression)
        {
            Type typeT = typeof(T);

            Anito.Data.Schema.TypeTable schemaTable = Provider.GetSchema(typeT);

            Command sqlCommand = new Command();

            string tempTable = SHARP + "D89646AM" + schemaTable.ViewSource;

            StringBuilder columnBuilder     = new StringBuilder();
            StringBuilder keyBuilder        = new StringBuilder();
            StringBuilder keyCompareBuilder = new StringBuilder();

            foreach (Anito.Data.Schema.TypeColumn column in schemaTable)
            {
                if (columnBuilder.Length > 0)
                {
                    columnBuilder.Append(COMMA + SPACE);
                }
                columnBuilder.Append(schemaTable.ViewSource + DOT + column.Name);

                if (column.IsPrimaryKey)
                {
                    if (keyBuilder.Length > 0)
                    {
                        keyBuilder.Append(COMMA + SPACE);
                    }
                    keyBuilder.Append(column.Name);

                    if (keyCompareBuilder.Length > 0)
                    {
                        keyCompareBuilder.Append(SPACE + AND + SPACE);
                    }
                    keyCompareBuilder.Append(tempTable + DOT + column.Name);
                    keyCompareBuilder.Append(SPACE + EQUALS + SPACE);
                    keyCompareBuilder.Append(schemaTable.ViewSource + DOT + column.Name);
                }
            }

            Translator translator = new Translator(Provider);
            string     whereText  = translator.Translate(expression);

            sqlCommand.SqlCommand.CommandText = string.Format(GET_TABLE_BY_PAGE_WHERE,
                                                              tempTable,
                                                              tempTable,
                                                              keyBuilder.ToString(),
                                                              keyBuilder.ToString(),
                                                              tempTable,
                                                              schemaTable.ViewSource,
                                                              whereText,
                                                              SelectColumnsStatement(typeT),
                                                              schemaTable.ViewSource,
                                                              tempTable,
                                                              keyCompareBuilder.ToString(),
                                                              tempTable,
                                                              tempTable);

            sqlCommand.AddParam(string.Format("{0}{1}", PARAM_IDENTIFIER, PAGE));
            sqlCommand.AddParam(string.Format("{0}{1}", PARAM_IDENTIFIER, PAGE_SIZE));

            return(sqlCommand);
        }
        public override ICommand CreateGetListByPageCommand <T>()
        {
            Type   typeT    = typeof(T);
            string uniqueId = "D89646AE-F93B-4D59-9A0C-7AF5A6B79F5D";

            if (!CommandCache.ContainsKey(string.Format(CACHE_KEY, uniqueId, typeT.FullName)))
            {
                Anito.Data.Schema.TypeTable schemaTable = Provider.GetSchema(typeT);

                Command sqlCommand = new Command();

                string tempTable = SHARP + "D89646AE" + schemaTable.ViewSource;

                StringBuilder columnBuilder     = new StringBuilder();
                StringBuilder keyBuilder        = new StringBuilder();
                StringBuilder keyCompareBuilder = new StringBuilder();

                foreach (Anito.Data.Schema.TypeColumn column in schemaTable)
                {
                    if (columnBuilder.Length > 0)
                    {
                        columnBuilder.Append(COMMA + SPACE);
                    }
                    columnBuilder.Append(schemaTable.ViewSource + DOT + column.Name);

                    if (column.IsPrimaryKey)
                    {
                        if (keyBuilder.Length > 0)
                        {
                            keyBuilder.Append(COMMA + SPACE);
                        }
                        keyBuilder.Append(column.Name);

                        if (keyCompareBuilder.Length > 0)
                        {
                            keyCompareBuilder.Append(SPACE + AND + SPACE);
                        }
                        keyCompareBuilder.Append(tempTable + DOT + column.Name);
                        keyCompareBuilder.Append(SPACE + EQUALS + SPACE);
                        keyCompareBuilder.Append(schemaTable.ViewSource + DOT + column.Name);
                    }
                }

                sqlCommand.SqlCommand.CommandText = string.Format(GET_TABLE_BY_PAGE,
                                                                  tempTable,
                                                                  tempTable,
                                                                  keyBuilder.ToString(),
                                                                  keyBuilder.ToString(),
                                                                  tempTable,
                                                                  schemaTable.ViewSource,
                                                                  SelectColumnsStatement(typeT),
                                                                  schemaTable.ViewSource,
                                                                  tempTable,
                                                                  keyCompareBuilder.ToString(),
                                                                  tempTable,
                                                                  tempTable);

                sqlCommand.AddParam(string.Format("{0}{1}", PARAM_IDENTIFIER, PAGE));
                sqlCommand.AddParam(string.Format("{0}{1}", PARAM_IDENTIFIER, PAGE_SIZE));

                CommandCache.Add(string.Format(CACHE_KEY, uniqueId, typeT.FullName), sqlCommand);
            }
            return(CommandCache[string.Format(CACHE_KEY, uniqueId, typeT.FullName)]);
        }