From 0c33ac4ba1ed770994323ab00200e2013f4f6a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Nieto?= <xiam@menteslibres.org> Date: Sun, 2 Dec 2012 14:35:34 -0600 Subject: [PATCH] Splitting files. --- db.go | 121 ++-------------------------------------------- item.go | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 117 deletions(-) create mode 100644 item.go diff --git a/db.go b/db.go index 104e258b..b901de2f 100644 --- a/db.go +++ b/db.go @@ -21,15 +21,14 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* +This package is a wrapper of many third party database drivers. The goal of this abstraction is to provide a common, +simplified and consistent layer for working with different databases without the need of SQL statements. +*/ package db import ( "fmt" - "github.com/gosexy/sugar" - "regexp" - "strconv" - "strings" - "time" ) // Handles conditions and operators in an expression. @@ -220,118 +219,6 @@ type MultiFlag bool type SqlValues []string type SqlArgs []string -// Returns the item value as a string. -func (item Item) GetString(name string) string { - return fmt.Sprintf("%v", item[name]) -} - -// Returns the item value as a Go date. -func (item Item) GetDate(name string) time.Time { - date := time.Date(0, time.January, 0, 0, 0, 0, 0, time.UTC) - - switch item[name].(type) { - case time.Time: - date = item[name].(time.Time) - case string: - var matched bool - value := item[name].(string) - - matched, _ = regexp.MatchString(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$`, value) - - if matched { - date, _ = time.Parse("2006-01-02 15:04:05", value) - } - } - return date -} - -// Returns the item value as a Go duration. -func (item Item) GetDuration(name string) time.Duration { - duration, _ := time.ParseDuration("0h0m0s") - - switch item[name].(type) { - case time.Duration: - duration = item[name].(time.Duration) - case string: - var matched bool - var re *regexp.Regexp - value := item[name].(string) - - matched, _ = regexp.MatchString(`^\d{2}:\d{2}:\d{2}$`, value) - - if matched { - re, _ = regexp.Compile(`^(\d{2}):(\d{2}):(\d{2})$`) - all := re.FindAllStringSubmatch(value, -1) - - formatted := fmt.Sprintf("%sh%sm%ss", all[0][1], all[0][2], all[0][3]) - duration, _ = time.ParseDuration(formatted) - } - } - return duration -} - -// Returns the item value as a Tuple. -func (item Item) GetTuple(name string) sugar.Tuple { - tuple := sugar.Tuple{} - - switch item[name].(type) { - case map[string]interface{}: - for k, _ := range item[name].(map[string]interface{}) { - tuple[k] = item[name].(map[string]interface{})[k] - } - case sugar.Tuple: - tuple = item[name].(sugar.Tuple) - } - - return tuple -} - -// Returns the item value as an array. -func (item Item) GetList(name string) sugar.List { - list := sugar.List{} - - switch item[name].(type) { - case []interface{}: - list = make(sugar.List, len(item[name].([]interface{}))) - - for k, _ := range item[name].([]interface{}) { - list[k] = item[name].([]interface{})[k] - } - } - - return list -} - -// Returns the item value as an integer. -func (item Item) GetInt(name string) int64 { - i, _ := strconv.ParseInt(fmt.Sprintf("%v", item[name]), 10, 64) - return i -} - -// Returns the item value as a floating point number. -func (item Item) GetFloat(name string) float64 { - f, _ := strconv.ParseFloat(fmt.Sprintf("%v", item[name]), 64) - return f -} - -// Returns the item value as a boolean. -func (item Item) GetBool(name string) bool { - - if item[name] == nil { - return false - } - - switch item[name].(type) { - default: - b := strings.ToLower(fmt.Sprintf("%v", item[name])) - if b == "" || b == "0" || b == "false" { - return false - } - } - - return true -} - var wrappers = make(map[string]Database) /* diff --git a/item.go b/item.go new file mode 100644 index 00000000..f0f211c3 --- /dev/null +++ b/item.go @@ -0,0 +1,145 @@ +/* + Copyright (c) 2012 JosĂŠ Carlos Nieto, http://xiam.menteslibres.org/ + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +package db + +import ( + "fmt" + "github.com/gosexy/sugar" + "regexp" + "strconv" + "strings" + "time" +) + +// Returns the item value as a string. +func (item Item) GetString(name string) string { + return fmt.Sprintf("%v", item[name]) +} + +// Returns the item value as a Go date. +func (item Item) GetDate(name string) time.Time { + date := time.Date(0, time.January, 0, 0, 0, 0, 0, time.UTC) + + switch item[name].(type) { + case time.Time: + date = item[name].(time.Time) + case string: + var matched bool + value := item[name].(string) + + matched, _ = regexp.MatchString(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$`, value) + + if matched { + date, _ = time.Parse("2006-01-02 15:04:05", value) + } + } + return date +} + +// Returns the item value as a Go duration. +func (item Item) GetDuration(name string) time.Duration { + duration, _ := time.ParseDuration("0h0m0s") + + switch item[name].(type) { + case time.Duration: + duration = item[name].(time.Duration) + case string: + var matched bool + var re *regexp.Regexp + value := item[name].(string) + + matched, _ = regexp.MatchString(`^\d{2}:\d{2}:\d{2}$`, value) + + if matched { + re, _ = regexp.Compile(`^(\d{2}):(\d{2}):(\d{2})$`) + all := re.FindAllStringSubmatch(value, -1) + + formatted := fmt.Sprintf("%sh%sm%ss", all[0][1], all[0][2], all[0][3]) + duration, _ = time.ParseDuration(formatted) + } + } + return duration +} + +// Returns the item value as a Tuple. +func (item Item) GetTuple(name string) sugar.Tuple { + tuple := sugar.Tuple{} + + switch item[name].(type) { + case map[string]interface{}: + for k, _ := range item[name].(map[string]interface{}) { + tuple[k] = item[name].(map[string]interface{})[k] + } + case sugar.Tuple: + tuple = item[name].(sugar.Tuple) + } + + return tuple +} + +// Returns the item value as an array. +func (item Item) GetList(name string) sugar.List { + list := sugar.List{} + + switch item[name].(type) { + case []interface{}: + list = make(sugar.List, len(item[name].([]interface{}))) + + for k, _ := range item[name].([]interface{}) { + list[k] = item[name].([]interface{})[k] + } + } + + return list +} + +// Returns the item value as an integer. +func (item Item) GetInt(name string) int64 { + i, _ := strconv.ParseInt(fmt.Sprintf("%v", item[name]), 10, 64) + return i +} + +// Returns the item value as a floating point number. +func (item Item) GetFloat(name string) float64 { + f, _ := strconv.ParseFloat(fmt.Sprintf("%v", item[name]), 64) + return f +} + +// Returns the item value as a boolean. +func (item Item) GetBool(name string) bool { + + if item[name] == nil { + return false + } + + switch item[name].(type) { + default: + b := strings.ToLower(fmt.Sprintf("%v", item[name])) + if b == "" || b == "0" || b == "false" { + return false + } + } + + return true +} -- GitLab