Пример #1
0
func CreateDriveTable(tx dbutil.DBLike) error {
	stmt, err := tx.Prepare(`



CREATE TABLE Drive (

    ID INTEGER PRIMARY KEY,

    Name VARCHAR(255),

    SerialNumber VARCHAR(255)

);

`)
	if err != nil {
		return err
	}

	_, err = stmt.Exec()
	if err != nil {
		return err
	}
	return nil
}
Пример #2
0
func (s *Drive) Delete(tx dbutil.DBLike) error {
	stmt, err := tx.Prepare("DELETE FROM Drive WHERE ID = ?")
	if err != nil {
		return err
	}

	_, err = stmt.Exec(s.ID)
	if err != nil {
		return err
	}

	return nil
}
Пример #3
0
func (s *Drive) Insert(tx dbutil.DBLike) error {
	stmt, err := tx.Prepare("INSERT INTO Drive(Name,SerialNumber) VALUES(?,?)")
	if err != nil {
		return err
	}

	result, err := stmt.Exec(s.Name, s.SerialNumber)
	if err != nil {
		return err
	}

	s.ID, err = result.LastInsertId()
	if err != nil {
		return err
	}
	return nil
}
Пример #4
0
func (s *Drive) Update(tx dbutil.DBLike) error {
	stmt, err := tx.Prepare(fmt.Sprintf("UPDATE Drive(%s) VALUES(?,?,?) WHERE Drive.ID = ?", sqlFieldsForDrive()))

	if err != nil {
		return err
	}

	params := []interface{}{s.ID, s.Name, s.SerialNumber}
	params = append(params, s.ID)

	_, err = stmt.Exec(params...)
	if err != nil {
		return err
	}

	return nil
}
Пример #5
0
func Select(tx dbutil.DBLike, cond string, condFields ...interface{}) ([]*Drive, error) {
	ret := []*Drive{}
	sql := fmt.Sprintf("SELECT %s from Drive %s", sqlFieldsForDrive(), cond)
	rows, err := tx.Query(sql, condFields...)
	if err != nil {
		return nil, err
	}
	for rows.Next() {
		item, err := loadDrive(rows)
		if err != nil {
			return nil, err
		}
		ret = append(ret, item)
	}
	rows.Close()
	return ret, nil
}
Пример #6
0
func (s *File) Insert(tx dbutil.DBLike) error {
	stmt, err := tx.Prepare("INSERT INTO File(DriveID,Path,Hash,Thumbnail,Size,CreatedAt,ToBeDeleted) VALUES(?,?,?,?,?,?,?)")
	if err != nil {
		return err
	}

	result, err := stmt.Exec(s.DriveID, s.Path, s.Hash, s.Thumbnail, s.Size, s.CreatedAt, s.ToBeDeleted)
	if err != nil {
		return err
	}

	s.ID, err = result.LastInsertId()
	if err != nil {
		return err
	}
	return nil
}
Пример #7
0
func (s *File) Update(tx dbutil.DBLike) error {
	stmt, err := tx.Prepare(fmt.Sprintf("UPDATE File(%s) VALUES(?,?,?,?,?,?,?,?) WHERE File.ID = ?", sqlFieldsForFile()))

	if err != nil {
		return err
	}

	params := []interface{}{s.ID, s.DriveID, s.Path, s.Hash, s.Thumbnail, s.Size, s.CreatedAt, s.ToBeDeleted}
	params = append(params, s.ID)

	_, err = stmt.Exec(params...)
	if err != nil {
		return err
	}

	return nil
}
Пример #8
0
func CreateFileTable(tx dbutil.DBLike) error {
	stmt, err := tx.Prepare(`



CREATE TABLE File (

    ID INTEGER PRIMARY KEY,

    DriveID INTEGER,

    Path VARCHAR(255),

    Hash VARCHAR(255),

    Thumbnail VARCHAR(255),

    Size INTEGER,

    CreatedAt DATETIME,

    ToBeDeleted BOOLEAN

);

`)
	if err != nil {
		return err
	}

	_, err = stmt.Exec()
	if err != nil {
		return err
	}
	return nil
}