Пример #1
0
    public static void Update <T>(this ISqlConnectionProvider sqlConnectionProvider,
                                  T objectToUpdate,
                                  Expression <Func <T, object> > keyProperties = null,
                                  string tableName        = null,
                                  string schemaName       = null,
                                  bool processColumnNames = true)
    {
        tableName = SqlTextFunctions.GetTableName(objectToUpdate.GetType(), tableName, schemaName);

        using (var myConnection = sqlConnectionProvider.ToSqlConnection())
        {
            using (var myCommand = new SqlCommand {
                Connection = myConnection
            })
            {
                var setClauseProperties   = SqlClrHelpers.GetRelevantPropertyInfos(objectToUpdate, null);
                var whereClauseProperties = SqlClrHelpers.GetPropertiesFromObject(objectToUpdate, keyProperties);
                SqlTextFunctions.UnUnderscoreColumnNames = processColumnNames;
                BuildOutUpdateCommand(objectToUpdate,
                                      tableName,
                                      setClauseProperties,
                                      whereClauseProperties,
                                      myCommand,
                                      processColumnNames);
                SqlTextFunctions.UnUnderscoreColumnNames = true;
                myConnection.Open();
                SqlLogger.LogSqlCommand(myCommand);
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
        }
    }
    public static IEnumerable <T> MyQuery <T>(this ISqlConnectionProvider sqlConnectionProvider,
                                              string whereClause = null,
                                              string tableName   = null,
                                              string schemaName  = null)
    {
        whereClause = whereClause ?? "1 = 1";
        var selectClause = GenerateSelectClause <T>();

        tableName = SqlTextFunctions.GetTableName(typeof(T), tableName, schemaName);
        var sqlStatement = $"SELECT {selectClause} FROM {tableName} WHERE {whereClause}";

        SqlLogger.LogSqlStatement(sqlStatement);
        var results = sqlConnectionProvider.Query <T>(sqlStatement).ToList();

        return(results);
    }
    public static void Delete <T>(this ISqlConnectionProvider sqlConnectionProvider, T objToDelete,
                                  Expression <Func <T, object> > keyProperties = null, string tableName = null, string schemaName = null, bool processColumnNames = true)
    {
        tableName = SqlTextFunctions.GetTableName(objToDelete.GetType(), tableName, schemaName);

        using (var myConnection = sqlConnectionProvider.ToSqlConnection())
        {
            using (var myCommand = new SqlCommand()
            {
                Connection = myConnection
            })
            {
                SqlTextFunctions.UnUnderscoreColumnNames = processColumnNames;
                BuildOutMyCommand(objToDelete, keyProperties, tableName, myCommand);
                SqlTextFunctions.UnUnderscoreColumnNames = true;

                myConnection.Open();
                SqlLogger.LogSqlStatement(myCommand.CommandText);
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
        }
    }
    public static void Insert(this ISqlConnectionProvider sqlConnectionProvider,
                              object obj,
                              string tableName  = null,
                              string schemaName = null,
                              IEnumerable <string> columnsToIgnore = null,
                              bool processColumnNames = true,
                              bool useIdentityInsert  = false)
    {
        tableName = SqlTextFunctions.GetTableName(obj.GetType(), tableName, schemaName);

        var propertyInfos = SqlClrHelpers.GetRelevantPropertyInfos(obj, columnsToIgnore);

        if (propertyInfos.Count == 0)
        {
            return;
        }

        using (var myConnection = sqlConnectionProvider.GetSqlConnectionWithTimeout(60))
        {
            using (var myCommand = new SqlCommand {
                Connection = myConnection
            })
            {
                SqlTextFunctions.UnUnderscoreColumnNames = processColumnNames;
                BuildOutMyCommand(obj, tableName, propertyInfos, myCommand, processColumnNames);
                SqlTextFunctions.UnUnderscoreColumnNames = true;

                myConnection.Open();
                SqlLogger.LogSqlCommand(myCommand);

                // if (useIdentityInsert)
                //     myConnection.Execute("set identity_insert on;");
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
        }
    }