コード例 #1
0
ファイル: DatabaseTable.cs プロジェクト: nrag/yapper
        public ITableRow InsertRow(ITableRow row)
        {
            // Validate that we are attempting to insert only the columns that
            // exist in the table
            foreach (IColumn column in row.ColumnValues.Keys)
            {
                if (!this.Columns.Contains(column))
                {
                    throw new Exception("Attempting to insert a column that doesn't exist in the table");
                }
            }

            foreach (IColumn column in this.Columns)
            {
                if (column.IsRequired && !row.ColumnValues.ContainsKey(column))
                {
                    throw new Exception("Value for required column not present");
                }
            }

            if (this.Identity != null && row.ColumnValues.ContainsKey(this.Identity))
            {
                throw new Exception("Value should not be set for identity column");
            }

            using (DatabaseInsertOperator insertOp = new DatabaseInsertOperator(this))
            {
                object identity = insertOp.Execute(row);

                if (this.Identity != null)
                {
                    row.ColumnValues.Add(this.Identity, identity);
                }
            }

            return(row);
        }
コード例 #2
0
ファイル: DatabaseTable.cs プロジェクト: nrag/yapper
        public ITableRow InsertRow(ITableRow row)
        {
            // Validate that we are attempting to insert only the columns that
            // exist in the table
            foreach (IColumn column in row.ColumnValues.Keys)
            {
                if (!this.Columns.Contains(column))
                {
                    throw new Exception("Attempting to insert a column that doesn't exist in the table");
                }
            }

            foreach (IColumn column in this.Columns)
            {
                if (column.IsRequired && !row.ColumnValues.ContainsKey(column))
                {
                    throw new Exception("Value for required column not present");
                }
            }

            if (this.Identity != null && row.ColumnValues.ContainsKey(this.Identity))
            {
                throw new Exception("Value should not be set for identity column");
            }

            using (DatabaseInsertOperator insertOp = new DatabaseInsertOperator(this))
            {
                object identity = insertOp.Execute(row);

                if (this.Identity != null)
                {
                    row.ColumnValues.Add(this.Identity, identity);
                }
            }

            return row;
        }
コード例 #3
0
        internal object Execute(ITableRow row)
        {
            this.connection.StartTransaction(System.Data.IsolationLevel.ReadCommitted);

            // Save the blob first
            if (table.BlobValueColumn != null && table.BlobNameColumn != null)
            {
                object guidObject = null;
                Guid   blobName   = Guid.Empty;
                if (!row.ColumnValues.TryGetValue(table.BlobNameColumn, out guidObject))
                {
                    blobName = Guid.NewGuid();
                    row.ColumnValues.Add(table.BlobNameColumn, blobName);
                }
                else
                {
                    blobName = (Guid)guidObject;
                }

                object blobValue = null;
                if (!row.ColumnValues.TryGetValue(table.BlobValueColumn, out blobValue))
                {
                    row.ColumnValues[table.BlobNameColumn] = Guid.Empty;
                    blobName = Guid.Empty;
                }

                IBlobStore blobStore = BlobStoreFactory.Instance.GetBlobStore();
                blobStore.SaveBlob(row.BlobContainer, blobName.ToString(), (byte[])blobValue);
            }

            // Save the row in the table
            DbCommand command = this.BuildInsertCommand(row);

            if (table.Identity != null)
            {
                SqlParameter outParameter = new SqlParameter(string.Format("@{0}", table.Identity.Name), DatabaseInsertOperator.GetSqlType(table.Identity.Type));
                outParameter.Direction = ParameterDirection.Output;
                command.Parameters.Add(outParameter);
            }

            object outParam = command.ExecuteScalar();

            this.connection.CommitTransaction();

            return(null);
        }