public static bool Exists <T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int?commandTimeout = null) where T : class
        {
            var type     = typeof(T);
            var metadata = MetadataFor(type);

            var keys = metadata.Properties.Where(x => x.IsPK);

            if (!keys.Any())
            {
                throw new Exception("This only support entites with a single key property at the moment.");
            }
            if (keys.Count() > 1)
            {
                throw new Exception("This only support entites with a single key property at the moment.");
            }

            string sql;

            if (!s_existsQueries.TryGetValue(type.TypeHandle, out sql))
            {
                s_existsQueries[type.TypeHandle] = sql = TSQLGenerator.BuildDelete(metadata);
            }

            Log(sql, entity);

            var noOfRowsMatching = connection.Execute(
                sql,
                entity,
                transaction: transaction,
                commandTimeout: commandTimeout);

            return(noOfRowsMatching > 0);
        }
Beispiel #2
0
        public void BuildDelete_WithSchema()
        {
            var tableMD = GetTestMetadata();

            tableMD.Schema = "testschema";

            var sql = TSQLGenerator.BuildDelete(tableMD);

            Assert.AreEqual("DELETE FROM [testschema].[testtable] WHERE [a]=@aa", sql);
        }
Beispiel #3
0
        public void BuildDelete_NoSchema()
        {
            var tableMD = GetTestMetadata();

            Assert.IsNull(tableMD.Schema);

            var sql = TSQLGenerator.BuildDelete(tableMD);

            Assert.AreEqual("DELETE FROM [testtable] WHERE [a]=@aa", sql);
        }
        public static bool Delete <T>(this IDbConnection connection, T entityToDelete, IDbTransaction transaction = null, int?commandTimeout = null) where T : class
        {
            var type     = typeof(T);
            var metadata = MetadataFor(type);

            string sql;

            if (!s_deleteQueries.TryGetValue(type.TypeHandle, out sql))
            {
                s_deleteQueries[type.TypeHandle] = sql = TSQLGenerator.BuildDelete(metadata);
            }

            Log(sql, entityToDelete);

            var deleted = connection.Execute(sql, entityToDelete, transaction: transaction, commandTimeout: commandTimeout);

            return(deleted > 0);
        }