Ejemplo n.º 1
0
        private void SaveKeywords(long Id, string Keywords)
        {
            if (Keywords != null) {
            string[] keys = Keywords.Split(',');

            foreach (string key in keys) {
              string lowCaseKey = key.ToLower();
              int keyword_id = -1;

              using (var db = new DbManager(Manager.DbCon)) {
            string sql = "SELECT id FROM `keywords` WHERE keyword=@keyword";
            db.QueryReader(sql, DB.Param("keyword", lowCaseKey));
            if (db.DataReader.HasRows) {
              db.DataReader.Read();
              keyword_id = (int)db.DataReader.GetValue(0);
            }
              }

              if (keyword_id == -1) {
            using (var dbinner = new DbManager(Manager.DbCon)) {
              long tmpkwdid;
              string sql = "INSERT INTO keywords (keyword) VALUES (@kwd)";
              dbinner.QueryInsert(out tmpkwdid, sql,
                                                DB.Param("kwd", lowCaseKey));
              keyword_id = (int)tmpkwdid;
            }
              }

              using (var dbinners = new DbManager(Manager.DbCon)) {
            long tmpid;
            string sql = "INSERT INTO keyword_rel (file_id,keyword_id) " +
                         "VALUES (@fileid,@keyid)";
            dbinners.QueryInsert(out tmpid, sql,
                                 DB.Param("fileid", Id),
                                 DB.Param("keyid", keyword_id));
              }
            }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Save to database. Inserts if new, updates otherwise
        /// </summary>
        /// <returns>
        /// A <see cref="System.Boolean"/>
        /// </returns>
        public bool Save()
        {
            if (Id == 0) {
                string sql = "INSERT INTO `file` (name, fullname, directory_id,"       +
                             " mimetype, title, description, copyright, width, height,"+
                             " size, resolution, exif, created, modified, keywords, "  +
                             " sha1_hash) "                                            +
                             "VALUES (@name, @fullname, @directory_id, @mimetype,"     +
                             " @title, @description, @copyright, @width, @height,"     +
                             " @size, @resolution, @exif, @created, @modified, "       +
                             " @keywords, @sha1_hash)";

                long tmpid;
                object mddate = null;
                if (Modified != Manager.NullDate)
                    mddate = Modified;
                var db = new DbManager(Manager.DbCon);
                if (db.QueryInsert(out tmpid, sql,
                                   DB.Param("name", Name),
                                   DB.Param("fullname", FullName),
                                   DB.Param("directory_id", DirectoryId),
                                   DB.Param("mimetype", Mimetype),
                                   DB.Param("title", Title),
                                   DB.Param("description", Description),
                                   DB.Param("copyright", Copyright),
                                   DB.Param("width", Width),
                                   DB.Param("height", Height),
                                   DB.Param("size", Size),
                                   DB.Param("resolution", Resolution),
                                   DB.Param("exif", Exif),
                                   DB.Param("created", Created),
                                   DB.Param("modified", mddate),
                                   DB.Param("keywords", Keywords),
                                                     DB.Param("sha1_hash", Sha1Hash)))
                {
                    Id = tmpid;

              SaveKeywords(Id, Keywords);

                    // Update the file index in Manager
                    Manager.AddToFileIndex(FullName);

                    foreach (PreviewFile f in Previews) {
                        f.FileId = Id;
                        if (!f.Save()) {
                            Log.Warning("  >>> Failed inserting preview \"{0}\" for " +
                                        "\"{1}\"\n", f.Name, FullName);
                        }
                    }

                    db.Dispose();
                    return true;
                }
                else {
                    Log.Debug("Failed inserting {0} into database!\n", FullName);
                    db.Dispose();
                    return false;
                }
            }
            else {
                // To regenerate hash
                sha1Hash = null;

                DB.Query("DELETE FROM `preview` WHERE file_id=@id",
                                 DB.Param("id", Id));

                string sql = "UPDATE `file` SET "           +
                                         " sha1_hash=@sha1_hash,"       +
                                         " name=@name,"                 +
                                         " fullname=@fullname,"         +
                                         " directory_id=@directory_id," +
                                         " mimetype=@mimetype,"         +
                                         " title=@title,"               +
                                         " description=@description,"   +
                                         " copyright=@copyright,"       +
                                         " width=@width,"               +
                                         " height=@height,"             +
                                         " resolution=@resolution,"     +
                                         " exif=@exif,"                 +
                                         " modified=@modified,"         +
                                         " keywords=@keywords "         +
                                         "WHERE id=@id";

                if (DB.Query(sql, DB.Param("sha1_hash", Sha1Hash),
                                  DB.Param("name", Name),
                                  DB.Param("fullname", FullName),
                                  DB.Param("directory_id", DirectoryId),
                                                    DB.Param("mimetype", Mimetype),
                                                    DB.Param("title", Title),
                                                    DB.Param("description", Description),
                                                    DB.Param("copyright", Copyright),
                                                    DB.Param("width", Width),
                                                    DB.Param("height", Height),
                                                    DB.Param("resolution", Resolution),
                                                    DB.Param("exif", Exif),
                                                    DB.Param("modified", Modified),
                                                    DB.Param("keywords", Keywords),
                                                    DB.Param("id", Id)))
                 {
              if (Previews.Count == 0) {
            // var fi = new FileInfo(FullName);
            // MediaType mt = Manager.GetMediaType(fi);
            // FileHandler fh = Manager.GetFileHandler(fi, mt);
            // fh.
              }
                    foreach (PreviewFile f in Previews) {
                        f.FileId = Id;
                        if (!f.Save()) {
                            Log.Warning("  >>> Failed inserting preview \"{0}\" for " +
                                                    "\"{1}\"\n", f.Name, FullName);
                        }
                    }
                    return true;
                }

                Log.Debug("^^^ Failed updating {0}\n", FullName);
                return false;
            }
        }