-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OG now can connect to mariadb (#1521)
* OG now can connect to mariadb * fix string * fix port * lint * add flag
- Loading branch information
Showing
9 changed files
with
165 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package database | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
_ "github.com/go-sql-driver/mysql" // Importing MariaDB driver | ||
"github.com/obscuronet/go-obscuro/go/common/errutil" | ||
"github.com/obscuronet/go-obscuro/tools/walletextension/common" | ||
) | ||
|
||
type MariaDB struct { | ||
db *sql.DB | ||
} | ||
|
||
// NewMariaDB creates a new MariaDB connection instance | ||
func NewMariaDB(dbURL string) (*MariaDB, error) { | ||
db, err := sql.Open("mysql", dbURL) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect to database: %w", err) | ||
} | ||
|
||
return &MariaDB{db: db}, nil | ||
} | ||
|
||
func (m *MariaDB) AddUser(userID []byte, privateKey []byte) error { | ||
stmt, err := m.db.Prepare("REPLACE INTO users(user_id, private_key) VALUES (?, ?)") | ||
if err != nil { | ||
return err | ||
} | ||
defer stmt.Close() | ||
|
||
_, err = stmt.Exec(userID, privateKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *MariaDB) DeleteUser(userID []byte) error { | ||
stmt, err := m.db.Prepare("DELETE FROM users WHERE user_id = ?") | ||
if err != nil { | ||
return err | ||
} | ||
defer stmt.Close() | ||
|
||
_, err = stmt.Exec(userID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *MariaDB) GetUserPrivateKey(userID []byte) ([]byte, error) { | ||
var privateKey []byte | ||
err := m.db.QueryRow("SELECT private_key FROM users WHERE user_id = ?", userID).Scan(&privateKey) | ||
if err != nil { | ||
if err == sql.ErrNoRows { | ||
// No rows found for the given userID | ||
return nil, errutil.ErrNotFound | ||
} | ||
return nil, err | ||
} | ||
|
||
return privateKey, nil | ||
} | ||
|
||
func (m *MariaDB) AddAccount(userID []byte, accountAddress []byte, signature []byte) error { | ||
stmt, err := m.db.Prepare("INSERT INTO accounts(user_id, account_address, signature) VALUES (?, ?, ?)") | ||
if err != nil { | ||
return err | ||
} | ||
defer stmt.Close() | ||
|
||
res, err := stmt.Exec(userID, accountAddress, signature) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(res) | ||
|
||
return nil | ||
} | ||
|
||
func (m *MariaDB) GetAccounts(userID []byte) ([]common.AccountDB, error) { | ||
rows, err := m.db.Query("SELECT account_address, signature FROM accounts WHERE user_id = ?", userID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
var accounts []common.AccountDB | ||
for rows.Next() { | ||
var account common.AccountDB | ||
if err := rows.Scan(&account.AccountAddress, &account.Signature); err != nil { | ||
return nil, err | ||
} | ||
accounts = append(accounts, account) | ||
} | ||
if err := rows.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return accounts, nil | ||
} | ||
|
||
func (m *MariaDB) GetAllUsers() ([]common.UserDB, error) { | ||
rows, err := m.db.Query("SELECT user_id, private_key FROM users") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
var users []common.UserDB | ||
for rows.Next() { | ||
var user common.UserDB | ||
err = rows.Scan(&user.UserID, &user.PrivateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
users = append(users, user) | ||
} | ||
|
||
if err = rows.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return users, nil | ||
} |
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