Ejemplo n.º 1
0
func ExampleNewBackup() {
	dst, err := sqlite.Open(":memory:")
	check(err)
	defer dst.Close()
	src, err := sqlite.Open(":memory:")
	check(err)
	defer src.Close()
	err = src.Exec("CREATE TABLE test (content BLOB); INSERT INTO test VALUES (zeroblob(100))")
	check(err)

	bck, err := sqlite.NewBackup(dst, "main", src, "main")
	check(err)
	defer bck.Close()

	cbs := make(chan sqlite.BackupStatus)
	ack := make(chan bool)
	go func() {
		for {
			s := <-cbs
			fmt.Printf("backup progress (remaining: %d)\n", s.Remaining)
			ack <- true
		}
	}()
	err = bck.Run(100, 250000, cbs)
	check(err)
	<-ack
	// Output: backup progress (remaining: 0)
}
Ejemplo n.º 2
0
func ExampleOpen() {
	db, err := sqlite.Open(":memory:")
	check(err)
	defer db.Close()
	fmt.Printf("db path: %q\n", db.Filename("main"))
	// Output: db path: ""
}
Ejemplo n.º 3
0
func ExampleStmt_Scan() {
	db, err := sqlite.Open(":memory:")
	check(err)
	defer db.Close()

	s, err := db.Prepare("SELECT 1 as id, 'Go' as name, 'Y' as status UNION SELECT 2, 'SQLite', 'yes'")
	check(err)
	defer s.Finalize()

	var id int
	var name string
	var status bool

	converter := func(value interface{}) (bool, error) {
		status = value == "Y" || value == "yes"
		return false, nil
	}

	err = s.Select(func(s *sqlite.Stmt) (err error) {
		if err = s.Scan(&id, &name, converter); err != nil {
			return
		}
		fmt.Println(id, name, status)
		return
	})
	// Output: 1 Go true
	// 2 SQLite true
}
Ejemplo n.º 4
0
func init() {
	var err error

	db, err = sql.Open("database.db")
	if err != nil {
		panic(err)
	}

	err = db.Exec(`
	create table if not exists story (
		story integer not null primary key,
		date integer not null
	);

	create table if not exists storyversion (
		story integer not null references story(story),
		lang text not null,
		title text not null,
		summary text not null,
		content text not null,
		primary key(story, lang)
	);
	
	create table if not exists user (
		username text not null primary key,
		passhash text not null
	);`)

	if err != nil {
		panic(err)
	}
}
Ejemplo n.º 5
0
func ExampleConn_Exec() {
	db, err := sqlite.Open(":memory:")
	check(err)
	defer db.Close()

	err = db.Exec("CREATE TABLE test1 (content TEXT); CREATE TABLE test2 (content TEXT); INSERT INTO test1 VALUES ('DATA')")
	check(err)
	tables, err := db.Tables("")
	check(err)
	fmt.Printf("%d tables\n", len(tables))
	// Output: 2 tables
}
Ejemplo n.º 6
0
func ExampleStmt_ExecDml() {
	db, err := sqlite.Open(":memory:")
	check(err)
	defer db.Close()
	err = db.Exec("CREATE TABLE test (content TEXT); INSERT INTO test VALUES ('Go'); INSERT INTO test VALUES ('SQLite')")
	check(err)

	s, err := db.Prepare("UPDATE test SET content = content || 'lang' where content like ?")
	check(err)
	defer s.Finalize()
	changes, err := s.ExecDml("%o")
	check(err)
	fmt.Printf("%d change(s)\n", changes)
	// Output: 1 change(s)
}
Ejemplo n.º 7
0
func ExampleConn_NewBlobReader() {
	db, err := sqlite.Open(":memory:")
	check(err)
	err = db.Exec("CREATE TABLE test (content BLOB); INSERT INTO test VALUES (zeroblob(10))")
	check(err)
	rowid := db.LastInsertRowid()

	br, err := db.NewBlobReader("main", "test", "content", rowid)
	check(err)
	defer br.Close()
	size, err := br.Size()
	check(err)
	// TODO A real 'incremental' example...
	content := make([]byte, size)
	n, err := br.Read(content)
	check(err)
	fmt.Printf("blob (size: %d, read: %d, content: %q\n", size, n, content)
	// Output: blob (size: 10, read: 10, content: "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
}
Ejemplo n.º 8
0
func ExampleStmt_Insert() {
	db, err := sqlite.Open(":memory:")
	check(err)
	defer db.Close()
	err = db.Exec("CREATE TABLE test (content TEXT)")
	check(err)

	s, err := db.Prepare("INSERT INTO test VALUES (?)")
	check(err)
	defer s.Finalize()
	data := []string{"Go", "SQLite", "Driver"}
	for _, d := range data {
		rowId, err := s.Insert(d)
		check(err)
		fmt.Printf("%d\n", rowId)
	}
	// Output: 1
	// 2
	// 3
}
Ejemplo n.º 9
0
func ExampleStmt_NamedScan() {
	db, err := sqlite.Open(":memory:")
	check(err)
	defer db.Close()

	s, err := db.Prepare("SELECT 1 as id, 'Go' as name UNION SELECT 2, 'SQLite'")
	check(err)
	defer s.Finalize()

	var id int
	var name string
	err = s.Select(func(s *sqlite.Stmt) (err error) {
		if err = s.NamedScan("name", &name, "id", &id); err != nil {
			return
		}
		fmt.Println(id, name)
		return
	})
	// Output: 1 Go
	// 2 SQLite
}
Ejemplo n.º 10
0
func (s *store) serve() error {
	db, err := sqlite.Open(s.path)
	if err != nil {
		return err
	}
	defer db.Close()
	err = db.Exec("create table if not exists feed(id string primary key, type string, json string)")
	if err != nil {
		return err
	}
	err = db.Exec("create table if not exists item(id string primary key, json string)")
	if err != nil {
		return err
	}
	s.conn = db
	for {
		select {
		case c := <-s.PutFeedChan:
			c.Response <- s.putFeed(c.Feed)
		case r := <-s.GetFeedChan:
			r.Response <- s.getFeed(r.Id)
		case c := <-s.GetUserChan:
			c.Response <- s.getUser(c.User)
		case c := <-s.GetTypeChan:
			c.Response <- s.getType(c.Type)
		case c := <-s.GetInfoChan:
			c.Response <- s.getInfo()
		case <-s.CloseChan:
			close(s.PutFeedChan)
			close(s.GetFeedChan)
			close(s.GetUserChan)
			close(s.GetTypeChan)
			close(s.CloseChan)
			return nil
		}
	}
	return nil
}
Ejemplo n.º 11
0
func Example() {
	db, err := sqlite.Open(":memory:") // path to db or "" for temp db
	check(err)
	defer db.Close()
	err = db.Exec("CREATE TABLE test(id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL UNIQUE); -- ... and other ddls separated by semi-colon")
	check(err)
	ins, err := db.Prepare("INSERT INTO test (name) VALUES (?)")
	check(err)
	defer ins.Finalize()
	_, err = ins.Insert("gosqlite driver")
	check(err)
	s, err := db.Prepare("SELECT name from test WHERE name like ?", "%go%")
	check(err)
	defer s.Finalize()
	var name string
	err = s.Select(func(s *sqlite.Stmt) (err error) {
		if err = s.Scan(&name); err != nil {
			return
		}
		fmt.Printf("%s\n", name)
		return
	})
	// Output: gosqlite driver
}
Ejemplo n.º 12
0
//===================================
func main() {
	sep := "\\" //filepath.Separator
	fi, err := ioutil.ReadDir(os.ExpandEnv(BACKUP_PATH))

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if len(fi) == 0 {
		fmt.Println("Please sync your iPhone using iTunes first.")
		os.Exit(1)
	}

	m_path := os.ExpandEnv(BACKUP_PATH + string(sep) + fi[0].Name() + string(sep) + SMS_FILE)
	m_conn, err := sqlite.Open(m_path)
	if err != nil {
		fmt.Println("Unable to open the Message database: %s", err)
		os.Exit(1)
	}
	defer m_conn.Close()

	ab_path := os.ExpandEnv(BACKUP_PATH + string(sep) + fi[0].Name() + string(sep) + AB_FILE)
	ab_conn, err := sqlite.Open(ab_path)
	if err != nil {
		fmt.Println("Unable to open the AddressBook database: %s", err)
		os.Exit(1)
	}
	defer ab_conn.Close()

	// Gather all of the contacts up front.
	cmap := make(utility.SMap)
	ab_stmt, err := ab_conn.Prepare("select First,Middle,Last,Nickname,value from ABPerson p LEFT JOIN ABMultiValue a ON p.ROWID = a.record_id;")
	defer ab_stmt.Finalize()
	//ab_stmt.Exec()
	for {
		more, _ := ab_stmt.Next()
		if more {
			contact := &Contact{}
			ab_stmt.Scan(&contact.First, &contact.Middle, &contact.Last, &contact.Nickname, &contact.Address)
			if err != nil {
				fmt.Printf("Error while getting AddressBook row data: %s\n", err)
				os.Exit(1)
			}

			contact.Process()
			cmap.Insert(contact, contact)
		} else {
			break
		}
	}

	//v := reflect.TypeOf(Message{})
	//fmt.Printf("%s, %s\n", v.NumField(), v.Field(0).Name)

	m_stmt, err := m_conn.Prepare("select address,text,flags,date,is_madrid,madrid_flags,madrid_handle,madrid_date_delivered,madrid_date_read from message ORDER BY rowid")
	defer m_stmt.Finalize()
	//err = m_stmt.Exec()
	// if err != nil {
	//   fmt.Println("Error while selecting: %s", err)
	// }

	tmpl, _ := template.ParseFiles("output.tmpl")

	var messages Messages
	var lastMessage Message
	var currentPath string
	var f *os.File
	for {
		more, _ := m_stmt.Next()
		var message Message

		if more {
			m_stmt.Scan(
				&message.Address,
				&message.Text,
				&message.Flags,
				&message.Date,
				&message.Is_madrid,
				&message.Madrid_flags,
				&message.Madrid_handle,
				&message.Madrid_date_delivered,
				&message.Madrid_date_read)

			if err != nil {
				fmt.Printf("Error while getting Message row data: %s\n", err)
				//os.Exit(1)
			}

			if len(message.Address) == 0 && len(message.Madrid_handle) == 0 {
				continue
			}

			message.Process()

			faddress := format_address(message.Address)
			contact, ok := cmap[faddress]
			if ok {
				//fmt.Println(contact.Value.(*Contact))
				message.Contact = *contact.Value.(*Contact)
			} else {
				message.Contact = *&Contact{Identifier: faddress}
			}
		}

		if !more || message.Address != lastMessage.Address || (len(lastMessage.TimeDate) != 0 && message.TimeDate != lastMessage.TimeDate) {
			currentPath = filepath.Join(OUTPUT_DIR, message.Contact.Identifier)
			os.MkdirAll(currentPath, 0666)

			if f != nil {
				tmpl.Execute(f, messages)
				f.Close()
			}

			if more {
				f, err = os.OpenFile(filepath.Join(currentPath, message.TimeDate+".txt"), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
				messages.Data = nil
			}
		}

		messages.Data = append(messages.Data, message)
		lastMessage = message

		if !more {
			break
		}

		//		results, err := m_stmt.ResultsAsMap()
		//    if err == nil {
		//		  fmt.Printf("%v\n", string(results["text"]))
		//		}
	}

	defer f.Close()
}