Last active
June 18, 2018 03:12
-
-
Save xgz123/b5345d966257e739e26a81f94d7a9edc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-- mysql 5.7.21 Homebrew | |
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
/*!40101 SET NAMES utf8 */; | |
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; | |
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; | |
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; | |
# Dump of table file | |
# ------------------------------------------------------------ | |
DROP TABLE IF EXISTS `file`; | |
CREATE TABLE `file` ( | |
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, | |
`created_at` timestamp NULL DEFAULT NULL, | |
`updated_at` timestamp NULL DEFAULT NULL, | |
`deleted_at` timestamp NULL DEFAULT NULL, | |
`raw_url` text, | |
`dst` text, | |
`flag` int(11) DEFAULT '0', | |
PRIMARY KEY (`id`), | |
KEY `idx_download_deleted_at` (`deleted_at`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; | |
LOCK TABLES `file` WRITE; | |
/*!40000 ALTER TABLE `file` DISABLE KEYS */; | |
INSERT INTO `file` (`id`, `created_at`, `updated_at`, `deleted_at`, `raw_url`, `dst`, `flag`) | |
VALUES | |
(1,'2018-06-17 09:40:28','2018-06-17 09:40:28',NULL,'http://someurl','http://someurl',0), | |
(2,'2018-06-17 09:40:28','2018-06-17 09:40:28',NULL,'http://someurl','http://someurl',0), | |
(3,'2018-06-17 09:40:28','2018-06-17 09:40:28',NULL,'http://someurl','http://someurl',0); | |
/*!40000 ALTER TABLE `file` ENABLE KEYS */; | |
UNLOCK TABLES; | |
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; | |
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; | |
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; | |
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | |
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | |
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
This file contains hidden or 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 | |
import ( | |
"fmt" | |
"net/url" | |
_ "github.com/go-sql-driver/mysql" | |
"github.com/jinzhu/gorm" | |
) | |
type File struct { | |
gorm.Model | |
RawURL string `gorm:"column:raw_url;type:text" json:"raw_url,omitempty"` | |
URL *url.URL `gorm:"-" json:"url,omitempty"` | |
Dst string `gorm:"column:dst;type:text" json:"dst,omitempty"` | |
Flag int64 `gorm:"column:flag;type:int;default:0" json:"flag,omitempty"` | |
} | |
func (f *File) TableName() string { | |
return "file" | |
} | |
func main() { | |
files := []File{} | |
db, err := gorm.Open("mysql", "root@/test?charset=utf8mb4&parseTime=True") | |
if err != nil { | |
fmt.Println(err) | |
} | |
rows, err := db.Model(&File{}).Where("flag = ?", 0).Limit(2).Find(&files).Rows() | |
if err != nil { | |
fmt.Println(err) | |
} | |
db.ScanRows(rows, files) | |
fmt.Println(files) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment