What is "db" in Prisma datasource block? - database

In Prisma For MongogDB quick start guide, they wrote something like the following
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
My question is what is "db" right after "datasource" keyword?

The value db is tied to the name of the datasource in your schema file.
datasource db is a convention - however, you can give your data source any name - for example, datasource mysql or datasource data.
Here's a reference for datasource which should be helpful.
In your schema file when using native database types you need to refer to the datasource name which is defined. So if your datasource is defined as data you would refer the native type as #data.String instead of conventional #db.String

Related

How to set schema in datasource for DB2 connection in Spring boot

I have different database connections but same tables in them. So for reusing those entity classes I have to fetch schema name dynamically. Tried to set schema in properties file and in datasource and is not working.
application.properties file :
spring.datasource1.url=jdbc:db2://localhost:5054/AB01
spring.datasource1.username=abc
spring.datasource1.password=abc
spring.datasource1.driver-class-name=com.ibm.db2.jcc.DB2Driver
spring.datasource1.testWhileIdle=true
spring.datasource1.validationQuery=SELECT 1
Have tried to set schema in properties file as below and got error "The DDM parameter value is not supported. DDM parameter code point having unsupported value : 0x2110." error.
spring.datasource.url=jdbc:db2://localhost:5054/AB01?currentSchema=schema
spring.datasource.url=jdbc:db2://localhost:5054/AB01?search_path=schema
spring.datasource.url=jdbc:db2://localhost:5054/AB01?searchpath=schema
Tried like below in configuration class also but not working.
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource(databaseURL, username, pwd);
ds.setDriverClassName(driverClassName);
Properties connectionProperties = new Properties();
connectionProperties.setProperty("spring.datasource.schema", "schema");
ds.setConnectionProperties(connectionProperties);
return ds;
}
Schema can be referred in application.properties as below
spring.datasource1.serverName=160.60.660.6
spring.datasource1.database=ABC
spring.datasource1.port=5083
spring.datasource1.schema=fdsf
spring.datasource1.username=usr
spring.datasource1.password=pwd
use DB2SimpleDataSource to create database
public DataSource dataSource() {
DB2SimpleDataSource datasource = new DB2SimpleDataSource ();
datasource.setUser(username);
datasource.setPassword(password);
datasource.setServerName(server);
datasource.setDatabaseName(databse);
datasource.setPortNumber(port);
datasource.setDriverType(4);
datasource.setCurrentSchema(schema);
return datasource;
}

How can I specify a schema name for DbUnit to do the insert into?

I'm using DBUnit to insert data (dumped from a Postgres DB) into SQL Server, but want to do the insert into schema "rules", not the default "dbo" schema:
Class.forName(net.sourceforge.jtds.jdbc.Driver.class.getName());
Connection sqlsCon = DriverManager.getConnection("jdbc:jtds:sqlserver://5.5.5.5:7000;databaseName=THE_DB", "THE_USER", "THE_PW");
IDatabaseConnection sqlsDbCon = new DatabaseConnection(sqlsCon);
DatabaseOperation.CLEAN_INSERT.execute(sqlsDbCon, partialDataSet);
Thank you!
If you use spring boot 2 then you can provide a custom configuration in your config:
#TestConfiguration
public class MyDbUnitConfiguration {
#Bean
public DatabaseDataSourceConnectionFactoryBean dbUnitDatabaseConnection(DataSource dataSource) {
DatabaseConfigBean databaseConfig = new DatabaseConfigBean();
databaseConfig.setQualifiedTableNames(Boolean.TRUE);
DatabaseDataSourceConnectionFactoryBean databaseDataSourceConnectionFactory =
new DatabaseDataSourceConnectionFactoryBean();
databaseDataSourceConnectionFactory.setDatabaseConfig(databaseConfig);
databaseDataSourceConnectionFactory.setDataSource(dataSource);
return databaseDataSourceConnectionFactory;
}
}
Include this configuration and update your datasets.xml to fully qualified names.
There are a few ways to support that, see the documentation here:
http://dbunit.sourceforge.net/faq.html#AmbiguousTableNameException
For example you could enable the qualified table names property and use the fully qualified table names like SCHEMA.TABLE. Enabling that involves the following code:
conn=getConnection();
conn.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);

Entity Framework: multiple DB having the same schema

I have just created an ASP.NET MVC 4 & WebAPI project. After that I have added .edmx data source to project.
I have multiple databases with the same schema. Dynamically I want to replace connection string using default constructor provided in EF.
But in Model1.Designer.cs, every time I get error like "Member with same signature already declared".
I'm unable to solve this problem.
Yes, it works! All you need to change is the connection string.
And I have just tested it in order to satisfy my own curiosity.
Here are the steps that I took:
Take an existing database and create a model for it.
Create a new empty database.
In SQL Management Studio right click the first database -> Tasks -> Export Data. Export all it's data to the newly created database.
Remove some records from the second database.
Write this code:
TMS_MiscEntities db = new TMS_MiscEntities();
TMS_MiscEntities dbCopy = new TMS_MiscEntities();
dbCopy.Database.Connection.ConnectionString = db.Database.Connection.ConnectionString.Replace("initial catalog=TMS_Misc", "initial catalog=TMS_Misc_new");
Response.Write(string.Format("DB 1 records: {0}<br/>", db.ZipCodes.Count()));
Response.Write(string.Format("DB 2 records: {0}<br/>", dbCopy.ZipCodes.Count()));
Check results:
DB 1 records: 869164
DB 2 records: 868709
Conclude that it works :)
This is how my connection string looks:
<add name="TMS_MiscEntities" connectionString="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=ws2008;initial catalog=TMS_Misc;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
I'm using Entity Framework 6.1.3. I have added a constructor to my DbContext that takes a string parameter. This string can be the name of the connection stored in your App.config or a full connection string. Something like this:
public partial class MyDBContext : DbContext
{
public MyDBContext(string connectionString)
: base(connectionString)
{
}
// DbSets, OnModelCreating, etc
}
In my case, I manage a multi-tenant application and I use a ContextFactory to build the proper connection string and return my initialized context.
public class ContextFactory
{
public MyDbContext GetContext()
{
string connectionString;
// do some stuff here
return new MyDbContext(connectionString);
}
}

Updating domain class in grails

I want to add new fields to a domain class so that it updates the generated table's attributes set. I have a domain class like this,,
Class Book {
String name
static constraints = {
name nullable:false
}
String toString() {
return name
}
}
The GORM generated includes name along with id and version. Now I want to add ISBN to Book domain class as following
Class Book {
String name
String ISBN
static constraints = {
name nullable:false
ISBN nullable:true
}
String toString() {
return name
}
}
I don't want to use create-drop in DataSource.groovy because it will delete all my previous data. My DataSource.groovy looks like this,,
dataSource {
dbCreate = "update"
url = "jdbc:jtds:sqlserver://localhost:1433/databaseName"
username = "username"
password = "password"
}
I want GORM to add ISBN field to book table. But its not happening. Where am I wrong ?
I am using sql server 2008 and grails version 2.1.1
dataSource {
dbCreate = "update"
}
should do the trick. It updates you db without dropping the tables
you will need to crate this field in sql server by hand it use to be a easy task in sqlserver because all its tools, and you should take a look to grails database migration plugin documentation. It should be already installed in your grails version

Entity Framework 4 - Trim Database Char(50) value for Name on legacy database

This should be simple, but I haven't found a way yet...
I have a legacy database with name fields that are stored as CHAR(50). When this is bound to a TextBox with a Max Length of 50, you cannot insert.
How can I make the EF trim these values or at least map to RTrim(Column)?
I've tried using value converters, but the round trip causes issues with back spacing and spaces getting deleted between words.
Note that I only want to trim some fields, not all.
We are using SQL Server 2000 as the database. Soon to move to SQL 2008.
Thanks!
Entity framework is able to map only to table directly. You can also map to view or custom DB query but in such case your entity will became readonly unless you also map Insert, Delete and Update operations to stored procedures.
I think the problem you describes is related to ANSI PADDING behavior. It can be turned on but:
It is not recommended. In future version of SQL server it will be considered as error.
Must be configured before you create a column
You must handle trimming in the application. You can for example modify T4 template (if you use them) to trim string properties. Not sure how it works with WPF but you probably can inherit text box and override Text property to trim values.
Another way is handling ObjectMaterialized event on ObjectContext and manually trimming text properties but it can slow down your execution of your queries.
There's no way to do this with EF and SQL Server that I have found. I solved it with an extension method on IEnumerable<T> that calls TrimEnd() on each string property:
public static IEnumerable<TEntity> Trim<TEntity>(this IEnumerable<TEntity> collection)
{
Type type = typeof(TEntity);
IEnumerable<PropertyDescriptor> properties = TypeDescriptor.GetProperties(type).Cast<PropertyDescriptor>()
.Where(p => p.PropertyType == typeof(string));
foreach (TEntity entity in collection)
{
foreach (PropertyDescriptor property in properties)
{
string value = (string) property.GetValue(entity);
if (!String.IsNullOrEmpty(value))
{
value = value.TrimEnd();
property.SetValue(entity, value);
}
}
}
return collection;
}
Just make sure you call it after EF has retrieved the entities from the database. For example, after ToList():
public IEnumerable<Country> FetchCountries()
{
return _context.Set<Country>().ToList().Trim();
}
Have a look at the available attributes for your Database Connection String. I had a similar issue with Sybase Advantage database and solved with it's TrimTrailingSpaces attribute. Your database may support something similar.
Data Source=\\serverx\volumex\path\db.add;User ID=user;Password=pass;ServerType=REMOTE;TrimTrailingSpaces=TRUE;
http://www.connectionstrings.com/

Resources