Created
May 30, 2020 04:27
-
-
Save SnowyPainter/4d38abd1cd679fca9013818bf2928b9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
//CREATE TABLE | |
db.CreateTable("account", true, map[string]string{ | |
"email": "TEXT", | |
"date": "TEXT", | |
"amount": "INTEGER", | |
}) | |
//INSERT | |
db.InsertColumn("account", map[string]interface{}{ | |
"email": "[email protected]", | |
"date": "20219-23-2", | |
"amount": 30, | |
}) | |
//SELECT | |
rows, _ := db.Query("SELECT * FROM account") | |
a := AccountStructure{} | |
for rows.Next() { | |
fatalError(rows.Scan(StructScanable(&a.name, &a.email)...)) | |
fmt.Println(" Email : ", a.Email, " Amount : ", a.Amount) | |
} | |
*/ | |
import ( | |
"database/sql" | |
"errors" | |
"fmt" | |
"strings" | |
_ "github.com/mattn/go-sqlite3" | |
) | |
//ErrDBNotOpened is one of errors of SQLite | |
var ErrDBNotOpened error = errors.New("SQLite Object is not opened") | |
//SQLite is cover-ment object of sql | |
type SQLite struct { | |
db *sql.DB | |
statement *sql.Stmt | |
isOpened bool | |
} | |
//IsOpened checks the sqlite was opened | |
func (db *SQLite) IsOpened() bool { | |
if db.isOpened { | |
return true | |
} | |
return false | |
} | |
//OpenSQLite return sqlite pointer | |
func OpenSQLite(database string) (*SQLite, error) { | |
db, err := sql.Open("sqlite3", database) | |
if err != nil { | |
return nil, err | |
} | |
return &SQLite{ | |
db: db, | |
statement: nil, | |
isOpened: true, | |
}, nil | |
} | |
//CreateTable creates a table | |
func (db *SQLite) CreateTable(name string, existCheck bool, fields map[string]string) (err error) { | |
if !db.IsOpened() { | |
return ErrDBNotOpened | |
} | |
merged, i := make([]string, len(fields)), 0 | |
for field, attr := range fields { | |
merged[i] = fmt.Sprintf("%s %s", field, attr) | |
i++ | |
} | |
query := fmt.Sprintf("CREATE TABLE %s %s (%s);", | |
boolTernary(existCheck, "IF NOT EXISTS", "\b"), name, strings.Join(merged, ",")) | |
db.statement, err = db.db.Prepare(query) | |
if err != nil { | |
return err | |
} | |
return ignoreFirstNErrorCheck(db.statement.Exec()) | |
} | |
//InsertColumn inserts data which contains fields and data into table | |
func (db *SQLite) InsertColumn(table string, data map[string]interface{}) (err error) { | |
//mapKeys := reflect.ValueOf(data).MapKeys() | |
keys := make([]string, len(data)) | |
vals := make([]interface{}, len(data)) | |
i := 0 | |
for k, v := range data { | |
keys[i] = k | |
vals[i] = v | |
i++ | |
} | |
query := fmt.Sprintf("INSERT INTO %s (%v) VALUES (%s);", table, | |
strings.Join(keys, ","), listChar(",", "?", len(data))) | |
db.statement, err = db.db.Prepare(query) | |
if err != nil { | |
return err | |
} | |
return ignoreFirstNErrorCheck(db.statement.Exec(vals...)) | |
} | |
//Query is same with sql.DB.Query | |
func (db *SQLite) Query(raw string) (*sql.Rows, error) { | |
return db.db.Query(raw) | |
} | |
func listChar(distChar string, char string, length int) (result string) { | |
for i := 0; i < length; i++ { | |
if i == 0 { | |
result += char | |
continue | |
} | |
result += distChar + char | |
} | |
return | |
} | |
func boolTernary(value bool, valueTrue interface{}, valueFalse interface{}) interface{} { | |
if value { | |
return valueTrue | |
} | |
return valueFalse | |
} | |
func ignoreFirstNErrorCheck(ignored interface{}, err error) error { | |
if err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment