コード例 #1
0
        /// <summary>
        ///     Inserts a new object into the database.
        /// </summary>
        /// <param name="metaTable">TableMatadata from which the object is created</param>
        public int Create(TableMetadata metaTable)
        {
            //collection of queries which will be executed
            List <ExecutionQuery> listQueries = null;

            int resultCounter = 0;

            DatabaseField field = metaTable.GetPrimaryKeyField();

            SqlGenerator generator = new SqlGenerator();

            ExecutionEngine exec = null;

            try
            {
                listQueries = new List <ExecutionQuery>();

                //get the attached tabels
                TableMetadata[] attachedData = metaTable.AttachedData;

                //check if the table has attached tables.
                //If not generate the insert only for a single table.
                if (attachedData.Length == 0)
                {
                    //generate the sql command
                    ExecutionQuery insertQuery = generator.GenerateInsertQuery(database, metaTable);

                    //add PK constraint if necessary
                    if (field.isValueAutogenerated)
                    {
                        insertQuery.Query = ConstraintManager.GeneratePrimaryKeyConstraint(field.fieldName, metaTable.TableName, insertQuery.Query);
                    }

                    listQueries.Add(insertQuery);
                }
                else
                {
                    //generate the multiple table's insert.
                    List <ExecutionQuery> multipleQueries = generator.GenerateMultipleInsertQueries(database, metaTable);
                    // containsSpecialModifications = true;

                    //add the queries to the
                    foreach (ExecutionQuery var in multipleQueries)
                    {
                        listQueries.Add(var);
                    }
                }

                //run in the  current session
                if (contextSession != null)
                {
                    //the context is in a transaction so just cache the inserts.
                    if (contextSession.IsInTransaction)
                    {
                        foreach (ExecutionQuery var in listQueries)
                        {
                            contextSession.Queries.Add(var);
                        }
                    }
                    else
                    {
                        resultCounter = execEngine.ExecuteNonQuery(listQueries);
                    }
                }
                else
                {
                    //
                    BeforeExecutingQueries(Operation.Create, ref listQueries);

                    exec = new ExecutionEngine();

                    //check if we need the PK or not
                    if (field.isValueAutogenerated && listQueries.Count == 1)
                    {
                        List <object> listPrimaryKeysValues = new List <object>();

                        resultCounter = exec.ExecuteNonQuery(listQueries, Configuration.DefaultTransactionIsolationLevel, out listPrimaryKeysValues);

                        if (listPrimaryKeysValues.Count > 0)
                        {
                            metaTable.SetFieldValue(field.fieldName, listPrimaryKeysValues[0]);
                        }
                    }
                    else
                    {
                        resultCounter = exec.ExecuteNonQuery(listQueries, Configuration.DefaultTransactionIsolationLevel);
                    }
                }

                return(resultCounter);
            }
            finally
            {
                if (exec != null)
                {
                    exec.Dispose();
                }
            }
        }
コード例 #2
0
 /// <summary>
 ///     Initialize a new PersistentObject
 /// </summary>
 /// <param name="mainTable"></param>
 public PersistentObject(TableMetadata mainTable)
     : this(Configuration.DatabaseServerType, Configuration.ConnectionString, mainTable)
 {
 }
コード例 #3
0
 /// <summary>
 ///     Initialize a new PersistentObject
 /// </summary>
 /// <param name="database">Database server dataType</param>
 /// <param name="connectionString">Connection string</param>
 /// <param name="mainTable">TableMetadata associated to this PersistentObject</param>
 public PersistentObject(DatabaseServer database, string connectionString, TableMetadata mainTable)
 {
     this.database         = database;
     this.connectionString = connectionString;
     mappedObject          = mainTable;
 }
コード例 #4
0
ファイル: MySqlGenerator.cs プロジェクト: mariusmg/DataBlock
 public string GeneratePaginatedQuery(TableMetadata metadata, int numberOfItems, int pageNumber)
 {
     throw new NotImplementedException();
 }
コード例 #5
0
ファイル: TableMetadata.cs プロジェクト: mariusmg/DataBlock
        /// <summary>
        ///     Gets the data from a related table in a ManyToMany relation.
        /// </summary>
        /// <param name="relatedTableType">Type of the related entity</param>
        /// <param name="intermediaryRelatedTableType">Type of the intermediary table</param>
        /// <returns>TableMetadata array which contains the results</returns>
        protected Array GetRelatedTableData(Type relatedTableType, Type intermediaryRelatedTableType)
        {
            PersistentObject        persistent = null;
            ManyToManyTableRelation relation   = null;
            DataSet ds = null;

            try
            {
                //create a instance of the table so we can check the relations between out table and this
                TableMetadata relatedTable      = (TableMetadata)Activator.CreateInstance(relatedTableType);
                TableMetadata intermediateTable = (TableMetadata)Activator.CreateInstance(intermediaryRelatedTableType);

                object primaryKeyValue = GetPrimaryKeyField().fieldValue;

                //first check the fieldValue of the primary key
                if (GetPrimaryKeyField().fieldValue == null)
                {
                    throw new InvalidOperationException("The primary key does not have a fieldValue");
                }

                //get the relation
                foreach (TableRelation var in Relations)
                {
                    if (var is ManyToManyTableRelation)
                    {
                        if (var.RelatedTableName == relatedTable.TableName)
                        {
                            relation = (ManyToManyTableRelation)var;
                            break;
                        }
                    }
                }

                //check if we got the relation
                if (relation == null)
                {
                    throw new ArgumentException("A relation cannot be found between the tables");
                }

                //we have the relation between the 2 tables.
                persistent = new PersistentObject(this);

                QueryCriteria qcThis = new QueryCriteria(TableName, GetPrimaryKeyField());
                qcThis.Add(CriteriaOperator.Equality, GetPrimaryKeyField(), primaryKeyValue);

                //generate here the inner join
                QueryCriteria qcIntermediaryTable = new QueryCriteria(intermediateTable.TableName, intermediateTable.GetField(relation.IntermediaryKeyFieldFromParentTable));

                QueryCriteria qcChild = new QueryCriteria(relatedTable);

                qcThis.AddJoin(JoinType.Inner, TableName, GetPrimaryKeyField(), intermediateTable.TableName,
                               intermediateTable.GetField(relation.IntermediaryKeyFieldFromParentTable), qcIntermediaryTable);

                qcThis.AddJoin(JoinType.Inner, intermediateTable.TableName, intermediateTable.GetField(relation.IntermediaryKeyFieldFromChildTable), relatedTable.TableName,
                               relatedTable.GetField(relation.IntermediaryKeyFieldFromChildTable), qcChild);

                ds = persistent.GetDataSet(qcThis);

                Array data = Array.CreateInstance(relatedTableType, ds.Tables[0].Rows.Count);

                for (int i = 0; i < data.Length; i++)
                {
                    object instance = Activator.CreateInstance(relatedTableType);
                    data.SetValue(instance, i);

                    //loop thru dataset
                    for (int j = START_INDEX_FIELD; j < ds.Tables[0].Columns.Count; j++)
                    {
                        Type tp = data.GetValue(i).GetType();

                        object[] args = new[] { ds.Tables[0].Columns[j].ColumnName, ds.Tables[0].Rows[i][j] };
                        tp.InvokeMember("SetFieldValue", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, instance, args);
                    }
                }

                return(data);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (ds != null)
                {
                    ds.Dispose();
                }
            }
        }
コード例 #6
0
ファイル: TableMetadata.cs プロジェクト: mariusmg/DataBlock
        /// <summary>
        ///     Returns data from a related table in Parent -> Child relationship. This is
        ///     the underlying implementation of the generated GetXXX tables.
        /// </summary>
        /// <param name="childTableType">Type of the related entity</param>
        /// <returns>Array with the result</returns>
        protected TableMetadata[] GetRelatedTableData(Type childTableType)
        {
            PersistentObject persistent = null;

            TableRelation relation = null;

            try
            {
                //create a instance of the table so we can check the relations between out table and this
                TableMetadata childTable = (TableMetadata)Activator.CreateInstance(childTableType);

                //first check the fieldValue of the primary key
                if (GetPrimaryKeyField().fieldValue == null)
                {
                    throw new InvalidOperationException("The primary key does not have a fieldValue");
                }

                //get the relation
                foreach (TableRelation var in Relations)
                {
                    if (var.RelatedTableName == childTable.TableName)
                    {
                        relation = var;
                        break;
                    }
                }

                if (relation is ChildTableRelation)
                {
                    return(GetChildRelatedTableData(childTableType, (ChildTableRelation)relation));
                }

                //check if we got the relation
                if (relation == null)
                {
                    throw new ArgumentException("A relation cannot be found between the tables");
                }

                //we have the relation between the 2 tables.
                persistent = new PersistentObject(this);

                TableMetadata[] data = null;

                if (relation.RelationCardinality == TableRelationCardinality.OneToOne)
                {
                    ParentTableRelation pr = (ParentTableRelation)(relation);
                    //take the fk value
                    data = (TableMetadata[])persistent.GetTableMetadata(relation.RelatedTableName, childTableType, GetField(pr.ForeignKeyName).fieldValue);
                }
                else
                {
                    //take the pk value
                    data = (TableMetadata[])persistent.GetTableMetadata(relation.RelatedTableName, childTableType, GetPrimaryKeyField().fieldValue);
                }

                return(data);
            }
            catch
            {
                throw;
            }
        }