func FindEntry(word string) (*Entry, error) { var e Entry db := zhDicts.Db() // Find Entry eRow, err := db.Query("SELECT * FROM md_entries WHERE title = '" + word + "'") if err != nil { return nil, err } defer eRow.Close() var entry_id int if eRow.Next() { var radical sql.NullString eRow.Scan(&entry_id, &e.Title, &radical, &e.Stroke_count, &e.Non_radical_stroke_count) if radical.Valid { e.Radical = radical.String } else { e.Radical = "" } } else { return nil, nil } e.Heteronyms, err = findHeteronyms(entry_id) if err != nil { return nil, err } return &e, nil }
// FindRecords searches the database of cedict records and returns a slice of type // []Record and an error. It returns an empty slice if no matches could be found. func FindRecords(word string, charSet chinese.CharSet) ([]Record, error) { // construct db query based on charSet sql := "SELECT * FROM cedict " switch charSet { case chinese.Trad: sql += "WHERE trad LIKE '" + word + "'" case chinese.Simp: sql += "WHERE simp LIKE '" + word + "'" default: return nil, fmt.Errorf("cedict: unrecognized character set") } // execute the query and defer closing it rows, err := zhDicts.Db().Query(sql) if err != nil { return nil, err } defer rows.Close() // create slice to hold records records := make([]Record, 0) // populate records with the data from the db query for rows.Next() { var id, runecount int var trad, simp, pinyin, english string rows.Scan(&id, &trad, &simp, &pinyin, &english, &runecount) records = append(records, Record{Trad: trad, Simp: simp, Pinyin: pinyin, English: english}) } return records, nil }
func findHeteronyms(entry_id int) ([]Heteronym, error) { db := zhDicts.Db() hs := make([]Heteronym, 0) // Find heteronyms hRows, err := db.Query(fmt.Sprintf("SELECT id, pinyin, bopomofo, bopomofo2 FROM md_heteronyms WHERE entry_id = %d ORDER BY idx", entry_id)) if err != nil { return nil, err } defer hRows.Close() for hRows.Next() { var h Heteronym var heteronym_id int var pinyin, bopomofo, bopomofo2 sql.NullString hRows.Scan(&heteronym_id, &pinyin, &bopomofo, &bopomofo2) if pinyin.Valid { h.Pinyin = pinyin.String } else { h.Pinyin = "" } if bopomofo.Valid { h.Bopomofo = bopomofo.String } else { h.Bopomofo = "" } if bopomofo2.Valid { h.Bopomofo2 = bopomofo2.String } else { h.Bopomofo2 = "" } h.Definitions, err = findDefinitions(heteronym_id) if err != nil { return nil, err } hs = append(hs, h) } return hs, nil }
func Initialize() error { db := zhDicts.Db() if db == nil { return fmt.Errorf("Database not loaded") } // get max runecount sqlMaxRunecount := "SELECT MAX(runecount) AS maxRunecount FROM cedict" rows, err := db.Query(sqlMaxRunecount) if err != nil { return err } defer rows.Close() rows.Next() rows.Scan(&maxRunecount) return nil }
func findDefinitions(heteronym_id int) ([]Definition, error) { db := zhDicts.Db() ds := make([]Definition, 0) dRows, err := db.Query(fmt.Sprintf("SELECT def, quotes, examples, type, link, synonyms, antonyms FROM md_definitions WHERE heteronym_id = %d ORDER BY idx", heteronym_id)) if err != nil { return nil, err } defer dRows.Close() for dRows.Next() { var d Definition var def, quotes, examples, defType, links, synonyms, antonyms sql.NullString dRows.Scan(&def, "es, &examples, &defType, &links, &synonyms, &antonyms) if def.Valid { d.Def = def.String } else { d.Def = "" } if quotes.Valid { d.Quote = strings.Split(quotes.String, "|||") } else { d.Quote = make([]string, 0) } if examples.Valid { d.Example = strings.Split(examples.String, "|||") } else { d.Example = make([]string, 0) } if defType.Valid { d.DefType = defType.String } else { d.DefType = "" } if links.Valid { d.Link = strings.Split(links.String, "|||") } else { d.Link = make([]string, 0) } if synonyms.Valid { d.Synonyms = synonyms.String } else { d.Synonyms = "" } if antonyms.Valid { d.Antonyms = antonyms.String } else { d.Antonyms = "" } ds = append(ds, d) } return ds, nil }