Skip to content

Commit

Permalink
Rebranding refactoring, adding color to tags
Browse files Browse the repository at this point in the history
  • Loading branch information
bykovme committed Jul 3, 2022
1 parent 0507984 commit 9e8a5c0
Show file tree
Hide file tree
Showing 20 changed files with 141 additions and 128 deletions.
4 changes: 2 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (sdb *storageDB) Open(filePath string) error {
sdb.dbOpen = false

if _, err = os.Stat(filePath); err == nil {
// File exists, open and check integrity
// File exists, open and check the integrity
sdb.sDB, err = sql.Open("sqlite3", "file:"+filePath+cDBOpenParms)
if err != nil {
return err
Expand All @@ -79,7 +79,7 @@ func (sdb *storageDB) Open(filePath string) error {
return nil
}

// File does not exists
// File does not exist, create it
sdb.sDB, err = sql.Open("sqlite3", "file:"+filePath+cDBOpenParms)
if err != nil {
return err
Expand Down
46 changes: 22 additions & 24 deletions db_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const sqlInsertField = `
VALUES (?,?,?,?,?,?,?,0)
`

func (sdb *storageDB) dbInsertField(itemID int64, field BSField) (fieldId int64, err error) {
func (sdb *storageDB) dbInsertField(itemID int64, field OxiField) (fieldId int64, err error) {

if sdb.sTX == nil {
return 0, formError(BSERR00003DbTransactionFailed, "dbInsertField")
Expand Down Expand Up @@ -72,7 +72,7 @@ const sqlListItemFields = `
WHERE deleted='0' and item_id=?
`

func (sdb *storageDB) dbSelectAllItemFields(itemId int64) (fields []BSField, err error) {
func (sdb *storageDB) dbSelectAllItemFields(itemId int64) (fields []OxiField, err error) {

rows, err := sdb.sDB.Query(sqlListItemFields, itemId)
if err != nil {
Expand All @@ -85,22 +85,22 @@ func (sdb *storageDB) dbSelectAllItemFields(itemId int64) (fields []BSField, err
}
}()

var bsField BSField
var field OxiField

for rows.Next() {
err = rows.Scan(&bsField.ID,
&bsField.Name,
&bsField.Icon,
&bsField.Value,
&bsField.ValueType,
&bsField.Created,
&bsField.Updated,
&bsField.Deleted)
err = rows.Scan(&field.ID,
&field.Name,
&field.Icon,
&field.Value,
&field.ValueType,
&field.Created,
&field.Updated,
&field.Deleted)
if err != nil {
return fields, err
}

fields = append(fields, bsField)
fields = append(fields, field)
}
return fields, nil
}
Expand Down Expand Up @@ -138,7 +138,7 @@ const sqlGetField = `
WHERE field_id=?
`

func (sdb *storageDB) dbGetFieldById(fieldId int64) (field BSField, err error) {
func (sdb *storageDB) dbGetFieldById(fieldId int64) (field OxiField, err error) {

rows, err := sdb.sDB.Query(sqlGetField, fieldId)
if err != nil {
Expand All @@ -151,18 +151,16 @@ func (sdb *storageDB) dbGetFieldById(fieldId int64) (field BSField, err error) {
}
}()

var bsField BSField

if rows.Next() {
err = rows.Scan(&bsField.ID,
&bsField.Name,
&bsField.Icon,
&bsField.Value,
&bsField.ValueType,
&bsField.Created,
&bsField.Updated,
&bsField.Deleted)
return bsField, err
err = rows.Scan(&field.ID,
&field.Name,
&field.Icon,
&field.Value,
&field.ValueType,
&field.Created,
&field.Updated,
&field.Deleted)
return field, err
}
return field, errors.New(BSERR00021FieldsReadFailed)
}
38 changes: 19 additions & 19 deletions db_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const sqlListItemsWithDeleted = `
FROM items
`

func (sdb *storageDB) dbSelectAllItems(returnDeleted bool) (items []BSItem, err error) {
func (sdb *storageDB) dbSelectAllItems(returnDeleted bool) (items []OxiItem, err error) {
var sqlList string
if returnDeleted {
sqlList = sqlListItemsWithDeleted
Expand All @@ -126,24 +126,24 @@ func (sdb *storageDB) dbSelectAllItems(returnDeleted bool) (items []BSItem, err

}()

var bsItem BSItem
var item OxiItem

for rows.Next() {
err = rows.Scan(&bsItem.ID,
&bsItem.Name,
&bsItem.Icon,
&bsItem.Created,
&bsItem.Updated,
&bsItem.Deleted)
err = rows.Scan(&item.ID,
&item.Name,
&item.Icon,
&item.Created,
&item.Updated,
&item.Deleted)
if err != nil {
return items, err
}

if bsItem.Deleted && !returnDeleted {
if item.Deleted && !returnDeleted {
continue
}

items = append(items, bsItem)
items = append(items, item)
}
return items, nil
}
Expand Down Expand Up @@ -188,7 +188,7 @@ const sqlGetItemByIdWithDeleted = `
WHERE item_id=?
`

func (sdb *storageDB) dbGetItemById(itemId int64, withDeleted bool) (item BSItem, err error) {
func (sdb *storageDB) dbGetItemById(itemId int64, withDeleted bool) (item OxiItem, err error) {
var sqlRequest string
if withDeleted {
sqlRequest = sqlGetItemByIdWithDeleted
Expand Down Expand Up @@ -216,17 +216,17 @@ func (sdb *storageDB) dbGetItemById(itemId int64, withDeleted bool) (item BSItem
}()

if rows.Next() {
var bsItem BSItem
err = rows.Scan(&bsItem.ID,
&bsItem.Name,
&bsItem.Icon,
&bsItem.Created,
&bsItem.Updated,
&bsItem.Deleted)

err = rows.Scan(&item.ID,
&item.Name,
&item.Icon,
&item.Created,
&item.Updated,
&item.Deleted)
if err != nil {
return item, err
}
return bsItem, nil
return item, nil
}
return item, formError(BSERR00019ItemNotFound)
}
Expand Down
8 changes: 4 additions & 4 deletions db_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type SettingInfo struct {

const sqlCreateTableSettings = `
CREATE TABLE IF NOT EXISTS settings (
database_id CHAR PRIMARY KEY NOT NULL,
keyword CHAR NOT NULL,
crypt_id CHAR NOT NULL,
database_version INT NOT NULL,
database_id VARCHAR PRIMARY KEY NOT NULL,
keyword VARCHAR NOT NULL,
crypt_id VARCHAR NOT NULL,
database_version INTEGER NOT NULL,
update_timestamp DATETIME NOT NULL,
sync_timestamp DATETIME NOT NULL
)
Expand Down
42 changes: 23 additions & 19 deletions db_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const sqlCreateTableTags = `
CREATE TABLE IF NOT EXISTS tags (
tag_id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR NOT NULL,
color VARCHAR NOT NULL,
created DATETIME NOT NULL,
updated DATETIME NOT NULL,
deleted BOOLEAN NOT NULL CHECK (deleted IN (0,1)) default '0'
Expand All @@ -19,8 +20,9 @@ const sqlCreateTableTags = `
const sqlCreateTableItemsTags = `
CREATE TABLE IF NOT EXISTS items_tags (
it_id INTEGER PRIMARY KEY AUTOINCREMENT,
item_id INT NOT NULL,
tag_id INT NOT NULL,
item_id INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
created DATETIME NOT NULL,
updated DATETIME NOT NULL,
deleted BOOLEAN NOT NULL CHECK (deleted IN (0,1)) default '0',
FOREIGN KEY (item_id) REFERENCES items(item_id),
Expand All @@ -29,11 +31,11 @@ const sqlCreateTableItemsTags = `
`

const sqlInsertTag = `
INSERT INTO tags (name,created,updated,deleted)
VALUES (?,?,?,0)
INSERT INTO tags (name,color,created,updated,deleted)
VALUES (?,?,?,?,0)
`

func (sdb *storageDB) dbInsertTag(tagName string) (tagId int64, err error) {
func (sdb *storageDB) dbInsertTag(tagName string, color string) (tagId int64, err error) {
if sdb.sTX == nil {
return 0, formError(BSERR00003DbTransactionFailed, "dbInsertTag")
}
Expand All @@ -45,6 +47,7 @@ func (sdb *storageDB) dbInsertTag(tagName string) (tagId int64, err error) {
return 0, formError(BSERR00006DbInsertFailed, err.Error(), "dbInsertTag")
}
res, errStmt := stmt.Exec(tagName,
color,
creationTime,
creationTime)

Expand All @@ -64,8 +67,8 @@ func (sdb *storageDB) dbInsertTag(tagName string) (tagId int64, err error) {
}

const sqlAssignTagToItem = `
INSERT INTO items_tags (item_id, tag_id, updated, deleted)
VALUES (?,?,?,0)
INSERT INTO items_tags (item_id, tag_id, created, updated, deleted)
VALUES (?,?,?,?,0)
`

func (sdb *storageDB) dbAssignTag(tagId int64, itemId int64) (itId int64, err error) {
Expand All @@ -80,7 +83,7 @@ func (sdb *storageDB) dbAssignTag(tagId int64, itemId int64) (itId int64, err er
return 0, formError(BSERR00006DbInsertFailed, err.Error(), "dbAssignTag")
}
res, errStmt := stmt.Exec(itemId, tagId,
creationTime)
creationTime, creationTime)

if errStmt != nil {
return 0, formError(BSERR00006DbInsertFailed, errStmt.Error(), "dbAssignTag")
Expand All @@ -99,7 +102,7 @@ func (sdb *storageDB) dbAssignTag(tagId int64, itemId int64) (itId int64, err er

// sqlListItemTags - List all non-deleted items
const sqlListItemTags = `
SELECT tags.tag_id, tags.name, tags.created, tags.updated, tags.deleted
SELECT tags.tag_id, tags.name, tags.color, tags.created, tags.updated, tags.deleted
FROM tags
INNER JOIN items_tags it on tags.tag_id = it.tag_id
WHERE tags.deleted='0'
Expand All @@ -109,13 +112,13 @@ const sqlListItemTags = `

// sqlListTags - List all available tags (excluding deleted)
const sqlListTags = `
SELECT tag_id, name, created, updated, deleted
SELECT tag_id, name, color, created, updated, deleted
FROM tags
WHERE tags.deleted='0'
`

// dbSelectItemTags - select tags assigned to requested the item
func (sdb *storageDB) dbSelectItemTags(itemId int64) (tags []BSTag, err error) {
func (sdb *storageDB) dbSelectItemTags(itemId int64) (tags []OxiTag, err error) {
var rows *sql.Rows

if itemId == -1 {
Expand All @@ -133,19 +136,20 @@ func (sdb *storageDB) dbSelectItemTags(itemId int64) (tags []BSTag, err error) {
}
}()

var bsTag BSTag
var tag OxiTag

for rows.Next() {
err = rows.Scan(&bsTag.ID,
&bsTag.Name,
&bsTag.Created,
&bsTag.Updated,
&bsTag.Deleted)
err = rows.Scan(&tag.ID,
&tag.Name,
&tag.Color,
&tag.Created,
&tag.Updated,
&tag.Deleted)
if err != nil {
return tags, err
}

tags = append(tags, bsTag)
tags = append(tags, tag)
}
sort.Slice(tags, func(i, j int) bool {
return tags[i].Name < tags[j].Name
Expand All @@ -155,6 +159,6 @@ func (sdb *storageDB) dbSelectItemTags(itemId int64) (tags []BSTag, err error) {
}

// dbSelectTags - select all available tags
func (sdb *storageDB) dbSelectTags() (tags []BSTag, err error) {
func (sdb *storageDB) dbSelectTags() (tags []OxiTag, err error) {
return sdb.dbSelectItemTags(-1)
}
2 changes: 1 addition & 1 deletion db_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const sqlInsertFieldTemplate = `
VALUES (?,?,?,?,?,?,0)
`

func (sdb *storageDB) dbInsertFieldTemplate(itemID int64, field BSField) (fieldId int64, err error) {
func (sdb *storageDB) dbInsertFieldTemplate(itemID int64, field OxiField) (fieldId int64, err error) {

if sdb.sTX == nil {
return 0, formError(BSERR00003DbTransactionFailed, "dbInsertFieldTemplate")
Expand Down
10 changes: 5 additions & 5 deletions bkvstorage_fields.go → oxi_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func (storage *StorageSingleton) DeleteField(deleteFieldForm UpdateFieldForm) (r

// AddNewItem - adds new item
func (storage *StorageSingleton) AddNewField(addFieldForm UpdateFieldForm) (response FieldAddedResponse, err error) {
var field BSField
var field OxiField

err = storage.checkReadiness()
if err != nil {
return response, err
}
if err := ValidateField(addFieldForm.BSField); err != nil {
if err := ValidateField(addFieldForm.OxiField); err != nil {
return response, formError(BSERR00006DbInsertFailed, err.Error())

}
Expand Down Expand Up @@ -88,7 +88,7 @@ func (storage *StorageSingleton) AddNewField(addFieldForm UpdateFieldForm) (resp
}

// ReadFieldsByItemID - real all the fields by ItemId
func (storage *StorageSingleton) ReadFieldsByItemID(itemId int64) (fields []BSField, err error) {
func (storage *StorageSingleton) ReadFieldsByItemID(itemId int64) (fields []OxiField, err error) {
fieldsEncrypted, err := storage.dbObject.dbSelectAllItemFields(itemId)
if err != nil {
return fields, err
Expand All @@ -106,7 +106,7 @@ func (storage *StorageSingleton) ReadFieldsByItemID(itemId int64) (fields []BSFi
}

// AddNewItem - adds new item
func (storage *StorageSingleton) ReadFieldsByFieldID(fieldId int64) (field BSField, err error) {
func (storage *StorageSingleton) ReadFieldsByFieldID(fieldId int64) (field OxiField, err error) {
fieldEncrypted, err := storage.dbObject.dbGetFieldById(fieldId)
if err != nil {
return field, err
Expand All @@ -119,7 +119,7 @@ func (storage *StorageSingleton) ReadFieldsByFieldID(fieldId int64) (field BSFie
return fieldReady, nil
}

func (storage *StorageSingleton) DecryptField(fieldEncrypted BSField) (field BSField, err error) {
func (storage *StorageSingleton) DecryptField(fieldEncrypted OxiField) (field OxiField, err error) {
field.Value, err = storage.encObject.Decrypt(fieldEncrypted.Value)
if err != nil {
return field, err
Expand Down
4 changes: 2 additions & 2 deletions bkvstorage_fields_test.go → oxi_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func testHelperCreateField(itemId int64) (fieldId int64, err error) {
fieldResult, errField := bsInstance.AddNewField(
UpdateFieldForm{
ItemID: itemId,
BSField: BSField{
OxiField: OxiField{
Name: cFieldName01,
Icon: cFieldIcon01,
ValueType: VTText,
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestDeleteField(t *testing.T) {
}

respDel, errDel := bsInstance.DeleteField(UpdateFieldForm{
BSField: BSField{
OxiField: OxiField{
ID: fieldId,
},
})
Expand Down
Loading

0 comments on commit 9e8a5c0

Please sign in to comment.