Ejemplo n.º 1
0
 public MtPropertyInfo(int index, string name, MtTypeDefinition declaringType, MtTypeDefinition mtPropertyType)
 {
     Index          = index;
     Name           = name;
     DeclaringType  = declaringType;
     MtPropertyType = mtPropertyType;
 }
Ejemplo n.º 2
0
        private MtTypeDefinition ReadType(SqlDataReader reader, string typeIdField, Type clrType)
        {
            MtTypeDefinition mtType;
            var id          = (Guid)reader[typeIdField];
            var isComplex   = (bool)reader["IsComplex"];
            var isPrimitive = (bool)reader["IsPrimitive"];

            if (isComplex)
            {
                mtType = MtTypeDefinition.MakeComplexType(id, clrType);
            }
            else if (isPrimitive)
            {
                mtType = MtTypeDefinition.MakePrimitive(id, clrType);
            }
            else
            {
                mtType = MtTypeDefinition.MakeType(id, clrType);
            }

            return(mtType);
        }
Ejemplo n.º 3
0
 public AstToSqlConverter(MtTypeDefinition mtType, IMtObjectConverter converter, string tableName = null)
 {
     _tableName = tableName;
     _mtType    = mtType;
     _converter = converter;
 }
Ejemplo n.º 4
0
 public IEnumerable <MtObject> Query(ISqlConnectionProvider connectionProvider, string fr8AccountId, MtTypeDefinition type, AstNode @where)
 {
     for (int index = 0; index < _mtObjects.Count; index++)
     {
         var obj  = _mtObjects[index];
         var eval = new AstEvaluator(obj, _converter);
         if (eval.Evaluate(@where))
         {
             yield return(obj);
         }
     }
 }
Ejemplo n.º 5
0
        public int Delete(ISqlConnectionProvider connectionProvider, string fr8AccountId, MtTypeDefinition type, AstNode @where)
        {
            int changed = 0;

            for (int index = _mtObjects.Count - 1; index >= 0; index--)
            {
                var obj  = _mtObjects[index];
                var eval = new AstEvaluator(obj, _converter);
                if (eval.Evaluate(@where))
                {
                    _mtObjects.RemoveAt(index);
                }
            }

            return(changed);
        }
Ejemplo n.º 6
0
 public Guid?GetObjectId(ISqlConnectionProvider connectionProvider, string fr8AccountId, MtTypeDefinition type, AstNode where)
 {
     return(null);
 }
Ejemplo n.º 7
0
        public int QueryScalar(ISqlConnectionProvider connectionProvider, string fr8AccountId, MtTypeDefinition type, AstNode @where)
        {
            int count = 0;

            for (int index = 0; index < _mtObjects.Count; index++)
            {
                var obj  = _mtObjects[index];
                var eval = new AstEvaluator(obj, _converter);
                if (eval.Evaluate(@where))
                {
                    count++;
                }
            }

            return(count);
        }
Ejemplo n.º 8
0
 public bool TryLoadType(ISqlConnectionProvider connectionProvider, Type clrType, out MtTypeDefinition mtType)
 {
     lock (Types)
     {
         mtType = Types.FirstOrDefault(x => x.ClrType == clrType);
         return(mtType != null);
     }
 }
Ejemplo n.º 9
0
 public void PersistType(ISqlConnectionProvider connectionProvider, MtTypeDefinition mtType)
 {
     _newTypes.Add(mtType);
 }
Ejemplo n.º 10
0
        public Guid?GetObjectId(ISqlConnectionProvider connectionProvider, string fr8AccountId, MtTypeDefinition type, AstNode where)
        {
            if (where == null)
            {
                throw new ApplicationException("Where clause must be provided.");
            }

            using (var connection = OpenConnection(connectionProvider))
            {
                using (var command = new SqlCommand())
                {
                    command.Connection = connection;

                    var astConverter = new AstToSqlConverter(type, _converter);
                    astConverter.Convert(where);

                    var sqlCommand = @"SELECT [Id] FROM [dbo].[MtData] WHERE " + astConverter.SqlCommand;
                    command.CommandText = sqlCommand;

                    for (int index = 0; index < astConverter.Constants.Count; index++)
                    {
                        command.Parameters.AddWithValue("@param" + index, astConverter.Constants[index]);
                    }

                    using (var reader = command.ExecuteReader())
                    {
                        if (!reader.Read())
                        {
                            return(null);
                        }
                        return(reader.GetGuid(0));
                    }
                }
            }
        }
Ejemplo n.º 11
0
        public int Delete(ISqlConnectionProvider connectionProvider, string fr8AccountId, MtTypeDefinition type, AstNode @where)
        {
            if (where == null)
            {
                throw new ApplicationException("Where clause must be provided.");
            }

            using (var connection = OpenConnection(connectionProvider))
            {
                using (var command = new SqlCommand())
                {
                    command.Connection = connection;

                    var astConverter = new AstToSqlConverter(type, _converter);
                    astConverter.Convert(where);

                    var sqlCommand = @"delete FROM [dbo].[MtData] WHERE fr8AccountId = @accountId and Type = @type and " + astConverter.SqlCommand;

                    command.Parameters.AddWithValue("@type", type.Id);
                    command.Parameters.AddWithValue("@accountId", fr8AccountId);

                    command.CommandText = sqlCommand;

                    for (int index = 0; index < astConverter.Constants.Count; index++)
                    {
                        command.Parameters.AddWithValue("@param" + index, astConverter.Constants[index]);
                    }

                    return(command.ExecuteNonQuery());
                }
            }
        }
Ejemplo n.º 12
0
        public IEnumerable <MtObject> Query(ISqlConnectionProvider connectionProvider, string fr8AccountId, MtTypeDefinition type, AstNode @where)
        {
            var fields = new List <string>
            {
                "Type",
                "fr8AccountId",
                "IsDeleted"
            };

            var parameters = new List <string>
            {
                "@type",
                "@account",
                "@isDeleted"
            };

            foreach (var mtPropertyInfo in type.Properties)
            {
                parameters.Add("@val" + (mtPropertyInfo.Index + 1));
                fields.Add("Value" + (mtPropertyInfo.Index + 1));
            }

            var tableDefintionOuter = string.Join(", ", fields.Select(x => "tmp." + x));
            var tableDefintionInner = string.Join(", ", fields.Select(x => "[md]." + x));

            using (var connection = OpenConnection(connectionProvider))
            {
                using (var command = new SqlCommand())
                {
                    command.Connection = connection;
                    command.Parameters.AddWithValue("@type", type.Id);
                    command.Parameters.AddWithValue("@account", fr8AccountId);
                    command.Parameters.AddWithValue("@isDeleted", false);

                    string whereTemplate = string.Empty;

                    if (where != null)
                    {
                        var astConverter = new AstToSqlConverter(type, _converter, "[md]");

                        astConverter.Convert(@where);

                        whereTemplate = " and " + astConverter.SqlCommand;

                        for (int index = 0; index < astConverter.Constants.Count; index++)
                        {
                            command.Parameters.AddWithValue("@param" + index, astConverter.Constants[index]);
                        }
                    }

                    command.CommandText    = string.Format(MtSelectQueryTemplate, tableDefintionOuter, tableDefintionInner, whereTemplate);
                    command.CommandTimeout = 120;

                    var result = new List <MtObject>();

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var obj = new MtObject(type);

                            foreach (var mtPropertyInfo in type.Properties)
                            {
                                var val = reader["Value" + (mtPropertyInfo.Index + 1)];

                                if (val != DBNull.Value)
                                {
                                    obj.Values[mtPropertyInfo.Index] = (string)val;
                                }
                            }

                            result.Add(obj);
                        }
                    }

                    return(result);
                }
            }
        }
Ejemplo n.º 13
0
        public int QueryScalar(ISqlConnectionProvider connectionProvider, string fr8AccountId, MtTypeDefinition type, AstNode @where)
        {
            using (var connection = OpenConnection(connectionProvider))
            {
                using (var command = new SqlCommand())
                {
                    command.Connection = connection;
                    command.Parameters.AddWithValue("@type", type.Id);
                    command.Parameters.AddWithValue("@account", fr8AccountId);
                    command.Parameters.AddWithValue("@isDeleted", false);

                    string whereTemplate = string.Empty;

                    if (where != null)
                    {
                        var astConverter = new AstToSqlConverter(type, _converter, "[md]");

                        astConverter.Convert(@where);

                        whereTemplate = " and " + astConverter.SqlCommand;

                        for (int index = 0; index < astConverter.Constants.Count; index++)
                        {
                            command.Parameters.AddWithValue("@param" + index, astConverter.Constants[index]);
                        }
                    }

                    command.CommandText = string.Format(MtSelectScalarTemplate, whereTemplate);

                    return((int)Convert.ChangeType(command.ExecuteScalar(), typeof(int)));
                }
            }
        }
Ejemplo n.º 14
0
        // Currently we don't need a support for object properties that have type of 'object'.
        // All our properties are of primitive types (types that has no own properties) and  we don't want to introduce unnecessary complications related to types graphs
        // So we make assumption that we can always safely load the type with all possible references by loading only topmost level of type hierarchy
        // This is as easy as making one sql join.
        public bool TryLoadType(ISqlConnectionProvider connectionProvider, Type clrType, out MtTypeDefinition mtType)
        {
            // we want handle Manifests in the special way. Instead of storing manifest name as CLR type name we'll store it as manifest id.
            // this will make out system more robust because it will not depend on manifest names.
            CrateManifestType manifestType;

            mtType = null;

            using (var connection = OpenConnection(connectionProvider))
            {
                SqlCommand loadTypeCommand;

                // search type by manifest Id
                if (ManifestDiscovery.Default.TryGetManifestType(clrType, out manifestType))
                {
                    loadTypeCommand = new SqlCommand(@"select top 1 * from MtTypes where ManifestId = @manifestId")
                    {
                        Connection = connection
                    };

                    loadTypeCommand.Parameters.AddWithValue("@manifestId", manifestType.Id);
                }
                else
                {
                    loadTypeCommand = new SqlCommand(@"select top 1 * from MtTypes where ClrName = @clrType")
                    {
                        Connection = connection
                    };

                    loadTypeCommand.Parameters.AddWithValue("@clrType", clrType.AssemblyQualifiedName);
                }

                using (var reader = loadTypeCommand.ExecuteReader())
                {
                    // read type
                    while (reader.Read())
                    {
                        mtType = ReadType(reader, "Id", clrType);
                        break;
                    }

                    // we didn't find type
                    if (mtType == null)
                    {
                        return(false);
                    }
                }

                if (!mtType.IsComplexType && !mtType.IsPrimitive)
                {
                    using (var loadPropCommand = new SqlCommand(@"select  MtProperties.*, ptype.IsPrimitive, ptype.IsComplex, pType.ClrName, pType.ManifestId from MtProperties 
                                                                   inner join MtTypes as ptype on ptype.Id = MtProperties.Type      
                                                                   where MtProperties.DeclaringType = @declaringType order by Offset"))
                    {
                        loadPropCommand.Connection = connection;

                        loadPropCommand.Parameters.AddWithValue("@declaringType", mtType.Id);

                        using (var reader = loadPropCommand.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                var propName     = (string)reader["Name"];
                                var fullTypeName = reader["ClrName"];

                                if (fullTypeName == DBNull.Value)
                                {
                                    continue;
                                }

                                var type = Type.GetType((string)fullTypeName);

                                if (type == null)
                                {
                                    continue;
                                }

                                var propType = ReadType(reader, "Type", type);
                                var index    = (int)reader["Offset"];
                                mtType.Properties.Add(new MtPropertyInfo(index, propName, mtType, propType));
                            }
                        }

                        for (int i = 0; i < mtType.Properties.Count; i++)
                        {
                            if (mtType.Properties[i].Index != i)
                            {
                                throw new Exception(string.Format("MtProperties table is corrupted for typeId '{0}' [ClrType: {1}]", mtType.Id, clrType.FullName));
                            }
                        }
                    }
                }
            }

            return(true);
        }