xlang v4.0 Release
程序设计语言基础库文档
载入中...
搜索中...
未找到
Mysql.xcsm
浏览该文件的文档.
1//xlang Source, Name:Mysql.xcsm
2//Date: Tue Jul 16:06:32 2019
3
4class Mysql : Sql.Connection{
5
6 static const String DRIVERNAME = "mysql";
7 byte [] _mysql ;
8
9 int port = 3306;
10 int timeout = 0;
11
12 public enum MysqlOption{
16 SELECTDB
17 };
18
19 static class MysqlRegister : Sql.ConnectionRegister{
21 if (drivername.equals("mysql")){
22 return new Mysql();
23 }
24 return nilptr;
25 }
26 };
27
28 public static bool registry(){
29 if (Helper.init()){
30 Sql.Database.reigstry(DRIVERNAME, new MysqlRegister());
31 return true;
32 }
33 return false;
34 }
35
36 static class Helper :Library {
37 static bool bloaded = false;
38
39 public static bool init(){
40 if (bloaded == false){
41 try{
42 loadLibrary("mysql_helper");
43 bloaded = true;
44 }catch(Exception e){
46 }
47 }
48 return bloaded;
49 }
50
51 public import{
52 Pointer cdecl rs_create(Pointer);
53 void cdecl rs_free(Pointer);
54 bool cdecl rs_next(Pointer);
55 bool cdecl rs_first(Pointer);
56 int cdecl rs_label2Index(Pointer, String);
57 long cdecl rs_getRowCount(Pointer);
58 bool cdecl rs_last(Pointer);
59 bool cdecl rs_isValid(Pointer);
60 bool cdecl rs_isLast(Pointer);
61 String cdecl rs_getValue(Pointer, String);
62 int cdecl rs_getValueI(Pointer, String);
63 long cdecl rs_getValueL(Pointer, String);
64 double cdecl rs_getValueD(Pointer, String);
65
66 String cdecl rs_getIValue(Pointer, int);
67 int cdecl rs_getIValueI(Pointer, int);
68 long cdecl rs_getIValueL(Pointer, int);
69 double cdecl rs_getIValueD(Pointer, int);
70
71 Pointer cdecl x_mysql_init (ObjectPtr );
72
73 int cdecl x_mysql_options(ObjectPtr, int option, ObjectPtr );
74
75 Pointer cdecl x_mysql_real_connect(ObjectPtr , String host,
76 String user,
77 String passwd,
78 String db,
79 int port,
80 String unix_socket,
81 int clientflag);
82
83 int cdecl x_mysql_set_character_set(ObjectPtr , String csname);
84
85 int cdecl x_mysql_select_db(ObjectPtr , String db);
86
87 int cdecl x_mysql_errno(ObjectPtr );
88 String cdecl x_mysql_info(ObjectPtr );
89 String cdecl x_mysql_state(ObjectPtr );
90 String cdecl x_mysql_error(ObjectPtr );
91
92 int cdecl x_mysql_query(ObjectPtr , String );
93
94 void cdecl x_mysql_close(ObjectPtr);
95
96 Pointer cdecl x_mysql_store_result(ObjectPtr);
97
98 void cdecl x_mysql_data_seek(Pointer result,
99 long offset);
100
101 Pointer cdecl x_mysql_row_seek(Pointer result,
102 long offset);
103
104 Pointer cdecl x_mysql_row_tell(Pointer res);
105
106 Pointer cdecl x_mysql_fetch_row(Pointer result);
107
108 long cdecl x_mysql_num_rows(Pointer res);
109
110 void cdecl x_mysql_free_result(Pointer result);
111
112 int cdecl x_mysql_num_fields(Pointer res);
113 };
114 };
115
116 static bool registried = registry();
117
118 public static class MysqlResultSet : Sql.ResultSet{
119 long hres;
120
121 public MysqlResultSet(long r){
122 hres = Helper.rs_create(r);
123 }
124
125 public bool first()override{
126 return Helper.rs_first(hres);
127 }
128
129 public bool last()override{
130 return Helper.rs_last(hres);
131 }
132
133 public bool next()override{
134 return Helper.rs_next(hres);
135 }
136
137 public bool isEof()override{
138 throw new Sql.DatabaseNotSupportException("isEof");
139 return false;
140 }
141
142 public bool previous()override{
143 throw new Sql.DatabaseNotSupportException("previous");
144 return false;
145 }
146
147 public bool isFirst()override{
148 throw new Sql.DatabaseNotSupportException("isFirst");
149 return false;
150 }
151
152 public bool isLast()override{
153 return Helper.rs_isLast(hres);
154 }
155
156 public bool isValid()override{
157 return Helper.rs_isValid(hres);
158 }
159
160 public int getInt(String columnlabel)override{
161 return Helper.rs_getValueI(hres, columnlabel);
162 }
163
164 public long getLong(String columnlabel)override{
165 return Helper.rs_getValueL(hres, columnlabel);
166 }
167
168 public double getDouble(String columnlabel)override{
169 return Helper.rs_getValueD(hres, columnlabel);
170 }
171
172 public byte getByte(String columnlabel)override{
173 return Helper.rs_getValueI(hres, columnlabel);
174 }
175
176 public bool getBoolean(String columnlabel)override{
177 return Helper.rs_getValueI(hres, columnlabel) != 0;
178 }
179
180 public String getString(String columnlabel)override{
181 return Helper.rs_getValue(hres, columnlabel);
182 }
183
184 public int getInt(int columnIndex)override{
185 return Helper.rs_getIValueI(hres, columnIndex);
186 }
187
188 public long getLong(int columnIndex)override{
189 return Helper.rs_getIValueL(hres, columnIndex);
190 }
191
192 public double getDouble(int columnIndex)override{
193 return Helper.rs_getIValueD(hres, columnIndex);
194 }
195
196 public byte getByte(int columnIndex)override{
197 return Helper.rs_getIValueI(hres, columnIndex);
198 }
199
200 public bool getBoolean(int columnIndex)override{
201 return Helper.rs_getIValueI(hres, columnIndex) != 0;
202 }
203
204 public String getString(int columnIndex)override{
205 return Helper.rs_getIValue(hres, columnIndex);
206 }
207
208 public int findColumn(String)override{
209 throw new Sql.DatabaseNotSupportException("findColumn");
210 return -1;
211 }
212
213 public String getColumnName(int i){
214 throw new Sql.DatabaseNotSupportException("getColumnName");
215 return nilptr;
216 }
217
218 public int getColumnCount()override{
219 throw new Sql.DatabaseNotSupportException("getColumnCount");
220 return -1;
221 }
222
223 public long getRowCount()override{
224 return Helper.rs_getRowCount(hres);
225 }
226
227 public int getRow()override{
228 throw new Sql.DatabaseNotSupportException("getRow");
229 return 0;
230 }
231
232 public void close()override{
233 if (hres != 0){
234 Helper.rs_free(hres);
235 hres = 0;
236 }
237 }
238
239 void finalize(){
240 close();
241 }
242 };
243
244 public static class MysqlStatement : Sql.Statement{
246
248 mysql = m;
249 }
250
251 public int execute(String sql)override{
252 if (0 != Helper.x_mysql_query(mysql._mysql,sql)){
253 throw new Sql.SqlException(mysql.getErrorCode(), mysql.getError());
254 }
255 return 0;
256 }
257
258 public Sql.ResultSet executeQuery(String sql)override{
259 if (0 != Helper.x_mysql_query(mysql._mysql,sql)){
260 throw new Sql.SqlException(mysql.getErrorCode(), mysql.getError());
261 }
262 return getResult();
263 }
264
265 public int executeUpdate(String sql)override{
266 if (0 != Helper.x_mysql_query(mysql._mysql,sql)){
267 throw new Sql.SqlException(mysql.getErrorCode(), mysql.getError());
268 }
269 return 0;
270 }
271
272 public Sql.ResultSet getResult()override{
273 long res = Helper.x_mysql_store_result(mysql._mysql);
274 if (res != 0){
275 return new MysqlResultSet(res);
276 }
277 return nilptr;
278 }
279
280 public int get_changes()override{
281 throw new SqlException(-1, "get_changes");
282 return 0;
283 }
284
285 public void close()override{
286
287 }
288 };
289
290
291 public class MysqlPreparedStatement : Sql.PreparedStatement{
293
295 super(sql);
296 mysql = m;
297 }
298
299 public int execute(String sql)override{
300 if (0 != Helper.x_mysql_query(mysql._mysql,sql)){
301 throw new Sql.SqlException(mysql.getErrorCode(), mysql.getError());
302 }
303 return 0;
304 }
305
306 public Sql.ResultSet executeQuery(String sql)override{
307 if (0 != Helper.x_mysql_query(mysql._mysql,sql)){
308 throw new Sql.SqlException(mysql.getErrorCode(), mysql.getError());
309 }
310 return getResult();
311 }
312
313 public int executeUpdate(String sql)override{
314 if (0 != Helper.x_mysql_query(mysql._mysql,sql)){
315 throw new Sql.SqlException(mysql.getErrorCode(), mysql.getError());
316 }
317 return 0;
318 }
319
320 public Sql.ResultSet getResult()override{
321 long res = Helper.x_mysql_store_result(mysql._mysql);
322 if (res != 0){
323 return new MysqlResultSet(res);
324 }
325 return nilptr;
326 }
327
328 public void close()override{
329
330 }
331 };
332
333
334 public void close() override {
335 //TODO:
336 if (_mysql != nilptr){
337 Helper.x_mysql_close(_mysql);
338 _mysql = nilptr;
339 }
340 }
341
342 void finalize(){
343 close();
344 }
345
346 public void create(String uri, String username, String pwd) override {
347 //TODO:
348 byte [] __mysql = new byte[2048];
349
350
351 if (0l == Helper.x_mysql_init(__mysql)){
352 throw new Sql.SqlException(Helper.x_mysql_errno(__mysql), Helper.x_mysql_error(__mysql));
353 }
354
355 if (timeout != 0){
356 if (0 == Helper.x_mysql_options(__mysql,0,timeout)){
357 throw new Sql.SqlException(Helper.x_mysql_errno(__mysql), Helper.x_mysql_error(__mysql));
358 }
359 }
360
361 if (0l == Helper.x_mysql_real_connect(__mysql,uri,username,pwd,nilptr,port,nilptr,0)){
362 throw new Sql.SqlException(Helper.x_mysql_errno(__mysql), Helper.x_mysql_error(__mysql));
363 }
364 _mysql = __mysql;
365 }
366
367 public bool isClosed() override {
368 //TODO:
369 return _mysql != nilptr;
370 }
371
372
373 public int getErrorCode()override{
374 return Helper.x_mysql_errno(_mysql);
375 }
376
377 public String getError()override{
378 return Helper.x_mysql_error(_mysql);
379 }
380
382 return Helper.x_mysql_state(_mysql);
383 }
384
385 public String getInfo(){
386 return Helper.x_mysql_info(_mysql);
387 }
388
389 public Object getOption(int opt) override {
390 //TODO:
391 throw new Sql.DatabaseNotSupportException("getOption");
392 return nilptr;
393 }
394
395 public void setOption(int opt, Object option) override {
396 switch(opt){
397 case MysqlOption.PORT:
398 port = (int)option;
399 break;
400 case MysqlOption.TIMEOUT:
401 timeout = (int)option;
402 break;
403 case MysqlOption.CHARSET:
404 if (_mysql == nilptr){
405 throw new Sql.SqlException(-1, "mysql not connected!");
406 }
407 if (0 != Helper.x_mysql_set_character_set(_mysql, (String)option)){
408 throw new Sql.SqlException(Helper.x_mysql_errno(_mysql), Helper.x_mysql_error(_mysql));
409 }
410 break;
411 case MysqlOption.SELECTDB:
412 if (_mysql == nilptr){
413 throw new Sql.SqlException(-1, "mysql not connected!");
414 }
415 if (0 != Helper.x_mysql_select_db(_mysql, (String)option)){
416 throw new Sql.SqlException(Helper.x_mysql_errno(_mysql), Helper.x_mysql_error(_mysql));
417 }
418 break;
419 default:
420 throw new Sql.DatabaseNotSupportException("setOption:" + opt);
421 break;
422 }
423 }
424
425 public Sql.Statement createStatement() override {
426 return new MysqlStatement(this);
427 }
428
430 return new MysqlPreparedStatement(this, sql);
431 }
432
433
434};
系统和IO相关
static final void output(String)
异常类
final String getMessage()
static final void loadLibrary(String)
static bool bloaded
Definition Mysql.xcsm:37
static bool init()
Definition Mysql.xcsm:39
void close() override
Definition Mysql.xcsm:328
int execute(String sql) override
Definition Mysql.xcsm:299
Sql.ResultSet executeQuery(String sql) override
Definition Mysql.xcsm:306
int executeUpdate(String sql) override
Definition Mysql.xcsm:313
MysqlPreparedStatement(Mysql m, String sql)
Definition Mysql.xcsm:294
Sql.ResultSet getResult() override
Definition Mysql.xcsm:320
Sql.Connection allocConnection(String drivername) override
Definition Mysql.xcsm:20
String getString(int columnIndex) override
Definition Mysql.xcsm:204
String getString(String columnlabel) override
Definition Mysql.xcsm:180
bool getBoolean(int columnIndex) override
Definition Mysql.xcsm:200
bool isFirst() override
Definition Mysql.xcsm:147
bool next() override
Definition Mysql.xcsm:133
MysqlResultSet(long r)
Definition Mysql.xcsm:121
int getRow() override
Definition Mysql.xcsm:227
void close() override
Definition Mysql.xcsm:232
bool isValid() override
Definition Mysql.xcsm:156
bool isLast() override
Definition Mysql.xcsm:152
int getColumnCount() override
Definition Mysql.xcsm:218
double getDouble(String columnlabel) override
Definition Mysql.xcsm:168
long getRowCount() override
Definition Mysql.xcsm:223
int getInt(String columnlabel) override
Definition Mysql.xcsm:160
bool isEof() override
Definition Mysql.xcsm:137
long getLong(int columnIndex) override
Definition Mysql.xcsm:188
int findColumn(String) override
Definition Mysql.xcsm:208
bool first() override
Definition Mysql.xcsm:125
double getDouble(int columnIndex) override
Definition Mysql.xcsm:192
bool previous() override
Definition Mysql.xcsm:142
String getColumnName(int i)
Definition Mysql.xcsm:213
bool last() override
Definition Mysql.xcsm:129
byte getByte(String columnlabel) override
Definition Mysql.xcsm:172
long getLong(String columnlabel) override
Definition Mysql.xcsm:164
bool getBoolean(String columnlabel) override
Definition Mysql.xcsm:176
byte getByte(int columnIndex) override
Definition Mysql.xcsm:196
int getInt(int columnIndex) override
Definition Mysql.xcsm:184
int get_changes() override
Definition Mysql.xcsm:280
void close() override
Definition Mysql.xcsm:285
int execute(String sql) override
Definition Mysql.xcsm:251
Sql.ResultSet executeQuery(String sql) override
Definition Mysql.xcsm:258
int executeUpdate(String sql) override
Definition Mysql.xcsm:265
Sql.ResultSet getResult() override
Definition Mysql.xcsm:272
MysqlStatement(Mysql m)
Definition Mysql.xcsm:247
int getErrorCode() override
Definition Mysql.xcsm:373
void close() override
Definition Mysql.xcsm:334
void create(String uri, String username, String pwd) override
Definition Mysql.xcsm:346
Object getOption(int opt) override
Definition Mysql.xcsm:389
void finalize()
Definition Mysql.xcsm:342
int timeout
Definition Mysql.xcsm:10
bool isClosed() override
Definition Mysql.xcsm:367
static const String DRIVERNAME
Definition Mysql.xcsm:6
static bool registry()
Definition Mysql.xcsm:28
String getSqlState()
Definition Mysql.xcsm:381
int port
Definition Mysql.xcsm:9
String getInfo()
Definition Mysql.xcsm:385
byte [] _mysql
Definition Mysql.xcsm:7
Sql.Statement createStatement() override
Definition Mysql.xcsm:425
Sql.PreparedStatement prepareStatement(String sql) override
Definition Mysql.xcsm:429
void setOption(int opt, Object option) override
Definition Mysql.xcsm:395
String getError() override
Definition Mysql.xcsm:377
static bool registried
Definition Mysql.xcsm:116
static bool reigstry(String name, ConnectionRegister reg)
Definition xsql.xcs:210
字符串类
bool equals(String)
Definition xsql.xcs:3