Exemplo n.º 1
0
        public void GetFactory_GivenTypeWithNotPublicConstructor_ShouldThrow()
        {
            var pd = PocoData.ForObject(TestEntity.Instance, "Id", new ConventionMapper());

            Should.Throw <InvalidOperationException>(() => pd.GetFactory("", "", 1, 1, null, null));
        }
Exemplo n.º 2
0
        private async Task <object> ExecuteInsertAsync(string tableName, string primaryKeyName, bool autoIncrement, object poco)
        {
            try {
                await OpenSharedConnectionAsync().ConfigureAwait(false);

                try {
                    using (var cmd = CreateCommand(_sharedConnection, "", new object[0])) {
                        var pd   = PocoData.ForObject(poco, primaryKeyName);
                        var type = poco.GetType();
                        var sql  = insert.Get(Tuple.Create(type, _sqlHelper._sqlType, 1), () => {
                            return(CteateInsertSql(pd, 1, tableName, primaryKeyName, autoIncrement));
                        });

                        foreach (var i in pd.Columns)
                        {
                            if (i.Value.ResultColumn)
                            {
                                continue;
                            }
                            if (autoIncrement && primaryKeyName != null && string.Compare(i.Value.ColumnName, primaryKeyName, true) == 0)
                            {
                                continue;
                            }
                            AddParam(cmd, i.Value.GetValue(poco), i.Value.PropertyInfo);
                        }
                        cmd.CommandText = sql;

                        if (!autoIncrement)
                        {
                            DoPreExecute(cmd);
                            await((SqlCommand)cmd).ExecuteNonQueryAsync().ConfigureAwait(false);
                            OnExecutedCommand(cmd);

                            PocoColumn pkColumn;
                            if (primaryKeyName != null && pd.Columns.TryGetValue(primaryKeyName, out pkColumn))
                            {
                                return(pkColumn.GetValue(poco));
                            }
                            else
                            {
                                return(null);
                            }
                        }

                        object id = await _provider.ExecuteInsertAsync(this, (SqlCommand)cmd, primaryKeyName);

                        // Assign the ID back to the primary key property
                        if (primaryKeyName != null && !poco.GetType().Name.Contains("AnonymousType"))
                        {
                            PocoColumn pc;
                            if (pd.Columns.TryGetValue(primaryKeyName, out pc))
                            {
                                pc.SetValue(poco, pc.ChangeType(id));
                            }
                        }

                        return(id);
                    }
                } finally {
                    CloseSharedConnection();
                }
            } catch (Exception x) {
                if (OnException(x))
                {
                    throw new SqlExecuteException(x, _sqlHelper._sql.LastCommand);
                }
                return(null);
            }
        }
Exemplo n.º 3
0
        private async Task <int> ExecuteUpdateAsync(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable <string> columns)
        {
            try {
                await OpenSharedConnectionAsync().ConfigureAwait(false);

                try {
                    using (var cmd = CreateCommand(_sharedConnection, "", new object[0])) {
                        var type = poco.GetType();
                        var pd   = PocoData.ForObject(poco, primaryKeyName);
                        var sql  = update.Get(Tuple.Create(type, _sqlHelper._sqlType), () => {
                            var sb    = new StringBuilder();
                            var index = 0;
                            foreach (var i in pd.Columns)
                            {
                                if (String.Compare(i.Value.ColumnName, primaryKeyName, StringComparison.OrdinalIgnoreCase) == 0)
                                {
                                    continue;
                                }
                                if (i.Value.ResultColumn)
                                {
                                    continue;
                                }

                                // Build the sql
                                if (index > 0)
                                {
                                    sb.Append(", ");
                                }
                                sb.AppendFormat("{0} = {1}{2}", _provider.EscapeSqlIdentifier(i.Value.ColumnName), _paramPrefix, index++);
                            }

                            return(string.Format("UPDATE {0} SET {1} WHERE {2} = {3}{4}",
                                                 _provider.EscapeTableName(tableName), sb.ToString(), _provider.EscapeSqlIdentifier(primaryKeyName), _paramPrefix, index++));
                        });

                        cmd.CommandText = sql;

                        foreach (var i in pd.Columns)
                        {
                            if (i.Value.ResultColumn)
                            {
                                continue;
                            }
                            if (string.Compare(i.Value.ColumnName, primaryKeyName, true) == 0)
                            {
                                if (primaryKeyValue == null)
                                {
                                    primaryKeyValue = i.Value.GetValue(poco);
                                }
                                continue;
                            }
                            AddParam(cmd, i.Value.GetValue(poco), i.Value.PropertyInfo);
                        }

                        // Find the property info for the primary key
                        PropertyInfo pkpi = null;
                        if (primaryKeyName != null)
                        {
                            PocoColumn col = pd.Columns.FirstOrDefault(q => q.Value.ColumnName == primaryKeyName).Value;
                            if (col != null)
                            {
                                pkpi = col.PropertyInfo;
                            }
                            else
                            {
                                pkpi = new { Id = primaryKeyValue }
                            }.GetType().GetProperties()[0];
                        }
                        AddParam(cmd, primaryKeyValue, pkpi);

                        DoPreExecute(cmd);

                        // Do it
                        var retv = await((SqlCommand)cmd).ExecuteNonQueryAsync().ConfigureAwait(false);
                        OnExecutedCommand(cmd);
                        return(retv);
                    }
Exemplo n.º 4
0
        public PocoData GetPocoDataForObject(object poco)
        {
            var pd = GetPocoDataForType(poco.GetType());

            return(PocoData.ForObject(poco, pd.TableInfo.PrimaryKey, DefaultMapper));
        }