forked from 0c34/govwa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
148 additions
and
124 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,67 @@ | ||
package database | ||
|
||
import( | ||
"fmt" | ||
"log" | ||
import ( | ||
"database/sql" | ||
"github.com/govwa/util/config" | ||
"fmt" | ||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/govwa/util/config" | ||
"log" | ||
) | ||
|
||
func Connect()(*sql.DB, error){ | ||
func Connect() (*sql.DB, error) { | ||
|
||
config := config.LoadConfig() | ||
|
||
var dsn string | ||
var db *sql.DB | ||
|
||
dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/", config.User,config.Password,config.Sqlhost,config.Sqlport) | ||
db, err := sql.Open("mysql",dsn) | ||
dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/", config.User, config.Password, config.Sqlhost, config.Sqlport) | ||
db, err := sql.Open("mysql", dsn) | ||
|
||
if err != nil{ | ||
return nil,err | ||
if err != nil { | ||
return nil, err | ||
} | ||
_,err = db.Exec("CREATE DATABASE IF NOT EXISTS "+config.Dbname) | ||
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS " + config.Dbname) | ||
|
||
if err != nil{ | ||
return nil,err | ||
}else{ | ||
if err != nil { | ||
return nil, err | ||
} else { | ||
|
||
dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", config.User,config.Password,config.Sqlhost,config.Sqlport,config.Dbname) | ||
db, err = sql.Open("mysql",dsn) | ||
dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", config.User, config.Password, config.Sqlhost, config.Sqlport, config.Dbname) | ||
db, err = sql.Open("mysql", dsn) | ||
|
||
if err != nil{ | ||
return nil,err | ||
if err != nil { | ||
return nil, err | ||
|
||
} | ||
} | ||
|
||
return db,nil | ||
return db, nil | ||
} | ||
|
||
var DB *sql.DB | ||
func CheckDatabase()(bool, error){ | ||
|
||
func CheckDatabase() (bool, error) { | ||
|
||
/* this function use to check if no database selected and will redirect to setup page */ | ||
|
||
DB, err := Connect() | ||
if err != nil{ | ||
log.Printf("Connection Error %s ",err.Error()) | ||
if err != nil { | ||
log.Printf("Connection Error %s ", err.Error()) | ||
} | ||
|
||
const ( | ||
checksql = `SELECT 1 FROM Users limit 1` //this will check if Table dbname.Users exist otherways will redirect to setup page | ||
checksql = `SELECT 1 FROM Users limit 1` //this will check if Table dbname.Users exist otherways will redirect to setup page | ||
) | ||
result, err := DB.Exec(checksql) | ||
|
||
if err != nil{ | ||
if err != nil { | ||
log.Println(err.Error()) | ||
return false,err | ||
return false, err | ||
} | ||
if result == nil{ | ||
if result == nil { | ||
return false, err | ||
} | ||
log.Println(result) | ||
return true, nil | ||
} | ||
} |
Oops, something went wrong.