Пример #1
0
        private StoredProcGenerator GeneartStoredProc(string tableName, string itemName, bool isDBReadOnly)
        {
            StoredProcGenerator generator = new StoredProcGenerator(tableName, itemName, isDBReadOnly);

            generator.CreatorIDFieldName    = AppSettings.Default.CreatorIDField;
            generator.DateCreatedFieldName  = AppSettings.Default.CreatedDateField;
            generator.ModifierIDFieldName   = AppSettings.Default.ModifierIDField;
            generator.DateModifiedFieldName = AppSettings.Default.ModifiedDateField;

            foreach (TableSchema.FieldsRow row in _tableSchema.Fields.Rows)
            {
                if (row.TableName == tableName)
                {
                    LoadField(generator.AddField(), row);
                }
            }

            return(generator);
        }
Пример #2
0
        public string Generate(string templateText)
        {
            StoredProcGenerator procs = new StoredProcGenerator(_tableName, _itemName, _isReadOnly);

            procs.CreatorIDFieldName    = CreatorIDFieldName;
            procs.DateCreatedFieldName  = DateCreatedFieldName;
            procs.ModifierIDFieldName   = ModifierIDFieldName;
            procs.DateModifiedFieldName = DateModifiedFieldName;

            procs._fields = _fields;

            string output = templateText;

            output = output.Replace("<$itemName$>", LowerCaseFirstLetter(_itemName));
            output = output.Replace("<$ItemName$>", _itemName);
            output = output.Replace("<$collectionName$>", LowerCaseFirstLetter(_collectionName));
            output = output.Replace("<$CollectionName$>", _collectionName);
            output = output.Replace("<$primaryKey$>", LowerCaseFirstLetter(_primaryKey));
            output = output.Replace("<$PrimaryKey$>", _primaryKey);
            output = output.Replace("<$BaseItemClass$>", _baseItemClass);
            output = output.Replace("<$BaseCollectionClass$>", _baseCollectionClass);
            output = output.Replace("<$TableName$>", _tableName);
            output = output.Replace("<$QueryInsert$>", procs.GenerateInsertQuery());
            output = output.Replace("<$QueryUpdate$>", procs.GenerateUpdateQuery());
            output = output.Replace("<$QueryDelete$>", procs.GenerateDeleteQuery());
            output = output.Replace("<$QuerySelect$>", procs.GenerateSelectQuery());

            int begin;
            int end;

            begin = output.IndexOf("<$BeginFields=[");
            end   = output.IndexOf("<$EndFields$>");
            while (begin > -1 && end > -1)
            {
                int      filterEnd = output.IndexOf(']', begin);
                string   filter    = output.Substring(begin + 15, filterEnd - begin - 15);
                string[] filters   = filter.Split(',');
                List <FieldFilterType> filterTypes = new List <FieldFilterType>();
                foreach (string s in filters)
                {
                    filterTypes.Add(GetFilterType(s.Trim()));
                }

                int    contentStart = output.IndexOf("$>", begin);
                string content      = output.Substring(contentStart + 2, end - contentStart - 2);
                output = output.Remove(begin, end - begin + 13);
                _fields.Reverse();

                foreach (Field field in _fields)
                {
                    string dataType = ConfirmNullableType(field.CustomType, field.AllowDBNull);
                    bool   fieldOk  = true;

                    foreach (FieldFilterType filterType in filterTypes)
                    {
                        if (!fieldOk)
                        {
                            break;
                        }
                        switch (filterType)
                        {
                        case FieldFilterType.All: fieldOk = true; break;

                        case FieldFilterType.DateTime: fieldOk = field.SystemType == "System.DateTime"; break;

                        case FieldFilterType.ReadOnly: fieldOk = field.IsReadOnly; break;

                        case FieldFilterType.AllowDBNull: fieldOk = field.AllowDBNull; break;

                        case FieldFilterType.Update: fieldOk = IsUpdateField(field.FieldName); break;

                        case FieldFilterType.Insert: fieldOk = IsInsertField(field); break;

                        case FieldFilterType.Nullable: fieldOk = field.AllowDBNull; break;

                        case FieldFilterType.NotDateTime: fieldOk = field.SystemType != "System.DateTime"; break;

                        case FieldFilterType.NotReadOnly: fieldOk = !field.IsReadOnly; break;

                        case FieldFilterType.NotAllowDBNull: fieldOk = !field.AllowDBNull; break;

                        case FieldFilterType.NotUpdate: fieldOk = !IsUpdateField(field.FieldName); break;

                        case FieldFilterType.NotInsert: fieldOk = !IsInsertField(field); break;

                        case FieldFilterType.NotNullable: fieldOk = !field.AllowDBNull; break;

                        default: fieldOk = false; break;
                        }
                    }

                    if (fieldOk)
                    {
                        string selection = content;
                        selection = selection.Replace("<$DataType$>", dataType);
                        selection = selection.Replace("<$DBType$>", field.SqlDbType);
                        selection = selection.Replace("<$PropertyName$>", field.PropertyName);
                        selection = selection.Replace("<$propertyName$>", LowerCaseFirstLetter(field.PropertyName));
                        selection = selection.Replace("<$FieldName$>", field.FieldName);
                        selection = selection.Replace("<$fieldName$>", LowerCaseFirstLetter(field.FieldName));
                        selection = selection.Replace("<$FieldSize$>", field.Size.ToString());
                        selection = selection.Replace("<$FieldPrecision$>", field.Precision.ToString());
                        selection = selection.Replace("<$FieldScale$>", field.Precision.ToString());
                        output    = output.Insert(begin, selection);
                    }
                }

                begin = output.IndexOf("<$BeginFields=[");
                end   = output.IndexOf("<$EndFields$>");
            }

            while (true)
            {
                begin = output.IndexOf("<$BeginCondition=[");
                if (begin < 0)
                {
                    break;
                }
                int    close     = output.IndexOf("]", begin);
                string condition = output.Substring(begin + 18, close - begin - 18);
                end = output.IndexOf("<$EndCondition$>");

                if (IsConditionOk(condition))
                {
                    //delete begin and end flags
                    output = output.Remove(end, 16);
                    output = output.Remove(begin, close - begin + 3);
                }
                else
                {
                    //delete whole block
                    output = output.Remove(begin, end - begin + 16);
                }
            }

            return(output);
        }