Skip to content

Writing Your Own Data Adapters

MichaelAz edited this page Jan 19, 2013 · 1 revision

Since TINA-ORM only comes with built in support for a limited amount of databases and your favorite database might not be one of them, Tina allows you to easily create a data adapter that you can use.

A data adapter is, in fact, a configuration class that inherits TinaConfig and defines a simple operation - creating a table that conforms to a constant schema if it does not exist. Everything else is encapsulated in the core Tina class.

The data adapter does this by overriding the CreateTableIfNotExists method. For example, here's how the MySql adapter does it.

public override void CreateTableIfNotExists()
        {
            const string createTable =
                @"CREATE TABLE IF NOT EXISTS Tina
                                        (
                                            id INT AUTO_INCREMENT UNIQUE PRIMARY KEY, 
                                            Type VARCHAR(4000) CHARSET utf8, 
                                            Contents VARCHAR(4000) CHARSET utf8
                                        );";

            using (var conn = Connection)
            {
                conn.Open();

                var cmd = conn.CreateCommand();
                cmd.CommandText = createTable;
                cmd.ExecuteNonQuery();

                conn.Close();
            }
        }

The schema is, always, a three column table - id, Type and Contents. id is the primary key and is an auto incrementing integer. Type and Contents are both variable length Unicode strings.

Clone this wiki locally