forked from yugabyte/pgloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmytest.lisp
150 lines (123 loc) · 4.11 KB
/
mytest.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
;; pg1
;; mysql -h gala3.galaxya.fr -u pg1 -pAFmhKERxD9PVjgQD
;; mysql -h gala4.galaxya.fr -u pg1 -pAFmhKERxD9PVjgQD
;;
;; pg2
;; mysql -h gala3.galaxya.fr -u pg2 -pFhssUAVTDaQZGrLd
;; mysql -h gala4.galaxya.fr -u pg2 -pFhssUAVTDaQZGrLd
(in-package :galaxya-loader)
(defparameter *myconn-host* "gala3.galaxya.fr")
(defparameter *myconn-user* "pg1")
(defparameter *myconn-pass* "AFmhKERxD9PVjgQD")
(setq *myconn-host* "127.0.0.1"
*myconn-user* "galaxyad"
*myconn-pass* "gabrielghk")
(defun toto ()
(cl-mysql:connect :host *myconn-host*
:user *myconn-user*
:password *myconn-pass*)
(cl-mysql:query "SET NAMES 'utf8'")
(cl-mysql:use "galaxya1")
(prog1
(cl-mysql:query "SELECT * FROM toto;" :type-map nil)
(cl-mysql:disconnect)))
(defun mytest ()
;; connect
(cl-mysql:connect :host *myconn-host*
:user *myconn-user*
:password *myconn-pass*)
(unwind-protect
(progn
;; Ensure we're talking utf-8 and connect to DBNAME in MySQL
(cl-mysql:query "SET NAMES 'utf8'")
(cl-mysql:use "galaxya1")
(let* ((sql "SELECT * FROM toto;")
(q (cl-mysql:query sql :store nil))
(rs (cl-mysql:next-result-set q)))
(declare (ignore rs))
;; "SELECT id, email, date_debut, date_redige, ip
;; FROM commentaires
;; WHERE date_redige = '0000-00-00' LIMIT 10;"
;; Now fetch MySQL rows
(loop
for row = (cl-mysql:next-row q :type-map (make-hash-table))
while row collect row)))
;; free resources
(cl-mysql:disconnect)))
(defun mytest-to-csv (dbname table-name filename)
"extract MySQL data to CSV file"
(cl-mysql:connect :host *myconn-host*
:user *myconn-user*
:password *myconn-pass*)
(unwind-protect
(progn
;; Ensure we're talking utf-8 and connect to DBNAME in MySQL
(cl-mysql:query "SET NAMES 'utf8'")
(cl-mysql:query "SET character_set_results = utf8;")
(cl-mysql:use dbname)
(let* ((sql (format nil "SELECT * FROM ~a;" table-name))
(q (cl-mysql:query sql :store nil))
(rs (cl-mysql:next-result-set q)))
(declare (ignore rs))
;; Now fetch MySQL rows directly in the stream
(with-open-file (csv filename
:direction :output
:if-exists :supersede
:external-format :utf-8)
(loop
for count from 1
for row = (cl-mysql:next-row q :type-map (make-hash-table))
while row
do (cl-csv:write-csv-row (reformat-row row '(2 3))
:stream csv
:separator #\;
:quote #\"
:newline '(#\Newline))
finally (return count)))))
;; free resources
(cl-mysql:disconnect)))
(defun reformat-row (row date-columns)
"cl-mysql returns universal date, we want PostgreSQL date strings"
(loop
for i from 1
for col in row
when (member i date-columns)
collect (reformat-date col)
else collect col))
(defun pgtest (mydbname table-name &key (truncate t))
(cl-mysql:connect :host *myconn-host*
:user *myconn-user*
:password *myconn-pass*)
;; Ensure we're talking utf-8 and connect to DBNAME in MySQL
(cl-mysql:query "SET NAMES 'utf8'")
(cl-mysql:query "SET character_set_results = utf8;")
(cl-mysql:use mydbname)
;; TRUNCATE the table in PostgreSQL
(when truncate
(format t "TRUNCATE ~a;~%" table-name)
(pomo:with-connection '("dim" "dim" "none" "localhost")
(pomo:execute (format nil "truncate ~a;" table-name))))
;; Now fetch MySQL rows and feed them to our COPY stream
(let* ((my-sql (format nil
"SELECT * FROM ~a ORDER BY id LIMIT 1;"
table-name))
(my-q (cl-mysql:query my-sql :store nil))
(my-rs (cl-mysql:next-result-set my-q))
(pgconn '("dim" "dim" "none" "localhost"))
(pgstream
(cl-postgres:open-db-writer pgconn table-name nil)))
(declare (ignore my-rs))
(unwind-protect
(loop
for count from 0
;; read MySQL data
for row = (cl-mysql:next-row my-q :type-map (make-hash-table))
while row
;; write it to PostgreSQL
do (cl-postgres:db-write-row pgstream
(mapcar (lambda (x)
(if (null x) :null x))
row))
finally (return count))
;; in case of error, close copier and database
(cl-postgres:close-db-writer pgstream))))