Skip to content
Mithrandyr edited this page Jul 28, 2023 · 17 revisions

SimplySql - Talking to relational databases the PowerShell way.
Simple commands... Powerful opportunities.

Supporting MSSQL, Oracle, MySql, SQLite, PostGreSql.

Announcing: 2.0 coming soon!

2.0 is actively being worked on (as of November 2022). The module will have full support for PS 5.1 and PS7+ (it will be based on .NET Standard 2.0). Variously QoL improvements are coming along with it.

Introduction

SimplySql is a module that provides an intuitive set of cmdlets for talking to databases that abstracts the vendor specifics, allowing you to focus on getting work done.

The basic pattern is to connect to a database, execute one or more sql statements and then close your database connection. This module provides cmdlets that map to this basic pattern.

You can also extend SimplySql by adding your own providers: Provider Customization

Overview

  • Open/Close/Show/Test/Set/Get -SqlConnection

    • These cmdlets (Open/Close) enable you to connect to databases in straightforward terms without worrying about differences between database vendors. There is a specific "Open-*" cmdlet for each provider. (Sql Server, SQLite, Oracle, PostGreSql, MySql).

    • Show/Test cmdlets allow you to see what connections are currently active in your powershell session and see specific details about those connections.

    • Set cmdlet allows you to change the default commandTimeout and the database/catalog in use (if the provider supports it).

    • Get cmdlet returns the underlying connection object itself.

  • Invoke-SqlScalar/SqlQuery/SqlUpdate

    • These cmdlets allow you to execute sql statements against the database connections that you have opened.  Any type of statement can be used with any cmdlet, but the output is tailored to specific types of activity.

    • -SqlScalar is great for returning a single value.

    • -SqlQuery is used for returning one or more result sets (output is DataRow for single resultset and Table for multiple resultsets). You can use the switch -Stream to return PSObject instead of DataRow.

    • -SqlUpdate is used for making modifications (insert, update, delete, etc) and its output is the number of rows effected.

  • Get/Clear -SqlMessage

    • These cmdlets provider access to informational messages, if the provider supports them. The messages are timestamped to when they were received by calling command (and not necessarily when they were generated on the server since many implementations are Asynchronous.)

    • Get will return the messages generated by Invoke-SqlScalar/SqlQuery/SqlUpdate cmdlets. Messages are consumed as they are read.

    • Clear will remove all remaining unread messages.

  • Invoke-SqlBulkCopy

    • This is intended to make moving data from one connection to another connection (even cross vendor) simple.  This is highly optimized for destination connections that are SQL Server or SQLite.

    • Custom optimizations are used for Oracle, PostGre, and MySql.  Unfortunately neither ODP.net, Npgsql, or MySQL.Data have a managed bulkcopy class to leverage that supports a generic DataReader as its source.  So for these implementations we are using provider specific implementations of batching up inserts.

  • Start/Complete/Undo/Get -SqlTransaction

    • These cmdlets provide a simple way to wrap Invoke-Sql* (except for SqlBulkCopy) into a transaction and then either commit or rollback.

    • Complete-SqlTransaction maps to COMMIT and Undo-SqlTransaction maps to ROLLBACK.

    • Get cmdlet returns the underlying transaction object for a connection.