Exemplo n.º 1
0
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // public methods                                                                                                   //
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>Gets the entity SQL.</summary>
        /// <returns>SQL string.</returns>
        public string GetSQL()
        {
            __Entity bse = Member.BaseType._GetEntity();

            string rval = "SELECT ";

            for (int i = 0; i < Internals.Length; i++)
            {
                if (i > 0)
                {
                    rval += ", ";
                }
                rval += Internals[i].ColumnName;
            }
            rval += (" FROM " + TableName);

            if (bse.IsMaterial)
            {
                rval += " INNER JOIN " + bse.TableName + " ON " + PrimaryKey.ColumnName + " = " + ChildKey;
            }

            if (!string.IsNullOrWhiteSpace(SubsetQuery))
            {
                rval += " WHERE (" + SubsetQuery + ")";
            }

            return(rval);
        }
Exemplo n.º 2
0
        /// <summary>Updates references.</summary>
        /// <param name="obj">Object.</param>
        public void UpdateReferences(object obj)
        {
            if (!IsExternal)
            {
                return;
            }
            if (GetValue(obj) == null)
            {
                return;
            }

            Type     innerType   = Type.GetGenericArguments()[0];
            __Entity innerEntity = innerType._GetEntity();
            object   pk          = Entity.PrimaryKey.ToColumnType(Entity.PrimaryKey.GetValue(obj));

            if (IsManyToMany)
            {
                IDbCommand cmd = Orm.Connection.CreateCommand();
                cmd.CommandText = ("DELETE FROM " + AssignmentTable + " WHERE " + ColumnName + " = :pk");
                IDataParameter p = cmd.CreateParameter();
                p.ParameterName = ":pk";
                p.Value         = pk;
                cmd.Parameters.Add(p);

                cmd.ExecuteNonQuery();
                cmd.Dispose();

                foreach (object i in (IEnumerable)GetValue(obj))
                {
                    cmd             = Orm.Connection.CreateCommand();
                    cmd.CommandText = ("INSERT INTO " + AssignmentTable + "(" + ColumnName + ", " + RemoteColumnName + ") VALUES (:pk, :fk)");
                    p = cmd.CreateParameter();
                    p.ParameterName = ":pk";
                    p.Value         = pk;
                    cmd.Parameters.Add(p);

                    p = cmd.CreateParameter();
                    p.ParameterName = ":fk";
                    p.Value         = innerEntity.PrimaryKey.ToColumnType(innerEntity.PrimaryKey.GetValue(i));
                    cmd.Parameters.Add(p);

                    cmd.ExecuteNonQuery();
                    cmd.Dispose();
                }
            }
            else
            {
                __Field remoteField = innerEntity.GetFieldForColumn(ColumnName);

                if (remoteField.IsNullable)
                {
                    try
                    {
                        IDbCommand cmd = Orm.Connection.CreateCommand();
                        cmd.CommandText = ("UPDATE " + innerEntity.TableName + " SET " + ColumnName + " = NULL WHERE " + ColumnName + " = :fk");
                        IDataParameter p = cmd.CreateParameter();
                        p.ParameterName = ":fk";
                        p.Value         = pk;
                        cmd.Parameters.Add(p);

                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                    }
                    catch (Exception) {}
                }

                foreach (object i in (IEnumerable)GetValue(obj))
                {
                    remoteField.SetValue(i, obj);

                    IDbCommand cmd = Orm.Connection.CreateCommand();
                    cmd.CommandText = ("UPDATE " + innerEntity.TableName + " SET " + ColumnName + " = :fk WHERE " + innerEntity.PrimaryKey.ColumnName + " = :pk");
                    IDataParameter p = cmd.CreateParameter();
                    p.ParameterName = ":fk";
                    p.Value         = pk;
                    cmd.Parameters.Add(p);

                    p = cmd.CreateParameter();
                    p.ParameterName = ":pk";
                    p.Value         = innerEntity.PrimaryKey.ToColumnType(innerEntity.PrimaryKey.GetValue(i));
                    cmd.Parameters.Add(p);

                    cmd.ExecuteNonQuery();
                    cmd.Dispose();
                }
            }
        }
Exemplo n.º 3
0
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // constructors                                                                                                     //
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>Creates a new instance of this class.</summary>
        /// <param name="entity">Parent entity.</param>
        public __Field(__Entity entity)
        {
            Entity = entity;
        }
Exemplo n.º 4
0
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // private properties                                                                                               //
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>Fills the object.</summary>
        /// <param name="t">Type.</param>
        /// <param name="localCache">Local cache.</param>
        private void _Fill(Type t, ICollection <object> localCache)
        {
            List <Query <T> > ops = new List <Query <T> >();

            Query <T> q = this;

            while (q != null)
            {
                ops.Insert(0, q);
                q = q._Previous;
            }

            __Entity ent = t._GetEntity();

            string sql = ent.GetSQL();
            List <Tuple <string, object> > parameters = new List <Tuple <string, object> >();
            string conj  = (string.IsNullOrWhiteSpace(ent.SubsetQuery) ? " WHERE (" : " AND (");
            bool   not   = false;
            string opbrk = "";
            string clbrk = "";
            int    n     = 0;
            string op;

            __Field field;

            foreach (Query <T> i in ops)
            {
                switch (i._Op)
                {
                case __QueryOperation.OR:
                    if (!conj.EndsWith("("))
                    {
                        conj = " OR ";
                    }
                    break;

                case __QueryOperation.NOT:
                    not = true; break;

                case __QueryOperation.GRP:
                    opbrk += "("; break;

                case __QueryOperation.ENDGRP:
                    clbrk += ")"; break;

                case __QueryOperation.EQUALS:
                case __QueryOperation.LIKE:
                    field = ent.GetFieldByName((string)i._Args[0]);

                    if (i._Op == __QueryOperation.LIKE)
                    {
                        op = (not ? " NOT LIKE " : " LIKE ");
                    }
                    else
                    {
                        op = (not ? " != " : " = ");
                    }

                    sql += clbrk + conj + opbrk;
                    sql += (((bool)i._Args[2] ? "Lower(" + field.ColumnName + ")" : field.ColumnName) + op +
                            ((bool)i._Args[2] ? "Lower(:p" + n.ToString() + ")" : ":p" + n.ToString()));

                    if ((bool)i._Args[2])
                    {
                        i._Args[1] = ((string)i._Args[1]).ToLower();
                    }
                    parameters.Add(new Tuple <string, object>(":p" + n++.ToString(), field.ToColumnType(i._Args[1])));

                    opbrk = clbrk = ""; conj = " AND "; not = false;
                    break;

                case __QueryOperation.IN:
                    field = ent.GetFieldByName((string)i._Args[0]);

                    sql += clbrk + conj + opbrk;
                    sql += field.ColumnName + (not ? " NOT IN (" : " IN (");
                    for (int k = 1; k < i._Args.Length; k++)
                    {
                        if (k > 1)
                        {
                            sql += ", ";
                        }
                        sql += (":p" + n.ToString());
                        parameters.Add(new Tuple <string, object>(":p" + n++.ToString(), field.ToColumnType(i._Args[k])));
                    }
                    sql += ")";

                    opbrk = clbrk = ""; conj = " AND "; not = false;
                    break;

                case __QueryOperation.GT:
                case __QueryOperation.LT:
                    field = ent.GetFieldByName((string)i._Args[0]);

                    if (i._Op == __QueryOperation.GT)
                    {
                        op = (not ? " <= " : " > ");
                    }
                    else
                    {
                        op = (not ? " >= " : " < ");
                    }

                    sql += clbrk + conj + opbrk;
                    sql += (field.ColumnName + op + ":p" + n.ToString());

                    parameters.Add(new Tuple <string, object>(":p" + n++.ToString(), field.ToColumnType(i._Args[1])));

                    opbrk = clbrk = ""; conj = " AND "; not = false;
                    break;
                }
            }

            if (!conj.EndsWith("("))
            {
                sql += ")";
            }
            Orm._FillList(t, _InternalValues, sql, parameters, localCache);
        }