Ejemplo n.º 1
0
        private string SQLite_Table_Create_Statement(Type type, bool addDropStatement)
        {
            StringBuilder     sql        = new StringBuilder();
            DatabaseEntityDef definition = _entityDefFactory.GetDef(type);

            if (definition.DbTableReservedName.IsNullOrEmpty())
            {
                throw new DatabaseException($"Type : {definition.EntityFullName} has null or empty DbTableReservedName");
            }

            foreach (DatabaseEntityPropertyDef info in definition.Properties)
            {
                if (!info.IsTableProperty)
                {
                    continue;
                }

                if (info.PropertyName.IsIn("Id", "Deleted", "LastUser", "LastTime", "Version"))
                {
                    continue;
                }

                string dbTypeStatement = info.TypeConverter == null
                    ? _databaseEngine.GetDbTypeStatement(info.PropertyType)
                    : info.TypeConverter.TypeToDbTypeStatement(info.PropertyType);

                string nullable = info.IsNullable ? "" : " NOT NULL ";

                string defaultValue = info.DbDefaultValue.IsNullOrEmpty() ? "" : " DEFAULT " + info.DbDefaultValue;

                string unique = info.IsUnique ? " UNIQUE " : "";

                sql.AppendLine($" {info.DbReservedName} {dbTypeStatement} {nullable} {unique} {defaultValue} ,");
            }

            string dropStatement = addDropStatement ? $"Drop table if exists {definition.DbTableReservedName};" : string.Empty;

            return
                ($@"{dropStatement}
CREATE TABLE {definition.DbTableReservedName} (
	""Id""    INTEGER PRIMARY KEY AUTOINCREMENT,
	{sql}
	""Deleted""   NUMERIC NOT NULL DEFAULT 0,
	""LastUser"" TEXT,
	""LastTime"" NUMERIC,
	""Version"" INTEGER NOT NULL
);");
        }