コード例 #1
0
        /// <summary>
        /// Registers a new partial table that references a full TableObject registered earlier.
        /// </summary>
        /// <typeparam name="T">Class partially mapping the table.</typeparam>
        /// <param name="query">Enable/Disable SELECT statement generation</param>
        /// <param name="insert">Enable/Disable INSERT statement generation</param>
        /// <param name="update">Enable/Disable UPDATE statement generation</param>
        /// <returns></returns>
        public static PartialTableObject RegisterPartial <T>(bool query = true, bool insert = true, bool update = true) where T : class
        {
            var type     = typeof(T);
            var propList = type.GetProperties();
            var attrList = type.CustomAttributes;

            var mainTableAttr   = attrList.FirstOrDefault(x => x.AttributeType.Name == "PartialModelAttribute") ?? throw new Exception("Attribute not found");
            var referencedTable = mainTableAttr.ConstructorArguments[0].Value as Type ?? throw new Exception("Can't cast attribute to Type");

            var fetchMainTable = MainTableCache.TryGetValue(referencedTable, out TableObject mainTable);

            if (!fetchMainTable)
            {
                throw new Exception("Can't find main table in table cache.");
            }

            var partialTable = new PartialTableObject()
            {
                MainTable    = referencedTable,
                QueryString  = (query) ? QueryBuilder.BuildQuery(mainTable.TableName, propList) : null,
                InsertString = (insert) ? InsertBuilder.BuildQuery(mainTable.TableName, mainTable.PrimaryKey, propList) : null,
                UpdateString = (update) ? UpdateBuilder.BuildQuery(mainTable.TableName, mainTable.PrimaryKey, propList) : null
            };

            return((PartialTableCache.TryAdd(type, partialTable)) ? partialTable : throw new Exception("Error adding partialTableObject to dictionary"));
        }
コード例 #2
0
        /// <summary>
        /// Register a new table in the main table cache.
        /// </summary>
        /// <typeparam name="T">Class mapping the table</typeparam>
        /// <param name="appendS">Enable/Disable appending an 's' char at the end when generating the table name</param>
        /// <param name="query">Enable/Disable SELECT statement generation</param>
        /// <param name="insert">Enable/Disable INSERT statement generation</param>
        /// <param name="update">Enable/Disable UPDATE statement generation</param>
        /// <returns>Returns a the generated TableObject that can be stored to check the generated values but normally it should be ignored.</returns>
        public static TableObject RegisterTable <T>(bool appendS = true, bool query = true, bool insert = true, bool update = true) where T : class
        {
            var type     = typeof(T);
            var propList = type.GetProperties();
            var attrList = type.CustomAttributes;

            var tableNameAttr = attrList.FirstOrDefault(x => x.AttributeType.Name == "TableNameAttribute");
            var tableName     = (tableNameAttr != null) ? tableNameAttr.ConstructorArguments[0].Value as string : type.Name;

            if (appendS)
            {
                tableName += "s";
            }

            var pKeyAttr = propList.FirstOrDefault(prop => Attribute.IsDefined(prop, typeof(PKeyAttribute)));
            var pKey     = (pKeyAttr != null) ? pKeyAttr.Name : propList.Single(prop => prop.Name == "Id").Name;

            var sdKeyAttr = propList.FirstOrDefault(prop => Attribute.IsDefined(prop, typeof(SoftDeleteKeyAttribute)));
            var sdKey     = sdKeyAttr?.Name;


            var tableObject = new TableObject()
            {
                Type          = type,
                TableName     = tableName,
                PrimaryKey    = pKey,
                SoftDeleteKey = sdKey,
                QueryString   = (query) ? QueryBuilder.BuildQuery(tableName, propList) : null,
                InsertString  = (insert) ? InsertBuilder.BuildQuery(tableName, pKey, propList) : null,
                UpdateString  = (update) ? UpdateBuilder.BuildQuery(tableName, pKey, propList) : null
            };

            return((MainTableCache.TryAdd(type, tableObject)) ? tableObject : throw new Exception("Error adding tableObject to dictionary"));
        }