//
// Generated by PHiLe v{version}
// Copyright (C) 2017, {authors}
//

package {namespace}

import "time"
import "gopkg.in/mgo.v2"

type _ time.Time

type {ctxtype} struct {{
    Session *mgo.Session
    Db      *mgo.Database
}}

func OpenCtx(connStr string) ({ctxtype}, error) {{
    session, err := mgo.Dial(connStr)
    if err != nil {{
        return {ctxtype} {{ nil, nil }}, err
    }}

    db := session.DB("")
    {ctxname} := {ctxtype} {{
        Session: session,
        Db:      db,
    }}

    return {ctxname}, nil
}}

func ({ctxname} {ctxtype}) CloseCtx() {{
    {ctxname}.Session.Close()
    {ctxname}.Db = nil
}}

func ({ctxname} {ctxtype}) CreateSchema(dropDb bool) error {{
    if dropDb {{
        if err := {ctxname}.Db.DropDatabase(); err != nil {{
            return err
        }}
    }}

    // In MongoDB, collections are created lazily upon first insertion.
    // Therefore, the actual creation of collections is a no-op here.
    return nil
}}

//
// Top-level Queries
//

