예제 #1
0
 protected override Expression VisitMember(MemberExpression node)
 {
     if (_parameters.Contains(node.Expression) && node.Member is PropertyInfo property)
     {
         return(new ColumnExpression(node.Type, "", Orm.GetColumnName(property)));
     }
     return(base.VisitMember(node));
 }
예제 #2
0
        internal Table(SQLiteQueryProvider provider, string name) : base(provider)
        {
            _db  = provider.Database;
            Name = name;

            _primaryKeys = typeof(T).GetRuntimeProperties()
                           .Where(p => !p.IsDefined(typeof(IgnoreAttribute)) && p.IsDefined(typeof(PrimaryKeyAttribute)))
                           .ToArray();
            _otherColumns = typeof(T).GetRuntimeProperties()
                            .Where(p => !p.IsDefined(typeof(IgnoreAttribute)) && !p.IsDefined(typeof(PrimaryKeyAttribute)))
                            .ToArray();

            if (_primaryKeys.Length == 1)
            {
                var property   = _primaryKeys.Single();
                var primaryKey = property.GetCustomAttribute <PrimaryKeyAttribute>();
                if (primaryKey.AutoIncrement)
                {
                    _insertColumns        = _otherColumns;
                    _hasIntegerPrimaryKey = false;
                }
                else
                {
                    _insertColumns        = _primaryKeys.Concat(_otherColumns).ToArray();
                    _hasIntegerPrimaryKey = Orm.IntegralTypes.Contains(property.PropertyType);
                }
            }
            else
            {
                _insertColumns        = _primaryKeys.Concat(_otherColumns).ToArray();
                _hasIntegerPrimaryKey = false;
            }

            if (_primaryKeys.Length + _otherColumns.Length > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("INSERT INTO [").Append(Name).Append("]");
                sb.Append("(");
                foreach (var property in _insertColumns)
                {
                    sb.Append("[").Append(Orm.GetColumnName(property)).Append("]");
                    sb.Append(", ");
                }
                sb.Remove(sb.Length - 2, 2).Append(")");

                sb.Append(" VALUES(")
                .Append(string.Join(", ", Enumerable.Repeat("?", _insertColumns.Length)))
                .Append(");");

                _insertSQL = sb.ToString();
                sb.Insert("INSERT".Length, " OR {0}");
                _insertWithConflictSQL = sb.ToString();
            }

            if (_primaryKeys.Length > 0 && _otherColumns.Length > 0)
            {
                StringBuilder sb = new StringBuilder();

                sb.Append("UPDATE [").Append(Name).Append("] SET ");
                for (int i = 0; i < _otherColumns.Length; i++)
                {
                    if (i > 0)
                    {
                        sb.Append(", ");
                    }
                    sb.Append("[").Append(Orm.GetColumnName(_otherColumns[i])).Append("] = ?");
                }

                sb.Append(" WHERE ");
                for (int i = 0; i < _primaryKeys.Length; i++)
                {
                    if (i > 0)
                    {
                        sb.Append(" && ");
                    }
                    sb.Append("[").Append(Orm.GetColumnName(_primaryKeys[i])).Append("] == ?");
                }
                sb.Append(";");

                _updateSQL = sb.ToString();
                sb.Insert("UPDATE".Length, " OR {0}");
                _updateWithConflictSQL = sb.ToString();
            }
        }