Exemplo n.º 1
0
        public virtual DbObject Create(User user, string objXml)
        {
            DbObject obj2 = this.Parse(objXml);
            DbObject des  = Activator.CreateInstance(obj2.GetType()) as DbObject;

            if (user != null)
            {
                this.SetCreator(obj2, user.Id);
                this.SetLastUpdater(obj2, user.Id);
            }
            A.Commons.Utils.CloneUtils.CloneObject(obj2, des, new string[0]);
            return(this.InvokeGenericMethod(this.DbContext, obj2.GetType(), "Create", new Type[] { obj2.GetType() }, new object[] { obj2 }) as DbObject);
        }
Exemplo n.º 2
0
        public virtual void Remove(string objXml)
        {
            DbObject            obj2 = this.Parse(objXml);
            DynamicPropertyInfo pkDynamicPropertyInfo = DbObjectTools.GetPkDynamicPropertyInfo(DbObjectTools.GetDbObjectInfo(obj2.GetType()), true);
            string str = obj2.GetValue(pkDynamicPropertyInfo.PropertyName).ToString();

            this.InvokeGenericMethod(this.DbContext, obj2.GetType(), "Delete", new Type[] { typeof(string) }, new object[] { str });
        }
Exemplo n.º 3
0
        public virtual DbObject Update(User user, string objXml)
        {
            DbObject            obj2 = this.Parse(objXml);
            DynamicPropertyInfo pkDynamicPropertyInfo = DbObjectTools.GetPkDynamicPropertyInfo(DbObjectTools.GetDbObjectInfo(obj2.GetType()), true);
            string           arg     = obj2.GetValue(pkDynamicPropertyInfo.PropertyName).ToString();
            SqlStringBuilder builder = new SqlStringBuilder(pkDynamicPropertyInfo.DataFieldName);

            builder.AppendFormat(" = '{0}'", arg);
            DbObject des = this.InvokeGenericMethod(this.DbContext, obj2.GetType(), "QueryObject", new Type[] { typeof(string), typeof(bool) }, new object[] { builder.ToString(), true }) as DbObject;

            if (user != null)
            {
                this.SetLastUpdater(obj2, user.Id);
            }
            A.Commons.Utils.CloneUtils.CloneObjectIgnoreId(obj2, des);
            this.InvokeGenericMethod(this.DbContext, obj2.GetType(), "Update", new Type[] { obj2.GetType() }, new object[] { des });
            return(des);
        }
Exemplo n.º 4
0
        public static bool IsValid(DbObject item)
        {
            if (item == null)
            {
                return(false);
            }

            switch (item.GetType().Name)
            {
            case nameof(Vehicle):
                return(IsVehicleValid(item as Vehicle));

            case nameof(Addon):
                return(IsAddonValid(item as Addon));

            case nameof(Customer):
                return(IsCustomerValid(item as Customer));

            default: return(false);
            }
        }
Exemplo n.º 5
0
        public static DbQuerySql CreateUpdateAllOn(ITable table, DbObject value, string fieldOn, object valueOn, DbObjectDatabase database, object userState = null)
        {
            // If the table is not configured, throw an exception.
            if (!table.IsConfigured) throw new DbException("Cannot create a database query for the table \'{0}\'. The table is not configured.".FormatWith(table.LocalName));
            // If the table  requires a database, check that a database is configured for this server.
            if (table.DefaultDatabase && (database == null)) throw new DbException("Cannot create a database query for the table \'{0}\'. The table requires a database but the server does not have a database configured.".FormatWith(table.LocalName));
            // Check the table type matches the database object type.
            if (table.Type != value.GetType()) throw new DbException("Cannot create a database update query for table \'{0}\', because object type \'{1}\' does not match the table type \'{2}\'.".FormatWith(table.LocalName, table.Type.Name, value.GetType().Name));

            // Get the table name.
            string tableName = DbQuerySql.GetTableName(table, database);

            // Get the on field.
            DbField field = table[fieldOn];
            // Check the on field exists.
            if(null == field) throw new DbException("Cannot create a database query on matching field \'{0}\' for the table \'{1}\', because the field does not exist.".FormatWith(fieldOn, table.LocalName));

            // Get the fields to update, excluding the on field.
            int fieldIndex = 0;
            string fields = DbQuerySql.GetFieldsSetExcluding(table, fieldOn, database, ref fieldIndex);

            // Create the query.
            DbQuerySql query = new DbQuerySql(
                "UPDATE {0} SET {1} WHERE {2}={{{3}}}".FormatWith(
                    tableName,
                    fields,
                    field.GetDatabaseName(),
                    fieldIndex++
                ),
                table,
                userState);
            // Return the query.
            return query;
        }
Exemplo n.º 6
0
 public static DbQuerySql CreateInsertAll(ITable table, DbObject value, DbObjectDatabase database, object userState = null)
 {
     // If the table is not configured, throw an exception.
     if (!table.IsConfigured) throw new DbException("Cannot create a database query for the table \'{0}\'. The table is not configured.".FormatWith(table.LocalName));
     // If the table  requires a database, check that a database is configured for this server.
     if (table.DefaultDatabase && (database == null)) throw new DbException("Cannot create a database query for the table \'{0}\'. The table requires a database but the server does not have a database configured.".FormatWith(table.LocalName));
     // Check the table type matches the database object type.
     if (table.Type != value.GetType()) throw new DbException("Cannot create a database insert query for table \'{0}\', because object type \'{1}\' does not match the table type \'{2}\'.".FormatWith(table.LocalName, table.Type.Name, value.GetType().Name));
     // Get the table name.
     string tableName = DbQuerySql.GetTableName(table, database);
     // Create the query.
     DbQuerySql query = new DbQuerySql(
         "INSERT INTO {0} ({1}) VALUES ({2})".FormatWith(
             DbQuerySql.GetFieldNames(table, tableName),
             DbQuerySql.GetFieldIndices(table, 0)
         ),
         table, userState);
     // Add the parameters.
     foreach (DbField field in table.Fields)
     {
         query.parameters.Add(value.GetValue(field.LocalName));
     }
     // Return the query.
     return query;
 }