Ejemplo n.º 1
0
func NewReaderDB(conn *sqlite3.Conn) *ReaderDB {
	var err error

	r := &ReaderDB{conn: conn}

	r.selectSymbDecl, err = conn.Prepare(`
        SELECT st.name, st.unisr, f2.path, st.line, st.col
            FROM symbol_decls st, symbol_uses su, files f1, files f2
            WHERE
                -- symbol use and symbol declaration join
                su.dec_file = st.file AND
                su.dec_line = st.line AND
                su.dec_col = st.col AND
                -- symbol declaration to file join
                f2.id = st.file AND
                -- symbol use and file join
                su.file = f1.id AND
                -- select input
                f1.path = ? AND su.line = ? AND su.col = ?;
	`)
	if err != nil {
		log.Panic("prepare select symbol ", err)
	}

	r.selectSymbUses, err = conn.Prepare(`
        SELECT f2.path, su2.line, su2.col
            FROM files f1, files f2, symbol_uses su1, symbol_uses su2
            WHERE
                -- symbol use and files join
                f1.id = su1.file AND
                f2.id = su2.file AND
                -- symbol uses with same declaration
                su1.dec_file = su2.dec_file AND
                su1.dec_line = su2.dec_line AND
                su1.dec_col = su2.dec_col AND
                -- select input
                f1.path = ? AND su1.line = ? AND su1.col = ?;
	`)
	if err != nil {
		log.Panic("prepare select symbol uses ", err)
	}

	r.selectSymbDef, err = conn.Prepare(`
	SELECT f1.path, fdd.def_line, fdd.def_col
	    FROM files f1, files f2, symbol_decls sd, symbol_uses su,
	         func_decs_defs fdd
	    WHERE
	        -- symbol decls and files join
		f1.id = fdd.def_file AND
		-- symbol use and files join
		f2.id = su.file AND
		-- symbol uses and symbol decls join
		su.dec_file = sd.file AND
		su.dec_line = sd.line AND
		su.dec_col = sd.col AND
		-- symbol decls and symbol defs join
		sd.file = fdd.dec_file AND
		sd.line = fdd.dec_line AND
		sd.col = fdd.dec_col AND
		-- select input
		f2.path = ? AND su.line = ? AND su.col = ?;
	`)
	if err != nil {
		log.Panic("prepare select symbol def ", err)
	}

	r.selectSymbDefs, err = conn.Prepare(`
	SELECT f1.path, sd1.line, sd1.col
	    FROM files f1, files f2, symbol_decls sd1, symbol_decls sd2,
	         symbol_uses su
	    WHERE
	        -- symbol decls and files join
		f1.id = sd1.file AND
		-- file and symbol uses join
		f2.id = su.file AND
		-- symbol uses and symbol decls join
		su.dec_file = sd2.file AND
		su.dec_line = sd2.line AND
		su.dec_col = sd2.col AND
		-- symbol decls and symbol decls same USR
		sd1.unisr = sd2.unisr AND
		sd1.def = 1 AND
		-- select input
		f2.path = ? AND su.line = ? AND su.col = ?;
	`)
	if err != nil {
		log.Panic("prepare select symbol defs ", err)
	}

	return r
}
Ejemplo n.º 2
0
func NewWriterDB(conn *sqlite3.Conn) *WriterDB {
	var err error

	r := &WriterDB{conn: conn}

	// DB selects

	r.selectFileInfo, err = conn.Prepare(`
        SELECT file_info FROM files WHERE path = ?;
	`)
	if err != nil {
		log.Panic("prepare select hash ", err)
	}

	r.selectFileDeps, err = conn.Prepare(`
        SELECT f1.path FROM files f1, files f2, files_deps d
	WHERE f2.path = ? AND f2.id = d.depend AND f1.id = d.id;
	`)
	if err != nil {
		log.Panic("prepare select hash ", err)
	}

	// DB inserts

	r.insertFile, err = conn.Prepare(`
        INSERT INTO files(path, file_info) VALUES (?, ?);
	`)
	if err != nil {
		log.Panic("prepare insert files ", err)
	}

	r.insertSymb, err = conn.Prepare(`
        INSERT OR IGNORE INTO symbol_decls(name, unisr, file, line, col, param)
            SELECT ?, ?, id, ?, ?, ? FROM files
            WHERE path = ?;
	`)
	if err != nil {
		log.Panic("prepare insert symbol ", err)
	}

	r.insertFuncDef, err = conn.Prepare(`
        INSERT OR IGNORE INTO symbol_decls(name, unisr, file, line, col, def)
            SELECT ?, ?, id, ?, ?, 1 FROM files
            WHERE path = ?;
	`)
	if err != nil {
		log.Panic("prepare insert func def ", err)
	}

	r.insertFuncDecDef, err = conn.Prepare(`
        INSERT OR IGNORE INTO func_decs_defs
            SELECT f1.id, ?, ?, f2.id, ?, ? FROM files f1, files f2
            WHERE f1.path = ? AND f2.path = ?;
	`)
	if err != nil {
		log.Panic("prepare insert func dec/def ", err)
	}

	r.insertSymbUse, err = conn.Prepare(`
        INSERT OR IGNORE INTO symbol_uses
            SELECT 0, f1.id, ?, ?, f2.id, ?, ? FROM files f1, files f2
                WHERE f1.path = ? AND f2.path = ?;
	`)
	if err != nil {
		log.Panic("preapre insert symbol use ", err)
	}

	r.insertFuncCall, err = conn.Prepare(`
        INSERT OR REPLACE INTO symbol_uses
            SELECT 1, f1.id, ?, ?, f2.id, ?, ? FROM files f1, files f2
                WHERE f1.path = ? AND f2.path = ?;
	`)
	if err != nil {
		log.Panic("preapre insert func call ", err)
	}

	r.insertDepend, err = conn.Prepare(`
	INSERT OR IGNORE INTO files_deps
	    SELECT f1.id, f2.id FROM files f1, files f2
	        WHERE f1.path = ? AND f2.path = ?;
	`)
	if err != nil {
		log.Panic("prepare insert files deps ", err)
	}

	// DB (only) delete

	r.delFileRef, err = conn.Prepare(`
        DELETE FROM files WHERE path = ?;
	`)
	if err != nil {
		log.Panic("prepare delete file ", err)
	}

	r.delDepends, err = conn.Prepare(`
	DELETE FROM files WHERE id IN (
	    SELECT d.id FROM files_deps d, files f
	        WHERE f.path = ? AND d.depend = f.id
	);
	`)
	if err != nil {
		log.Panic("prepare delete dependencies ", err)
	}

	return r
}