Пример #1
0
        /// <summary>
        /// An asynchronous version of <see cref="Update(string, RecordSet)"/> that calls the respective INSERT, UPDATE, or DELETE statements
        /// for each added, updated, or deleted row in the specified <see cref="RecordSet"/>.
        /// </summary>
        /// <param name="tableName">The name of the source table.</param>
        /// <param name="recordSet"><see cref="RecordSet"/> to use to update the data source.</param>
        /// <param name="cancel">The cancellation instruction.</param>
        /// <returns>The number of rows successfully updated.</returns>
        public async Task <int> UpdateAsync(string tableName, RecordSet recordSet, CancellationToken cancel)
        {
            EnsurePrimaryKey(recordSet);
            int affected = 0;

            using (var rsAdapter = new RecordSetAdapter(this, tableName, recordSet)) {
                affected = await rsAdapter.UpdateAsync(cancel).ConfigureAwait(false);
            }
            recordSet.AcceptChanges();
            return(affected);
        }
Пример #2
0
        /// <summary>
        /// Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified <see cref="RecordSet"/>.
        /// </summary>
        /// <param name="tableName">The name of the source table.</param>
        /// <param name="recordSet"><see cref="RecordSet"/> to use to update the data source.</param>
        /// <returns>The number of rows successfully updated.</returns>
        /// <remarks>
        /// <para><see cref="RecordSet.PrimaryKey"/> should be set before calling <see cref="DbDataAdapter.Update(string, RecordSet)"/>.</para>
        /// When an application calls the Update method, <see cref="DbDataAdapter"/> examines the <see cref="RecordSet.Row.State"/> property,
        /// and executes the required INSERT, UPDATE, or DELETE statements iteratively for each row (based on the order of rows in RecordSet).
        /// </remarks>
        public int Update(string tableName, RecordSet recordSet)
        {
            EnsurePrimaryKey(recordSet);
            int affected = 0;

            using (var rsAdapter = new RecordSetAdapter(this, tableName, recordSet)) {
                affected = rsAdapter.Update();
            }
            recordSet.AcceptChanges();
            return(affected);
        }
Пример #3
0
        /// <summary>
        /// Creates a <see cref="RecordSet"/> and reads all rows from the supplied <see cref="IDataReader"/>.
        /// </summary>
        /// <param name="rdr">An <see cref="IDataReader"/> that provides a result set.</param>
        /// <param name="rowsCount">Max number of rows to load (-1 means no limit).</param>
        /// <returns><see cref="RecordSet"/> with schema inferred by reader and populated with incoming data.</returns>
        public static RecordSet FromReader(IDataReader rdr, int rowsCount)
        {
            RecordSet rs      = DataHelper.GetRecordSetByReader(rdr);
            int       read    = 0;
            bool      loadAll = rowsCount < 0;

            while ((loadAll || read < rowsCount) && rdr.Read())
            {
                // just copy values
                var rowValues = new object[rdr.FieldCount];
                rdr.GetValues(rowValues);
                rs.Add(rowValues).AcceptChanges();
                read++;
            }
            return(rs);
        }
Пример #4
0
        /// <summary>
        /// Creates a <see cref="RecordSet"/> from a data source using the supplied <see cref="IDataReader"/>.
        /// </summary>
        /// <param name="rdr">An <see cref="IDataReader"/> that provides a result set.</param>
        /// <returns><see cref="RecordSet"/> with schema inferred by reader and populated with incoming data.</returns>
        public static RecordSet FromReader(IDataReader rdr)
        {
            RecordSet rs = null;

            while (rdr.Read())
            {
                if (rs == null)
                {
                    rs = DataHelper.GetRecordSetByReader(rdr);
                }
                // just copy values
                var rowValues = new object[rdr.FieldCount];
                rdr.GetValues(rowValues);
                rs.Add(rowValues).AcceptChanges();
            }
            return(rs);
        }
Пример #5
0
        /// <summary>
        /// Creates a <see cref="RecordSet"/> with schema and rows inferred from the annotated object models.
        /// </summary>
        /// <typeparam name="T">annotated model type</typeparam>
        /// <param name="models">sequence of models</param>
        /// <param name="rowState">intial state of rows created by models</param>
        /// <returns><see cref="RecordSet"/> with rows</returns>
        public static RecordSet FromModel <T>(IEnumerable <T> models, RowState rowState)
        {
            var schema = DataMapper.Instance.GetSchema(typeof(T));

            if (schema.Columns.Length == 0)
            {
                throw new ArgumentException($"Model of type {typeof(T).Name} has no columns");
            }
            var rsCols = new Column[schema.Columns.Length];
            var pkCols = new List <Column>(schema.Key.Length);

            for (int i = 0; i < rsCols.Length; i++)
            {
                var modelCol = schema.Columns[i];
                rsCols[i] = new Column(modelCol.ColumnName, modelCol.ValueType)
                {
                    AutoIncrement = modelCol.IsIdentity,
                    ReadOnly      = modelCol.IsReadOnly
                };
                if (modelCol.IsKey)
                {
                    pkCols.Add(rsCols[i]);
                }
            }
            var rs = new RecordSet(rsCols);

            rs.PrimaryKey = pkCols.ToArray();
            if (models != null)
            {
                foreach (var dto in models)
                {
                    var rowData = new object[schema.Columns.Length];
                    for (int i = 0; i < schema.Columns.Length; i++)
                    {
                        var modelCol = schema.Columns[i];
                        if (modelCol.GetVal != null)
                        {
                            rowData[i] = modelCol.GetVal(dto);
                        }
                    }
                    rs.Add(rowData).rowState = rowState;
                }
            }
            return(rs);
        }
Пример #6
0
        /// <summary>
        /// Asynchronously creates a <see cref="RecordSet"/> from a data source using the supplied <see cref="IDataReader"/>.
        /// </summary>
        public static async Task <RecordSet> FromReaderAsync(IDataReader rdr, CancellationToken cancel)
        {
            RecordSet rs = null;

            while (await rdr.ReadAsync(cancel).ConfigureAwait(false))
            {
                if (rs == null)
                {
                    rs = DataHelper.GetRecordSetByReader(rdr);
                }
                // just copy values
                var rowValues = new object[rdr.FieldCount];
                await rdr.GetValuesAsync(rowValues, cancel).ConfigureAwait(false);

                rs.Add(rowValues).AcceptChanges();
            }
            return(rs);
        }
Пример #7
0
        internal static RecordSet GetRecordSetByReader(IDataReader rdr)
        {
            var rsCols   = new List <RecordSet.Column>(rdr.FieldCount);
            var rsPkCols = new List <RecordSet.Column>();

                        #if NET_STANDARD
            // lets populate data schema
            if (rdr is DbDataReader)
            {
                var dbRdr = (DbDataReader)rdr;
                if (dbRdr.CanGetColumnSchema())
                {
                    foreach (var dbCol in dbRdr.GetColumnSchema())
                    {
                        var c = new RecordSet.Column(dbCol);
                        rsCols.Add(c);
                        if (dbCol.IsKey.HasValue && dbCol.IsKey.Value)
                        {
                            rsPkCols.Add(c);
                        }
                    }
                }
            }
                        #endif

            if (rsCols.Count == 0)
            {
                // lets suggest columns by standard IDataReader interface
                for (int i = 0; i < rdr.FieldCount; i++)
                {
                    var colName = rdr.GetName(i);
                    var colType = rdr.GetFieldType(i);
                    rsCols.Add(new RecordSet.Column(colName, colType));
                }
            }
            var rs = new RecordSet(rsCols.ToArray(), 1);
            if (rsPkCols.Count > 0)
            {
                rs.PrimaryKey = rsPkCols.ToArray();
            }
            return(rs);
        }
Пример #8
0
 public void Dispose()
 {
     RS               = null;
     DbAdapter        = null;
     setColumns       = null;
     autoIncrementCol = null;
     if (InsertCmd != null)
     {
         InsertCmd.Dispose();
         InsertCmd = null;
     }
     if (UpdateCmd != null)
     {
         UpdateCmd.Dispose();
         UpdateCmd = null;
     }
     if (DeleteCmd != null)
     {
         DeleteCmd.Dispose();
         DeleteCmd = null;
     }
 }
Пример #9
0
 public RecordSetReader(RecordSet rs)
 {
     RS = rs;
 }
Пример #10
0
 /// <summary>
 /// An asynchronous version of <see cref="Update(string, RecordSet)"/> that calls the respective INSERT, UPDATE, or DELETE statements
 /// for each added, updated, or deleted row in the specified <see cref="RecordSet"/>.
 /// </summary>
 /// <param name="tableName">The name of the source table.</param>
 /// <param name="recordSet"><see cref="RecordSet"/> to use to update the data source.</param>
 /// <param name="cancel">The cancellation instruction.</param>
 /// <returns>The number of rows successfully updated.</returns>
 public Task <int> UpdateAsync(string tableName, RecordSet recordSet)
 {
     return(UpdateAsync(tableName, recordSet, CancellationToken.None));
 }
Пример #11
0
 internal void Detach()
 {
     rs       = null;
     rowState = RowState.Detached;
 }
Пример #12
0
 internal Row(RecordSet rs, object[] values)
 {
     this.rs     = rs;
     this.values = values;
     rowState    = RowState.Added;
 }
Пример #13
0
 /// <summary>
 /// Creates a <see cref="RecordSet"/> with schema and row inferred from the annotated object model.
 /// </summary>
 /// <typeparam name="T">annotated model type</typeparam>
 /// <param name="model">model instance</param>
 /// <param name="rowState">intial state of row created by model</param>
 /// <returns><see cref="RecordSet"/> with one row</returns>
 public static RecordSet FromModel <T>(T model, RowState rowState)
 {
     return(RecordSet.FromModel <T>(new[] { model }, rowState));
 }
Пример #14
0
 /// <summary>
 /// Creates an empty <see cref="RecordSet"/> with schema inferred from the annotated object model.
 /// </summary>
 /// <typeparam name="T">annotated model type</typeparam>
 /// <returns>empty <see cref="RecordSet"/></returns>
 public static RecordSet FromModel <T>()
 {
     return(RecordSet.FromModel <T>(null, RowState.Added));
 }