// SeriesConfig sets up the table and triggers for seriesmeta. noReplace prevents
// series metadata from seriesmeta from replacing existing metadata, and noPersist
// allows the series metadata to be changed by something else later. If uninstall
// is true, the table and triggers will be removed.
SeriesConfig sets up the table and triggers for seriesmeta. noReplace prevents
series metadata from seriesmeta from replacing existing metadata, and noPersist
allows the series metadata to be changed by something else later. If uninstall
is true, the table and triggers will be removed.
// SeriesConfig sets up the table and triggers for seriesmeta . noReplace prevents // series metadata from seriesmeta from replacing existing metadata and noPersist // allows the series metadata to be changed by something else later . If uninstall // is true the table and triggers will be removed .
func (k *Kobo) SeriesConfig(noReplace, noPersist, uninstall bool) error {
	buf := bytes.NewBuffer(nil)
	template.Must(template.New("").Parse(`
		{{if .Uninstall}}
		DROP TABLE _seriesmeta;
		{{else}}
		CREATE TABLE IF NOT EXISTS _seriesmeta (
			ImageId      TEXT NOT NULL UNIQUE,
			Series       TEXT,
			SeriesNumber TEXT,
			PRIMARY KEY(ImageId)
		);
		{{end}}
		/* Adding series metadata on import */
		DROP TRIGGER IF EXISTS _seriesmeta_content_insert;
		{{if not .Uninstall}}
		CREATE TRIGGER _seriesmeta_content_insert
			AFTER INSERT ON content WHEN
				{{if .NoReplace}}(new.Series IS NULL) AND{{end}}
				(new.ImageId LIKE "file____mnt_onboard_%") AND
				(SELECT count() FROM _seriesmeta WHERE ImageId = new.ImageId)
			BEGIN
				UPDATE content
				SET
					Series       = (SELECT Series       FROM _seriesmeta WHERE ImageId = new.ImageId),
					SeriesNumber = (SELECT SeriesNumber FROM _seriesmeta WHERE ImageId = new.ImageId),
					/* Get the SeriesID from books from the Kobo Store (WorkId NOT NULL) where the series name matches, otherwise just use the series name as the SeriesID (https://www.mobileread.com/forums/showthread.php?p=3959768) */
					SeriesID     = coalesce((SELECT SeriesID FROM content WHERE Series = (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId) AND WorkId NOT NULL AND SeriesID NOT NULL AND WorkId != "" AND SeriesID != "" LIMIT 1), (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId))
				WHERE ImageId = new.ImageId;
				{{if .NoPersist}}DELETE FROM _seriesmeta WHERE ImageId = new.ImageId;{{end}}
			END;
		{{end}}
		DROP TRIGGER IF EXISTS _seriesmeta_content_update;
		{{if not .Uninstall}}
		CREATE TRIGGER _seriesmeta_content_update
			AFTER UPDATE ON content WHEN
				{{if .NoReplace}}(new.Series IS NULL) AND{{end}}
				(new.ImageId LIKE "file____mnt_onboard_%") AND
				(SELECT count() FROM _seriesmeta WHERE ImageId = new.ImageId)
			BEGIN
				UPDATE content
				SET
					Series       = (SELECT Series       FROM _seriesmeta WHERE ImageId = new.ImageId),
					SeriesNumber = (SELECT SeriesNumber FROM _seriesmeta WHERE ImageId = new.ImageId),
					/* Get the SeriesID from books from the Kobo Store (WorkId NOT NULL) where the series name matches, otherwise just use the series name as the SeriesID (https://www.mobileread.com/forums/showthread.php?p=3959768) */
					SeriesID     = coalesce((SELECT SeriesID FROM content WHERE Series = (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId) AND WorkId NOT NULL AND SeriesID NOT NULL AND WorkId != "" AND SeriesID != "" LIMIT 1), (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId))
				WHERE ImageId = new.ImageId;
				{{if .NoPersist}}DELETE FROM _seriesmeta WHERE ImageId = new.ImageId;{{end}}
			END;
		{{end}}
		DROP TRIGGER IF EXISTS _seriesmeta_content_delete;
		{{if not .Uninstall}}
		CREATE TRIGGER _seriesmeta_content_delete
			AFTER DELETE ON content
			BEGIN
				DELETE FROM _seriesmeta WHERE ImageId = old.ImageId;
			END;
		{{end}}
		/* Adding series metadata directly when already imported */
		{{if not .Uninstall}}
		DROP TRIGGER IF EXISTS _seriesmeta_seriesmeta_insert;
		CREATE TRIGGER _seriesmeta_seriesmeta_insert
			AFTER INSERT ON _seriesmeta WHEN
				(SELECT count() FROM content WHERE ImageId = new.ImageId)
				{{if .NoReplace}}AND ((SELECT Series FROM content WHERE ImageId = new.ImageId) IS NULL){{end}}
			BEGIN
				UPDATE content
				SET
					Series       = new.Series,
					SeriesNumber = new.SeriesNumber,
					/* Get the SeriesID from books from the Kobo Store (WorkId NOT NULL) where the series name matches, otherwise just use the series name as the SeriesID (https://www.mobileread.com/forums/showthread.php?p=3959768) */
					SeriesID     = coalesce((SELECT SeriesID FROM content WHERE Series = new.Series AND WorkId NOT NULL AND SeriesID NOT NULL AND WorkId != "" AND SeriesID != "" LIMIT 1), new.Series)
				WHERE ImageId = new.ImageId;
				{{if .NoPersist}}DELETE FROM _seriesmeta WHERE ImageId = new.ImageId;{{end}}
			END;
		{{end}}
		DROP TRIGGER IF EXISTS _seriesmeta_seriesmeta_update;
		{{if not .Uninstall}}
		CREATE TRIGGER _seriesmeta_seriesmeta_update
			AFTER UPDATE ON _seriesmeta WHEN
				(SELECT count() FROM content WHERE ImageId = new.ImageId)
				{{if .NoReplace}}AND ((SELECT Series FROM content WHERE ImageId = new.ImageId) IS NULL){{end}}
			BEGIN
				UPDATE content
				SET
					Series       = new.Series,
					SeriesNumber = new.SeriesNumber,
					/* Get the SeriesID from books from the Kobo Store (WorkId NOT NULL) where the series name matches, otherwise just use the series name as the SeriesID (https://www.mobileread.com/forums/showthread.php?p=3959768) */
					SeriesID     = coalesce((SELECT SeriesID FROM content WHERE Series = new.Series AND WorkId NOT NULL AND SeriesID NOT NULL AND WorkId != "" AND SeriesID != "" LIMIT 1), new.Series)
				WHERE ImageId = new.ImageId;
				{{if .NoPersist}}DELETE FROM _seriesmeta WHERE ImageId = new.ImageId;{{end}}
			END;
		{{end}}
	`)).Execute(buf, map[string]interface{}{
		"NoReplace": noReplace,
		"NoPersist": noPersist,
		"Uninstall": uninstall,
	})
	_, err := k.DB.Exec(buf.String())
	return err
}
func ( k * Kobo ) SeriesConfig ( noReplace , noPersist , uninstall bool ) error { buf := bytes . NewBuffer ( nil ) template . Must ( template . New ( " " ) . Parse ( `
		{{if .Uninstall}}
		DROP TABLE _seriesmeta;
		{{else}}
		CREATE TABLE IF NOT EXISTS _seriesmeta (
			ImageId      TEXT NOT NULL UNIQUE,
			Series       TEXT,
			SeriesNumber TEXT,
			PRIMARY KEY(ImageId)
		);
		{{end}}

		/* Adding series metadata on import */

		DROP TRIGGER IF EXISTS _seriesmeta_content_insert;
		{{if not .Uninstall}}
		CREATE TRIGGER _seriesmeta_content_insert
			AFTER INSERT ON content WHEN
				{{if .NoReplace}}(new.Series IS NULL) AND{{end}}
				(new.ImageId LIKE "file____mnt_onboard_%") AND
				(SELECT count() FROM _seriesmeta WHERE ImageId = new.ImageId)
			BEGIN
				UPDATE content
				SET
					Series       = (SELECT Series       FROM _seriesmeta WHERE ImageId = new.ImageId),
					SeriesNumber = (SELECT SeriesNumber FROM _seriesmeta WHERE ImageId = new.ImageId),
					/* Get the SeriesID from books from the Kobo Store (WorkId NOT NULL) where the series name matches, otherwise just use the series name as the SeriesID (https://www.mobileread.com/forums/showthread.php?p=3959768) */
					SeriesID     = coalesce((SELECT SeriesID FROM content WHERE Series = (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId) AND WorkId NOT NULL AND SeriesID NOT NULL AND WorkId != "" AND SeriesID != "" LIMIT 1), (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId))
				WHERE ImageId = new.ImageId;
				{{if .NoPersist}}DELETE FROM _seriesmeta WHERE ImageId = new.ImageId;{{end}}
			END;
		{{end}}

		DROP TRIGGER IF EXISTS _seriesmeta_content_update;
		{{if not .Uninstall}}
		CREATE TRIGGER _seriesmeta_content_update
			AFTER UPDATE ON content WHEN
				{{if .NoReplace}}(new.Series IS NULL) AND{{end}}
				(new.ImageId LIKE "file____mnt_onboard_%") AND
				(SELECT count() FROM _seriesmeta WHERE ImageId = new.ImageId)
			BEGIN
				UPDATE content
				SET
					Series       = (SELECT Series       FROM _seriesmeta WHERE ImageId = new.ImageId),
					SeriesNumber = (SELECT SeriesNumber FROM _seriesmeta WHERE ImageId = new.ImageId),
					/* Get the SeriesID from books from the Kobo Store (WorkId NOT NULL) where the series name matches, otherwise just use the series name as the SeriesID (https://www.mobileread.com/forums/showthread.php?p=3959768) */
					SeriesID     = coalesce((SELECT SeriesID FROM content WHERE Series = (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId) AND WorkId NOT NULL AND SeriesID NOT NULL AND WorkId != "" AND SeriesID != "" LIMIT 1), (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId))
				WHERE ImageId = new.ImageId;
				{{if .NoPersist}}DELETE FROM _seriesmeta WHERE ImageId = new.ImageId;{{end}}
			END;
		{{end}}

		DROP TRIGGER IF EXISTS _seriesmeta_content_delete;
		{{if not .Uninstall}}
		CREATE TRIGGER _seriesmeta_content_delete
			AFTER DELETE ON content
			BEGIN
				DELETE FROM _seriesmeta WHERE ImageId = old.ImageId;
			END;
		{{end}}

		/* Adding series metadata directly when already imported */

		{{if not .Uninstall}}
		DROP TRIGGER IF EXISTS _seriesmeta_seriesmeta_insert;
		CREATE TRIGGER _seriesmeta_seriesmeta_insert
			AFTER INSERT ON _seriesmeta WHEN
				(SELECT count() FROM content WHERE ImageId = new.ImageId)
				{{if .NoReplace}}AND ((SELECT Series FROM content WHERE ImageId = new.ImageId) IS NULL){{end}}
			BEGIN
				UPDATE content
				SET
					Series       = new.Series,
					SeriesNumber = new.SeriesNumber,
					/* Get the SeriesID from books from the Kobo Store (WorkId NOT NULL) where the series name matches, otherwise just use the series name as the SeriesID (https://www.mobileread.com/forums/showthread.php?p=3959768) */
					SeriesID     = coalesce((SELECT SeriesID FROM content WHERE Series = new.Series AND WorkId NOT NULL AND SeriesID NOT NULL AND WorkId != "" AND SeriesID != "" LIMIT 1), new.Series)
				WHERE ImageId = new.ImageId;
				{{if .NoPersist}}DELETE FROM _seriesmeta WHERE ImageId = new.ImageId;{{end}}
			END;
		{{end}}

		DROP TRIGGER IF EXISTS _seriesmeta_seriesmeta_update;
		{{if not .Uninstall}}
		CREATE TRIGGER _seriesmeta_seriesmeta_update
			AFTER UPDATE ON _seriesmeta WHEN
				(SELECT count() FROM content WHERE ImageId = new.ImageId)
				{{if .NoReplace}}AND ((SELECT Series FROM content WHERE ImageId = new.ImageId) IS NULL){{end}}
			BEGIN
				UPDATE content
				SET
					Series       = new.Series,
					SeriesNumber = new.SeriesNumber,
					/* Get the SeriesID from books from the Kobo Store (WorkId NOT NULL) where the series name matches, otherwise just use the series name as the SeriesID (https://www.mobileread.com/forums/showthread.php?p=3959768) */
					SeriesID     = coalesce((SELECT SeriesID FROM content WHERE Series = new.Series AND WorkId NOT NULL AND SeriesID NOT NULL AND WorkId != "" AND SeriesID != "" LIMIT 1), new.Series)
				WHERE ImageId = new.ImageId;
				{{if .NoPersist}}DELETE FROM _seriesmeta WHERE ImageId = new.ImageId;{{end}}
			END;
		{{end}}
	` ) ) . Execute ( buf , map [ string ] interface { } { " " : noReplace , " " : noPersist , " " : uninstall , } ) _ , err := k . DB . Exec ( buf . String ( ) ) return err }
SeriesConfig sets up the table and triggers for seriesmeta.
SeriesConfig sets up the table and triggers for seriesmeta .