· 9 years ago · Feb 01, 2017, 05:52 PM
1var addComment = function(user,comment,mysql,pool,callback) {
2 console.log(user,comment);
3 var self = this;
4 pool.getConnection(function(err,connection){
5 if (err) {
6 //connection.release();
7 return callback(true,null);
8 } else {
9 var sqlQuery = "INSERT into UserComment (UserName,UserId,Comment) VALUES ((SELECT UserName FROM User WHERE UserName = user),id,comment)";
10 // var inserts =["UserComment","UserId","UserName",
11 // "Comment","UserId","User","UserName",
12 // user,user,comment];
13 //sqlQuery = mysql.format(sqlQuery,inserts);
14 connection.query(sqlQuery,function(err,rows){
15 connection.release();
16 if (err) {
17 return callback(true,null);
18 } else {
19 callback(false,"comment added");
20 }
21 });
22 }
23 connection.on('error', function(err) {
24 return callback(true,null);
25 });
26 });
27};
28
29module.exports.addComment = addComment;
30
31SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
32SET time_zone = "+00:00";
33
34
35/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
36/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
37/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
38/*!40101 SET NAMES utf8mb4 */;
39
40--
41-- Database: `socketDemo`
42--
43
44-- --------------------------------------------------------
45
46--
47-- Table structure for table `User`
48--
49
50CREATE TABLE IF NOT EXISTS `User` (
51 `UserId` int(11) NOT NULL,
52 `UserName` varchar(25) NOT NULL,
53 `Password` varchar(25) NOT NULL
54) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
55
56--
57-- Dumping data for table `User`
58--
59
60INSERT INTO `User` (`UserId`, `UserName`, `Password`) VALUES
61(1, 'Harshit', 'Harshit');
62
63-- --------------------------------------------------------
64
65--
66-- Table structure for table `UserComment`
67--
68
69CREATE TABLE IF NOT EXISTS `UserComment` (
70 `UserId` int(11) NOT NULL,
71 `UserName` varchar(11) NOT NULL,
72 `Comment` text NOT NULL,
73 `PostId` int(11) NOT NULL
74) ENGINE=InnoDB DEFAULT CHARSET=latin1;
75
76--
77-- Dumping data for table `UserComment`
78--
79
80INSERT INTO `UserComment` (`UserId`, `UserName`, `Comment`, `PostId`) VALUES
81(1, 'Harshit', 'n n n n n ', 0);
82
83
84-- --------------------------------------------------------
85
86--
87-- Table structure for table `UserPost`
88--
89
90CREATE TABLE IF NOT EXISTS `UserPost` (
91 `UserPostId` int(11) NOT NULL,
92 `UserPostContent` text NOT NULL
93) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
94
95--
96-- Dumping data for table `UserPost`
97--
98
99INSERT INTO `UserPost` (`UserPostId`, `UserPostContent`) VALUES
100(1, 'This is test comment.');
101
102--
103-- Indexes for dumped tables
104--
105
106--
107-- Indexes for table `User`
108--
109ALTER TABLE `User`
110 ADD PRIMARY KEY (`UserName`),
111 ADD KEY `UserIdIndex` (`UserId`);
112
113--
114-- Indexes for table `UserComment`
115--
116ALTER TABLE `UserComment`
117 ADD KEY `UserIdIndexComment` (`UserId`),
118 ADD KEY `PostIdIndex` (`PostId`);
119
120--
121-- Indexes for table `UserPost`
122--
123ALTER TABLE `UserPost`
124 ADD PRIMARY KEY (`UserPostId`);
125
126--
127-- AUTO_INCREMENT for dumped tables
128--
129
130--
131-- AUTO_INCREMENT for table `User`
132--
133ALTER TABLE `User`
134 MODIFY `UserId` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
135--
136-- AUTO_INCREMENT for table `UserPost`
137--
138ALTER TABLE `UserPost`
139 MODIFY `UserPostId` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
140/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
141/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
142/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;