Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C# 4.0

Relationship Oriented Programming - The IDE plus More on Agile Project Management

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
12 Mar 2012CPOL81 min read 77.4K   1.2K   49  
An Integrated Development Environment (IDE) for the Relationship Oriented Programming Tool.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;

using Clifton.Tools.Strings;
using Clifton.Tools.Strings.Extensions;

using ROPLib;
using ROPLib.Interfaces;

namespace SQLLiteDataProvider
{
	public class SQLite	: IDataProvider
	{
		public StringBuilder Errors { get; protected set; }
		public DataSet DataSet { get { throw new NotImplementedException(); } }

		protected string filename;
		protected SQLiteConnection conn;
		protected Dictionary<DataType, string> dataTypeMap = new Dictionary<DataType, string>()
		{
			{DataType.Bool, "boolean"},
			{DataType.DateTime, "datetime"},
			{DataType.Int, "integer"},
			{DataType.String, "text"},
		};

		public SQLite()
		{
			Errors = new StringBuilder();
		}

		public void CreateDatabase(string filename, bool replaceExisting)
		{
			this.filename = filename;

			if (replaceExisting)
			{
				File.Delete(filename);
			}

			// Merely opening the database will create it.
			Open();
			Close();
		}

		public void CreateTables()
		{
			Open();

			Schema.Instance.EntitiesContainer.ForEach(t => t.Entities.ForEach(e =>
				{
					StringBuilder sql = GetCreateTableSql(e);
					ExecuteNonQuery(sql);
				}));

			Close();
		}

		public void CreateConstraints()
		{
			// SQLite does not support "alter table add constraint" capability.  Foreign Key constraints are created
			// when the table is created.
		}

		public StringBuilder GetCreateTableSql(Entity entity)
		{
			string comma = string.Empty;
			StringBuilder sb = new StringBuilder();
			sb.Append("create table ");
			sb.Append(entity.Name.Brace());
			sb.Append("(");

			entity.EntityAttributes.ForEach(a =>
				{
					sb.Append(comma);
					ROPLib.Attribute attr=Schema.Instance.GetAttribute(a.Name);
					sb.Append(a.FieldNameOrName.Brace());
					sb.Append(" ");
					sb.Append(dataTypeMap[attr.DataType]);

					if (a.IsPrimaryKey)
					{
						sb.Append(" PRIMARY KEY AUTOINCREMENT NOT NULL");
					}

					comma = ", ";
				});

			sb.Append(")");

			return sb;
		}

		public void Open()
		{
			conn = new SQLiteConnection("Data Source=" + filename + "; Pooling=true; FailIfMissing=false");
			conn.Open();
		}

		public void Close()
		{
			conn.Close();
			conn = null;
		}

		public void ExecuteNonQuery(StringBuilder sql)
		{
			SQLiteCommand cmd = conn.CreateCommand();
			cmd.CommandText = sql.ToString();
			cmd.ExecuteNonQuery();
		}
	}
}


/*
			SQLiteCommand cmd = conn.CreateCommand();
			cmd.CommandText = "create table Foobar (FoobarID integer PRIMARY KEY AUTOINCREMENT NOT NULL)";
			cmd.ExecuteNonQuery();
*/

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions