-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
escape sql query and insert query to mysql table
- Loading branch information
Showing
6 changed files
with
232 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[backend] | ||
dsn = user_test:Aksj@qop@tcp(127.0.0.1:3306)/test?charset=utf8 | ||
|
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,22 @@ | ||
/*config read to verify normal user*/ | ||
package main | ||
|
||
import ( | ||
"github.com/chenzhe07/goconfig" | ||
) | ||
|
||
func get_config(conf string) (c *goconfig.ConfigFile, err error) { | ||
c, err = goconfig.ReadConfigFile(conf) | ||
if err != nil { | ||
return c, err | ||
} | ||
return c, nil | ||
} | ||
|
||
func get_backend_dsn(c *goconfig.ConfigFile) (dsn string, err error) { | ||
dsn, err = c.GetString("backend", "dsn") | ||
if err != nil { | ||
return dsn, err | ||
} | ||
return dsn, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/*config read to verify normal user*/ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
_ "github.com/go-sql-driver/mysql" | ||
"log" | ||
) | ||
|
||
func dbh(dsn string) (db *sql.DB, err error) { | ||
db, err = sql.Open("mysql", dsn) | ||
if err != nil { | ||
return db, err | ||
} | ||
return db, nil | ||
} | ||
|
||
func Query(db *sql.DB, q string) (*sql.Rows, error) { | ||
if Verbose { | ||
log.Printf("Query: %s\n", q) | ||
} | ||
return db.Query(q) | ||
} | ||
|
||
func QueryRow(db *sql.DB, q string) *sql.Row { | ||
if Verbose { | ||
log.Printf("Query: %s", q) | ||
} | ||
return db.QueryRow(q) | ||
} | ||
|
||
func ExecQuery(db *sql.DB, q string) (sql.Result, error) { | ||
if Verbose { | ||
log.Printf("ExecQuery: %s\n", q) | ||
} | ||
return db.Exec(q) | ||
} | ||
|
||
func insertlog(db *sql.DB, t *query) bool { | ||
insertSql := ` | ||
insert into query_log(bindport, client, client_port, server, server_port, sql_type, | ||
sql_string, create_time) values (%d, '%s', %d, '%s', %d, '%s', '%s', now()) | ||
` | ||
_, err := ExecQuery(db, fmt.Sprintf(insertSql, t.bindPort, t.client, t.cport, t.server, t.sport, t.sqlType, t.sqlString)) | ||
if err != nil { | ||
return false | ||
} | ||
return true | ||
} |
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,15 @@ | ||
CREATE TABLE `query_log` ( | ||
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, | ||
`bindport` smallint(5) unsigned NOT NULL, | ||
`client` char(15) NOT NULL DEFAULT '', | ||
`client_port` smallint(5) unsigned NOT NULL, | ||
`server` char(15) NOT NULL DEFAULT '', | ||
`server_port` smallint(5) unsigned NOT NULL, | ||
`sql_type` varchar(30) NOT NULL DEFAULT 'Query', | ||
`sql_string` text, | ||
`create_time` datetime NOT NULL, | ||
PRIMARY KEY (`id`), | ||
KEY `idx_client` (`client`), | ||
KEY `idx_server` (`server`), | ||
KEY `idx_cretime` (`create_time`) | ||
) ENGINE=InnoDB AUTO_INCREMENT=9945 DEFAULT CHARSET=utf8 |