/// <inheritdoc />
        /// <summary>
        /// Resolves the column name for the property.
        /// Looks for the [Column] attribute. Otherwise it's just the name of the property.
        /// </summary>
        public virtual Tuple <string, Type> ResolveDataColumn(NonaProperty propertyInfo)
        {
            var columnAttr = propertyInfo.PropertyInfo.GetCustomAttribute <ColumnAttribute>();

            return(columnAttr != null ? new Tuple <string, Type>(columnAttr.Name, columnAttr.GetType()) :
                   new Tuple <string, Type>(propertyInfo.PropertyInfo.Name, propertyInfo.PropertyInfo.GetType()));
        }
        /// <summary>
        /// Resolves the column name for the property.
        /// Looks for the [Column] attribute. Otherwise it's just the name of the property.
        /// </summary>
        public virtual string Resolve(NonaProperty propertyInfo)
        {
            var columnAttr = propertyInfo.PropertyInfo.GetCustomAttribute <ColumnAttribute>();

            if (columnAttr != null)
            {
                return(columnAttr.Name);
            }

            return(propertyInfo.Name);
        }
Esempio n. 3
0
            /// <summary>
            /// Gets the the data column for the specified type,
            /// using the configured <see cref="IDataColumnResolver"/>.
            /// </summary>
            /// <param name="propertyInfo">The <see cref="NonaProperty"/> to get the data column for.</param>
            /// <returns>The data column for <paramref name="propertyInfo"/>.</returns>
            public static Tuple <string, Type> DataColumn(NonaProperty propertyInfo)
            {
                var key = propertyInfo.PropertyKey;

                if (!DataColumnCache.TryGetValue(key, out var column))
                {
                    column = _dataColumnResolver.ResolveDataColumn(propertyInfo);
                    DataColumnCache.TryAdd(key, column);
                }

                LogReceived?.Invoke($"Resolved column name '{column.Item1}' for '{propertyInfo.PropertyInfo}'");
                return(column);
            }
        /// <inheritdoc/>
        public string Resolve(NonaProperty NonaProperty)
        {
            if (!FluentMapper.EntityMaps.TryGetValue(NonaProperty.Type, out var entityMap))
            {
                return(DefaultResolvers.ColumnNameResolver.Resolve(NonaProperty));
            }

            if (!(entityMap is INonaEntityMap))
            {
                return(DefaultResolvers.ColumnNameResolver.Resolve(NonaProperty));
            }

            var propertyMaps = entityMap.PropertyMaps.Where(m => m.PropertyInfo.Name == NonaProperty.Name).ToList();

            return(propertyMaps.Count == 1 ? propertyMaps[0].ColumnName : DefaultResolvers.ColumnNameResolver.Resolve(NonaProperty));
        }
Esempio n. 5
0
            /// <summary>
            /// Gets the name of the column in the database for the specified type,
            /// using the configured <see cref="IColumnNameResolver"/>.
            /// </summary>
            /// <param name="propertyInfo">The <see cref="NonaProperty"/> to get the column name for.</param>
            /// <returns>The column name in the database for <paramref name="propertyInfo"/>.</returns>
            public static string Column(NonaProperty propertyInfo)
            {
                var key = propertyInfo.PropertyKey;

                if (!ColumnNameCache.TryGetValue(key, out var columnName))
                {
                    columnName = _columnNameResolver.Resolve(propertyInfo);
                    if (EscapeCharacterStart != char.MinValue || EscapeCharacterEnd != char.MinValue)
                    {
                        columnName = EscapeCharacterStart + columnName + EscapeCharacterEnd;
                    }
                    ColumnNameCache.TryAdd(key, columnName);
                }

                LogReceived?.Invoke($"Resolved column name '{columnName}' for '{propertyInfo.PropertyInfo}'");
                return(columnName);
            }
Esempio n. 6
0
        /// <inheritdoc/>
        public Tuple <string, Type> ResolveDataColumn(NonaProperty propertyInfo)
        {
            if (propertyInfo.Type == null)
            {
                return(NonaMapper.Resolvers.Default.DataColumnResolver.ResolveDataColumn(propertyInfo));
            }

            if (!FluentMapper.EntityMaps.TryGetValue(propertyInfo.Type, out var entityMap))
            {
                return(NonaMapper.Resolvers.Default.DataColumnResolver.ResolveDataColumn(propertyInfo));
            }

            if (!(entityMap is INonaEntityMap))
            {
                return(NonaMapper.Resolvers.Default.DataColumnResolver.ResolveDataColumn(propertyInfo));
            }

            var propertyMaps = entityMap.PropertyMaps.Where(m => m.PropertyInfo.Name == propertyInfo.PropertyInfo.Name).ToList();

            return(propertyMaps.Count == 1 ? new Tuple <string, Type>(propertyMaps[0].ColumnName, propertyMaps[0].PropertyInfo.PropertyType) :
                   NonaMapper.Resolvers.Default.DataColumnResolver.ResolveDataColumn(propertyInfo));
        }
Esempio n. 7
0
 /// <inheritdoc/>
 public string BuildInsert(string tableName, string[] columnNames, string[] paramNames, NonaProperty keyProperty)
 {
     return($"insert into {tableName} ({string.Join(", ", columnNames)}) values ({string.Join(", ", paramNames)}); select last_insert_rowid() id");
 }
 /// <inheritdoc/>
 public string BuildInsert(string tableName, string[] columnNames, string[] paramNames, NonaProperty keyProperty)
 {
     return($"set nocount on insert into {tableName} ({string.Join(", ", columnNames)}) values ({string.Join(", ", paramNames)}) select cast(scope_identity() as int)");
 }
Esempio n. 9
0
        /// <inheritdoc/>
        public string BuildInsert(string tableName, string[] columnNames, string[] paramNames, NonaProperty keyProperty)
        {
            var sql = $"insert into {tableName} ({string.Join(", ", columnNames)}) values ({string.Join(", ", paramNames)})";

            if (keyProperty != null)
            {
                var keyColumnName = NonaMapper.Resolvers.Column(keyProperty);

                sql += " RETURNING " + keyColumnName;
            }
            else
            {
                // todo: what behavior is desired here?
                throw new Exception("A key property is required for the PostgresSqlBuilder.");
            }

            return(sql);
        }
Esempio n. 10
0
 public ForeignKeyInfo(NonaProperty propertyInfo, ForeignKeyRelation relation)
 {
     NonaProperty = propertyInfo;
     Relation     = relation;
 }
Esempio n. 11
0
 public KeyPropertyInfo(NonaProperty propertyInfo, bool isIdentity)
 {
     NonaProperty = propertyInfo;
     IsIdentity   = isIdentity;
 }
Esempio n. 12
0
 /// <summary>
 /// Builds an insert query using the specified table name, column names and parameter names.
 /// A query to fetch the new id will be included as well.
 /// </summary>
 /// <param name="tableName">The name of the table to query.</param>
 /// <param name="columnNames">The names of the columns in the table.</param>
 /// <param name="paramNames">The names of the parameters in the database command.</param>
 /// <param name="keyProperty">The key property. This can be used to query a specific column for the new id. This is
 /// optional.</param>
 /// <returns>
 /// An insert query including a query to fetch the new id.
 /// </returns>
 /// <inheritdoc />
 public string BuildInsert(string tableName, string[] columnNames, string[] paramNames, NonaProperty keyProperty)
 {
     return($"insert into {tableName} ({string.Join(", ", columnNames)}) values ({string.Join(", ", paramNames)}) select cast(@@IDENTITY as int)");
 }
Esempio n. 13
0
        /// <summary>
        /// Builds an insert query using the specified table name, column names and parameter names.
        /// A query to fetch the new id will be included as well.
        /// </summary>
        /// <param name="tableName">The name of the table to query.</param>
        /// <param name="columnNames">The names of the columns in the table.</param>
        /// <param name="paramNames">The names of the parameters in the database command.</param>
        /// <param name="keyProperty">The key property. This can be used to query a specific column for the new id. This is
        /// optional.</param>
        /// <returns>
        /// An insert query including a query to fetch the new id.
        /// </returns>
        /// <inheritdoc />
        public string BuildInsert(string tableName, string[] columnNames, string[] paramNames, NonaProperty keyProperty)
        {
            if (NonaMapper.EscapeCharacterStart == char.MinValue && NonaMapper.EscapeCharacterEnd == char.MinValue)
            {
                // Fall back to the default behavior.
                return($"insert into `{tableName}` (`{string.Join("`, `", columnNames)}`) values ({string.Join(", ", paramNames)}); select LAST_INSERT_ID() id");
            }

            // Table and column names are already escaped.
            return($"insert into {tableName} ({string.Join(", ", columnNames)}) values ({string.Join(", ", paramNames)}); select LAST_INSERT_ID() id");
        }