martinjw/dbschemareader

Question: What is the intended way to set the DataType when creating a schema manually?

Arnagos opened this issue · 2 comments

What is the intended way to set the DataType when creating a schema manually? As far as I can see almost all of the code is set to internal. I'm currently creating the type manually but I'm not quite sure, whether I'm creating them the same way as you.

DatabaseReader doesn't actually check the database for DataTypes (normally*), so you can populate it with a dummy connection string (even string.Empty). Take the schema from that - you can discard the reader-, and all the manual building will use the data types.

*It has to for some of the older dbs where we still use their provider- Sybase dbs, Ingres, VistaDb. But it is ok for SqlServer/Oracle/MySql/Sqlite/PostgreSql etc

            var reader = new DatabaseReader("dummyNotNull", SqlType.PostgreSql);
            reader.DataTypes();
            var schema = reader.DatabaseSchema;
            schema.AddTable("DEPARTMENTS")
                .AddColumn("DEPARTMENT_ID", "NUMBER(4,0)").AddPrimaryKey()
                .AddColumn("DEPARTMENT_NAME","VARCHAR2(30)")
                .AddColumn<int>("DEPARTMENT_HEAD_ID")
                .AddColumn<string>("COSTCENTER_CODE").AddLength(20);

Added a convenience method that does the above, with an explicit SqlType for the supported databases.

var schema = new DatabaseSchema(null, SqlType.PostgreSql)
                .AddDataTypes(SqlType.PostgreSql);