예제 #1
0
        public virtual object GetInstance(Type type, SQLiteStatement statement, SQLiteLoadOptions options)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            object instance;

            if (options?.GetInstanceFunc != null)
            {
                instance = options.GetInstanceFunc(type, statement, options);
            }
            else
            {
                instance = null;
                if (typeof(ISQLiteObject).IsAssignableFrom(type))
                {
                    try
                    {
                        instance = Activator.CreateInstance(type, Database);
                    }
                    catch
                    {
                        // do nothing
                    }
                }

                if (instance == null)
                {
                    try
                    {
                        instance = Activator.CreateInstance(type);
                    }
                    catch (Exception e)
                    {
                        throw new SqlNadoException("0011: Cannot create an instance for the '" + Name + "' table.", e);
                    }
                }
            }

            if (instance is ISQLiteObject so)
            {
                if (so.Database == null)
                {
                    so.Database = Database;
                }
            }
            InitializeAutomaticColumns(instance);
            return(instance);
        }
예제 #2
0
        public virtual string GetCreateSql(string tableName)
        {
            string sql = "CREATE ";

            if (IsVirtual)
            {
                sql += "VIRTUAL ";
            }

            sql += "TABLE " + SQLiteStatement.EscapeName(tableName);

            if (!IsVirtual)
            {
                sql += " (";
                sql += string.Join(",", Columns.Select(c => c.GetCreateSql(SQLiteCreateSqlOptions.ForCreateColumn)));

                if (PrimaryKeyColumns.Skip(1).Any())
                {
                    string pk = string.Join(",", PrimaryKeyColumns.Select(c => c.EscapedName));
                    if (!string.IsNullOrWhiteSpace(pk))
                    {
                        sql += ",PRIMARY KEY (" + pk + ")";
                    }
                }

                sql += ")";
            }

            if (DisableRowId)
            {
                // https://sqlite.org/withoutrowid.html
                sql += " WITHOUT ROWID";
            }

            if (IsVirtual)
            {
                sql += " USING " + Module;
                if (ModuleArguments != null && ModuleArguments.Length > 0)
                {
                    sql += "(" + string.Join(",", ModuleArguments) + ")";
                }
            }
            return(sql);
        }
예제 #3
0
        public virtual string BuildCreateSql(string tableName)
        {
            string sql = "CREATE TABLE " + SQLiteStatement.EscapeName(tableName) + " (";

            sql += string.Join(",", Columns.Select(c => c.GetCreateSql(SQLiteCreateSqlOptions.ForCreateColumn)));

            if (PrimaryKeyColumns.Count() > 1)
            {
                string pk = string.Join(",", PrimaryKeyColumns.Select(c => c.EscapedName));
                if (!string.IsNullOrWhiteSpace(pk))
                {
                    sql += ",PRIMARY KEY (" + pk + ")";
                }
            }

            sql += ")";

            if (DisableRowId)
            {
                // https://sqlite.org/withoutrowid.html
                sql += " WITHOUT ROWID";
            }
            return(sql);
        }
예제 #4
0
 public virtual string BuildColumnsStatement() => string.Join(",", Columns.Select(c => SQLiteStatement.EscapeName(c.Name)));
예제 #5
0
 public virtual string BuildWherePrimaryKeyStatement() => string.Join(" AND ", PrimaryKeyColumns.Select(c => SQLiteStatement.EscapeName(c.Name) + "=?"));
예제 #6
0
 public T GetInstance <T>(SQLiteStatement statement) => GetInstance <T>(statement, null);
예제 #7
0
 public virtual string BuildColumnsInsertStatement() => string.Join(",", Columns.Where(c => !c.AutomaticValue && !c.UpdateOnly && !c.ComputedValue).Select(c => SQLiteStatement.EscapeName(c.Name)));
예제 #8
0
 public virtual string BuildColumnsUpdateSetStatement() => string.Join(",", Columns.Where(c => !c.AutomaticValue && !c.IsPrimaryKey && !c.InsertOnly && !c.ComputedValue).Select(c => SQLiteStatement.EscapeName(c.Name) + "=?"));