Exemple #1
0
 private string[] GetColumns(EntityType type)
 {
     string[] result;
     if (!_columnsByType.TryGetValue(type, out result))
     {
         result = type.GetColumns();
         _columnsByType.Add(type, result);
     }
     return result;
 }
        public void CreateTable(EntityType type)
        {
            //String[] arr = type.Name.Split('.');
            String tableName = type.TableName;//String.Format("{0}_{1}", arr[arr.Length - 2], arr[arr.Length - 1]);
            String columns = "";
            String columnsOnly = "";

            var indexedFields = new List<string>();

            foreach (string column in type.GetColumns())
            {
                Type columnType = GetType(type.GetPropertyType(column));
                if (!_supportedTypes.ContainsKey(columnType))
                    throw new Exception(String.Format("Unsupported column type '{0}'", columnType));

                String columnName = String.Format("{0}", column);
                String typeDeclaration = String.Format(_supportedTypes[columnType].TypeName);

                String primaryKey = type.IsPrimaryKey(column) ? "PRIMARY KEY" : "";

                const string notNull = "";
                
                if (type.IsIndexed(column))
                    indexedFields.Add(columnName);

                String s = String.Format("[{0}] {1} {2} {3}", columnName, typeDeclaration, notNull, primaryKey);
                String s1 = String.Format("[{0}]", columnName);

                if (!String.IsNullOrEmpty(columns))
                {
                    s = "," + s;
                    s1 = "," + s1;
                }

                columns = columns + s;
                columnsOnly = columnsOnly + s1;
            }

            columns += ",[IsTombstone] INTEGER,[IsDirty] INTEGER";
            columnsOnly += ",[IsDirty]";

            //create table
            using (var cmd = new SqliteCommand(String.Format("CREATE TABLE [_{0}] ({1})", tableName, columns), ActiveConnection))
                cmd.ExecuteNonQuery();

            //create tran table
            using (var cmd = new SqliteCommand(String.Format("CREATE TABLE [__{0}] ({1})", tableName, columns), ActiveConnection))
                cmd.ExecuteNonQuery();

            //create indexes
            indexedFields.Add("IsTombstone");
            foreach (String idx in indexedFields)
            {
                String idxScript = String.Format("CREATE INDEX [{0}_{1}] ON _{0}([{1}])", tableName, idx);
                using (var idxCmd = new SqliteCommand(idxScript, ActiveConnection))
                    idxCmd.ExecuteNonQuery();
            }

            //create view
            using (var cmd = new SqliteCommand(String.Format(
                "CREATE VIEW [{0}] AS SELECT {1} FROM [_{0}] WHERE IsTombstone = 0", tableName, columnsOnly), ActiveConnection))
                cmd.ExecuteNonQuery();
        }