|
| 1 | +(this page is part of [the Rezoom.SQL tutorial](README.md)) |
| 2 | + |
| 3 | +# Switching from SQLite to SQL Server or PostgreSQL |
| 4 | + |
| 5 | +Rezoom.SQL translates its own dialect of SQL to different "backends". Currently |
| 6 | +[SQLite](https://www.sqlite.org/), MS SQL Server (T-SQL), and |
| 7 | +[PostgreSQL](https://www.postgresql.org/) are supported. |
| 8 | + |
| 9 | +So far, this tutorial has stuck to SQLite. However, most apps in the .NET |
| 10 | +ecosystem store their data in SQL Server, so they use T-SQL. |
| 11 | + |
| 12 | +## T-SQL |
| 13 | + |
| 14 | +Note: if you want to use Postgres, just skip down the page. The Postgres section |
| 15 | +repeats any information you need from from this part. |
| 16 | + |
| 17 | +If you're starting a fresh project and want to target T-SQL, it's as easy as |
| 18 | +installing |
| 19 | +[Rezoom.SQL.Provider.TSQL](https://www.nuget.org/packages/Rezoom.SQL.Provider.TSQL/) |
| 20 | +instead of |
| 21 | +[Rezoom.SQL.Provider.SQLite](https://www.nuget.org/packages/Rezoom.SQL.Provider.SQLite/). |
| 22 | +However, both packages are just thin wrappers around the [base |
| 23 | +library](https://www.nuget.org/packages/Rezoom.SQL.Provider/). They don't |
| 24 | +actually have any code, they just bundle some default config files and the |
| 25 | +initial `V1.model.sql`. |
| 26 | + |
| 27 | +**With your existing project, you can easily change the config yourself** to |
| 28 | + target a different database backend. Here's how. |
| 29 | + |
| 30 | +There's a file in your project called `rzsql.json`. Open it up and you'll see this: |
| 31 | + |
| 32 | +```javascript |
| 33 | +{ |
| 34 | + "backend": "sqlite", |
| 35 | + "optionals": "f#", |
| 36 | + "migrations": ".", |
| 37 | + "connectionname": "rzsql" |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +Just change the `"backend"` setting from `"sqlite"` to `"tsql"`. Then rebuild your project. |
| 42 | + |
| 43 | +You may get build errors if you have queries using the `last_insert_rowid()` |
| 44 | +function. This is because that is a SQLite function, and doesn't exist in T-SQL. |
| 45 | +Rezoom.SQL unifies the syntax of SQL queries, but it's not a complete |
| 46 | +compatibility layer: the functions available are still determined by the |
| 47 | +backend. **In this case, the T-SQL equivalent function is `scope_identity()`**. |
| 48 | + |
| 49 | +At this point your project should build, but you're not done yet. To be able to |
| 50 | +actually run the code, you'll need to edit your `App.config` with connection |
| 51 | +settings for SQL Server. This part isn't actually Rezoom-specific, it's standard |
| 52 | +.NET connection string stuff. However, nobody can remember the details, so here |
| 53 | +they are. Open up `App.config`, and you'll find something like this: |
| 54 | + |
| 55 | + |
| 56 | +```xml |
| 57 | +<configuration> |
| 58 | +<connectionStrings> |
| 59 | + <add |
| 60 | + name="rzsql" providerName="System.Data.SQLite" |
| 61 | + connectionString="Data Source=rzsql.db" |
| 62 | + /> |
| 63 | +</connectionStrings> |
| 64 | +<system.data> |
| 65 | + <DbProviderFactories> |
| 66 | + <remove invariant="System.Data.SQLite" /> |
| 67 | + <add |
| 68 | + name="SQLite Data Provider" |
| 69 | + invariant="System.Data.SQLite" |
| 70 | + description=".NET Framework Data Provider for SQLite" |
| 71 | + type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" |
| 72 | + /> |
| 73 | + </DbProviderFactories> |
| 74 | +</system.data> |
| 75 | +</configuration> |
| 76 | +``` |
| 77 | + |
| 78 | +Change the `<connectionStrings>` and `<DbProviderFactories>` sections like so: |
| 79 | + |
| 80 | +```xml |
| 81 | +<configuration> |
| 82 | +<connectionStrings> |
| 83 | + <add |
| 84 | + name="rzsql" providerName="System.Data.SqlClient" |
| 85 | + connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=rzsql" |
| 86 | + /> |
| 87 | +</connectionStrings> |
| 88 | +<system.data> |
| 89 | + <DbProviderFactories> |
| 90 | + <remove invariant="System.Data.SqlClient" /> |
| 91 | + <add |
| 92 | + name="SqlClient Data Provider" |
| 93 | + invariant="System.Data.SqlClient" |
| 94 | + description=".Net Framework Data Provider for SqlServer" |
| 95 | + type="System.Data.SqlClient.SqlClientFactory, System.Data" |
| 96 | + /> |
| 97 | + </DbProviderFactories> |
| 98 | +</system.data> |
| 99 | +</configuration> |
| 100 | +``` |
| 101 | + |
| 102 | +In the above configuration I am assuming your SQL server is located at |
| 103 | +.\SQLEXPRESS. If it isn't, change the `connectionString` attribute accordingly. |
| 104 | + |
| 105 | +## Postgres |
| 106 | + |
| 107 | +If you're starting a new project from scratch, you can just install |
| 108 | +[Rezoom.SQL.Provider.Postgres](https://www.nuget.org/packages/Rezoom.SQL.Provider.Postgres/) |
| 109 | +instead of |
| 110 | +[Rezoom.SQL.Provider.SQLite](https://www.nuget.org/packages/Rezoom.SQL.Provider.SQLite/). |
| 111 | + |
| 112 | +But if you want to keep working on the project you already created, you can do that easily too: |
| 113 | + |
| 114 | +There's a file in your project called `rzsql.json`. Open it up and you'll see this: |
| 115 | + |
| 116 | +```javascript |
| 117 | +{ |
| 118 | + "backend": "sqlite", |
| 119 | + "optionals": "f#", |
| 120 | + "migrations": ".", |
| 121 | + "connectionname": "rzsql" |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +Just change the `"backend"` setting from `"sqlite"` to `"postgres"`. Then rebuild your project. |
| 126 | + |
| 127 | +You may get build errors if you have queries using the `last_insert_rowid()` |
| 128 | +function. This is because that is a SQLite function, and doesn't exist in Postgres. |
| 129 | +Rezoom.SQL unifies the syntax of SQL queries, but it's not a complete |
| 130 | +compatibility layer: the functions available are still determined by the |
| 131 | +backend. **In this case, the Postgres equivalent function is `lastval()`**. |
| 132 | + |
| 133 | +At this point your project should build, but you're not done yet. To be able to |
| 134 | +actually run the code, you'll need to edit your `App.config` with connection |
| 135 | +settings for Postgres. This part isn't actually Rezoom-specific, it's standard |
| 136 | +.NET connection string stuff. However, nobody can remember the details, so here |
| 137 | +they are. Open up `App.config`, and you'll find something like this: |
| 138 | + |
| 139 | + |
| 140 | +```xml |
| 141 | +<configuration> |
| 142 | +<connectionStrings> |
| 143 | + <add |
| 144 | + name="rzsql" providerName="System.Data.SQLite" |
| 145 | + connectionString="Data Source=rzsql.db" |
| 146 | + /> |
| 147 | +</connectionStrings> |
| 148 | +<system.data> |
| 149 | + <DbProviderFactories> |
| 150 | + <remove invariant="System.Data.SQLite" /> |
| 151 | + <add |
| 152 | + name="SQLite Data Provider" |
| 153 | + invariant="System.Data.SQLite" |
| 154 | + description=".NET Framework Data Provider for SQLite" |
| 155 | + type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" |
| 156 | + /> |
| 157 | + </DbProviderFactories> |
| 158 | +</system.data> |
| 159 | +</configuration> |
| 160 | +``` |
| 161 | + |
| 162 | +Change the `<connectionStrings>` and `<DbProviderFactories>` sections like so. |
| 163 | +You'll need to fill in your own username and password in the connection string. |
| 164 | + |
| 165 | +```xml |
| 166 | +<configuration> |
| 167 | + <connectionStrings> |
| 168 | + <add |
| 169 | + name="rzsql" providerName="Npgsql" |
| 170 | + connectionString="Host=localhost;Database=rzsql;Username=your_user_here;Password=your_password_here" |
| 171 | + /> |
| 172 | + </connectionStrings> |
| 173 | + <system.data> |
| 174 | + <DbProviderFactories> |
| 175 | + <remove invariant="Npgsql" /> |
| 176 | + <add |
| 177 | + name="Npgsql Data Provider" |
| 178 | + invariant="Npgsql" |
| 179 | + support="FF" |
| 180 | + description=".Net Framework Data Provider for Postgresql" |
| 181 | + type="Npgsql.NpgsqlFactory, Npgsql" |
| 182 | + /> |
| 183 | + </DbProviderFactories> |
| 184 | + </system.data> |
| 185 | +</configuration> |
| 186 | +``` |
0 commit comments