Esempio n. 1
0
        } // Insert()

        /// <summary>
        ///
        /// </summary>
        /// <param name="o"></param>
        /// <param name="fullUpdate"></param>
        /// <returns>number of rows affected</returns>
        public Int32 Update(DataObject o, Boolean fullUpdate = false)
        {
            if (!o.AuthorizeUpdate())
            {
                throw new Exception("Not authorized for Update.");
            }

            DataDefinition d  = o.GetDefinition();
            var            dv = o.ToDictionary(fullUpdate);

            var PK_key   = d.PrimaryKey;
            var PK_value = dv[d.PrimaryKey];

            dv.Remove(PK_key);

            if (dv.Count == 0)
            {
                return(0);
            }

            using (IDbCommand query = CreateCommand())
            {
                query.CommandText = "update [" + d.DataEntity + "]";
                AppendSet(query, dv);
                AppendWhere(query, PK_key, "=", PK_value);

                return(ExecuteNonQuery(query));
            }
        } // Update();
Esempio n. 2
0
 private TransactionalCollection<DataObject> GetCollection(DataObject o)
 {
     var collectionName = o.GetDefinition().DataEntity;
     if (!Tables.ContainsKey(collectionName))
     {
         Tables[collectionName] = new TransactionalCollection<DataObject>();
     }
     return Tables[collectionName];
 }
Esempio n. 3
0
        }  // Find() ... (all)

        /// <summary>
        /// UPDATEs the provided DataObject if a PK is present, INSERTs if a PK is not present.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="o">The object to save</param>
        /// <param name="fullUpdate">Whether to set nulls, 0's, and empty strings values in the update, which are otherwise ignored.</param>
        /// <returns>number of rows affected</returns>
        public Int32 Save(DataObject o, Boolean fullUpdate = false)
        {
            if (!o.Validate())
            {
                throw new Exception("Invalid object state for saving.");
            }

            DataDefinition d = o.GetDefinition();
            Dictionary <String, String> v = o.ToDictionary();

            if (d.PrimaryKey != null && v.ContainsKey(d.PrimaryKey) && v[d.PrimaryKey] != null)
            {
                return(Update(o, fullUpdate));
            }
            else
            {
                return(Insert(o, fullUpdate));
            }
        }
Esempio n. 4
0
        } // Update();

        /// <summary>
        /// Deletes DataObjects as indicated by the matching fields in the supplied object.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="o"></param>
        /// <returns></returns>
        public Int32 Delete(DataObject o)
        {
            if (!o.AuthorizeDelete())
            {
                throw new Exception("Not authorized for Delete.");
            }

            var dv = o.ToDictionary();

            if (dv.Count == 0)
            {
                throw new Exception("No DELETE conditions were given.");
            }

            using (IDbCommand query = CreateCommand())
            {
                query.CommandText = "delete from " + o.GetDefinition().DataEntity;
                AppendWhere(query, dv);
                return(ExecuteNonQuery(query));
            }
        }
Esempio n. 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="o"></param>
        /// <param name="fullUpdate"></param>
        /// <returns>number of rows affected</returns>
        public Int32 Insert(DataObject o, Boolean fullUpdate = false)
        {
            if (!o.AuthorizeInsert())
            {
                throw new Exception("Not authorized for Insert.");
            }

            var            dv     = o.ToDictionary(fullUpdate);
            DataDefinition d      = o.GetDefinition();
            Boolean        GuidPK = true;

            if (d.PrimaryKey != null)
            {
                Guid newGuid = Guid.NewGuid();
                GuidPK = d.Maps[d.PrimaryKey].PropertyType.Equals(typeof(Guid));

                if (GuidPK && (!dv.ContainsKey(d.PrimaryKey) || dv[d.PrimaryKey] == null || dv[d.PrimaryKey].Equals(Guid.Empty.ToString())))
                {
                    dv[d.PrimaryKey] = newGuid.ToString();
                }
            }
            using (IDbCommand query = CreateCommand())
            {
                query.CommandText  = "insert into [" + o.GetDefinition().DataEntity + "]";
                query.CommandText += " (" + String.Join(",", dv.Keys.ToArray()) + ")";
                query.CommandText += " values (@" + String.Join(",@", dv.Keys.ToArray()) + ")";

                IDbDataParameter insertid = null;
                if (!GuidPK)
                {
                    query.CommandText     += " SET @insert_id = SCOPE_IDENTITY()";
                    insertid               = query.CreateParameter();
                    insertid.Direction     = ParameterDirection.Output;
                    insertid.ParameterName = "insert_id";
                    insertid.DbType        = DbType.Int32;
                    insertid.Size          = sizeof(Int32); // Convert.ToInt32(Math.Pow(2, 16));
                    query.Parameters.Add(insertid);
                }

                foreach (String k in dv.Keys)
                {
                    IDbDataParameter parameter = query.CreateParameter();
                    parameter.ParameterName = k;
                    parameter.Value         = dv[k] == null ? (object)DBNull.Value : (object)dv[k];
                    query.Parameters.Add(parameter);
                }

                Int32 rv = ExecuteNonQuery(query);

                //
                // NOTE : No idea whether the following insertid handling is correct.
                // It works in my single test-case. Much more testing is required ...
                //
                if (rv > 0)
                {
                    if (d.PrimaryKey != null)
                    {
                        if (GuidPK)
                        {
                            o.Populate(d.PrimaryKey, dv[d.PrimaryKey]);
                        }
                        else
                        {
                            o.Populate(d.PrimaryKey, insertid.Value.ToString());
                        }
                    }
                }

                return(rv);
            }
        } // Insert()
Esempio n. 6
0
 public JoinPath(DataObject a, DataObject b, Int32 maxNodes = 512)
     : this(a.GetDefinition(), b.GetDefinition(), maxNodes)
 {
 }