func (c *Cursor) ResetFetch() error { var e error c.Connection().(*Connection).Close() e = c.Connection().(*Connection).Connect() if e != nil { return errorlib.Error(packageName, modCursor, "Restart Connection", e.Error()) } c.headerColumn = c.Connection().(*Connection).headerColumn c.file = c.Connection().(*Connection).file c.reader = c.Connection().(*Connection).reader e = c.prepIter() if e != nil { return errorlib.Error(packageName, modCursor, "ResetFetch", e.Error()) } // c.PrepareCursor() // if e != nil { // return errorlib.Error(packageName, modCursor, "Prepare Cursor", e.Error()) // } return nil }
func (q *Query) HasPartExec() error { var jsonString []byte var err error if q.hasNewSave { jsonString, err = json.MarshalIndent(q.sliceData, "", " ") if err != nil { return errorlib.Error(packageName, modQuery+".Exec", "Has part exec Marshal JSON", err.Error()) } } else if q.hasSave { lastJson := q.Connection().(*Connection).getJsonToMap var allJsonData toolkit.Ms allJsonData = append(lastJson, q.sliceData...) jsonString, err = json.MarshalIndent(allJsonData, "", " ") if err != nil { return errorlib.Error(packageName, modQuery+".Exec", "Has part exec Marshal JSON", err.Error()) } } err = ioutil.WriteFile(q.Connection().(*Connection).filePath, jsonString, 0666) if err != nil { return errorlib.Error(packageName, modQuery+".Exec", "Write file", err.Error()) } return nil }
func Search(s SshSetting, path string, isParseble bool, search string) (string, error) { if path == "" { return "", errorlib.Error("", "", "SEARCH", "Path is Undivined") } if search == "" { return "", errorlib.Error("", "", "SEARCH", "Search param is undivined") } cmd := "" if isParseble { cmd = fmt.Sprintf(SEARCH_PARAM, path, search) } else { cmd = fmt.Sprintf(SEARCH, path, search) } res, e := s.GetOutputCommandSsh(cmd) if e != nil { e = errorlib.Error("", "", "SEARCH", e.Error()) } return res, e }
func (c *Connection) Connect() error { c.Close() ci := c.Info() if ci == nil || ci.Host == "" { return errorlib.Error(packageName, modConnection, "Connect", "No connection info") } _, e := os.Stat(ci.Host) if ci.Settings != nil { if ci.Settings["newfile"] == true { if os.IsNotExist(e) { create, _ := os.Create(ci.Host) create.Close() } } else { if os.IsNotExist(e) { return errorlib.Error(packageName, modConnection, "Connect", "Create new file is false") } } } else if os.IsNotExist(e) { return errorlib.Error(packageName, modConnection, "Connect", "No json file found") } c.filePath = ci.Host defaultPath, fileName, sep := c.GetBaseFilepath() c.basePath = defaultPath c.baseFileName = fileName c.separator = sep return nil }
func List(s SshSetting, path string, isParseble bool) (string, error) { if path == "" { return "", errorlib.Error("", "", "LIST", "Path is Undivined") } cmd := "" if isParseble { cmd = fmt.Sprintf(LIST_PARAM, path) } else { cmd = fmt.Sprintf(LIST, path) } res, e := s.GetOutputCommandSsh(cmd) if e != nil { e = errorlib.Error("", "", "LIST", e.Error()) } startStr := strings.Index(res, "\n") if res != "" { return res[startStr:], e } return res, e }
func Chmod(s SshSetting, path string, permission string, format bool) (e error) { if format { permission, e = constructPermission(permission) if e != nil { return errorlib.Error("", "", "CHMOD", e.Error()) } } if path == "" { return errorlib.Error("", "", "CHMOD", "Path is Undivined") } if permission == "" { return errorlib.Error("", "", "CHMOD", "Permission is Undivined") } cmd := fmt.Sprintf(CHMOD, permission, path) _, e = s.GetOutputCommandSsh(cmd) if e != nil { return errorlib.Error("", "", "CHMOD", e.Error()) } return e }
func Remove(s SshSetting, recursive bool, paths ...string) map[string]error { var es map[string]error if len(paths) < 1 { es[""] = errorlib.Error("", "", "REMOVE", "Paths is Undivined") return es } for _, path := range paths { cmd := "" if recursive { cmd = fmt.Sprintf(REMOVE_RECURSIVE, path) } else { cmd = fmt.Sprintf(REMOVE, path) } _, e := s.GetOutputCommandSsh(cmd) if e != nil { if es == nil { es = map[string]error{} } es[path] = errorlib.Error("", "", "REMOVE", e.Error()) } } return es }
func MakeFile(s SshSetting, content string, path string, permission string, format bool) error { if path == "" { return errorlib.Error("", "", "MAKEFILE", "Path is Undivined") } cmd := fmt.Sprintf(MKFILE, content, path) _, e := s.GetOutputCommandSsh(cmd) if e != nil { return errorlib.Error("", "", "MAKEFILE", e.Error()) } if permission == "" { permission = "755" format = false } e = Chmod(s, path, permission, format) if e != nil { return errorlib.Error("", "", "MAKEFILE", e.Error()) } return e }
func (c *Connection) OpenSession() error { c.Close() t, e := os.OpenFile(c.filePath, os.O_RDWR, 0) if e != nil { return errorlib.Error(packageName, modConnection, "Open File", e.Error()) } c.openFile = t i, e := ioutil.ReadFile(c.filePath) if e != nil { return errorlib.Error(packageName, modQuery+".Exec", "Read file", e.Error()) } var hoomanJson []toolkit.M e = toolkit.Unjson(i, &hoomanJson) if e != nil { return errorlib.Error(packageName, modQuery+".Exec", "Cannot Unjson", e.Error()) } c.lines = 0 c.sData = "" c.sameId = false if len(hoomanJson) == 0 { c.isNewSave = true } else { c.getJsonToMap = hoomanJson } return nil }
func (c *Connection) RdbmsConnect(drivername string, stringConnection string) error { if drivername == "hive" { connInfo := strings.Split(stringConnection, ",") c.Hive = hive.HiveConfig(connInfo[0], connInfo[1], connInfo[2], connInfo[3], connInfo[4], connInfo[5]) c.Drivername = drivername c.Hive.Conn.Open() e := c.Hive.Conn.TestConnection() if e != nil { return err.Error(packageName, modConnection, "Connect", e.Error()) } } else { sqlcon, e := sql.Open(drivername, stringConnection) if e != nil { return err.Error(packageName, modConnection, "Connect", e.Error()) } c.Sql = *sqlcon c.Drivername = drivername e = sqlcon.Ping() if e != nil { return err.Error(packageName, modConnection, "Connect", e.Error()) } } if c.Info().Settings.Has("dateformat") { c.DateFormat = toolkit.ToString(c.Info().Settings.Get("dateformat", "")) } return nil }
func (l *ConfigListBase) Load() error { e := l.Self().Validate() if e != nil { return e } if l.Self().NewItem() == nil { return err.Error(packageName, objConfigList, "Load", "No implementation of NewItem()") } fis, e := ioutil.ReadDir(l.configFolder) if e != nil { return err.Error(packageName, objConfigList, "Load", e.Error()) } l.Inititems() for _, fi := range fis { item := l.Self().NewItem() e = NewFromFile(filepath.Join(l.configFolder, fi.Name()), item) if e == nil { l.Set(item) } } return nil }
func Chown(s SshSetting, path string, user string, group string, recursive bool) error { if path == "" { return errorlib.Error("", "", "CHOWN", "Path is Undivined") } if user == "" { return errorlib.Error("", "", "CHOWN", "User is Undivined") } if group == "" { return errorlib.Error("", "", "CHOWN", "Group is Undivined") } cmd := "" if recursive { cmd = fmt.Sprintf(CHOWN_RECURSIVE, user, group, path) } else { cmd = fmt.Sprintf(CHOWN, user, group, path) } _, e := s.GetOutputCommandSsh(cmd) if e != nil { return errorlib.Error("", "", "CHOWN", e.Error()) } return e }
func (d *DataContext) setConnectionFromConfigFile(name string) error { d.ConnectionName = name if d.ConnectionName == "" { d.ConnectionName = fmt.Sprintf("Default") } connType := strings.ToLower(config.Get("Connection_" + d.ConnectionName + "_Type").(string)) host := config.Get("Connection_" + d.ConnectionName + "_Host").(string) username := config.Get("Connection_" + d.ConnectionName + "_Username").(string) password := config.Get("Connection_" + d.ConnectionName + "_Password").(string) database := config.Get("Connection_" + d.ConnectionName + "_database").(string) ci := new(dbox.ConnectionInfo) ci.Host = host ci.UserName = username ci.Password = password ci.Database = database conn, eConnect := dbox.NewConnection(connType, ci) if eConnect != nil { return err.Error(packageName, modCtx, "SetConnectionFromConfigFile", eConnect.Error()) } if eConnect = conn.Connect(); eConnect != nil { return err.Error(packageName, modCtx, "SetConnectionFromConfigFile", eConnect.Error()) } d.Connection = conn return nil }
func (q *Query) Cursor(in toolkit.M) (dbox.ICursor, error) { var cursor *Cursor setting, e := q.prepare(in) if e != nil { return nil, err.Error(packageName, modQuery, "Cursor", e.Error()) } commandtype := setting.GetString("commandtype") if commandtype != dbox.QueryPartSelect { return nil, err.Error(packageName, modQuery, "Cursor", "Cursor is only working with select command, for "+commandtype+" please use .Exec instead") } e = q.openFile() if e != nil { return nil, err.Error(packageName, modQuery, "Cursor", e.Error()) } //toolkit.Printf("Data count: %d \n", len(q.data)) cursor = newCursor(q) where := setting.Get("where", []*dbox.Filter{}).([]*dbox.Filter) if len(where) > 0 { cursor.where = where cursor.indexes = dbox.Find(q.data, where) } return cursor, nil }
func (q *Query) openFile() error { if q.fileHasBeenOpened { return nil } _, e := os.Stat(q.jsonPath) if e != nil && (strings.Contains(e.Error(), "does not exist") || strings.Contains(e.Error(), "no such file or directory")) { q.data = []toolkit.M{} return nil } else if e != nil { return err.Error(packageName, modQuery, "openFile: Open file fail", e.Error()) } bs, e := ioutil.ReadFile(q.jsonPath) if e != nil { return err.Error(packageName, modQuery, "openFile: Read file data fail", e.Error()) } jsonText := string(bs) var tempData []toolkit.M e = toolkit.UnjsonFromString(jsonText, &tempData) if e != nil { return err.Error(packageName, modQuery, "openFile: Serializaion fail", e.Error()) } q.data = tempData q.fileHasBeenOpened = true return nil }
func (q *Query) Cursor(in toolkit.M) (dbox.ICursor, error) { var cursor *Cursor setting, e := q.prepare(in) if e != nil { return nil, err.Error(packageName, modQuery, "Cursor", e.Error()) } if setting.GetString("commandtype") != dbox.QueryPartSelect { return nil, err.Error(packageName, modQuery, "Cursor", "Cursor is only working with select command, please use .Exec instead") } e = q.openFile() if e != nil { return nil, err.Error(packageName, modQuery, "Cursor", e.Error()) } cursor = newCursor(q) where := setting.Get("where", []*dbox.Filter{}).([]*dbox.Filter) // toolkit.Println("LINE 56 : ", where) cursor.skip = setting.Get("skip", 0).(int) cursor.limit = setting.Get("take", 0).(int) cursor.fields = setting.Get("fields", toolkit.M{}).(toolkit.M) // if len(where) > 0 { cursor.where = where cursor.indexes, e = q.generateIndex(where) if e != nil { return nil, err.Error(packageName, modQuery, "Cursor", e.Error()) } // } return cursor, nil }
func (c *Cursor) Fetch(m interface{}, n int, closeWhenDone bool) error { if closeWhenDone { defer c.Close() } e := c.prepIter() if e != nil { return errorlib.Error(packageName, modCursor, "Fetch", e.Error()) } if c.mgoIter == nil { return errorlib.Error(packageName, modCursor, "Fetch", "Iter object is not yet initialized") } if !IsPointer(m) { return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer") } //ds := dbox.NewDataSet(m) // var datas []interface{} // rt := reflect.TypeOf(m) //datias := reflect.MakeSlice(rt, 0, 0) if n == 0 { //datas := []interface{}{} e = c.mgoIter.All(m) if e != nil { return errorlib.Error(packageName, modCursor, "Fetch", e.Error()) } //ds.Data = datas } else if n == 1 { c.mgoIter.Next(m) } else if n > 1 { fetched := 0 fetching := true for fetching { dataHolder, e := GetEmptySliceElement(m) if e != nil { return errorlib.Error(packageName, modCursor, "Fetch", e.Error()) } var bOk bool if IsPointer(dataHolder) { bOk = c.mgoIter.Next(dataHolder) } else { bOk = c.mgoIter.Next(&dataHolder) } if bOk { AppendSlice(m, dataHolder) fetched++ if fetched == n { fetching = false } } else { fetching = false } } } return nil }
func (q *Query) execQueryPartUpdate(dt toolkit.M, Cond QueryCondition) error { if len(dt) == 0 { return errorlib.Error(packageName, "Query", modQuery, "data to update is not found") } writer := q.Connection().(*Connection).writer reader := q.Connection().(*Connection).reader tempHeader := []string{} for _, val := range q.Connection().(*Connection).headerColumn { tempHeader = append(tempHeader, val.name) } for { foundChange := false recData := toolkit.M{} dataTemp, e := reader.Read() for i, val := range dataTemp { recData.Set(tempHeader[i], val) } if len(Cond.Find) > 0 || (len(Cond.Find) == 0 && toolkit.IdField(dt) == "") { foundChange = Cond.getCondition(recData) } // Check ID IF Condition Not Found if nameid := toolkit.IdField(dt); nameid != "" && !foundChange { if recData.Has(nameid) && dt[nameid] == recData[nameid] { foundChange = true } } if foundChange && len(dataTemp) > 0 { for n, v := range tempHeader { if dt.Has(v) { dataTemp[n] = cast.ToString(dt[v]) } } } if e == io.EOF { if dataTemp != nil { writer.Write(dataTemp) writer.Flush() } break } else if e != nil { return errorlib.Error(packageName, modQuery, "Update", e.Error()) } if dataTemp != nil { writer.Write(dataTemp) writer.Flush() } } return nil }
func (q *Query) Cursor(in toolkit.M) (dbox.ICursor, error) { var cursor *Cursor setting, e := q.prepare(in) if e != nil { return nil, err.Error(packageName, modQuery, "Cursor", e.Error()) } commandtype := setting.GetString("commandtype") if commandtype != dbox.QueryPartSelect { return nil, err.Error(packageName, modQuery, "Cursor", "Cursor is only working with select command, for "+commandtype+" please use .Exec instead") } e = q.openFile(commandtype) if e != nil { return nil, err.Error(packageName, modQuery, "Cursor", e.Error()) } cursor = newCursor(q) skip := 0 if skip = setting.Get("skip").(int); skip > 0 { cursor.skip = skip } take := 0 if take = setting.Get("take").(int); take > 0 { cursor.take = take } if sort := setting.Get("sort").([]string); toolkit.SliceLen(sort) > 0 { fb := new(json.FilterBuilder) sorter := fb.SortFetch(sort, q.data) q.data = sorter } cursor.jsonSelect = setting.Get("fields").([]string) var count int count = toolkit.SliceLen(q.data) where := setting.Get("where", []*dbox.Filter{}).([]*dbox.Filter) if len(where) > 0 { cursor.where = where cursor.indexes = dbox.Find(q.data, where) count = toolkit.SliceLen(cursor.indexes) } if count <= skip { count = 0 } else { count -= skip } if count >= take && take > 0 { count = take } cursor.count = count return cursor, nil }
func (c *Connection) Execute(stmt string, parms M) (int, error) { var e error if parms == nil { parms = M{} } pooling := parms.Get("pooling", false).(bool) var sess *mgo.Session var coll *mgo.Collection if pooling { sess = c.mses coll = c.mses.DB(c.Database).C(stmt) } else { sess, coll = c.CopySession(stmt) defer sess.Close() } //sess = c.mses //coll := c.mdb.C(stmt) var op DB_OP ok := true if val, ok := parms["operation"]; !ok { return 0, err.Error(packageName, modConnection, "Execute", "Invalid operation in parms") } else { op = val.(DB_OP) } var data interface{} if data, ok = parms["data"]; !ok && op != DB_DELETE { return 0, err.Error(packageName, modConnection, "Execute", "Data is not valid") } if op == DB_INSERT { e = coll.Insert(data) if e != nil { return 0, err.Error(packageName, modConnection, "Execute - Insert", e.Error()) } } else { find, _ := parms["find"] if op == DB_SAVE { _, e = coll.Upsert(find, data) } else if op == DB_UPDATE { e = coll.Update(find, data) } else if op == DB_DELETE { _, e = coll.RemoveAll(find) } else { op = DB_UKNOWN } //_ = "breakpoint" if e != nil { return 0, err.Error(packageName, modConnection, "Execute - "+string(op), e.Error()) } } return 0, nil }
func (s *Sequence) Save() error { if Ctx == nil { return errorlib.Error(packageName, objSequence, "Save", "Context not yet initialized") } e := Ctx.Save(s) if e != nil { return errorlib.Error(packageName, objSequence, "Save", e.Error()) } return nil }
func (s *Sequence) ChangeNumberStatus(n int, status NumberStatus) error { if Ctx == nil { return errorlib.Error(packageName, objSequence, "ChangeNumberStatus", "Context not yet initialized") } used := NewUsedSequence(s.Id, n, status) e := Ctx.Save(used) if e != nil { return errorlib.Error(packageName, objSequence, "ChangeNumberStatus", e.Error()) } return nil }
func NewConnection(connector string, ci *ConnectionInfo) (IConnection, error) { if connectors == nil { return nil, errorlib.Error(packageName, "", "NewConnection", "Invalid connector") } fn, found := connectors[connector] if found == false { return nil, errorlib.Error(packageName, "", "NewConnection", "Invalid connector") } return fn(ci) }
func (l *ConfigListBase) Validate() error { if _, e := os.Stat(l.configFolder); e != nil { if os.IsNotExist(e) { return err.Error(packageName, objConfigList, "Validate", fmt.Sprintf("Directory %s is not exitst", l.configFolder)) } else { return err.Error(packageName, objConfigList, "Validate", e.Error()) } } return nil }
func (q *Query) Statement() (toolkit.M, error) { toolkit.Println(q.QStatement) out := toolkit.M{} tableData := toolkit.Ms{} fieldName := []string{} q.DateFormat = q.Connection().(*Connection).dateFormat // stmt, e := q.Connection().(*Connection).OdbcCon.Prepare(query) stmt, e := q.Connection().(*Connection).Sess.Prepare(q.QStatement) if e != nil { return nil, err.Error(packageName, modQuery, "statement", e.Error()) } defer stmt.Close() e = stmt.Execute() if e != nil { return nil, err.Error(packageName, modQuery, "statement", e.Error()) } rows, e := stmt.FetchAll() if e != nil { return nil, err.Error(packageName, modQuery, "statement", e.Error()) } nfields, e := stmt.NumFields() if e != nil { return nil, err.Error(packageName, modQuery, "statement", e.Error()) } for i := 0; i < nfields; i++ { field, e := stmt.FieldMetadata(i + 1) if e != nil { return nil, err.Error(packageName, modQuery, "statement", e.Error()) } fieldName = append(fieldName, field.Name) } for _, row := range rows { entry := toolkit.M{} for i := 0; i < len(row.Data); i++ { data := q.DataType(row.Data[i]) entry.Set(fieldName[i], data) } tableData = append(tableData, entry) } out.Set("count", len(rows)) out.Set("data", tableData) return out, nil }
func (q *Query) execQueryPartUpdate(dt toolkit.M) error { var e error e = q.startWriteMode() if e != nil { return err.Error(packageName, modQuery, "Exec-Update: ", e.Error()) } writer := q.writer reader := q.reader tempHeader := []string{} for _, val := range q.headerColumn { tempHeader = append(tempHeader, val.name) } var i int = 0 for { i += 1 dataTemp, e := reader.Read() if toolkit.HasMember(q.indexes, i) && len(dataTemp) > 0 { for n, v := range tempHeader { if dt.Has(v) { dataTemp[n] = cast.ToString(dt[v]) } } } if e == io.EOF { if len(dataTemp) > 0 { writer.Write(dataTemp) writer.Flush() } break } else if e != nil { _ = q.endWriteMode() return err.Error(packageName, modQuery, "Exec-Update:", e.Error()) } if len(dataTemp) > 0 { writer.Write(dataTemp) writer.Flush() } } q.execOpr = true e = q.endWriteMode() if e != nil { return err.Error(packageName, modQuery, "Exec-Update: ", e.Error()) } return nil }
func (c *Connection) Connect() error { if c.Folder == "" { return err.Error(packageName, modConnection, "Connect", "Folder path is empty") } _, e := os.Stat(c.Folder) if e != nil { return err.Error(packageName, modConnection, "Connect", e.Error()) } return nil }
func Cat(s SshSetting, path string) (string, error) { if path == "" { return "", errorlib.Error("", "", "CAT", "Path is Undivined") } cmd := fmt.Sprintf(CAT, path) res, e := s.GetOutputCommandSsh(cmd) if e != nil { e = errorlib.Error("", "", "CAT", e.Error()) } return res, e }
func (c *Connection) CloseWriteSession() error { c.Close() eRem := os.Remove(c.filePath) if eRem != nil { return errorlib.Error(packageName, modQuery+".Exec", "Close Write Session", eRem.Error()) } eRen := os.Rename(c.basePath+c.separator+"temp_"+c.baseFileName, c.filePath) if eRen != nil { return errorlib.Error(packageName, modQuery+".Exec", "Close Write Session", eRen.Error()) } return nil }
func (c *Cursor) ResetFetch() error { var e error e = c.resetConnection() if e != nil { return errorlib.Error(packageName, modCursor, "Reset Fetch", e.Error()) } if len(c.ConditionVal.where) > 0 { e = c.generateIndexes() if e != nil { return errorlib.Error(packageName, modCursor, "Reset Fetch", e.Error()) } } return nil }