Example #1
0
        internal static string buildInsertSql <T>(ReaderCachedCollection typeCollection, OnConflict resolution, string tableName = null)
        {
            var info = typeCollection.GetInfo <T>();

            if (tableName == null)
            {
                tableName = info.TypeName;
            }

            var names  = getNames(info, !info.IsAnonymousType);
            var fields = string.Join(",", names);
            var values = string.Join(",", names.Select(n => $"@{n}"));

            if (resolution == OnConflict.Abort)
            {
                return($"INSERT INTO {tableName} ({fields}) VALUES ({values}); SELECT last_insert_rowid();");
            }
            else
            {
                string txtConflict = resolution switch
                {
                    OnConflict.RollBack => "ROLLBACK",
                    OnConflict.Fail => "FAIL",
                    OnConflict.Ignore => "IGNORE",
                    OnConflict.Replace => "REPLACE",
                    _ => throw new ArgumentException($"Invalid resolution: {resolution}"),
                };

                return($"INSERT OR {txtConflict} INTO {tableName} ({fields}) VALUES ({values}); SELECT last_insert_rowid();");
            }
        }
Example #2
0
        internal static void fillParameters(SqliteCommand cmd, object parameters, ReaderCachedCollection typeCollection)
        {
            if (parameters == null)
            {
                return;
            }

            var type = typeCollection.GetInfo(parameters.GetType());

            foreach (var p in type.Items)
            {
                if (!p.CanRead)
                {
                    continue;
                }
                var value = TypeHelper.ReadParamValue(p, parameters);
                adjustInsertValue(ref value, p, parameters);

                if (value is null)
                {
                    value = DBNull.Value;
                }

                cmd.Parameters.AddWithValue(p.Name, value);
            }
        }
Example #3
0
        /// <summary>
        /// Creates a new instance
        /// </summary>
        public SqliteDB(string fileName)
        {
            var fi = new FileInfo(fileName);

            if (!fi.Directory.Exists)
            {
                fi.Directory.Create();
            }

            DatabaseFileName = fi.FullName;
            // if now exists, creates one (can be done in the ConnectionString)

            //if (!File.Exists(DatabaseFileName)) SqliteConnection.CreateFile(DatabaseFileName);
            //else backupDatabase();
            if (File.Exists(DatabaseFileName))
            {
                backupDatabase();
            }

            // uses builder to avoid escape issues
            SqliteConnectionStringBuilder sb = new SqliteConnectionStringBuilder
            {
                DataSource = DatabaseFileName,
                //Version = 3
            };

            cnnString      = sb.ToString();
            typeCollection = new ReaderCachedCollection();
            lockNonQuery   = new object();
        }
Example #4
0
 private SqliteDB(SqliteConnectionStringBuilder sb, string databaseFileName)
 {
     DatabaseFileName = databaseFileName;
     cnnString        = sb.ToString();
     typeCollection   = new ReaderCachedCollection();
     lockNonQuery     = new object();
 }
Example #5
0
 /// <summary>
 /// Creates a new instance
 /// </summary>
 public TableMapper(SqliteDB database, ReaderCachedCollection typeCollection)
 {
     db = database;
     this.typeCollection = typeCollection;
     tables = new List <Table>();
 }