|
@@ -0,0 +1,65 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+import os
|
|
4
|
+import logging as logger
|
|
5
|
+
|
|
6
|
+import sqlalchemy as sql
|
|
7
|
+
|
|
8
|
+from Database.sqlwrapper import SqlWrapper
|
|
9
|
+
|
|
10
|
+class SqlQueryBuilder():
|
|
11
|
+
|
|
12
|
+ def __init__(self, sqlwrapper, table):
|
|
13
|
+ if not type(sqlwrapper) is SqlWrapper:
|
|
14
|
+ logger.error("Unable to instanciate, bad argument...")
|
|
15
|
+ raise TypeError('Excepted a SqlWrapper not a '+str(type(sqlwrapper)))
|
|
16
|
+ self.table = table
|
|
17
|
+ self.sqlwrapper = sqlwrapper
|
|
18
|
+ self.proxy = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+ def Select(self, arg):
|
|
22
|
+ """ Alias for select clause
|
|
23
|
+ @param arg iterable: arg must be a Python list or other iterable and contain eather table name/type or colum/literal_column
|
|
24
|
+ """
|
|
25
|
+
|
|
26
|
+ self.proxy = sql.select(arg)
|
|
27
|
+ return self.proxy
|
|
28
|
+
|
|
29
|
+ def Where(self, arg):
|
|
30
|
+ """ Alias for where clause
|
|
31
|
+ @param arg SQL expression object or string
|
|
32
|
+ """
|
|
33
|
+ self.proxy = self.proxy.where(arg)
|
|
34
|
+
|
|
35
|
+ def From(self, arg):
|
|
36
|
+ """ Alias for select_from clause
|
|
37
|
+ @param arg Table or table('tablename') or join clause
|
|
38
|
+ """
|
|
39
|
+ self.proxy = self.proxy.select_from(arg)
|
|
40
|
+
|
|
41
|
+ def Update(self):
|
|
42
|
+ self.proxy = self.table.update()
|
|
43
|
+
|
|
44
|
+ def Value(self, arg):
|
|
45
|
+ """
|
|
46
|
+ Allow you to specifies the VALUES or SET clause of the statement.
|
|
47
|
+ @param arg: VALUES or SET clause
|
|
48
|
+ """
|
|
49
|
+ self.proxy = self.proxy.values(arg)
|
|
50
|
+
|
|
51
|
+ def Execute(self):
|
|
52
|
+ """
|
|
53
|
+ Execute the sql query constructed in the proxy and return the result.
|
|
54
|
+ If no query then return False.
|
|
55
|
+ @return: query result on success else False
|
|
56
|
+ """
|
|
57
|
+ if(self.proxy.__str__().split() == 'SELECT'):
|
|
58
|
+ return self.sqlwrapper.rconn.execute(self.proxy)
|
|
59
|
+ elif(self.proxy is not None):
|
|
60
|
+ return self.sqlwrapper.wconn.execute(self.proxy)
|
|
61
|
+ else:
|
|
62
|
+ return False
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|